Merge main into feat/import-photocraft

This commit is contained in:
dotCooCoo committed 2026-11-10 03:10:38 -08:00
commit dbd8234631
92 files changed
+6648 -529

No files matched your search

+1 -1
View File
@@ -99,7 +99,7 @@ Screenshots of EffectCraft itself, rendered headlessly, showing only procedurall
(sidecars `.attribution`).
- Bundled extension (ScriptUI panel) with its twelve built-in ease presets:
`extensions/scriptui-panels/Ease Presets.jsx` (sidecar `.attribution`).
- Example effect plug-in: `examples/plugins/posterize-bands` (plug-in API v1, WebAssembly).
- Example effect plug-in: `examples/plugins/posterize-bands` (WebAssembly ABI 1).
## Trained models (optional downloads, not bundled)
Generated
+47 -62
View File
@@ -1127,7 +1127,7 @@ dependencies = [
[[package]]
name = "craft-docpath"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
[[package]]
name = "craft-ecproj-format"
@@ -1142,12 +1142,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -1188,7 +1188,7 @@ dependencies = [
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-syntax",
"thiserror 2.0.21",
@@ -1197,7 +1197,7 @@ dependencies = [
[[package]]
name = "craft-quit"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"dispatch2",
"egui",
@@ -1208,7 +1208,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -1476,7 +1476,7 @@ dependencies = [
[[package]]
name = "effectcraft"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"cpal",
"craft-update",
@@ -1502,7 +1502,7 @@ dependencies = [
[[package]]
name = "effectcraft-aep"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-color",
"effectcraft-effects",
@@ -1516,7 +1516,7 @@ dependencies = [
[[package]]
name = "effectcraft-automation"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-engine",
"effectcraft-host",
@@ -1530,14 +1530,14 @@ dependencies = [
[[package]]
name = "effectcraft-av1enc"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"filmcraft-av1",
]
[[package]]
name = "effectcraft-cli"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"cpu-time",
"effectcraft-automation",
@@ -1556,7 +1556,7 @@ dependencies = [
[[package]]
name = "effectcraft-color"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"proptest",
"serde",
@@ -1564,7 +1564,7 @@ dependencies = [
[[package]]
name = "effectcraft-effects"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"chrono",
"effectcraft-color",
@@ -1582,7 +1582,7 @@ dependencies = [
[[package]]
name = "effectcraft-engine"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"chrono",
"craft-doc",
@@ -1623,7 +1623,7 @@ dependencies = [
[[package]]
name = "effectcraft-export"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-av1enc",
"effectcraft-color",
@@ -1654,7 +1654,7 @@ dependencies = [
[[package]]
name = "effectcraft-expr"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"boa_engine",
"effectcraft-color",
@@ -1671,7 +1671,7 @@ dependencies = [
[[package]]
name = "effectcraft-geom"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"proptest",
"serde",
@@ -1679,7 +1679,7 @@ dependencies = [
[[package]]
name = "effectcraft-gpu"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"bytemuck",
"effectcraft-color",
@@ -1702,7 +1702,7 @@ dependencies = [
[[package]]
name = "effectcraft-helper-host"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"bytemuck",
"effectcraft-effects",
@@ -1713,14 +1713,14 @@ dependencies = [
[[package]]
name = "effectcraft-hevcenc"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"filmcraft-hevc",
]
[[package]]
name = "effectcraft-host"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-engine",
"effectcraft-export",
@@ -1747,7 +1747,7 @@ dependencies = [
[[package]]
name = "effectcraft-interchange"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-color",
"effectcraft-keyframe",
@@ -1766,7 +1766,7 @@ dependencies = [
[[package]]
name = "effectcraft-keyframe"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-color",
"effectcraft-geom",
@@ -1778,7 +1778,7 @@ dependencies = [
[[package]]
name = "effectcraft-lottie"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"craft-docpath",
"effectcraft-color",
@@ -1796,7 +1796,7 @@ dependencies = [
[[package]]
name = "effectcraft-media"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"craft-doc",
"craft-docpath",
@@ -1830,7 +1830,7 @@ dependencies = [
[[package]]
name = "effectcraft-model"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-geom",
"image",
@@ -1843,14 +1843,14 @@ dependencies = [
[[package]]
name = "effectcraft-opusenc"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"filmcraft-opus",
]
[[package]]
name = "effectcraft-path"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-geom",
"effectcraft-keyframe",
@@ -1862,7 +1862,7 @@ dependencies = [
[[package]]
name = "effectcraft-pdf"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"craft-pdf",
"effectcraft-raster",
@@ -1878,7 +1878,7 @@ dependencies = [
[[package]]
name = "effectcraft-plugin"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-effects",
"effectcraft-keyframe",
@@ -1890,7 +1890,7 @@ dependencies = [
[[package]]
name = "effectcraft-project"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"craft-ecproj-format",
"effectcraft-color",
@@ -1904,14 +1904,14 @@ dependencies = [
[[package]]
name = "effectcraft-psd"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"miniz_oxide 0.9.1",
]
[[package]]
name = "effectcraft-raster"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-color",
"effectcraft-geom",
@@ -1921,7 +1921,7 @@ dependencies = [
[[package]]
name = "effectcraft-render"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-color",
"effectcraft-effects",
@@ -1944,7 +1944,7 @@ dependencies = [
[[package]]
name = "effectcraft-sam2"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"bytemuck",
"candle-core",
@@ -1959,7 +1959,7 @@ dependencies = [
[[package]]
name = "effectcraft-script"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"boa_engine",
"effectcraft-engine",
@@ -1977,7 +1977,7 @@ dependencies = [
[[package]]
name = "effectcraft-segment"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"miniz_oxide 0.9.1",
"ndarray",
@@ -1986,7 +1986,7 @@ dependencies = [
[[package]]
name = "effectcraft-svg"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-geom",
"effectcraft-path",
@@ -1998,7 +1998,7 @@ dependencies = [
[[package]]
name = "effectcraft-text"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-keyframe",
"harfrust",
@@ -2010,7 +2010,7 @@ dependencies = [
[[package]]
name = "effectcraft-time"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"proptest",
"serde",
@@ -2018,7 +2018,7 @@ dependencies = [
[[package]]
name = "effectcraft-track"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-raster",
"effectcraft-segment",
@@ -2030,7 +2030,7 @@ dependencies = [
[[package]]
name = "effectcraft-ui-egui"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"bytemuck",
"chrono",
@@ -2071,11 +2071,11 @@ dependencies = [
[[package]]
name = "effectcraft-vp9enc"
version = "0.21.0"
version = "0.23.1"
[[package]]
name = "effectcraft-web"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"effectcraft-engine",
"effectcraft-export",
@@ -3380,7 +3380,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a"
[[package]]
name = "hayro-jbig2"
version = "0.3.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-ccitt",
]
@@ -3394,7 +3394,7 @@ checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1"
[[package]]
name = "hayro-syntax"
version = "0.7.2"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-jbig2",
"hayro-jpeg2000",
@@ -7776,7 +7776,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
[[package]]
name = "xtask"
version = "0.21.0"
version = "0.23.1"
dependencies = [
"chrono",
"pkg-config",
@@ -8102,18 +8102,3 @@ dependencies = [
"syn 3.0.6",
"winnow",
]
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_system_font_provider"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
+9 -10
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*"]
[workspace.package]
version = "0.21.0"
version = "0.23.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
@@ -12,9 +12,9 @@ repository = "https://github.com/storytold/effectcraft"
[workspace.dependencies]
craft-doc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6ae033e6c18bde9cd6f18e505c0556667ba2226a" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6ae033e6c18bde9cd6f18e505c0556667ba2226a" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
@@ -118,7 +118,7 @@ half = "2.7"
# ui
# Shared frontend components; keep this dependency above the engine boundary.
# Shared UI and the complete egui family select reviewed immutable commits below.
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["serde"] }
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", features = ["serde"] }
# `default_fonts` and `monochrome_emoji_fonts` are explicit: the app's `FontDefinitions::default()`
# and its `✔`/`▣` chrome glyphs need both, and kittest's own dev-dependencies do not propagate.
egui = { version = "0.37", default-features = false, features = ["default_fonts", "monochrome_emoji_fonts"] }
@@ -243,8 +243,8 @@ lto = "thin"
codegen-units = 1
[patch.crates-io]
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.3.0" }
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.3.0" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
@@ -255,8 +255,7 @@ egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc79968
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
# Patch a crate only together with the dependency that needs it: an unused patch makes the Nix
# build, which uses vendored sources, fail (`cargo xtask lock-patches`).
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
+591
View File
@@ -0,0 +1,591 @@
//! A small, separate process reports an unexpected desktop exit (#3357).
//!
//! Allocation failure can abort without a panic hook. The GUI therefore owns a pipe to a
//! helper which never loads a project, font database or GPU. Only an explicit marker after
//! normal teardown suppresses the report. EOF alone identifies an unexpected exit, not its
//! cause. System-wide exhaustion can still prevent the helper or its native dialog working.
use std::ffi::{OsStr, OsString};
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
const FLAG: &str = "--internal-crash-reporter";
const CLEAN: &[u8] = b"EFFECTCRAFT_CLEAN_EXIT_V1\n";
const LOG_BYTES: usize = 64 * 1024;
const HEADER_BYTES: usize = 128;
/// Owned only by the GUI, never handed to another child. Dropping it without `finish` closes
/// the pipe and reports an unexpected exit; there is intentionally no clean-exit Drop hook.
pub struct Reporter {
pipe: ChildStdin,
child: Child,
}
impl Reporter {
/// Call only after application teardown, including handled startup/device-loss errors.
pub fn finish(mut self) -> io::Result<()> {
let sent = self.pipe.write_all(CLEAN).and_then(|()| self.pipe.flush());
drop(self.pipe);
// Do not let a suspended/broken helper hang shutdown. Reap it before the GUI
// exits where possible, so Windows updates do not race an old executable.
let cleanup = finish_child(&mut self.child, Duration::from_secs(2));
match (sent, cleanup) {
(Err(sent), Err(cleanup)) => Err(io::Error::other(format!("clean marker failed: {sent}; cleanup failed: {cleanup}"))),
(Err(error), _) | (_, Err(error)) => Err(error),
(Ok(()), Ok(())) => Ok(()),
}
}
}
fn finish_child(child: &mut Child, grace: Duration) -> io::Result<()> {
let start = Instant::now();
while start.elapsed() < grace {
if let Some(status) = child.try_wait()? {
return if status.success() { Ok(()) } else { Err(io::Error::other(format!("crash reporter exited with {status}"))) };
}
std::thread::sleep(Duration::from_millis(20));
}
child.kill()?;
// Even after termination, do not introduce an unbounded wait on a stuck OS process.
let start = Instant::now();
while start.elapsed() < Duration::from_secs(1) {
if child.try_wait()?.is_some() {
return Err(io::Error::other("crash reporter did not finish promptly and was terminated"));
}
std::thread::sleep(Duration::from_millis(20));
}
Err(io::Error::other("crash reporter termination could not be confirmed"))
}
/// Shared ownership lets macOS finish in on_exit, while main retains the startup-error path.
#[derive(Clone, Default)]
pub struct Shutdown(std::sync::Arc<std::sync::Mutex<Option<Reporter>>>);
impl Shutdown {
/// Install the one reporter created for this desktop launch.
pub fn install(&self, reporter: Reporter) -> io::Result<()> {
let mut slot = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?;
*slot = Some(reporter);
Ok(())
}
/// Consume the reporter exactly once, after the applicable platform teardown boundary.
pub fn finish(&self) -> io::Result<()> {
let reporter = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?.take();
reporter.map_or(Ok(()), Reporter::finish)
}
}
/// A panic during teardown must leave the reporter armed. macOS terminates after this hook
/// without returning to main; other platforms pass None and finish after run_native returns.
pub fn after_teardown(reporter: Option<&Shutdown>, teardown: impl FnOnce()) -> io::Result<()> {
teardown();
reporter.map_or(Ok(()), Shutdown::finish)
}
/// Each process/run has its own report, including simultaneous launches.
pub fn run_id() -> String {
let nanos = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos();
format!("{}-{nanos}", std::process::id())
}
pub fn session_marker(id: &str) -> String {
format!("crash-report session {id}")
}
/// Start before heavy work. A missing helper is a warning, never a reason to refuse the app.
pub fn start(id: &str, log: Option<&Path>, reports: &Path, interactive: bool) -> io::Result<Reporter> {
let exe = std::env::current_exe()?;
let args = [
OsString::from(FLAG),
OsString::from(id),
log.map_or_else(OsString::new, |p| p.as_os_str().to_owned()),
reports.as_os_str().to_owned(),
OsString::from(if interactive { "1" } else { "0" }),
];
spawn(&exe, &args)
}
fn spawn(exe: &Path, args: &[OsString]) -> io::Result<Reporter> {
let mut child = Command::new(exe).args(args).stdin(Stdio::piped()).stdout(Stdio::null()).spawn()?;
match child.stdin.take() {
Some(pipe) => Ok(Reporter { pipe, child }),
None => {
let _ = child.kill();
let _ = child.wait();
Err(io::Error::other("crash reporter has no control pipe"))
}
}
}
/// Handle the internal mode before logging, argument parsing, updates or GUI initialization.
/// The callback is a native dialog on this helper's main thread, not on the failed GUI's.
pub fn maybe_run(args: &[OsString], show: impl FnOnce(&str)) -> bool {
if args.first().map(OsString::as_os_str) != Some(OsStr::new(FLAG)) {
return false;
}
let parsed = match args {
[_, id, log, reports, interactive] => id
.to_str()
.filter(|id| valid_id(id))
.zip(match interactive.to_str() {
Some("0") => Some(false),
Some("1") => Some(true),
_ => None,
})
.map(|(id, interactive)| (id, (!log.is_empty()).then(|| Path::new(log)), Path::new(reports), interactive)),
_ => None,
};
let Some((id, log, reports, interactive)) = parsed else {
eprintln!("EffectCraft crash reporter: invalid internal arguments");
return true;
};
// Keep the exact file open while the GUI is alive: a later rotation must not substitute
// another instance's log. If rotation won the startup race, the session marker rejects it.
let mut log = SessionLog::open(log, id);
match clean_exit(&mut io::stdin().lock()) {
Ok(true) => {}
result => {
let failure = result.err().map(|e| e.to_string());
let saved = write_report(reports, id, &mut log, failure.as_deref());
let description = description(&saved);
eprintln!("{description}");
if interactive {
show(&description);
}
}
}
true
}
fn valid_id(id: &str) -> bool {
!id.is_empty() && id.len() <= 80 && id.bytes().all(|c| c.is_ascii_digit() || c == b'-')
}
/// Fixed-size comparison: arbitrary/truncated input never grows a buffer or counts as clean.
fn clean_exit(input: &mut impl Read) -> io::Result<bool> {
let mut marker = [0u8; CLEAN.len()];
let mut filled = 0;
while filled < marker.len() {
let remaining = marker.get_mut(filled..).ok_or_else(|| io::Error::other("invalid control marker offset"))?;
match input.read(remaining) {
Ok(0) => return Ok(false),
Ok(n) => filled += n,
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(marker == CLEAN)
}
enum SessionLog {
Captured { file: File, marker: String },
Unavailable(String),
}
impl SessionLog {
fn open(path: Option<&Path>, id: &str) -> Self {
let opened = (|| {
let path = path.ok_or_else(|| io::Error::other("no session log was opened"))?;
let mut file = File::open(path)?;
let mut header = [0u8; HEADER_BYTES];
let marker = format!("{}\n", session_marker(id));
let prefix = header.get_mut(..marker.len()).ok_or_else(|| io::Error::other("session marker too long"))?;
file.read_exact(prefix)?;
if prefix != marker.as_bytes() {
return Err(io::Error::other("matching session marker unavailable; the log may have rotated"));
}
Ok(file)
})();
match opened {
Ok(file) => Self::Captured { file, marker: format!("{}\n", session_marker(id)) },
Err(e) => Self::Unavailable(e.to_string()),
}
}
fn write_tail(&mut self, report: &mut File) -> io::Result<()> {
match self {
Self::Unavailable(reason) => writeln!(report, "Recent log unavailable: {reason}"),
Self::Captured { file, marker } => {
// An older concurrent launcher may have truncated the captured file.
// Recheck its identity before copying any content into this report.
file.seek(SeekFrom::Start(0))?;
let mut header = [0u8; HEADER_BYTES];
let prefix = header.get_mut(..marker.len()).ok_or_else(|| io::Error::other("session marker too long"))?;
if file.read_exact(prefix).is_err() || prefix != marker.as_bytes() {
return writeln!(report, "Recent log unavailable: session identity changed");
}
let size = file.metadata()?.len();
file.seek(SeekFrom::Start(size.saturating_sub(LOG_BYTES as u64)))?;
writeln!(report, "\nRecorded session log tail (last {LOG_BYTES} bytes at most; logging filters/size caps may omit later events):")?;
io::copy(&mut file.take(LOG_BYTES as u64), report)?;
Ok(())
}
}
}
}
fn write_report(reports: &Path, id: &str, log: &mut SessionLog, pipe_error: Option<&str>) -> io::Result<PathBuf> {
if !valid_id(id) {
return Err(io::Error::other("invalid report identity"));
}
std::fs::create_dir_all(reports)?;
let path = reports.join(format!("crash-{id}.txt"));
let mut report = OpenOptions::new().write(true).create_new(true).open(&path)?;
writeln!(
report,
"EffectCraft unexpected-exit diagnostic\nVersion: {}\nPlatform: {} / {}\nSession: {id}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH
)?;
writeln!(
report,
"The GUI's control pipe ended without its clean-exit marker.\nThe exit status and exact cause are not available to this reporter.\nMemory exhaustion, a native crash or external termination are possible.\nThis report was saved locally; it was not sent anywhere."
)?;
if let Some(error) = pipe_error {
writeln!(report, "Control pipe error: {error}")?;
}
if let Err(error) = log.write_tail(&mut report) {
writeln!(report, "\nCould not read the recent session log: {error}")?;
}
report.flush()?;
Ok(path)
}
fn description(saved: &io::Result<PathBuf>) -> String {
let diagnostic = match saved {
Ok(path) => format!("A diagnostic report was saved to:\n{}", path.display()),
Err(error) => format!("A diagnostic report could not be saved: {error}"),
};
format!(
"EffectCraft stopped unexpectedly.\n\n{diagnostic}\n\nThe exact cause is unknown. Reopen EffectCraft to check for an existing auto-save. Changes since the last auto-save may be lost.\n\nNo report has been sent anywhere."
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::{Duration, Instant};
static NEXT: AtomicUsize = AtomicUsize::new(0);
const ROLE: &str = "EFFECTCRAFT_CRASH_REPORT_TEST_ROLE";
fn folder() -> PathBuf {
let p = std::env::temp_dir().join(format!("ec-crash-report-{}-{}", run_id(), NEXT.fetch_add(1, Ordering::Relaxed)));
std::fs::create_dir_all(&p).unwrap();
p
}
fn test_name(name: &str) -> String {
// Also usable as a standalone std-only rustc test while the app's build cache is busy.
let prefix = module_path!().split_once("::").map_or("", |(_, rest)| rest);
format!("{prefix}::{name}")
}
fn helper(dir: &Path, id: &str) -> Reporter {
helper_mode(dir, id, false)
}
fn helper_mode(dir: &Path, id: &str, interactive: bool) -> Reporter {
let mut child = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("reporter_process"), "--ignored", "--nocapture"])
.env(ROLE, "reporter")
.env("EC_REPORT_DIR", dir)
.env("EC_REPORT_ID", id)
.env("EC_REPORT_INTERACTIVE", if interactive { "1" } else { "0" })
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.unwrap();
Reporter { pipe: child.stdin.take().unwrap(), child }
}
fn wait_file(path: &Path) {
let until = Instant::now() + Duration::from_secs(10);
while !path.is_file() && Instant::now() < until {
thread::sleep(Duration::from_millis(20));
}
assert!(path.is_file(), "report did not arrive: {}", path.display());
}
#[test]
fn bounded_protocol_rejects_eof_and_partial_or_wrong_markers() {
let mut clean = CLEAN;
assert!(clean_exit(&mut clean).unwrap());
for mut input in [b"".as_slice(), &CLEAN[..CLEAN.len() - 1], b"clean", b"EFFECTCRAFT_CLEAN_EXIT_V0\n"] {
assert!(!clean_exit(&mut input).unwrap());
}
assert!(!clean_exit(&mut &vec![b'x'; 100_000][..]).unwrap());
}
#[test]
fn report_is_bounded_and_keeps_its_session_after_log_rotation() {
let dir = folder();
let original = dir.join("effectcraft.log");
let id = "123-456";
std::fs::write(&original, format!("{}\n", session_marker(id))).unwrap();
let mut log = SessionLog::open(Some(&original), id);
std::fs::rename(&original, dir.join("effectcraft.1.log")).unwrap();
std::fs::write(&original, "OTHER SESSION MUST NOT APPEAR").unwrap();
let mut old = OpenOptions::new().append(true).open(dir.join("effectcraft.1.log")).unwrap();
old.write_all(&vec![b'a'; LOG_BYTES * 2]).unwrap();
old.write_all(b"\nGPU context retired\n").unwrap();
let report = write_report(&dir, id, &mut log, None).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.len() < LOG_BYTES + 2048);
assert!(text.contains("GPU context retired"));
assert!(!text.contains("OTHER SESSION"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn wrong_session_and_missing_logs_are_reported_without_reading_other_content() {
let dir = folder();
let path = dir.join("log");
std::fs::write(&path, "private other session").unwrap();
for (id, source) in [("1-1", Some(path.as_path())), ("1-2", None)] {
let report = write_report(&dir, id, &mut SessionLog::open(source, id), None).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.contains("Recent log unavailable"));
assert!(!text.contains("private other session"));
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn captured_log_rejects_a_session_overwritten_after_open() {
let dir = folder();
let path = dir.join("log");
std::fs::write(&path, "crash-report session 1-2\noriginal session\n").unwrap();
let mut log = SessionLog::open(Some(&path), "1-2");
std::fs::write(&path, "crash-report session 3-4\nprivate other session\n").unwrap();
let report = write_report(&dir, "1-2", &mut log, None).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.contains("Recent log unavailable"));
assert!(!text.contains("private other session"));
assert!(!text.contains("session 3-4"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn session_identity_requires_the_exact_first_line() {
let dir = folder();
let path = dir.join("log");
for header in ["crash-report session 1-20\n", "unrelated\ncrash-report session 1-2\n"] {
std::fs::write(&path, header).unwrap();
assert!(matches!(SessionLog::open(Some(&path), "1-2"), SessionLog::Unavailable(_)));
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn report_write_failure_is_described_and_existing_report_is_not_overwritten() {
let dir = folder();
let mut log = SessionLog::Unavailable("no log".into());
let first = write_report(&dir, "2-1", &mut log, None).unwrap();
let before = std::fs::read(&first).unwrap();
let result = write_report(&dir, "2-1", &mut log, None);
assert!(result.is_err());
assert!(description(&result).contains("could not be saved"));
assert_eq!(std::fs::read(first).unwrap(), before);
assert!(write_report(&dir, "../escape", &mut log, None).is_err());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn normal_and_handled_shutdown_send_clean_marker_without_a_report() {
for id in ["3-1", "3-2"] {
let dir = folder();
let r = helper(&dir, id);
r.finish().unwrap();
// A completion file proves the helper consumed the marker, not merely that it
// hadn't yet produced a report.
wait_file(&dir.join(format!("ready-{id}")));
assert!(!dir.join(format!("crash-{id}.txt")).exists());
std::fs::remove_dir_all(dir).unwrap();
}
}
#[test]
fn accepted_native_close_finishes_before_process_termination() {
let dir = folder();
let status = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"])
.env(ROLE, "native-close")
.env("EC_REPORT_DIR", &dir)
.stdout(Stdio::null())
.status()
.unwrap();
assert!(status.success());
wait_file(&dir.join("ready-8-1"));
assert!(!dir.join("crash-8-1.txt").exists());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn a_teardown_panic_does_not_mark_the_reporter_clean() {
let dir = folder();
let reporter = Shutdown::default();
reporter.install(helper(&dir, "8-2")).unwrap();
let result = std::panic::catch_unwind(|| {
let _ = after_teardown(Some(&reporter), || panic!("simulated teardown failure"));
});
assert!(result.is_err());
drop(reporter);
wait_file(&dir.join("ready-8-2"));
assert!(dir.join("crash-8-2.txt").exists());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn platforms_returning_to_main_keep_the_reporter_armed_until_then() {
let dir = folder();
let reporter = Shutdown::default();
reporter.install(helper(&dir, "8-3")).unwrap();
after_teardown(None, || {}).unwrap();
assert!(reporter.0.lock().unwrap().is_some());
reporter.clone().finish().unwrap();
reporter.finish().unwrap();
wait_file(&dir.join("ready-8-3"));
assert!(!dir.join("crash-8-3.txt").exists());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn an_aborting_parent_leaves_one_diagnostic_report() {
let dir = folder();
let status = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"])
.env(ROLE, "abort")
.env("EC_REPORT_DIR", &dir)
.stdout(Stdio::null())
.status()
.unwrap();
assert!(!status.success());
wait_file(&dir.join("ready-4-1"));
let text = std::fs::read_to_string(dir.join("crash-4-1.txt")).unwrap();
assert!(text.contains("exact cause are not available"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn an_unrelated_child_does_not_keep_the_control_pipe_open() {
let dir = folder();
let r = helper(&dir, "5-1");
let mut unrelated = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("lingering_child"), "--ignored", "--nocapture"])
.env(ROLE, "linger")
.stdout(Stdio::null())
.spawn()
.unwrap();
drop(r);
wait_file(&dir.join("ready-5-1"));
assert!(unrelated.try_wait().unwrap().is_none());
unrelated.kill().unwrap();
unrelated.wait().unwrap();
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn an_unresponsive_helper_cannot_hang_clean_shutdown() {
let mut child = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("lingering_child"), "--ignored", "--nocapture"])
.env(ROLE, "linger")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.unwrap();
let pipe = child.stdin.take().unwrap();
let started = Instant::now();
let result = Reporter { pipe, child }.finish();
assert!(result.unwrap_err().to_string().contains("was terminated"));
assert!(started.elapsed() < Duration::from_secs(5));
}
#[test]
fn simultaneous_sessions_write_separate_reports() {
let dir = folder();
let a = helper(&dir, "6-1");
let b = helper(&dir, "6-2");
drop(a);
drop(b);
wait_file(&dir.join("ready-6-1"));
wait_file(&dir.join("ready-6-2"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn interactive_failure_calls_dialog_once_after_saving_the_report() {
let dir = folder();
drop(helper_mode(&dir, "7-1", true));
wait_file(&dir.join("ready-7-1"));
let text = std::fs::read_to_string(dir.join("dialog-7-1")).unwrap();
assert!(text.contains("crash-7-1.txt"));
assert!(text.contains("No report has been sent"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn internal_mode_rejects_invalid_arguments_without_launching_the_app() {
assert!(!maybe_run(&[], |_| panic!("no dialog")));
assert!(!maybe_run(&[OsString::from("--version")], |_| panic!("no dialog")));
assert!(maybe_run(&[OsString::from(FLAG)], |_| panic!("no dialog")));
let args = [FLAG, "../escape", "", "", "1"].map(OsString::from);
assert!(maybe_run(&args, |_| panic!("no dialog")));
let args = [FLAG, "1-1", "", "", "invalid"].map(OsString::from);
assert!(maybe_run(&args, |_| panic!("no dialog")));
}
#[test]
fn missing_helper_is_an_error_not_a_panic() {
assert!(spawn(Path::new("/nonexistent-effectcraft-crash-helper"), &[]).is_err());
}
// Roles are ignored by ordinary test runs. Explicit subprocess invocations exercise
// real EOF/abort behavior without allocating a huge image or killing the test runner.
#[test]
#[ignore]
fn reporter_process() {
assert_eq!(std::env::var(ROLE).unwrap(), "reporter");
let dir = PathBuf::from(std::env::var_os("EC_REPORT_DIR").unwrap());
let id = std::env::var("EC_REPORT_ID").unwrap();
let interactive = std::env::var("EC_REPORT_INTERACTIVE").unwrap();
let args = [OsString::from(FLAG), OsString::from(&id), OsString::new(), dir.as_os_str().to_owned(), OsString::from(&interactive)];
assert!(maybe_run(&args, |description| {
assert_eq!(interactive, "1", "non-interactive reporter must not show a dialog");
assert!(dir.join(format!("crash-{id}.txt")).exists());
let mut shown = OpenOptions::new().write(true).create_new(true).open(dir.join(format!("dialog-{id}"))).unwrap();
shown.write_all(description.as_bytes()).unwrap();
}));
std::fs::write(dir.join(format!("ready-{id}")), b"done").unwrap();
}
#[test]
#[ignore]
fn aborting_parent() {
let role = std::env::var(ROLE).unwrap();
let dir = PathBuf::from(std::env::var_os("EC_REPORT_DIR").unwrap());
if role == "native-close" {
let reporter = Shutdown::default();
reporter.install(helper(&dir, "8-1")).unwrap();
after_teardown(Some(&reporter.clone()), || {}).unwrap();
std::process::exit(0); // Models macOS not returning from its native event loop.
}
assert_eq!(role, "abort");
let _reporter = helper(&dir, "4-1");
std::process::abort();
}
#[test]
#[ignore]
fn lingering_child() {
assert_eq!(std::env::var(ROLE).unwrap(), "linger");
thread::sleep(Duration::from_secs(30));
}
}
+47 -10
View File
@@ -22,6 +22,7 @@ mod appimage;
mod apple_events;
mod audio_out;
mod control_server;
mod crash_report;
mod launch_guard;
#[cfg(target_os = "macos")]
mod native_menu;
@@ -180,6 +181,23 @@ fn start_helpers_if_opted_in(session: &mut effectcraft_engine::Session) {
/// Builds the session, loads settings, plugins, the requested project/media and the optional control server, then
/// reports startup failure through `startup_failed`. Returns the event loop's result.
fn main() -> eframe::Result {
let args: Vec<_> = std::env::args_os().skip(1).collect();
if crash_report::maybe_run(&args, |description| {
rfd::MessageDialog::new().set_level(rfd::MessageLevel::Error).set_title("EffectCraft stopped unexpectedly").set_description(description).show();
}) {
return Ok(());
}
let reporter = crash_report::Shutdown::default();
let result = run_desktop(&reporter);
// run_desktop has dropped the app and handled any startup error with its existing dialog.
// An unwinding panic or abort never reaches this marker.
if let Err(error) = reporter.finish() {
log::warn!("could not mark crash reporter clean: {error}");
}
result
}
fn run_desktop(reporter: &crash_report::Shutdown) -> eframe::Result {
// Windows: the update helper is a copy of this program started with `--finish-update`.
#[cfg(windows)]
if let Some(result) = craft_update::self_update::finish(&effectcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
@@ -205,12 +223,23 @@ fn main() -> eframe::Result {
// The session log lives in the settings folder (`Logs/effectcraft.log`, the previous two runs
// kept): a launch from the Dock or a menu has no terminal. Opened after the arguments, so
// `--version` leaves no file behind; records logged until now are written to it first.
let run_id = crash_report::run_id();
if let Some(dir) = effectcraft_host::config_dir() {
match effectcraft_engine::logging::attach_session_log(&effectcraft_engine::logging::log_dir(Some(&dir))) {
let header = format!("{}\n", crash_report::session_marker(&run_id));
match effectcraft_engine::logging::attach_session_log_with_header(&effectcraft_engine::logging::log_dir(Some(&dir)), &header) {
Ok(path) => log::info!("effectcraft {}, log file {}", env!("CARGO_PKG_VERSION"), path.display()),
Err(e) => log::warn!("no log file: {e}"),
}
}
let logs = effectcraft_engine::logging::log_dir(effectcraft_host::config_dir().as_deref());
match crash_report::start(&run_id, effectcraft_engine::logging::session_file().as_deref(), &logs.join("Crash Reports"), control_port.is_none()) {
Ok(started) => {
if let Err(error) = reporter.install(started) {
log::warn!("crash reporter could not be retained: {error}");
}
}
Err(error) => log::warn!("crash reporter could not start: {error}"),
}
// The font menus list the installed fonts: read their names while the window opens.
effectcraft_engine::text::fonts::scan_system_in_background();
// Wayland shows the window's icon from its desktop entry: an AppImage brings its own.
@@ -245,6 +274,7 @@ fn main() -> eframe::Result {
let apple_events = apple_events::AppleEvents::install();
#[cfg(target_os = "macos")]
let apple_events = &apple_events;
let desktop_reporter = reporter.clone();
let result = eframe::run_native(
"EffectCraft",
options,
@@ -398,6 +428,7 @@ fn main() -> eframe::Result {
window: window::Tracker::new(opened, saved_window, control_port.is_none()),
launch,
drawn: false,
reporter: desktop_reporter,
}))
}),
);
@@ -491,6 +522,7 @@ struct Desktop {
launch: launch_guard::Launch,
/// The first frame was drawn (logged once).
drawn: bool,
reporter: crash_report::Shutdown,
}
impl eframe::App for Desktop {
@@ -525,15 +557,20 @@ impl eframe::App for Desktop {
}
fn on_exit(&mut self) {
// On macOS the app quits through `-[NSApplication terminate:]`, and the process ends
// before `eframe::run_native` returns (#341).
log::info!("the window is closing");
self.launch.exited();
self.window.save(self.app.session.config.as_deref());
self.app.on_exit();
effectcraft_host::stop_helpers();
// Scripts in Scripts/Shutdown run with the project closed.
let _ = self.app.session.execute("file.runShutdownScripts", json!({}));
// macOS terminates after this hook without returning from run_native (#341).
// Other platforms keep the reporter armed until main sees complete event-loop teardown.
let reporter = cfg!(target_os = "macos").then(|| self.reporter.clone());
if let Err(error) = crash_report::after_teardown(reporter.as_ref(), || {
log::info!("the window is closing");
self.launch.exited();
self.window.save(self.app.session.config.as_deref());
self.app.on_exit();
effectcraft_host::stop_helpers();
// Scripts in Scripts/Shutdown run with the project closed.
let _ = self.app.session.execute("file.runShutdownScripts", json!({}));
}) {
log::warn!("could not mark crash reporter clean after teardown: {error}");
}
}
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
+4 -2
View File
@@ -451,7 +451,9 @@ pub fn adjustment_crop(offset: [f64; 2], canvas_offset: [f64; 2], size: (u32, u3
/// Effects that grow their own buffer by what they need (blur and glow margins, Repeat Edge
/// Pixels through [`Buf::cover`], displacement through [`cover_pad`]): a stack of only these
/// skips the renderer's cover of the whole layer rectangle (#1205).
pub const SKIPS_COVER: &[&str] = &["ec.blur.gaussian", "ec.blur.fastbox", "ec.stylize.glow", "ec.distort.turbulentdisplace"];
/// Drop Shadow also pads by its distance and softness on both CPU and GPU: covering the
/// layer rectangle first only inflates working images on highly scaled text and shapes (#3357).
pub const SKIPS_COVER: &[&str] = &["ec.blur.gaussian", "ec.blur.fastbox", "ec.stylize.glow", "ec.distort.turbulentdisplace", "ec.perspective.dropshadow"];
/// The grid phase [`Buf::align`] keeps and the step [`cover_pad`] grows in: covers the levels
/// of the resampling pyramid used for downscaled layers.
@@ -1514,7 +1516,7 @@ pub const TIME_DEPENDENT: &[&str] = &[
pub fn is_time_dependent(id: &str) -> bool {
// Simulations replay from layer time 0, so every frame differs even when the defaults
// happen to look static at the test's two sample times.
id.starts_with("ec.sim.") || TIME_DEPENDENT.contains(&id)
id.starts_with("ec.sim.") || TIME_DEPENDENT.contains(&id) || (!id.starts_with("ec.") && plugin::reads_other_times(id))
}
/// Does effect `id` read its own parameters at earlier times (the stepped particle systems sample
+40 -7
View File
@@ -24,10 +24,12 @@
//! * rendering must be deterministic (same inputs, same output) and may run on several threads
//! at once.
//!
//! API 2 (native plug-ins; the WebAssembly ABI stays at 1) adds [`EffectPlugin::padding`] and
//! [`EffectPlugin::render_v2`], which gets a [`PluginHost`] to read the effect's input or other
//! layers at other times, and the extra parameter kinds. External effect helpers
//! (`effectcraft-helper-host`) are built on it.
//! API 2 (native plug-ins) adds [`EffectPlugin::padding`] and [`EffectPlugin::render_v2`], which
//! gets a [`PluginHost`] to read the effect's input or other layers at other times, and the extra
//! parameter kinds. External effect helpers (`effectcraft-helper-host`) are built on it.
//!
//! The WebAssembly ABI is versioned separately ([`WASM_API_VERSION`]): ABI 1 is `ec_render`;
//! ABI 2 adds `ec_render2`, which receives the layer's previous and next frames.
use std::sync::{Arc, OnceLock, RwLock};
@@ -39,9 +41,12 @@ use crate::{Buf, EffectCtx, EffectSpec, ParamSpec, Params, RenderFn};
/// The plug-in API version this build implements (manifests with API 1 or 2 are accepted).
pub const PLUGIN_API_VERSION: u32 = 2;
/// The plug-in API a WebAssembly module implements (its `ec_api_version`). API 2 is for native
/// and helper plug-ins; WebAssembly modules stay at 1.
pub const WASM_API_VERSION: u32 = 1;
/// The newest WebAssembly ABI this build implements (a module's `ec_api_version`). ABI 1 is
/// `ec_render`; ABI 2 adds `ec_render2` and the layer's neighbouring frames. This is separate
/// from the plug-in API version: a module's manifest `api` stays 1 or 2.
pub const WASM_API_VERSION: u32 = 2;
/// The oldest WebAssembly ABI this build still loads.
pub const MIN_WASM_API_VERSION: u32 = 1;
/// The most plug-in effects one process can register (commercial effect suites register a few hundred each).
pub const MAX_PLUGINS: usize = 1024;
@@ -269,6 +274,22 @@ impl PluginImage {
fn from_buf(b: Buf) -> PluginImage {
PluginImage { width: b.img.width, height: b.img.height, pixels: b.img.data, origin: b.offset, scale: b.scale }
}
/// This image resampled onto a `width × height` buffer grid at `origin`/`scale` (the current
/// frame's); the pixels unchanged when the grids already match.
pub fn on_grid(&self, width: u32, height: u32, origin: [f64; 2], scale: f64) -> Vec<[f32; 4]> {
if self.width == width && self.height == height && self.origin == origin && self.scale == scale {
return self.pixels.clone();
}
let k = self.scale / scale.max(1e-9);
let src = crate::Image { width: self.width, height: self.height, data: self.pixels.clone() };
crate::util::gen_image(width, height, |x, y| {
let lx = (x as f64 + 0.5 - origin[0]) * k + self.origin[0];
let ly = (y as f64 + 0.5 - origin[1]) * k + self.origin[1];
src.sample_bilinear(lx, ly)
})
.data
}
}
/// Services available during an API 2 render ([`EffectPlugin::render_v2`]).
@@ -388,6 +409,12 @@ pub trait EffectPlugin: Send + Sync {
fn render_v2(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64, _host: &dyn PluginHost) -> Result<(), String> {
self.render(frame, params, time)
}
/// Whether rendering reads the layer at other times (its neighbouring frames). Default
/// false; the render cache keys a layer with such an effect on its layer time. WebAssembly
/// ABI 2 plug-ins return true.
fn reads_other_times(&self) -> bool {
false
}
}
static SLOTS: [OnceLock<Arc<dyn EffectPlugin>>; MAX_PLUGINS] = [const { OnceLock::new() }; MAX_PLUGINS];
@@ -432,6 +459,12 @@ pub fn plugin(id: &str) -> Option<Arc<dyn EffectPlugin>> {
SLOTS.iter().filter_map(|s| s.get()).find(|p| p.manifest().id == id).cloned()
}
/// Whether the registered plug-in `id` reads the layer at other times (its neighbouring frames),
/// so the render cache must key its layer on the layer time.
pub fn reads_other_times(id: &str) -> bool {
SLOTS.iter().filter_map(|s| s.get()).any(|p| p.manifest().id == id && p.reads_other_times())
}
fn leak(s: &str) -> &'static str {
Box::leak(s.to_string().into_boxed_str())
}
+1
View File
@@ -161,6 +161,7 @@ mod params;
mod schema;
use super::commands;
pub use aep::is_aep_path;
pub use enabled::always;
pub(crate) use enabled::{has_comp, has_keys, has_layers, has_project_selection, not_yet, not_yet_run};
pub use params::match_path_of;
+21 -9
View File
@@ -1,5 +1,5 @@
//! File ▸ Import ▸ After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸ After Effects
//! Project… (`effectcraft_aep::export`).
//! File ▸ Import ▸ Adobe After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸
//! Adobe After Effects Project… (`effectcraft_aep::export`).
//!
//! Adds the items of an `.aep` (or its XML form, `.aepx`) to the project in a folder named after
//! the file (`docs/aep-import.md` lists what arrives). What the importer cannot bring over (effects
@@ -16,6 +16,14 @@ use serde_json::{Value, json};
use super::{CommandSpec, always, b_p, bad, str_p};
use crate::{EngineError, Result, Session, cmd};
/// Whether `path` is an After Effects project (`.aep`, or its XML form `.aepx`): imported as a
/// project (`file.importAep`), not as footage.
pub fn is_aep_path(path: &str) -> bool {
std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|e| e.eq_ignore_ascii_case("aep") || e.eq_ignore_ascii_case("aepx"))
}
/// `file.importAep`: the `.aep` / `.aepx` at `path` into the open project, in a folder named after
/// the file; refused ([`EngineError::Refused`]) when parts would be lost and `allowLosses` is not set.
fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
let path = str_p(p, "path").ok_or_else(|| bad("file.importAep", "missing `path`"))?.to_string();
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
@@ -40,11 +48,12 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(json!({"dryRun": true, "report": report}));
}
let importer = s.importer.clone();
let res = s.edit("Import After Effects Project", None, |proj, st| {
let res = s.edit("Import Adobe After Effects Project", None, |proj, st| {
let r = effectcraft_aep::import::import(proj, &bytes, &options).map_err(|e| match e {
AepImportError::Refused(_) => {
EngineError::Other(format!("{e} (nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report)"))
}
AepImportError::Refused(_) => EngineError::Refused {
losses: e.to_string(),
advice: "nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report",
},
other => EngineError::Other(format!("cannot import {path}: {other}")),
})?;
if apply {
@@ -90,6 +99,8 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
}))
}
/// `file.exportAep`: the open project as an `.aep` at `path`; refused ([`EngineError::Refused`])
/// when parts would be lost and `allowLosses` is not set.
fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
let path = str_p(p, "path").ok_or_else(|| bad("file.exportAep", "missing `path`"))?.to_string();
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
@@ -112,7 +123,7 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
};
let res = effectcraft_aep::export::export_with(&s.project, &ExportOptions { allow_losses }, &folder_time).map_err(|e| match e {
AepExportError::Refused(_) => {
EngineError::Other(format!("{e} (nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report)"))
EngineError::Refused { losses: e.to_string(), advice: "nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report" }
}
other => EngineError::Other(format!("cannot export {path}: {other}")),
})?;
@@ -126,11 +137,12 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"path": path, "bytes": res.bytes.len(), "report": res.report}))
}
/// File ▸ Import / Export ▸ Adobe After Effects Project….
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
"file.importAep",
"After Effects Project...",
"Adobe After Effects Project...",
["File", "Import"],
None,
"{path (.aep or .aepx), allowLosses?: bool (import although parts cannot be imported), dryRun?: bool (only return the report), applySettings?: bool (take over the file's project settings; default: when the project has no compositions)}",
@@ -139,7 +151,7 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"file.exportAep",
"After Effects Project...",
"Adobe After Effects Project...",
["File", "Export"],
None,
"{path (.aep), allowLosses?: bool (export although parts cannot be exported), dryRun?: bool (only return the report)}",
+1 -1
View File
@@ -1352,7 +1352,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Load Effect Plug-in...",
[],
None,
"{path (.wasm / .wat plug-in, API v1) | folder (loads every .wasm in it)}",
"{path (.wasm / .wat plug-in, WebAssembly ABI 1 or 2) | folder (loads every .wasm in it)}",
super::always,
plugins_load
),
+16 -2
View File
@@ -415,8 +415,11 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
let mut comps = vec![];
let mut items = vec![];
let mut errors = vec![];
// EffectCraft projects import as projects, as After Effects imports an .aep.
let (project_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
// EffectCraft projects import as projects, as After Effects imports an .aep; so do After
// Effects projects, refused when parts of them cannot come over. The app asks about those
// before it gets here (`panels::aep_losses`); a script or agent gets the refusal.
let (project_paths, paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
let (aep_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::is_aep_path(x));
let mut projects = vec![];
for path in project_paths {
match s.execute("file.importProject", json!({"path": path})) {
@@ -424,6 +427,17 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
Err(e) => errors.push(format!("{path}: {e}")),
}
}
for path in aep_paths {
match s.execute("file.importAep", json!({"path": path})) {
Ok(r) => projects.push(r),
// `file.import` has no `allowLosses`: point at the entry that lists the losses, and at
// the command that takes the consent.
Err(EngineError::Refused { losses, .. }) => errors.push(format!(
"{path}: {losses}; File ▸ Import ▸ Adobe After Effects Project… lists them and can import the rest (`file.importAep` with `allowLosses`)"
)),
Err(e) => errors.push(format!("{path}: {e}")),
}
}
if let Some(retain) = retain {
let mut rest = vec![];
for path in paths {
+4
View File
@@ -863,6 +863,10 @@ fn save_current_preview(s: &mut Session, p: &Value) -> Result<Value> {
storage: s.storage_quota.as_deref(),
label: "Save Current Preview".into(),
nested_switches: s.prefs.general.switches_affect_nested_comps,
caches: None,
renderer: None,
gpu_frames: s.prefs.memory.gpu_frames(),
};
let r = exporter.export(&job, &mut |_, _| true).map_err(EngineError::Other)?;
s.toast(format!("Saved preview to {}", r.path));
+4
View File
@@ -145,6 +145,10 @@ fn prerender(
storage: None,
label: format!("Pre-render {}", l.name),
nested_switches: cx.nested_switches,
caches: None,
renderer: None,
gpu_frames: None,
};
let r = exporter.export(&job, progress)?;
Ok(Prerendered { path: r.path, width: comp.width, height: comp.height, frame_rate: comp.frame_rate, duration: end - start })
+4
View File
@@ -15,6 +15,10 @@ pub enum EngineError {
Project(#[from] effectcraft_project::ProjectError),
#[error("{0}")]
Other(String),
/// Refused because parts would be lost in a conversion; nothing changed. `losses` names them,
/// `advice` how to go on (`file.importAep` / `file.exportAep` without `allowLosses`).
#[error("{losses} ({advice})")]
Refused { losses: String, advice: &'static str },
}
pub type Result<T> = std::result::Result<T, EngineError>;
+11 -4
View File
@@ -23,6 +23,12 @@
//! Libraries, Bridge, Media Encoder, Behance, Creative Cloud…) are intentionally absent; the
//! Essential Graphics workspace uses the Properties panel and the Essential Graphics panel, whose
//! templates use EffectCraft's own open `.ectemplate` format.
//!
//! Where EffectCraft has entries After Effects lacks, After Effects' entries keep their order and
//! the others get a fixed place. File ▸ Import: the project imports sit together where After
//! Effects has Adobe Premiere Pro Project…, in alphabetical order and named in full (Adobe After
//! Effects Project…, Adobe Premiere Pro Project…, EffectCraft Project…); the formats only
//! EffectCraft imports follow a separator at the end, in alphabetical order (#3103).
use std::sync::OnceLock;
@@ -577,14 +583,15 @@ File
Import
File... | file.import
Multiple Files... | file.importMultiple
Adobe After Effects Project... | file.importAep
Adobe Premiere Pro Project... | file.importTimeline
EffectCraft Project... | file.importProject
Vanishing Point (.vpe)... | file.importVanishingPoint
Placeholder... | file.importPlaceholder
Solid... | file.importSolid
After Effects Project... | file.importAep
Lottie... | file.importLottie
Vanishing Point (.vpe)... | file.importVanishingPoint
---
Essential Graphics Template... | essential.importTemplate
Lottie... | file.importLottie
Import Recent Footage
@dynamic:recentFootage
---
@@ -592,7 +599,7 @@ File
Export
Add to Render Queue | renderQueue.add
Adobe Premiere Pro Project... | file.exportTimeline
After Effects Project... | file.exportAep
Adobe After Effects Project... | file.exportAep
Lottie JSON... | file.exportLottie
Essential Graphics Template... | essential.exportTemplate
---
+46
View File
@@ -34,6 +34,42 @@ pub struct ExportJob<'a> {
pub label: String,
/// Settings ▸ General ▸ Switches Affect Nested Comps.
pub nested_switches: bool,
/// The layer caches of the Render Queue run this job belongs to (#3335): consecutive jobs
/// start warm, and the run trims the accelerator's pooled textures once at its end. `None`:
/// a one-off export (its own caches, trimmed when it ends).
pub caches: Option<&'a RunCaches>,
/// The compositor the queue item's output modules render on (#3335): the first module
/// chooses (measuring a GPU project's first frames) and stores it here, the others follow.
pub renderer: Option<&'a Mutex<Option<effectcraft_render::Backend>>>,
/// Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once: 1 to 3, `None` for Auto.
pub gpu_frames: Option<usize>,
}
/// What the jobs of one Render Queue run keep between them (#3335): a layer cache per
/// compositor (a buffer an effect made on the GPU is not the CPU's), together at the budget
/// each job's single cache had before.
pub struct RunCaches {
pub gpu: effectcraft_render::LayerCache,
pub cpu: effectcraft_render::LayerCache,
}
impl Default for RunCaches {
fn default() -> Self {
let half = effectcraft_render::LayerCache::default().budget() / 2;
RunCaches { gpu: effectcraft_render::LayerCache::new(half), cpu: effectcraft_render::LayerCache::new(half) }
}
}
/// Calls [`effectcraft_render::Accelerator::render_end`] when a Render Queue run ends, however
/// it ends (done, failed, stopped, panicked): its jobs leave the pooled textures to each other.
struct RunEnd<'a>(Option<&'a dyn effectcraft_render::Accelerator>);
impl Drop for RunEnd<'_> {
fn drop(&mut self) {
if let Some(a) = self.0 {
a.render_end();
}
}
}
/// What an export wrote.
@@ -212,6 +248,8 @@ struct Work {
items: Vec<Vec<(RenderQueueItem, String)>>,
/// Settings ▸ General ▸ Switches Affect Nested Comps.
nested_switches: bool,
/// Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once.
gpu_frames: Option<usize>,
}
/// Where a relative render output goes when there is no default folder and the project is unsaved:
@@ -307,6 +345,7 @@ pub(crate) async fn run_items(
storage: s.storage_quota.clone(),
items,
nested_switches: s.prefs.general.switches_affect_nested_comps,
gpu_frames: s.prefs.memory.gpu_frames(),
};
run_work(work, shared, notify).await;
}
@@ -317,6 +356,8 @@ async fn run_work(w: Work, shared: &JobShared, notify: &mut dyn FnMut(&JobShared
let mut s = lock(&shared.state);
s.items_total = w.items.len();
}
let caches = RunCaches::default();
let _run_end = RunEnd(w.accel.as_deref());
for (k, modules) in w.items.iter().enumerate() {
let Some((item, _)) = modules.first() else { continue };
if shared.cancel.load(Ordering::Relaxed) {
@@ -337,6 +378,7 @@ async fn run_work(w: Work, shared: &JobShared, notify: &mut dyn FnMut(&JobShared
// Every output module encodes the same frames (Composition ▸ Add Output Module); the
// first module's file is the item's output.
let mut r: Result<ExportResult, String> = Err("no output module".into());
let renderer = Mutex::new(None);
for (mi, (mitem, path)) in modules.iter().enumerate() {
let name = w.project.item(mitem.comp).map(|i| i.name.clone()).unwrap_or_default();
let index = w.project.render_queue.iter().position(|i| i.id == mitem.id).map_or(0, |i| i + 1);
@@ -350,6 +392,9 @@ async fn run_work(w: Work, shared: &JobShared, notify: &mut dyn FnMut(&JobShared
storage: w.storage.as_deref(),
label: format!("#{index} {name} (output module {})", mi + 1),
nested_switches: w.nested_switches,
caches: Some(&caches),
renderer: Some(&renderer),
gpu_frames: w.gpu_frames,
};
let rr = w
.exporter
@@ -610,6 +655,7 @@ impl Session {
storage: self.storage_quota.clone(),
items,
nested_switches: self.prefs.general.switches_affect_nested_comps,
gpu_frames: self.prefs.memory.gpu_frames(),
};
let shared = Arc::new(JobShared::default());
if wait {
+89
View File
@@ -248,3 +248,92 @@ fn locked_resize_uses_height_for_height_only_output_module_updates() {
let comp = s.project.comp(item.comp).unwrap();
assert_eq!(item.output.output_size(comp, &item.settings), (32, 32), "explicit width retains precedence");
}
#[test]
fn a_queue_run_keeps_its_jobs_warm() {
// #3335: every job started with a fresh layer cache and trimmed the GPU's texture pool when
// it ended, so each item's first frames were cold. A run's jobs share its caches; the pool is
// trimmed once, when the run ends; an item's output modules share one compositor choice.
use std::sync::atomic::{AtomicUsize, Ordering};
struct Recorder(Mutex<Vec<(Option<usize>, Option<usize>)>>);
impl Exporter for Recorder {
fn formats(&self) -> Vec<OutputFormat> {
OutputFormat::ALL.to_vec()
}
fn export(&self, job: &ExportJob, _: &mut dyn FnMut(u64, u64) -> bool) -> Result<ExportResult, String> {
let caches = job.caches.map(|c| c as *const _ as usize);
let slot = job.renderer.map(|r| {
// The first module chooses; the second sees its choice.
r.lock().unwrap().get_or_insert(effectcraft_render::Backend::Cpu);
r as *const _ as usize
});
self.0.lock().unwrap().push((caches, slot));
Ok(ExportResult { path: job.path.to_string(), ..Default::default() })
}
}
#[derive(Default)]
struct Ends(AtomicUsize);
impl effectcraft_render::Accelerator for Ends {
fn name(&self) -> String {
"ends".into()
}
fn comp_frame(&self, _: &effectcraft_render::Renderer, _: effectcraft_project::ItemId, _: effectcraft_time::Tick) -> Option<effectcraft_raster::Image> {
None
}
fn supports_effect(&self, _: &str) -> bool {
false
}
fn effects(&self, _: &[effectcraft_render::FxStep], _: &effectcraft_effects::Buf, _: Option<f32>) -> Option<effectcraft_effects::Buf> {
None
}
fn render_end(&self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
let mut s = demo();
let ex = Arc::new(Recorder(Mutex::new(vec![])));
s.exporter = Some(ex.clone());
let ends = Arc::new(Ends::default());
s.accel = Some(ends.clone());
s.execute("renderQueue.add", json!({"output": crate::tests::stubs::tmp_out("rq/warm_a.mp4")})).unwrap();
s.execute("render.addOutputModule", json!({"format": "png"})).unwrap();
s.execute("renderQueue.add", json!({"output": crate::tests::stubs::tmp_out("rq/warm_b.mp4")})).unwrap();
s.execute("renderQueue.render", json!({})).unwrap();
let jobs = ex.0.lock().unwrap().clone();
assert_eq!(jobs.len(), 3, "{jobs:?}");
assert!(jobs.iter().all(|(c, r)| c.is_some() && r.is_some()), "{jobs:?}");
assert!(jobs.iter().all(|(c, _)| *c == jobs[0].0), "every job of the run shares its caches");
assert_eq!(jobs[0].1, jobs[1].1, "an item's output modules share one choice");
assert_eq!(ends.0.load(Ordering::SeqCst), 1, "the pool is trimmed once, after the run");
}
#[test]
fn the_gpu_frames_setting_reaches_every_render_queue_job() {
// #3335: Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once is Auto (`None`) or 1–3.
struct Seen(Mutex<Vec<Option<usize>>>);
impl Exporter for Seen {
fn formats(&self) -> Vec<OutputFormat> {
OutputFormat::ALL.to_vec()
}
fn export(&self, job: &ExportJob, _: &mut dyn FnMut(u64, u64) -> bool) -> Result<ExportResult, String> {
self.0.lock().unwrap().push(job.gpu_frames);
Ok(ExportResult { path: job.path.to_string(), ..Default::default() })
}
}
let mut s = demo();
let ex = Arc::new(Seen(Mutex::new(vec![])));
s.exporter = Some(ex.clone());
assert_eq!(s.prefs.memory.gpu_frames_in_flight, "auto");
let pages = crate::settings::prefs::pages_json();
assert!(pages.to_string().contains("memory.gpuFramesInFlight"), "the Memory & CPU page lists it");
for (value, want) in [("auto", None), ("1", Some(1)), ("2", Some(2)), ("3", Some(3))] {
s.execute("prefs.set", json!({"key": "memory.gpuFramesInFlight", "value": value})).unwrap();
assert_eq!(s.prefs.memory.gpu_frames(), want, "{value}");
s.execute("renderQueue.add", json!({"output": crate::tests::stubs::tmp_out(&format!("rq/frames_{value}.mp4"))})).unwrap();
s.execute("renderQueue.render", json!({})).unwrap();
assert_eq!(ex.0.lock().unwrap().last().copied(), Some(want), "{value}");
}
// A hand-edited settings file's nonsense is Auto.
s.prefs.memory.gpu_frames_in_flight = "9".into();
assert_eq!(s.prefs.memory.gpu_frames(), None);
}
@@ -310,6 +310,7 @@ pub fn pages() -> Vec<Page> {
s("memory.mediaCacheMb", "Footage Frame Cache", Kind::Int(64, 1 << 20, "MB"), true),
s("memory.previewCacheMb", "Preview (RAM) Cache", Kind::Int(64, 1 << 20, "MB"), true),
s("memory.reduceCacheWhenLow", "Reduce Cache Size When System Is Low on Memory", B, true),
s("memory.gpuFramesInFlight", "GPU Frames Rendered at Once", Kind::Choice(&[("Auto", "auto"), ("1", "1"), ("2", "2"), ("3", "3")]), true),
],
},
Page {
@@ -201,8 +201,24 @@ page!(Memory {
/// RAM preview (viewer frame) cache budget (MB).
preview_cache_mb: u32 = 3072,
reduce_cache_when_low: bool = true,
/// GPU Frames Rendered at Once (Render Queue): `auto` (from the GPU memory a frame
/// measured, 1 to 3) or `1`, `2`, `3`. `EFFECTCRAFT_GPU_INFLIGHT` overrides it.
gpu_frames_in_flight: String = "auto".into(),
});
impl Memory {
/// [`Memory::gpu_frames_in_flight`] as a count: 1 to 3, or `None` for Auto (and anything
/// else, as a hand-edited settings file may hold).
pub fn gpu_frames(&self) -> Option<usize> {
match self.gpu_frames_in_flight.trim() {
"1" => Some(1),
"2" => Some(2),
"3" => Some(3),
_ => None,
}
}
}
page!(Video {
enable_output: bool = false,
device: String = String::new(),
+7 -4
View File
@@ -304,7 +304,8 @@ pub fn parse_catalog(bytes: &[u8]) -> std::result::Result<(Catalog, Vec<String>)
fn runs_here(r: &Release) -> bool {
// Without a pre-release suffix (`0.18.0-dev` is 0.18.0).
let app = env!("CARGO_PKG_VERSION").split(['-', '+']).next().unwrap_or_default();
r.api == crate::effects::plugin::WASM_API_VERSION && r.min_app.as_deref().is_none_or(|m| !newer(m, app))
(crate::effects::plugin::MIN_WASM_API_VERSION..=crate::effects::plugin::WASM_API_VERSION).contains(&r.api)
&& r.min_app.as_deref().is_none_or(|m| !newer(m, app))
}
/// The newest release this build can run (plug-in API and minimum app version).
@@ -789,12 +790,13 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
return Err(bad(
c,
format!(
"{} {} needs plug-in API {} and EffectCraft {} or later; this is EffectCraft {} with WebAssembly plug-in API {}",
"{} {} needs WebAssembly ABI {} and EffectCraft {} or later; this is EffectCraft {} with WebAssembly ABI {} to {}",
pkg.name,
rel.version,
rel.api,
rel.min_app.as_deref().unwrap_or("(any)"),
env!("CARGO_PKG_VERSION"),
crate::effects::plugin::MIN_WASM_API_VERSION,
crate::effects::plugin::WASM_API_VERSION
),
));
@@ -1125,8 +1127,9 @@ mod tests {
..Default::default()
};
assert_eq!(latest_compatible(&p).map(|r| r.version.as_str()), Some("1.2.0"));
assert!(runs_here(&rel("1.0.0", 1, None)), "API 1 modules still run on a build with a newer plug-in API");
assert!(!runs_here(&rel("1.0.0", 2, None)), "WebAssembly modules are API 1: an API 2 release can't load");
assert!(runs_here(&rel("1.0.0", 1, None)), "ABI 1 modules still run on a build with a newer ABI");
assert!(runs_here(&rel("1.0.0", 2, None)), "ABI 2 modules run on this build");
assert!(!runs_here(&rel("1.0.0", 3, None)), "a WebAssembly ABI this build doesn't implement can't load");
let j = package_json(&p, None, &Status::default());
assert_eq!(j["needsNewerApp"], true);
assert!(latest_compatible(&Package::default()).is_none());
+53 -2
View File
@@ -214,16 +214,31 @@ fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> {
}
}
let path = numbered(dir, 0);
let file = File::create(&path)?;
let file = create_session_file(&path)?;
Ok((path, file))
}
// Another process may create the current log after our rotation. Never truncate its
// file: its writer and crash reporter may already hold the same inode open.
fn create_session_file(path: &Path) -> std::io::Result<File> {
std::fs::OpenOptions::new().write(true).create_new(true).open(path)
}
/// Rotate the session logs in `dir` (the `Logs` folder, see [`log_dir`]) and write records to
/// a fresh `effectcraft.log` there, first the ones logged so far; returns its path.
pub fn attach_session_log(dir: &Path) -> Result<PathBuf, String> {
attach_session_log_with_header(dir, "")
}
/// Open a session log with a bounded, unfiltered identity header before buffered records.
/// A crash reporter can then recognize this run even with logging disabled or verbose startup.
pub fn attach_session_log_with_header(dir: &Path, header: &str) -> Result<PathBuf, String> {
if header.len() > 1024 {
return Err("session log header exceeds 1024 bytes".into());
}
let (path, file) = rotate(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
let mut sink = SESSION.lock().unwrap_or_else(PoisonError::into_inner);
sink.attach(file);
sink.attach_with_header(file, header);
sink.path = Some(path.clone());
Ok(path)
}
@@ -264,8 +279,14 @@ impl Sink {
}
/// Start writing to `file`, first the lines kept so far.
#[cfg(test)]
fn attach(&mut self, file: File) {
self.attach_with_header(file, "");
}
fn attach_with_header(&mut self, file: File, header: &str) {
self.file = Some(file);
self.append(header);
for line in std::mem::take(&mut self.pending) {
self.append(&line);
}
@@ -333,6 +354,22 @@ mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
#[test]
fn identity_header_precedes_buffered_records_independent_of_log_filter() {
let dir = temp_dir("identity-header");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("session");
let mut sink = Sink::new(MAX_FILE_BYTES);
sink.write(&"earlier startup record".repeat(1000));
sink.attach_with_header(File::create(&path).unwrap(), "crash-report session 1-2\n");
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.starts_with("crash-report session 1-2\n"));
assert!(text.contains("earlier startup record"));
assert!(attach_session_log_with_header(&dir, &"x".repeat(1025)).is_err());
drop(sink);
std::fs::remove_dir_all(dir).unwrap();
}
/// `RUST_LOG` picks what start-up prints to stderr (#234).
#[test]
fn rust_log_filters_by_level_and_target() {
@@ -397,6 +434,20 @@ mod tests {
assert_eq!(timestamp(u64::MAX), "584556019-04-03T14:25:51.615Z");
}
#[test]
fn concurrent_session_creation_does_not_truncate_the_winner() {
let dir = temp_dir("concurrent-create");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(SESSION_LOG);
let mut winner = create_session_file(&path).unwrap();
winner.write_all(b"crash-report session 1-2\nwinner content\n").unwrap();
assert_eq!(create_session_file(&path).unwrap_err().kind(), std::io::ErrorKind::AlreadyExists);
winner.write_all(b"still the same session\n").unwrap();
assert_eq!(read(&path), "crash-report session 1-2\nwinner content\nstill the same session\n");
drop(winner);
std::fs::remove_dir_all(dir).unwrap();
}
/// Each launch keeps the previous runs' logs, so the log of a run that crashed survives.
#[test]
fn rotation_keeps_the_previous_logs_and_starts_an_empty_file() {
+3 -2
View File
@@ -1,4 +1,4 @@
//! File ▸ Export ▸ After Effects Project… through the command registry.
//! File ▸ Export ▸ Adobe After Effects Project… through the command registry.
use serde_json::json;
@@ -73,11 +73,12 @@ fn bad_input_is_an_error_not_a_crash() {
assert!(empty.execute_checked("file.exportAep", json!({"path": path})).is_ok());
}
/// File ▸ Export ▸ Adobe After Effects Project… is in the menu under that label.
#[test]
fn aep_export_menu_entry_exists() {
let entries = crate::menus::entries();
let find = |cmd: &str| entries.iter().find(|(_, e)| e.command == cmd).map(|(p, e)| (p.join(" > "), e.label.clone()));
assert_eq!(find("file.exportAep"), Some(("File > Export".into(), "After Effects Project...".into())));
assert_eq!(find("file.exportAep"), Some(("File > Export".into(), "Adobe After Effects Project...".into())));
}
#[test]
+87 -2
View File
@@ -1,4 +1,4 @@
//! File ▸ Import ▸ After Effects Project… through the command registry, with the synthetic
//! File ▸ Import ▸ Adobe After Effects Project… through the command registry, with the synthetic
//! fixtures of `effectcraft-aep`.
use serde_json::json;
@@ -147,11 +147,96 @@ fn bad_input_is_an_error_not_a_crash() {
assert!(s.project.items.is_empty());
}
/// File ▸ Import: After Effects' own entries in its order, the project imports together where
/// After Effects has Adobe Premiere Pro Project… (alphabetical, named in full), then a separator
/// and the formats only EffectCraft imports, alphabetical (#3103).
#[test]
fn the_import_menu_groups_the_project_imports() {
use crate::menus::MenuNode;
let file = crate::menus::menu_bar().iter().find_map(|n| match n {
MenuNode::Submenu { label, children } if label == "File" => Some(children),
_ => None,
});
let import = file.into_iter().flatten().find_map(|n| match n {
MenuNode::Submenu { label, children } if label == "Import" => Some(children),
_ => None,
});
let labels: Vec<&str> = import
.into_iter()
.flatten()
.map(|n| match n {
MenuNode::Item(e) => e.label.as_str(),
MenuNode::Separator => "---",
MenuNode::Submenu { label, .. } => label.as_str(),
MenuNode::Dynamic { .. } => "@dynamic",
})
.collect();
assert_eq!(
labels,
[
"File...",
"Multiple Files...",
"Adobe After Effects Project...",
"Adobe Premiere Pro Project...",
"EffectCraft Project...",
"Vanishing Point (.vpe)...",
"Placeholder...",
"Solid...",
"---",
"Essential Graphics Template...",
"Lottie...",
]
);
}
/// `file.import` takes After Effects projects as After Effects' File… does, for scripts and
/// agents: one without losses imports as `file.importAep` does; one with losses is refused and
/// changes nothing, naming the menu entry that lists the losses; a file that is not one says why.
#[test]
fn file_import_takes_after_effects_projects() {
assert!(crate::cmd::is_aep_path("/a/Intro.aep") && crate::cmd::is_aep_path("/a/Intro.AEPX"));
assert!(!crate::cmd::is_aep_path("/a/Intro.ecproj") && !crate::cmd::is_aep_path("/a/aep") && !crate::cmd::is_aep_path("/a/clip.aep.mp4"));
let dir = std::env::temp_dir().join(format!("ec-file-import-aep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let message = |r: crate::Result<serde_json::Value>| match r {
Ok(v) => v["errors"].to_string(),
Err(e) => e.to_string(),
};
let intro = dir.join("Intro.aep").to_string_lossy().to_string();
let mut source = Session::default();
source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap();
source.execute("file.exportAep", json!({"path": intro, "allowLosses": true})).unwrap();
let mut s = Session::default();
let r = s.execute("file.import", json!({"paths": [intro]})).unwrap();
assert_eq!(r["projects"].as_array().map(Vec::len), Some(1), "{r}");
let folder = s.project.items.values().find(|i| i.name == "Intro.aep").map(|i| i.id);
assert!(folder.is_some(), "the project's folder");
assert!(s.project.items.values().any(|i| i.name == "Intro" && i.parent == folder), "its composition in the folder");
let lossy = concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep");
let mut s = Session::default();
let refused = message(s.execute("file.import", json!({"paths": [lossy]})));
assert!(refused.contains("synthetic.aep") && refused.contains("File ▸ Import ▸ Adobe After Effects Project… lists them"), "{refused}");
assert!(refused.contains("`file.importAep` with `allowLosses`"), "and the command that takes the consent: {refused}");
assert!(s.project.items.is_empty(), "nothing imported");
let junk = dir.join("Junk.AEPX");
std::fs::write(&junk, b"not a project").unwrap();
let mut s = Session::default();
let error = message(s.execute("file.import", json!({"paths": [junk.to_string_lossy()]})));
assert!(error.contains("Junk.AEPX"), "{error}");
assert!(s.project.items.is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
/// File ▸ Import ▸ Adobe After Effects Project… is in the menu under that label.
#[test]
fn aep_menu_entry_exists() {
let entries = crate::menus::entries();
let find = |cmd: &str| entries.iter().find(|(_, e)| e.command == cmd).map(|(p, e)| (p.join(" > "), e.label.clone()));
assert_eq!(find("file.importAep"), Some(("File > Import".into(), "After Effects Project...".into())));
assert_eq!(find("file.importAep"), Some(("File > Import".into(), "Adobe After Effects Project...".into())));
}
#[test]
+37 -1
View File
@@ -53,6 +53,7 @@ pub use effectcraft_project::render_queue;
pub use effectcraft_project::render_queue::StorageQuota;
pub use out::Sink;
pub(crate) use pipeline::Cx;
pub use pipeline::{CACHE_BUDGET, GPU_INFLIGHT_MAX, RENDERER_ENV, auto_gpu_frames, pick_renderer};
#[derive(Debug, thiserror::Error)]
pub enum ExportError {
@@ -107,6 +108,40 @@ pub struct JobOptions<'a> {
pub storage: Option<&'a dyn StorageQuota>,
/// Overflow folders, in order.
pub overflow: Vec<String>,
/// Layer caches shared by the consecutive jobs of a Render Queue run (#3335): the job
/// renders through them instead of fresh ones, and leaves the accelerator's pooled working
/// textures for the next job (whoever holds the caches calls
/// [`effectcraft_render::Accelerator::render_end`] after the last one). `None`: the job
/// starts cold and trims the pool when it ends, as a one-off export does.
pub caches: Option<JobCaches<'a>>,
/// The compositor of the job's frames (`Backend::Gpu` or `Backend::Cpu`; `Auto` counts as unset), shared by the output modules of
/// one queue item: when it holds a choice the job renders on it; otherwise the job stores
/// the compositor it chose there, so the item's other output modules encode the same
/// frames. `None`: the job decides alone.
pub renderer: Option<&'a std::sync::Mutex<Option<effectcraft_render::Backend>>>,
/// Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once: 1 to 3, or `None` for Auto (from
/// the device memory a frame measured, #3335). `EFFECTCRAFT_GPU_INFLIGHT` overrides it.
pub gpu_frames: Option<usize>,
}
/// The layer caches of a Render Queue run (see [`JobOptions::caches`]): one per compositor,
/// because a buffer an effect made on the GPU is not the CPU's, and neither may stand in for
/// the other.
#[derive(Clone, Copy)]
pub struct JobCaches<'a> {
pub gpu: &'a effectcraft_render::LayerCache,
pub cpu: &'a effectcraft_render::LayerCache,
}
/// The compositor a job's frames render on: the GPU's (GPU Acceleration, with an
/// accelerator) or the CPU's frame-parallel one (Software Rendering). The whole job renders
/// on one of them, never a mix: the two differ slightly. A GPU project with an accelerator
/// measures both on its first frames and renders the job on the faster (#3335); the choice is
/// in the render log. `EFFECTCRAFT_RQ_RENDERER=gpu|cpu|auto` overrides the measurement.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum JobRenderer {
Gpu,
Cpu,
}
/// Progress after a batch of frames.
@@ -176,7 +211,8 @@ pub fn export(job: &Job, progress: &mut dyn FnMut(&Progress) -> bool) -> Result<
pub async fn export_async(job: &Job<'_>, progress: &mut dyn FnMut(&Progress) -> bool) -> Result<Report> {
let t0 = Instant::now();
let cx = Cx::new(job);
let render = RenderEnd(job.accel);
// A job of a Render Queue run leaves the pooled working textures warm for the next one.
let render = RenderEnd(job.accel.filter(|_| job.options.caches.is_none()));
let mut r = export_with(&cx, progress, t0).await;
drop(render);
if matches!(r, Err(ExportError::Cancelled)) {
+577 -47
View File
@@ -13,30 +13,107 @@ use web_time::Instant;
use crate::{Job, RenderQuality, out};
/// GPU frames rendered at once by default: one, as the export scheduler's single GPU worker
/// renders image sequences (#898: frames in flight beyond one only with asynchronous YUV
/// readback). Measured on the #879 project at 1080p on an 8 GB card: 6.3 GB peak with one,
/// 7.0 to 7.8 GB with two, and the whole card with three, where main fell back to the CPU.
/// Not sized from free VRAM: the GPU backend reports no free or total device memory on every
/// platform, so a portable estimate is not available here.
/// GPU frames rendered at once until a frame has measured the device memory, and whenever it
/// can't be measured (#898, #879: measured on the #879 project at 1080p on an 8 GB card, 6.3 GB
/// peak with one frame, 7.0 to 7.8 GB with two, the whole card with three).
pub(crate) const GPU_INFLIGHT_DEFAULT: usize = 1;
/// Overrides [`GPU_INFLIGHT_DEFAULT`]: a whole number of frames, at least 1.
/// The most GPU frames Auto renders at once (#3335: four and more measured no faster).
pub const GPU_INFLIGHT_MAX: usize = 3;
/// Overrides Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once: a whole number of frames,
/// at least 1.
pub(crate) const GPU_INFLIGHT_ENV: &str = "EFFECTCRAFT_GPU_INFLIGHT";
/// GPU frames in flight from the `EFFECTCRAFT_GPU_INFLIGHT` value: a positive whole number, or
/// the default (with a warning when a value was given) when it is 0, negative, not a number or
/// too large.
pub(crate) fn gpu_inflight(var: Option<&str>) -> usize {
let Some(v) = var else { return GPU_INFLIGHT_DEFAULT };
/// `None` (with a warning when a value was given) when it is unset, 0, negative, not a number
/// or too large.
pub(crate) fn gpu_inflight_env(var: Option<&str>) -> Option<usize> {
let v = var?;
match v.trim().parse::<usize>() {
Ok(n) if n >= 1 => n,
Ok(n) if n >= 1 => Some(n),
_ => {
log::warn!(target: "effectcraft_export", "{GPU_INFLIGHT_ENV}={v:?} is not a whole number of frames of at least 1: using {GPU_INFLIGHT_DEFAULT}");
GPU_INFLIGHT_DEFAULT
log::warn!(target: "effectcraft_export", "{GPU_INFLIGHT_ENV}={v:?} is not a whole number of frames of at least 1: ignored");
None
}
}
}
/// Auto's GPU frames in flight from the device memory after a frame (#3335): as many more
/// frames as the budget has room for beyond the allocator's peak (which holds one frame's
/// working memory) and what the caches may still grow by, 1 to [`GPU_INFLIGHT_MAX`]. Unknown
/// memory: 1.
pub fn auto_gpu_frames(m: Option<effectcraft_render::FrameMemory>) -> usize {
let Some(m) = m.filter(|m| m.budget > 0 && m.frame > 0 && m.peak > 0) else { return GPU_INFLIGHT_DEFAULT };
let more = m.budget.saturating_sub(m.peak).saturating_sub(m.reserve) / m.frame;
usize::try_from(more.saturating_add(1)).unwrap_or(GPU_INFLIGHT_MAX).clamp(1, GPU_INFLIGHT_MAX)
}
/// The GPU frames a job renders at once and why (for the render log): the environment's
/// override, else the setting (`pref`, `None` = Auto), else Auto from `measured` (`None`: no
/// GPU frame has rendered yet, so nothing is settled).
pub(crate) fn resolve_gpu_frames(env: Option<&str>, pref: Option<usize>, measured: Option<Option<effectcraft_render::FrameMemory>>) -> Option<(usize, String)> {
if let Some(n) = gpu_inflight_env(env) {
return Some((n, format!("set by {GPU_INFLIGHT_ENV}")));
}
if let Some(n) = pref.filter(|n| *n >= 1) {
return Some((n, "Settings".into()));
}
let m = measured?;
let n = auto_gpu_frames(m);
let mib = |b: u64| b >> 20;
Some((
n,
match m {
Some(m) => format!(
"Auto: {} MiB per frame, {} MiB peak and {} MiB cache reserve in a {} MiB budget",
mib(m.frame),
mib(m.peak),
mib(m.reserve),
mib(m.budget)
),
None => "Auto: the GPU's memory is not known".into(),
},
))
}
/// Overrides the Render Queue's measured choice of compositor for GPU projects with an
/// accelerator (#3335): `gpu` (no measurement, the GPU), `cpu` (Software Rendering) or
/// `auto` (measure, the default).
pub const RENDERER_ENV: &str = "EFFECTCRAFT_RQ_RENDERER";
/// Frames the GPU renders, timed, when a job measures which compositor is faster (after one
/// untimed warm-up frame each: layer cache misses, uploads, kernel builds).
const PROBE_FRAMES: usize = 2;
/// Frames the CPU renders at once, timed, in that measurement: its throughput comes from
/// rendering several frames in parallel, which two frames understate.
const PROBE_CPU_FRAMES: usize = 8;
/// Jobs shorter than this render on the project's compositor without measuring: the frames
/// the slower one renders for the measurement would cost more than they could save.
const PROBE_MIN_JOB: u64 = 12;
/// Bytes each of a job's two layer caches (one per compositor) may hold: together what the
/// job's single cache held before (`LayerCache::default()`).
pub const CACHE_BUDGET: usize = 1 << 29;
/// The faster compositor from measured milliseconds per frame (`None`: not measured, or the
/// GPU stopped). As the viewer's Auto ([`effectcraft_render::AutoPick::MARGIN`]), the CPU wins
/// only when clearly faster: ties and noise stay on the GPU.
pub fn pick_renderer(gpu_ms: Option<f64>, cpu_ms: Option<f64>) -> crate::JobRenderer {
match (gpu_ms, cpu_ms) {
(Some(g), Some(c)) if g.is_finite() && c.is_finite() && c < g * effectcraft_render::AutoPick::MARGIN => crate::JobRenderer::Cpu,
(None, Some(_)) => crate::JobRenderer::Cpu,
_ => crate::JobRenderer::Gpu,
}
}
/// How a job's compositor was chosen, for the render log.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Choice {
pub renderer: crate::JobRenderer,
/// Measured ms per frame on each (`None`: not measured).
pub gpu_ms: Option<f64>,
pub cpu_ms: Option<f64>,
pub why: &'static str,
}
/// Caps CPU frames in flight (#1615): a whole number of frames, at least 1. Unset, CPU renders
/// keep one frame per thread in flight as before. A heavy comp can need gigabytes of working
/// memory per frame, so 16 threads at once can get the process OOM-killed; the headless CLI
@@ -168,8 +245,14 @@ pub(crate) struct Cx<'a> {
pub project: Project,
/// Processed layer buffers shared by the job's frames, as the viewer's layer cache: static
/// and transform-only layers render once per job (the GPU compositor then uploads them
/// once). Dropped with the job.
cache: LayerCache,
/// once). One per compositor; the job's own (dropped with it) or a Render Queue run's.
caches: Caches<'a>,
/// The compositor the job renders on, once chosen ([`Cx::backend`]).
choice: Mutex<Option<Choice>>,
/// GPU frames rendered at once and why, once settled ([`Cx::gpu_inflight`]).
gpu_frames: Mutex<Option<(usize, String)>>,
/// A frame has rendered on the GPU compositor: its memory can be measured.
gpu_rendered: std::sync::atomic::AtomicBool,
/// (output frame, seconds) of every rendered frame (Plus Per Frame Info logs).
pub frame_times: Option<Mutex<Vec<(u64, f64)>>>,
/// Files written to an overflow folder.
@@ -177,6 +260,23 @@ pub(crate) struct Cx<'a> {
written: Mutex<Vec<String>>,
}
/// A job's layer caches: its own, or those of the Render Queue run it belongs to.
enum Caches<'a> {
Own { gpu: Box<LayerCache>, cpu: Box<LayerCache> },
Run(crate::JobCaches<'a>),
}
impl Caches<'_> {
fn of(&self, r: crate::JobRenderer) -> &LayerCache {
match (self, r) {
(Caches::Own { gpu, .. }, crate::JobRenderer::Gpu) => gpu,
(Caches::Own { cpu, .. }, crate::JobRenderer::Cpu) => cpu,
(Caches::Run(c), crate::JobRenderer::Gpu) => c.gpu,
(Caches::Run(c), crate::JobRenderer::Cpu) => c.cpu,
}
}
}
impl<'a> std::ops::Deref for Cx<'a> {
type Target = Job<'a>;
fn deref(&self) -> &Job<'a> {
@@ -222,10 +322,95 @@ impl<'a> Cx<'a> {
// layer and effect quantises at it), not just the finished frame (#2022).
project.settings.bit_depth = job.settings.color_depth.resolve(project.settings.bit_depth);
let frame_times = (job.options.log == RenderLog::PlusPerFrameInfo).then(|| Mutex::new(Vec::new()));
let cache = LayerCache::default();
let caches = match job.options.caches {
Some(c) => Caches::Run(c),
None => Caches::Own { gpu: Box::new(LayerCache::new(CACHE_BUDGET)), cpu: Box::new(LayerCache::new(CACHE_BUDGET)) },
};
// With deferred readbacks, buffers computed from a missed pass's placeholders are not kept.
cache.set_gate(job.accel.and_then(|a| a.miss_gate()));
Cx { job, project, cache, frame_times, overflowed: Mutex::new(vec![]), written: Mutex::new(vec![]) }
for r in [crate::JobRenderer::Gpu, crate::JobRenderer::Cpu] {
caches.of(r).set_gate(job.accel.and_then(|a| a.miss_gate()));
}
let cx = Cx {
job,
project,
caches,
choice: Mutex::new(None),
gpu_frames: Mutex::new(None),
gpu_rendered: Default::default(),
frame_times,
overflowed: Mutex::new(vec![]),
written: Mutex::new(vec![]),
};
if let Some(c) = cx.fixed_choice(std::env::var(RENDERER_ENV).ok().as_deref()) {
cx.decide(c);
}
cx
}
/// The project's compositor: the GPU's for GPU Acceleration with an accelerator.
fn project_renderer(&self) -> crate::JobRenderer {
if self.project.settings.gpu_acceleration && self.job.accel.is_some() { crate::JobRenderer::Gpu } else { crate::JobRenderer::Cpu }
}
/// The job's compositor when it is decided without measuring: the queue item's first
/// output module's, the project's when there is nothing to choose between (Software Only,
/// no accelerator, deferred readbacks), or `env` ([`RENDERER_ENV`]). `None`: measure.
fn fixed_choice(&self, env: Option<&str>) -> Option<Choice> {
let fixed = |renderer, why| Some(Choice { renderer, gpu_ms: None, cpu_ms: None, why });
if self.project_renderer() == crate::JobRenderer::Cpu {
return fixed(crate::JobRenderer::Cpu, "the project's renderer");
}
if self.deferred() {
return fixed(crate::JobRenderer::Gpu, "the project's renderer");
}
let shared = self.job.options.renderer.and_then(|s| *s.lock().unwrap_or_else(std::sync::PoisonError::into_inner));
let shared = match shared {
Some(Backend::Gpu) => Some(crate::JobRenderer::Gpu),
Some(Backend::Cpu) => Some(crate::JobRenderer::Cpu),
Some(Backend::Auto) | None => None,
};
if let Some(r) = shared {
return Some(Choice { renderer: r, gpu_ms: None, cpu_ms: None, why: "as the queue item's first output module" });
}
match env.map(|v| v.trim().to_ascii_lowercase()).as_deref() {
Some("gpu") => fixed(crate::JobRenderer::Gpu, "set by EFFECTCRAFT_RQ_RENDERER"),
Some("cpu") => fixed(crate::JobRenderer::Cpu, "set by EFFECTCRAFT_RQ_RENDERER"),
None | Some("" | "auto") => None,
Some(v) => {
log::warn!(target: "effectcraft_export", "{RENDERER_ENV}={v:?} is not gpu, cpu or auto: measuring");
None
}
}
}
/// Settle the job's compositor (once): remember it for the queue item's other output
/// modules and say so in the log.
fn decide(&self, c: Choice) {
let mut slot = self.choice.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
if slot.is_some() {
return;
}
*slot = Some(c);
if let Some(shared) = self.job.options.renderer {
let mut shared = shared.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
if shared.is_none_or(|b| b == Backend::Auto) {
*shared = Some(match c.renderer {
crate::JobRenderer::Gpu => Backend::Gpu,
crate::JobRenderer::Cpu => Backend::Cpu,
});
}
}
log::info!(target: "effectcraft_export", "{}: {}", self.job.options.label, describe_choice(&c));
}
/// The job's choice, once made.
pub(crate) fn choice(&self) -> Option<Choice> {
*self.choice.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// The compositor frames render on: the choice, or the project's until it is made.
fn backend(&self) -> crate::JobRenderer {
self.choice().map_or_else(|| self.project_renderer(), |c| c.renderer)
}
pub fn comp(&self) -> Option<&Comp> {
@@ -234,12 +419,22 @@ impl<'a> Cx<'a> {
/// The renderer of the job's frames: the Render Settings' options, the job's accelerator
/// and layer cache.
#[cfg(test)]
fn renderer(&self) -> Renderer<'_> {
self.renderer_on(self.backend())
}
/// [`Cx::renderer`] on compositor `on`, with that compositor's layer cache.
fn renderer_on(&self, on: crate::JobRenderer) -> Renderer<'_> {
let s = self.job.settings;
// The whole job renders on the project's renderer (Mercury GPU Acceleration or Software
// Only), not on whichever compositor Auto timed faster for each frame: the CPU and GPU
// compositors differ slightly, so a per-frame choice made renders differ run to run.
let backend = if self.project.settings.gpu_acceleration { Backend::Gpu } else { Backend::Cpu };
// The whole job renders on one compositor (the project's, or the faster one measured on
// the first frames, #3335), not on whichever compositor Auto timed faster for each
// frame: the CPU and GPU compositors differ slightly, so a per-frame choice made renders
// differ within a render.
let backend = match on {
crate::JobRenderer::Gpu => Backend::Gpu,
crate::JobRenderer::Cpu => Backend::Cpu,
};
let opts = RenderOpts {
scale: self.comp().map(|c| s.effective_scale(c)).unwrap_or_else(|| s.resolution.clamp(0.01, 4.0)),
motion_blur: s.motion_blur_override() != SwitchOverride::OffForAll,
@@ -258,13 +453,13 @@ impl<'a> Cx<'a> {
let mut r = Renderer::new(&self.project, self.job.footage, opts);
r.expr = self.job.expr;
r.accel = self.job.accel;
r.cache = Some(&self.cache);
r.cache = Some(self.caches.of(on));
r
}
async fn render_at(&self, t: Tick) -> Image {
async fn render_at(&self, t: Tick, on: crate::JobRenderer) -> Image {
// With deferred GPU readbacks (a browser worker) the frame renders in passes.
effectcraft_render::passes::comp_frame(&self.renderer(), self.job.comp, t).await
effectcraft_render::passes::comp_frame(&self.renderer_on(on), self.job.comp, t).await
}
/// Whether frames render in passes (an accelerator with deferred readbacks).
@@ -283,10 +478,110 @@ impl<'a> Cx<'a> {
}
return out;
}
self.render_frames(ks, |k, on| self.frame_on(comp, k, on), f)
}
/// `render` then `encode` over the frames `ks` on the job's compositor, results in `ks`
/// order. The first call of a job that still has to choose its compositor measures both on
/// its first frames ([`Cx::probe`]) and keeps the faster one's.
fn render_frames<I: Send, T: Send>(
&self,
ks: Vec<u64>,
render: impl Fn(u64, crate::JobRenderer) -> I + Sync + Send,
encode: impl Fn(u64, I) -> T + Sync + Send,
) -> Vec<T> {
use rayon::prelude::*;
let probed = self.probe(&ks, &render);
// The GPU cap is for the GPU's memory: once it has stopped for good, frames render on the
// CPU, in parallel or under the CPU cap (#1615), which holds throughout.
let gpu_live = || self.job.accel.is_none_or(|a| a.available());
map_frames_while(ks, self.inflight(), gpu_live, |k| self.frame(comp, k), f)
let on = self.backend();
if probed.is_empty() {
return map_frames_while(ks, self.inflight(), gpu_live, |k| render(k, on), encode);
}
let rest: Vec<u64> = ks.iter().copied().filter(|k| !probed.iter().any(|(p, _)| p == k)).collect();
let mut done: Vec<(u64, T)> = probed.into_par_iter().map(|(k, i)| (k, encode(k, i))).collect();
done.extend(rest.iter().copied().zip(map_frames_while(rest.clone(), self.inflight(), gpu_live, |k| render(k, on), &encode)));
// Back in `ks` order.
let mut out = Vec::with_capacity(ks.len());
for k in &ks {
if let Some(i) = done.iter().position(|(d, _)| d == k) {
out.push(done.swap_remove(i).1);
}
}
out
}
/// Choose the job's compositor by timing both on the first frames of `ks` (#3335), as the
/// viewer's Auto does per comp: frame 0 warms each up (layer cache misses, uploads, kernel
/// builds) untimed, then the GPU renders the next [`PROBE_FRAMES`] under its in-flight cap
/// and the CPU up to [`PROBE_CPU_FRAMES`] after them in parallel (its throughput). The CPU
/// may render more frames at once in the job than here, so the measurement leans towards
/// the GPU, as the margin of [`pick_renderer`] does: the CPU is chosen only when clearly
/// faster. Returns
/// the frames the faster one rendered (the slower one's are rendered again); empty when
/// the choice was made already or is made without measuring (a short job).
fn probe<I: Send>(&self, ks: &[u64], render: &(impl Fn(u64, crate::JobRenderer) -> I + Sync + Send)) -> Vec<(u64, I)> {
if self.choice().is_some() {
return vec![];
}
let total = self.comp().map_or(0, |c| self.job.settings.frame_count(c));
if total < PROBE_MIN_JOB {
self.decide(Choice { renderer: crate::JobRenderer::Gpu, gpu_ms: None, cpu_ms: None, why: "the project's renderer (too few frames to measure)" });
return vec![];
}
// Nothing will be emitted for a skipped batch, so a later batch can still measure.
if ks.is_empty() {
return vec![];
}
// A resumed sequence can have only one or two missing frames in its first batch.
// Settle before emitting them: measuring a later batch could otherwise switch to the
// CPU after GPU pixels have already been written, including across output modules.
let n = PROBE_FRAMES.min(ks.len().saturating_sub(1) / 2);
if n == 0 {
self.decide(Choice {
renderer: crate::JobRenderer::Gpu,
gpu_ms: None,
cpu_ms: None,
why: "the project's renderer (too few pending frames to measure)",
});
return vec![];
}
let (Some(&first), Some(gpu_ks), Some(cpu_ks)) = (ks.first(), ks.get(1..1 + n), ks.get(1 + n..ks.len().min(1 + n + PROBE_CPU_FRAMES))) else {
return vec![];
};
let logged = || self.frame_times.as_ref().map_or(0, |ft| ft.lock().unwrap_or_else(std::sync::PoisonError::into_inner).len());
let cpu_cap = self.inflight().cpu;
let timed = |on: crate::JobRenderer, frames: &[u64]| {
let warm = render(first, on);
// The GPU's frames in flight settle with its warm-up frame (Auto measures it).
let caps = match on {
crate::JobRenderer::Gpu => Inflight { gpu: Some(self.gpu_inflight()), cpu: cpu_cap },
crate::JobRenderer::Cpu => Inflight { gpu: None, cpu: cpu_cap },
};
let t0 = Instant::now();
let gpu_live = || on == crate::JobRenderer::Cpu || self.job.accel.is_some_and(|a| a.available());
let items = map_frames_while(frames.to_vec(), caps, gpu_live, |k| render(k, on), |k, i| (k, i));
let ms = t0.elapsed().as_secs_f64() * 1e3 / frames.len().max(1) as f64;
(std::iter::once((first, warm)).chain(items).collect::<Vec<_>>(), ms)
};
let log0 = logged();
let (gpu, gpu_ms) = timed(crate::JobRenderer::Gpu, gpu_ks);
let log1 = logged();
let (cpu, cpu_ms) = timed(crate::JobRenderer::Cpu, cpu_ks);
// A GPU that stopped meanwhile rendered its frames on the CPU: it is no choice.
let gpu_ms = self.job.accel.is_some_and(|a| a.available()).then_some(gpu_ms);
let renderer = pick_renderer(gpu_ms, Some(cpu_ms));
// The per-frame log keeps the kept frames' times only.
if let Some(ft) = &self.frame_times {
let mut v = ft.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let (a, b) = if renderer == crate::JobRenderer::Gpu { (log1, v.len()) } else { (log0, log1) };
if a <= b && b <= v.len() {
v.drain(a..b);
}
}
self.decide(Choice { renderer, gpu_ms, cpu_ms: Some(cpu_ms), why: "measured on the first frames" });
if renderer == crate::JobRenderer::Gpu { gpu } else { cpu }
}
/// [`Cx::frames`] for `ks` while `encode` runs beside them on `beside`'s threads, so the
@@ -338,9 +633,29 @@ impl<'a> Cx<'a> {
/// GPU frames rendered at once (#879), decided once per batch: with a GPU accelerator every
/// frame in flight holds its own working textures, so a whole batch rendered at once can
/// exhaust VRAM (or shared memory, which crashes instead of falling back). `None` when the
/// frames render on the CPU: no accelerator, or a Software Only project.
/// frames render on the CPU: no accelerator, a Software Only project, or a job that chose
/// the CPU's compositor (#3335), whose frames render in parallel.
fn gpu_cap(&self) -> Option<usize> {
(self.job.accel.is_some() && self.project.settings.gpu_acceleration).then(|| gpu_inflight(std::env::var(GPU_INFLIGHT_ENV).ok().as_deref()))
(self.job.accel.is_some() && self.backend() == crate::JobRenderer::Gpu).then(|| self.gpu_inflight())
}
/// GPU frames in flight ([`resolve_gpu_frames`]): [`GPU_INFLIGHT_DEFAULT`] until settled,
/// which Auto is after the job's first GPU frame.
fn gpu_inflight(&self) -> usize {
if let Some((n, _)) = &*self.gpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner) {
return *n;
}
// Measured outside the lock (the allocator report walks every allocation).
let measured = self.gpu_rendered.load(std::sync::atomic::Ordering::Relaxed).then(|| self.job.accel.and_then(|a| a.frame_memory()));
let Some((n, why)) = resolve_gpu_frames(std::env::var(GPU_INFLIGHT_ENV).ok().as_deref(), self.job.options.gpu_frames, measured) else {
return GPU_INFLIGHT_DEFAULT;
};
let mut slot = self.gpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
slot.get_or_insert_with(|| {
log::info!(target: "effectcraft_export", "{}: {n} GPU frame(s) at once ({why})", self.job.options.label);
(n, why)
})
.0
}
/// The GPU and CPU frames-in-flight caps every frame loop shares (#1615).
@@ -349,20 +664,34 @@ impl<'a> Cx<'a> {
}
/// Output frame `i`: sampled (fields, pulldown), at the colour depth, cropped and resized.
#[cfg(test)]
pub fn frame(&self, comp: &Comp, i: u64) -> Image {
effectcraft_render::passes::block_on(self.frame_async(comp, i))
self.frame_on(comp, i, self.backend())
}
/// [`Cx::frame`] on compositor `on`.
fn frame_on(&self, comp: &Comp, i: u64, on: crate::JobRenderer) -> Image {
effectcraft_render::passes::block_on(self.frame_async_on(comp, i, on))
}
/// [`Cx::frame`] as a future (deferred readbacks: the frame renders in passes).
pub async fn frame_async(&self, comp: &Comp, i: u64) -> Image {
self.frame_async_on(comp, i, self.backend()).await
}
async fn frame_async_on(&self, comp: &Comp, i: u64, on: crate::JobRenderer) -> Image {
let t0 = Instant::now();
let gpu = on == crate::JobRenderer::Gpu && self.job.accel.is_some();
let img = match self.job.settings.sample(comp, i) {
FrameSample::Progressive(t) => self.render_at(t).await,
FrameSample::Progressive(t) => self.render_at(t, on).await,
FrameSample::Fields { first, second, upper_first } => {
let a = self.render_at(first).await;
if second == first { a } else { interleave(&a, &self.render_at(second).await, upper_first) }
let a = self.render_at(first, on).await;
if second == first { a } else { interleave(&a, &self.render_at(second, on).await, upper_first) }
}
};
if gpu {
self.gpu_rendered.store(true, std::sync::atomic::Ordering::Relaxed);
}
let img = self.crop_resize(img);
if let Some(ft) = &self.frame_times {
ft.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push((i, t0.elapsed().as_secs_f64()));
@@ -374,7 +703,7 @@ impl<'a> Cx<'a> {
/// by the accelerator ([`effectcraft_render::Accelerator::comp_frame_yuv420`]). Only for
/// frames that need nothing done after the render (progressive, at the project's depth, no
/// crop or resize, rendered at exactly `w`×`h`). `None`: render it with [`Cx::frame`].
pub fn yuv420_frame(&self, comp: &Comp, i: u64, w: u32, h: u32) -> Option<Vec<u8>> {
fn yuv420_frame(&self, comp: &Comp, i: u64, w: u32, h: u32, on: crate::JobRenderer) -> Option<Vec<u8>> {
if self.deferred() {
return None;
}
@@ -392,7 +721,7 @@ impl<'a> Cx<'a> {
return None;
}
let t0 = Instant::now();
let r = self.renderer();
let r = self.renderer_on(on);
let a = r.active_accel()?;
let planes = a.comp_frame_yuv420(&r, self.job.comp, t, comp.background)?;
let (cw, ch) = (w.div_ceil(2) as usize, h.div_ceil(2) as usize);
@@ -401,6 +730,7 @@ impl<'a> Cx<'a> {
}
// A frame rendered off `passes::comp_frame` ends here; on a `None` the fallback renders and ends it.
a.frame_end();
self.gpu_rendered.store(true, std::sync::atomic::Ordering::Relaxed);
if let Some(ft) = &self.frame_times {
ft.lock().unwrap_or_else(|e| e.into_inner()).push((i, t0.elapsed().as_secs_f64()));
}
@@ -419,12 +749,9 @@ impl<'a> Cx<'a> {
fallback: impl Fn(u64, Image) -> T + Sync + Send,
) -> Vec<T> {
// As in `frames`: once the GPU has stopped for good, the rest render in parallel.
let gpu_live = || self.job.accel.is_none_or(|a| a.available());
map_frames_while(
self.render_frames(
ks,
self.inflight(),
gpu_live,
|k| self.yuv420_frame(comp, k, w, h).ok_or_else(|| self.frame(comp, k)),
|k, on| self.yuv420_frame(comp, k, w, h, on).ok_or_else(|| self.frame_on(comp, k, on)),
|k, r| match r {
Ok(p) => planes(p),
Err(img) => fallback(k, img),
@@ -544,6 +871,14 @@ impl<'a> Cx<'a> {
Ok(r) => s.push_str(&format!("Result: Done ({} frames, {}×{}, {} bytes) in {seconds:.2} s\n", r.frames, r.width, r.height, r.bytes)),
Err(e) => s.push_str(&format!("Result: Failed\nError: {e}\n")),
}
if let Some(c) = self.choice() {
s.push_str(&format!("Renderer: {}\n", describe_choice(&c)));
}
if self.backend() == crate::JobRenderer::Gpu
&& let Some((n, why)) = &*self.gpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
{
s.push_str(&format!("GPU frames rendered at once: {n} ({why})\n"));
}
let over = self.overflowed.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
if !over.is_empty() {
s.push_str(&format!("Storage overflow: {} file(s) written to overflow folders (first: {})\n", over.len(), over[0]));
@@ -575,6 +910,20 @@ impl<'a> Cx<'a> {
}
}
/// The render log's account of a job's compositor: which, why, and what was measured.
pub(crate) fn describe_choice(c: &Choice) -> String {
let name = match c.renderer {
crate::JobRenderer::Gpu => "GPU Acceleration",
crate::JobRenderer::Cpu => "Software Rendering",
};
let ms = |v: Option<f64>| v.map_or_else(|| "n/a".to_string(), |v| format!("{v:.1} ms/frame"));
if c.gpu_ms.is_none() && c.cpu_ms.is_none() {
format!("{name} ({})", c.why)
} else {
format!("{name} ({}: GPU {}, CPU {})", c.why, ms(c.gpu_ms), ms(c.cpu_ms))
}
}
/// Weave two fields: the dominant field's lines (even lines when `upper_first`) from `first`.
pub(crate) fn interleave(first: &Image, second: &Image, upper_first: bool) -> Image {
let mut out = first.clone();
@@ -860,6 +1209,82 @@ mod tests {
assert_eq!(Cx::new(&job).gpu_cap(), None, "no accelerator");
}
/// A resumed sequence can start with one or two missing frames. Once emitted, their
/// compositor must also be used by every later batch and the item's other output modules.
#[test]
fn a_small_first_batch_settles_the_renderer_before_emitting_frames() {
if std::env::var_os(RENDERER_ENV).is_some() {
return;
}
use effectcraft_project::render_queue::{OutputFormat, OutputModule, RenderSettings};
let (settings, output) = (RenderSettings::default(), OutputModule::for_format(OutputFormat::PngSequence));
let mut p = Project::default();
p.settings.gpu_acceleration = true;
let comp = Comp::new(16, 8, effectcraft_time::FrameRate::FPS_25, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", effectcraft_color::Label::Sandstone, None, effectcraft_project::ItemKind::Comp(comp.into()));
let accel = NoAccel;
for n in [1, 2] {
let shared = Mutex::new(None);
let job = Job {
project: &p,
footage: &effectcraft_render::NoFootage,
expr: None,
accel: Some(&accel),
comp: cid,
settings: &settings,
output: &output,
path: "/r/f_[#####].png",
sink: None,
nested_switches: true,
options: crate::JobOptions { renderer: Some(&shared), ..Default::default() },
};
let cx = Cx::new(&job);
let empty = cx.render_frames(Vec::new(), |_, on| on, |_, on| on);
assert!(empty.is_empty());
assert_eq!(cx.choice(), None, "skipped batches do not choose");
let first = cx.render_frames((0..n).collect(), |_, on| on, |_, on| on);
assert_eq!(first, vec![crate::JobRenderer::Gpu; n as usize]);
assert_eq!(*shared.lock().unwrap(), Some(Backend::Gpu), "settled before any pixels are emitted");
let later = cx.render_frames((n..12).collect(), |_, on| on, |_, on| on);
assert!(later.iter().all(|on| *on == crate::JobRenderer::Gpu));
assert_eq!(Cx::new(&job).backend(), crate::JobRenderer::Gpu, "the next output module follows");
}
}
#[test]
fn a_job_that_chose_the_cpu_is_not_capped() {
// #3335: the CPU's frame-parallel compositor is the point of choosing it; a GPU project
// that measured the CPU faster must not keep rendering one frame at a time.
use effectcraft_project::render_queue::{OutputFormat, OutputModule, RenderSettings};
let (settings, output) = (RenderSettings::default(), OutputModule::for_format(OutputFormat::PngSequence));
let accel = NoAccel;
let mut p = Project::default();
p.settings.gpu_acceleration = true;
let job = Job {
project: &p,
footage: &effectcraft_render::NoFootage,
expr: None,
accel: Some(&accel),
comp: effectcraft_project::ItemId(0),
settings: &settings,
output: &output,
path: "/c/f_[#####].png",
sink: None,
nested_switches: true,
options: Default::default(),
};
let cx = Cx::new(&job);
if std::env::var_os(RENDERER_ENV).is_none() {
assert_eq!(cx.choice(), None, "a GPU project measures first");
assert!(cx.gpu_cap().is_some());
}
cx.decide(Choice { renderer: crate::JobRenderer::Cpu, gpu_ms: Some(117.0), cpu_ms: Some(68.0), why: "measured on the first frames" });
assert_eq!(cx.backend(), crate::JobRenderer::Cpu);
assert_eq!(cx.gpu_cap(), None);
assert_eq!(cx.renderer().active_accel().map(|a| a.name()), None, "CPU frames don't use the accelerator");
assert_eq!(describe_choice(&cx.choice().unwrap()), "Software Rendering (measured on the first frames: GPU 117.0 ms/frame, CPU 68.0 ms/frame)");
}
#[test]
fn cpu_inflight_is_off_unless_a_positive_number() {
assert_eq!(cpu_inflight(None), None);
@@ -958,12 +1383,117 @@ mod tests {
}
#[test]
fn gpu_inflight_override_is_a_positive_number_or_the_default() {
assert_eq!(gpu_inflight(None), GPU_INFLIGHT_DEFAULT);
assert_eq!(gpu_inflight(Some("3")), 3);
assert_eq!(gpu_inflight(Some(" 5 ")), 5);
fn gpu_inflight_override_is_a_positive_number_or_ignored() {
assert_eq!(gpu_inflight_env(None), None);
assert_eq!(gpu_inflight_env(Some("3")), Some(3));
assert_eq!(gpu_inflight_env(Some(" 5 ")), Some(5));
for bad in ["0", "", "-2", "three", "2.5", "99999999999999999999999"] {
assert_eq!(gpu_inflight(Some(bad)), GPU_INFLIGHT_DEFAULT, "{bad:?}");
assert_eq!(gpu_inflight_env(Some(bad)), None, "{bad:?}");
}
}
#[test]
fn auto_gpu_frames_follow_the_memory_budget() {
// #3335: Auto renders as many GPU frames at once as the budget has room for, 1 to 3.
use effectcraft_render::FrameMemory;
const GIB: u64 = 1 << 30;
let m = |budget: u64, peak: u64, frame: u64, reserve: u64| Some(FrameMemory { budget, peak, frame, reserve });
// Unknown memory (no allocator report, no budget): one frame.
assert_eq!(auto_gpu_frames(None), 1);
assert_eq!(auto_gpu_frames(m(0, GIB, GIB / 2, 0)), 1);
assert_eq!(auto_gpu_frames(m(3 * GIB, 0, GIB / 2, 0)), 1);
assert_eq!(auto_gpu_frames(m(3 * GIB, GIB, 0, 0)), 1);
// #879 on an 8 GB card: one frame peaked at 6.3 GB, over the 3 GiB budget: one.
assert_eq!(auto_gpu_frames(m(3 * GIB, 63 * GIB / 10, 5 * GIB, 0)), 1);
// Frames that had to wait for memory report the whole budget per frame: one.
assert_eq!(auto_gpu_frames(m(3 * GIB, GIB, 3 * GIB, 0)), 1);
// Room for half a frame more, or room the upload cache may still take: one.
assert_eq!(auto_gpu_frames(m(2 * GIB, 3 * GIB / 2, GIB, 0)), 1);
assert_eq!(auto_gpu_frames(m(3 * GIB, GIB, GIB / 2, 3 * GIB / 2)), 2);
assert_eq!(auto_gpu_frames(m(3 * GIB, GIB, GIB / 2, 2 * GIB)), 1);
// Room for one more frame: two; ample room: three, never more.
assert_eq!(auto_gpu_frames(m(3 * GIB, 2 * GIB, GIB, 0)), 2);
assert_eq!(auto_gpu_frames(m(3 * GIB, GIB / 2, GIB / 4, 0)), 3);
assert_eq!(auto_gpu_frames(m(u64::MAX, 1, 1, 0)), 3);
}
#[test]
fn gpu_frames_come_from_the_environment_the_setting_or_auto() {
use effectcraft_render::FrameMemory;
let ample = Some(Some(FrameMemory { budget: 3 << 30, peak: 1 << 29, frame: 1 << 28, reserve: 1 << 28 }));
let n = |env, pref, measured| resolve_gpu_frames(env, pref, measured).map(|(n, _)| n);
// Auto: unsettled until a GPU frame rendered, then measured.
assert_eq!(n(None, None, None), None);
assert_eq!(n(None, None, Some(None)), Some(1));
assert_eq!(n(None, None, ample), Some(3));
// The setting's 1, 2 or 3, without measuring.
for k in 1..=3 {
assert_eq!(n(None, Some(k), None), Some(k));
assert_eq!(n(None, Some(k), ample), Some(k));
}
// The environment overrides both; a bad value is ignored.
assert_eq!(n(Some("2"), Some(1), None), Some(2));
assert_eq!(n(Some("4"), None, ample), Some(4));
assert_eq!(n(Some("zero"), Some(1), None), Some(1));
let (_, why) = resolve_gpu_frames(None, None, ample).unwrap();
assert_eq!(why, "Auto: 256 MiB per frame, 512 MiB peak and 256 MiB cache reserve in a 3072 MiB budget");
}
/// An accelerator reporting `memory` once asked (renders nothing itself).
struct MemAccel(effectcraft_render::FrameMemory);
impl effectcraft_render::Accelerator for MemAccel {
fn name(&self) -> String {
"mem".into()
}
fn comp_frame(&self, _r: &Renderer, _comp: effectcraft_project::ItemId, _t: Tick) -> Option<Image> {
None
}
fn supports_effect(&self, _id: &str) -> bool {
false
}
fn effects(&self, _chain: &[effectcraft_render::FxStep], _buf: &effectcraft_effects::Buf, _levels: Option<f32>) -> Option<effectcraft_effects::Buf> {
None
}
fn frame_memory(&self) -> Option<effectcraft_render::FrameMemory> {
Some(self.0)
}
}
#[test]
fn auto_gpu_frames_settle_after_the_first_gpu_frame() {
// #3335: one GPU frame at a time until a frame measured the memory, then Auto's count
// for the rest of the job; the setting skips the measurement.
use effectcraft_project::render_queue::{OutputFormat, OutputModule, RenderSettings};
if std::env::var_os(GPU_INFLIGHT_ENV).is_some() || std::env::var_os(RENDERER_ENV).is_some() {
return;
}
let (settings, output) = (RenderSettings::default(), OutputModule::for_format(OutputFormat::PngSequence));
let mut p = Project::default();
p.settings.gpu_acceleration = true;
let comp = Comp::new(16, 8, effectcraft_time::FrameRate::FPS_25, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", effectcraft_color::Label::Sandstone, None, effectcraft_project::ItemKind::Comp(comp.into()));
let accel = MemAccel(effectcraft_render::FrameMemory { budget: 3 << 30, peak: 1 << 29, frame: 1 << 28, reserve: 0 });
for (pref, want) in [(None, 3), (Some(2), 2), (Some(1), 1)] {
let job = Job {
project: &p,
footage: &effectcraft_render::NoFootage,
expr: None,
accel: Some(&accel),
comp: cid,
settings: &settings,
output: &output,
path: "/c/f_[#####].png",
sink: None,
nested_switches: true,
options: crate::JobOptions { gpu_frames: pref, ..Default::default() },
};
let cx = Cx::new(&job);
if pref.is_none() {
assert_eq!(cx.gpu_cap(), Some(1), "before any GPU frame");
}
let c = cx.comp().unwrap();
cx.frame_on(c, 0, crate::JobRenderer::Gpu);
assert_eq!(cx.gpu_cap(), Some(want), "setting {pref:?}");
}
}
@@ -1002,7 +1532,7 @@ mod tests {
let cx = Cx::new(&job);
let comp = cx.comp().unwrap();
cx.frame(comp, 0);
let renders = cx.cache.stats().misses;
let renders = cx.caches.of(cx.backend()).stats().misses;
assert!(renders > 0);
for i in 1..10 {
let img = cx.frame(comp, i);
@@ -1012,7 +1542,7 @@ mod tests {
uncached.cache = None;
assert_eq!(img.data, uncached.comp_frame(cid, t).data, "frame {i}");
}
let stats = cx.cache.stats();
let stats = cx.caches.of(cx.backend()).stats();
assert_eq!(stats.misses, renders, "the solid rendered on the first frame only");
assert!(stats.hits >= 9, "{stats:?}");
}
+3 -1
View File
@@ -104,6 +104,8 @@ fn project() -> (Project, ItemId) {
fn run(p: &Project, cid: ItemId, accel: Option<&dyn Accelerator>, s: &RenderSettings, path: &Path) -> Vec<u8> {
let om = OutputModule { bitrate_kbps: 4000, ..OutputModule::for_format(OutputFormat::H264) };
let path = path.to_string_lossy().to_string();
// These tests are about the accelerator's own path: no measuring against the CPU (#3335).
let gpu = std::sync::Mutex::new(Some(effectcraft_render::Backend::Gpu));
let job = Job {
project: p,
footage: &NoFootage,
@@ -114,7 +116,7 @@ fn run(p: &Project, cid: ItemId, accel: Option<&dyn Accelerator>, s: &RenderSett
output: &om,
path: &path,
sink: None,
options: Default::default(),
options: effectcraft_export::JobOptions { renderer: Some(&gpu), ..Default::default() },
nested_switches: true,
};
export(&job, &mut |_| true).expect("export");
+147
View File
@@ -501,3 +501,150 @@ fn a_job_renders_on_one_compositor() {
assert_eq!(cpu.frames.load(Ordering::SeqCst), 0);
assert_eq!(c[0].get_pixel(10, 44).0, [255, 0, 0, 255], "the CPU drew the red block");
}
/// A GPU compositor stand-in that renders the frame exactly as the CPU does, after `delay`, and
/// either as it is or solid green (`green`), counting its frames.
struct TimedAccel {
delay: std::time::Duration,
green: bool,
frames: AtomicUsize,
}
impl TimedAccel {
fn new(delay_ms: u64, green: bool) -> TimedAccel {
TimedAccel { delay: std::time::Duration::from_millis(delay_ms), green, frames: AtomicUsize::new(0) }
}
}
impl Accelerator for TimedAccel {
fn name(&self) -> String {
"timed".into()
}
fn comp_frame(&self, r: &Renderer, comp: ItemId, t: Tick) -> Option<Image> {
self.frames.fetch_add(1, Ordering::SeqCst);
std::thread::sleep(self.delay);
let c = r.project.comp(comp)?;
Some(if self.green { Image::filled(c.width, c.height, [0.0, 1.0, 0.0, 1.0]) } else { r.comp_frame_cpu(comp, t) })
}
fn supports_effect(&self, _id: &str) -> bool {
false
}
fn effects(&self, _chain: &[FxStep], _buf: &Buf, _levels: Option<f32>) -> Option<Buf> {
None
}
}
/// The environment forces the compositor or the GPU frames in flight: the measured choice
/// isn't what these tests check then.
fn measurement_overridden() -> bool {
std::env::var_os(effectcraft_export::RENDERER_ENV).is_some() || std::env::var_os("EFFECTCRAFT_GPU_INFLIGHT").is_some()
}
/// [`run_on`] on a pool of 8 threads (a batch of 8 frames: room to measure both compositors on
/// the first one, whatever the machine), returning the PNGs and the render log.
fn run_pooled(p: &Project, cid: ItemId, s: &RenderSettings, path: &str, options: JobOptions, accel: &dyn Accelerator) -> (Vec<image::RgbaImage>, String) {
let om = OutputModule::for_format(OutputFormat::PngSequence);
let pool = rayon::ThreadPoolBuilder::new().num_threads(8).build().unwrap();
let options = JobOptions { log: RenderLog::PlusSettings, ..options };
let (r, files) = pool.install(|| run_on(p, cid, s, &om, path, options, (Some(accel), false)));
let n = r.expect("export").frames as usize;
let log = files.lock().unwrap().iter().find(|(p, _)| p.ends_with("_RenderLog.txt")).map(|(_, d)| String::from_utf8(d.clone()).unwrap()).expect("log");
((0..n).map(|i| png(&files, i)).collect(), log)
}
#[test]
fn a_transfer_bound_gpu_job_renders_on_the_cpu() {
// #3335: a GPU project whose GPU frames are slower than the CPU's renders the whole job on
// the CPU's frame-parallel compositor, measured on its first frames, and says so in the log.
if measurement_overridden() {
return;
}
let (mut p, cid) = project(FrameRate::new(25, 1), |_, _| {});
p.settings.gpu_acceleration = true;
let s = custom(0.0, 0.8);
let slow = TimedAccel::new(60, true);
let (frames, log) = run_pooled(&p, cid, &s, "/r/a_[#####].png", JobOptions::default(), &slow);
assert_eq!(frames.len(), 20);
// The GPU rendered its warm-up and timed frames only, and none of them is in the output.
assert_eq!(slow.frames.load(Ordering::SeqCst), 3);
for (i, f) in frames.iter().enumerate() {
assert_eq!(f.get_pixel(10, 44).0, [255, 0, 0, 255], "frame {i} is not the CPU's");
}
assert!(log.contains("Renderer: Software Rendering (measured on the first frames: GPU "), "{log}");
// The queue item's next output module follows the first one's choice without measuring.
let shared = Mutex::new(None);
let opts = || JobOptions { renderer: Some(&shared), ..Default::default() };
let slow = TimedAccel::new(60, true);
run_pooled(&p, cid, &s, "/r/b_[#####].png", opts(), &slow);
assert_eq!(*shared.lock().unwrap(), Some(effectcraft_render::Backend::Cpu));
let again = TimedAccel::new(0, true);
let (frames, log) = run_pooled(&p, cid, &s, "/r/c_[#####].png", opts(), &again);
assert_eq!(again.frames.load(Ordering::SeqCst), 0);
assert!(frames.iter().all(|f| f.get_pixel(10, 44).0 == [255, 0, 0, 255]));
assert!(log.contains("Renderer: Software Rendering (as the queue item's first output module)"), "{log}");
}
#[test]
fn a_gpu_job_that_measures_no_faster_on_the_cpu_stays_on_the_gpu() {
if measurement_overridden() {
return;
}
// A slow CPU (a large comp of eight rotating, translucent full-frame layers, composited
// anew every frame) against a GPU that only fills the frame: every frame is the GPU's.
let (mut p, cid) = project(FrameRate::new(25, 1), |p, comp| {
comp.width = 1920;
comp.height = 1080;
let big = p.add_item("Big", Label::Red, None, ItemKind::Solid(Solid { color: [0.2, 0.4, 0.6], width: 1920, height: 1080, pixel_aspect: 1.0 }));
for k in 0..8 {
let mut l = build::layer(p, comp, "Big", LayerSource::Solid { item: big }, (1920, 1080), None);
l.props.prop_mut("transform/rotation").unwrap().keys =
vec![Keyframe::new(Tick::ZERO, Value::Scalar(k as f64)), Keyframe::new(Tick::from_seconds_f64(1.0), Value::Scalar(90.0 + k as f64))];
l.props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(50.0);
comp.layers.insert(0, l);
}
});
p.settings.gpu_acceleration = true;
let s = custom(0.0, 0.6);
let fast = TimedAccel::new(0, true);
let (frames, log) = run_pooled(&p, cid, &s, "/r/d_[#####].png", JobOptions::default(), &fast);
assert_eq!(frames.len(), 15);
assert_eq!(fast.frames.load(Ordering::SeqCst), 15, "every kept frame, warm-up and measured ones included, is the GPU's");
assert!(frames.iter().all(|f| f.pixels().all(|px| px.0 == [0, 255, 0, 255])));
assert!(log.contains("Renderer: GPU Acceleration (measured on the first frames: GPU "), "{log}");
}
#[test]
fn gpu_cpu_and_measured_jobs_write_the_same_frames() {
// #3335: where the two compositors agree, a job is the same whichever renders it: the GPU
// (forced), the CPU (Software Only) or the measured choice.
if measurement_overridden() {
return;
}
let (mut p, cid) = project(FrameRate::new(25, 1), |_, _| {});
let s = custom(0.0, 0.8);
let echo = TimedAccel::new(5, false);
p.settings.gpu_acceleration = false;
let (cpu, log) = run_pooled(&p, cid, &s, "/i/a_[#####].png", JobOptions::default(), &echo);
assert!(log.contains("Renderer: Software Rendering (the project's renderer)"), "{log}");
p.settings.gpu_acceleration = true;
let gpu_slot = Mutex::new(Some(effectcraft_render::Backend::Gpu));
let (gpu, _) = run_pooled(&p, cid, &s, "/i/b_[#####].png", JobOptions { renderer: Some(&gpu_slot), ..Default::default() }, &echo);
let (auto, _) = run_pooled(&p, cid, &s, "/i/c_[#####].png", JobOptions::default(), &echo);
assert_eq!(cpu.len(), 20);
assert_eq!(cpu, gpu);
assert_eq!(cpu, auto);
}
#[test]
fn renderer_choice_follows_the_measurement() {
use effectcraft_export::{JobRenderer, pick_renderer};
assert_eq!(pick_renderer(Some(117.0), Some(68.0)), JobRenderer::Cpu);
assert_eq!(pick_renderer(Some(68.0), Some(117.0)), JobRenderer::Gpu);
// Ties and noise stay on the GPU: the CPU must be clearly faster.
assert_eq!(pick_renderer(Some(100.0), Some(90.0)), JobRenderer::Gpu);
assert_eq!(pick_renderer(Some(100.0), Some(80.0)), JobRenderer::Cpu);
// Unmeasured or nonsense: the GPU, unless the GPU stopped while measuring.
assert_eq!(pick_renderer(None, None), JobRenderer::Gpu);
assert_eq!(pick_renderer(Some(f64::NAN), Some(1.0)), JobRenderer::Gpu);
assert_eq!(pick_renderer(None, Some(50.0)), JobRenderer::Cpu);
}
+1 -1
View File
@@ -78,7 +78,7 @@ fn render(ctx: EvalCtx, comp: ItemId, layer: &Layer, post: bool, t: f64) -> Opti
r.reads = Some(&log);
let ectx = r.eval_ctx(comp, Tick::from_seconds_f64(t))?;
let out = if post {
r.content_buf(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img.clone()), offset: b.offset, scale: b.scale })
r.layer_content(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img.clone()), offset: b.offset, scale: b.scale })
} else {
r.layer_source(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img), offset: b.offset, scale: b.scale })
};
+26
View File
@@ -2070,3 +2070,29 @@ fn bench_evaluation() {
println!("BENCH {best:>9} ns/eval {label} (min of 3 x {iters})");
}
}
/// Viewport clipping must never affect layer-space expression reads (#1388).
#[test]
fn sample_image_reads_3d_shape_behind_camera_before_and_after_effects() {
let mut p = Project::default();
let comp = Comp::new(100, 100, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
let mut layer = build::layer(&mut p, &comp, "Shape", LayerSource::Shape, (100, 100), None);
let mut next = p.next_id;
let mut ids = build::Ids(&mut next);
let rect = build::shape_rect(&mut ids, [40.0, 40.0], [0.0, 0.0], 0.0);
let fill = build::shape_fill(&mut ids, [1.0, 0.0, 0.0, 1.0]);
let group = build::shape_group(&mut ids, "Box", vec![rect, fill]);
layer.props.sub_mut("contents").unwrap().children.push(group.into());
p.next_id = next;
layer.switches.three_d = true;
layer.props.prop_mut("transform/position").unwrap().value = Value::Vec3([50.0, 50.0, -300.0]);
let cid = p.add_item("C", Label::Sandstone, None, ItemKind::Comp(comp.into()));
p.comp_mut(cid).unwrap().layers.push(layer);
let comp = p.comp(cid).unwrap();
let layer = &comp.layers[0];
let ctx = EvalCtx::new(&p, cid, comp, Tick::ZERO);
for post in [false, true] {
let pixel = crate::sample::sample_image(ctx, cid, layer, [0.0, 0.0], [0.5, 0.5], post, 0.0).unwrap();
assert!(pixel[0] > 0.99 && pixel[3] > 0.99, "postEffect={post}: {pixel:?}");
}
}
+1 -1
View File
@@ -801,7 +801,7 @@ fn read_buffer(e: &mut Enc, buf: &wgpu::Buffer, size: u64) -> Option<Vec<u8>> {
});
e.record(&[]).copy_buffer_to_buffer(buf, 0, &rb, 0, size);
e.submit();
let out = e.g.read_buffer(&rb, row, row, 1).map_err(|e| log::error!("gpu 3d readback: {e}")).ok()?;
let out = e.g.read_buffer_after(&rb, e.last_submission(), row, row, 1).map_err(|e| log::error!("gpu 3d readback: {e}")).ok()?;
Some(out)
}
+227 -41
View File
@@ -128,6 +128,11 @@ const POOL_BUDGET: usize = 256 << 20;
/// miss (a new allocation), never a wrong pixel.
const STALE_FRAMES: u64 = 2;
/// The pool owner of uploaded textures: never registered by an [`Enc`] (their ids start at 1),
/// so an upload takes only from the shared free list ([`Pool::take`], textures no open encoder
/// can read) and gives its texture back through the guarded [`Pool::release`].
const UPLOAD_OWNER: u64 = 0;
/// Readback staging buffers kept for reuse across sizes (bytes).
const STAGING_BUDGET: u64 = 256 << 20;
@@ -693,6 +698,10 @@ pub struct GpuContext {
dummy_tex: wgpu::TextureView,
dummy_buf: wgpu::Buffer,
uploads: Mutex<Uploads>,
/// Uploaded footage pictures, shared by the layers that show them (see [`crate::upload`]).
pictures: Mutex<crate::upload::Pictures>,
/// Reused staging buffers of large uploads (see [`crate::upload::Staging`]).
upload_staging: crate::upload::Staging,
upload_budget: std::sync::atomic::AtomicUsize,
upload_full_warned: std::sync::atomic::AtomicBool,
pool: Arc<Mutex<Pool>>,
@@ -1017,6 +1026,8 @@ impl GpuContext {
dummy,
dummy_buf,
uploads: Mutex::new(Uploads::default()),
pictures: Default::default(),
upload_staging: Default::default(),
upload_budget: std::sync::atomic::AtomicUsize::new(UPLOAD_BUDGET),
upload_full_warned: Default::default(),
pool,
@@ -1124,6 +1135,24 @@ impl GpuContext {
}
}
/// The memory budget (the memory cap: a conservative share of the device, see `memcap`),
/// the allocator's peak, one frame's share of it and what the upload cache may still add.
/// `None` where any is unknown: no allocator report (Metal, WebGPU, GL) or the cap is off.
pub fn frame_memory(&self) -> Option<effectcraft_render::FrameMemory> {
let budget = self.memcap.cap();
let now = self.allocated_bytes();
self.memcap.note(now);
// The allocator's peak (sampled while frames render) holds a frame's working textures
// at their most; what is gone again after the frame (and at least what the pool keeps
// for the next) is one frame's share. Frames that had to wait for memory: no room.
let peak = self.memcap.peak();
let frame = if self.memcap.pressed() { budget } else { peak.saturating_sub(now).max(self.pool_retained_bytes() as u64) };
// The upload cache may still fill up to its budget.
let uploads = self.uploads.lock().map(|u| u.bytes).unwrap_or(0);
let reserve = self.upload_budget().saturating_sub(uploads) as u64;
(budget > 0 && peak > 0 && frame > 0).then_some(effectcraft_render::FrameMemory { budget, peak, frame, reserve })
}
/// Bytes held by wgpu's device allocator (`0` on backends that do not report).
pub(crate) fn allocated_bytes(&self) -> u64 {
self.device.generate_allocator_report().map_or(0, |r| r.total_allocated_bytes)
@@ -1132,11 +1161,11 @@ impl GpuContext {
/// Bytes held in long-lived or still-referenced GPU caches/leases that submitting and waiting
/// on `owner` (`closing_enc`) cannot free.
fn kept_bytes(&self, owner: u64, closing_enc: Option<u64>) -> u64 {
let uploads = self.uploads.lock().map(|u| u.bytes as u64).unwrap_or(0);
// (uploaded layer and footage textures are pooled: the pool's leases count them)
let pool = self.pool.lock().map(|p| p.kept_after_flush(owner, closing_enc) as u64).unwrap_or(0);
let staging: u64 = self.staging.lock().map(|s| s.iter().map(|(k, v)| k * v.len() as u64).sum()).unwrap_or(0);
let zeros: u64 = self.zeros.lock().map(|z| z.keys().map(|&(w, h)| w as u64 * h as u64 * 16).sum()).unwrap_or(0);
uploads + pool + staging + zeros
pool + staging + self.upload_staging.bytes() + zeros
}
#[cfg(test)]
@@ -1198,18 +1227,99 @@ impl GpuContext {
/// never consumes the readback deadline or holds the mutex across the whole timeout.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_buffer(&self, buffer: &wgpu::Buffer, row: usize, stride: usize, height: usize) -> Result<Vec<u8>, String> {
self.read_buffer_after(buffer, None, row, stride, height)
}
/// [`Self::read_buffer`] waiting only for `submission` (the one that wrote `buffer`), not
/// for every submission so far: another frame's GPU work submitted meanwhile doesn't hold
/// this readback up (#3335). `None`: wait for all submitted work.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_buffer_after(
&self,
buffer: &wgpu::Buffer,
submission: Option<wgpu::SubmissionIndex>,
row: usize,
stride: usize,
height: usize,
) -> Result<Vec<u8>, String> {
self.read_mapped(buffer, submission, |view| crate::readback::packed(view, row, stride, height))
}
/// Map `buffer`, wait for the mapping (only for `submission` when given) and hand the
/// mapped bytes to `read`, on this thread, before unmapping: `read` copies the rows
/// straight into their destination, once (#3335). The copy happens outside `poll_lock`, so
/// it doesn't hold up other threads' readbacks the way copying inside the map callback
/// (which runs during a device poll) would.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_mapped<R>(
&self,
buffer: &wgpu::Buffer,
submission: Option<wgpu::SubmissionIndex>,
read: impl FnOnce(&[u8]) -> Result<R, String>,
) -> Result<R, String> {
let (tx, rx) = std::sync::mpsc::channel();
self.map_readback(buffer, row, stride, height, move |r| {
let ticket = self.readbacks.start(move |r| {
let _ = tx.send(r);
})?;
if !ticket.active() {
return Err("GPU readback cancelled".into());
}
let b = buffer.clone();
buffer.map_async(wgpu::MapMode::Read, .., move |r| {
// Timed out or cancelled: nobody reads the mapping.
if !ticket.active() {
b.unmap();
return;
}
// Mapped: the waiting thread reads the rows and unmaps (an empty vector = mapped).
ticket.complete(r.map(|_| Vec::new()).map_err(|e| format!("GPU buffer mapping: {e}")));
});
// On any failure, unmap (harmless when not mapped; cancels a pending map) so a mapped
// buffer can never be handed out again.
let fail = |e: String| {
buffer.unmap();
e
};
self.wait_mapped(&rx, submission).map_err(fail)?.map_err(fail)?;
let result = {
let view = buffer.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}"));
view.and_then(|view| read(&view))
};
buffer.unmap();
result
}
/// Poll until `rx` reports the mapping (see [`Self::read_mapped`]): polling and receiving
/// share one deadline, started once `poll_lock` is first acquired and polled in
/// [`Self::POLL_SLICE`] slices so lock contention never consumes the readback deadline or
/// holds the mutex across the whole timeout. While another thread holds `poll_lock` it is
/// polling the device, which delivers this mapping too: wait for it instead of queueing.
#[cfg(not(target_arch = "wasm32"))]
fn wait_mapped(
&self,
rx: &std::sync::mpsc::Receiver<Result<Vec<u8>, String>>,
submission: Option<wgpu::SubmissionIndex>,
) -> Result<Result<Vec<u8>, String>, String> {
let mut deadline = None;
loop {
self.readbacks.check()?;
if let Ok(result) = rx.try_recv() {
return result;
return Ok(result);
}
let (remaining, slice, poll_res) = {
let _guard = self.poll_lock.lock().unwrap_or_else(|e| e.into_inner());
let _guard = match self.poll_lock.try_lock() {
Ok(g) => g,
Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
Err(std::sync::TryLockError::WouldBlock) => {
match rx.recv_timeout(std::time::Duration::from_millis(1)) {
Ok(result) => return Ok(result),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
Err(e) => return Err(format!("GPU readback completion channel: {e}")),
}
// The ticket's own deadline still bounds a wait that never gets the lock.
continue;
}
};
self.readbacks.check()?;
let d = *deadline.get_or_insert_with(|| std::time::Instant::now() + crate::readback::TIMEOUT);
let remaining = d.saturating_duration_since(std::time::Instant::now());
@@ -1217,12 +1327,12 @@ impl GpuContext {
(remaining, std::time::Duration::ZERO, Ok(wgpu::PollStatus::Poll))
} else {
let slice = remaining.min(Self::POLL_SLICE);
let res = self.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: Some(slice) });
let res = self.device.poll(wgpu::PollType::Wait { submission_index: submission.clone(), timeout: Some(slice) });
(remaining, slice, res)
}
};
if let Ok(result) = rx.try_recv() {
return result;
return Ok(result);
}
if remaining.is_zero() {
let error = "GPU readback receive timed out; context retired".to_string();
@@ -1241,7 +1351,7 @@ impl GpuContext {
}
// The callback may be running on another thread that polled the same device.
match rx.recv_timeout(remaining.min(std::time::Duration::from_millis(1))) {
Ok(result) => return result,
Ok(result) => return Ok(result),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
Err(e) => return Err(format!("GPU readback completion channel: {e}")),
}
@@ -1253,6 +1363,28 @@ impl GpuContext {
Err("synchronous GPU readback is unavailable in the browser".into())
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn read_buffer_after(
&self,
_buffer: &wgpu::Buffer,
_submission: Option<wgpu::SubmissionIndex>,
_row: usize,
_stride: usize,
_height: usize,
) -> Result<Vec<u8>, String> {
Err("synchronous GPU readback is unavailable in the browser".into())
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn read_mapped<R>(
&self,
_buffer: &wgpu::Buffer,
_submission: Option<wgpu::SubmissionIndex>,
_read: impl FnOnce(&[u8]) -> Result<R, String>,
) -> Result<R, String> {
Err("synchronous GPU readback is unavailable in the browser".into())
}
/// Switch to deferred readbacks (see [`crate::deferred`]): readbacks are never waited for.
pub fn set_deferred(&mut self, on: bool) {
self.readbacks.cancel_pending("GPU readback mode changed".into());
@@ -1446,6 +1578,8 @@ impl GpuContext {
dummy: self.dummy.clone(),
dummy_buf: self.dummy_buf.clone(),
uploads: Mutex::new(Uploads::default()),
pictures: Default::default(),
upload_staging: Default::default(),
upload_budget: std::sync::atomic::AtomicUsize::new(UPLOAD_BUDGET),
upload_full_warned: Default::default(),
pool: self.pool.clone(),
@@ -1625,16 +1759,54 @@ impl GpuContext {
return None;
}
self.transfers.count(true, img.data.len() * 16);
let g = self.image(img.width, img.height);
self.queue.write_texture(
wgpu::TexelCopyTextureInfo { texture: &g.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },
bytemuck::cast_slice(&img.data),
wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(img.width * 16), rows_per_image: Some(img.height) },
wgpu::Extent3d { width: img.width, height: img.height, depth_or_array_layers: 1 },
);
// A texture from the pool's shared free list: no open encoder can still read it, and the
// copy below runs before anything recorded after this call.
let (g, _) = self.pooled(img.width, img.height, UPLOAD_OWNER);
if !self.upload_staging.write(&self.device, &self.queue, img, &g.texture) {
self.queue.write_texture(
wgpu::TexelCopyTextureInfo { texture: &g.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },
bytemuck::cast_slice(&img.data),
wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(img.width * 16), rows_per_image: Some(img.height) },
wgpu::Extent3d { width: img.width, height: img.height, depth_or_array_layers: 1 },
);
}
Some(g)
}
/// Upload a decoded footage picture once while the footage source keeps it: every layer
/// and effect input showing the same frame shares the texture (`Renderer::footage_picture`).
pub(crate) fn upload_picture(&self, img: &Arc<Image>) -> Option<GpuImage> {
if let Some(t) = self.pictures.lock().ok().and_then(|mut p| p.get(img)) {
return Some(t);
}
let tex = self.upload_image(img)?;
if let Ok(mut p) = self.pictures.lock() {
p.insert(img, tex.clone(), crate::upload::PICTURE_BUDGET.min(self.upload_budget() / 4));
}
Some(tex)
}
/// Entries in the footage picture cache.
#[cfg(test)]
pub(crate) fn picture_entries(&self) -> usize {
self.pictures.lock().map(|p| p.len()).unwrap_or_default()
}
/// The upload staging buffers (tests).
#[cfg(test)]
pub(crate) fn upload_staging(&self) -> &crate::upload::Staging {
&self.upload_staging
}
/// A render is done: drop the footage picture textures and the upload staging buffers
/// (the next render's pictures upload again; their textures come from the pool).
pub fn trim_uploads(&self) {
if let Ok(mut p) = self.pictures.lock() {
p.clear();
}
self.upload_staging.clear();
}
/// Entries in the layer upload cache.
#[cfg(test)]
pub(crate) fn upload_entries(&self) -> usize {
@@ -1696,6 +1868,10 @@ impl GpuContext {
u.map.clear();
u.bytes = 0;
}
if let Ok(mut p) = self.pictures.lock() {
p.clear();
}
self.upload_staging.clear();
if let Ok(mut p) = self.pool.lock() {
p.clear();
}
@@ -1952,6 +2128,11 @@ impl<'g> Enc<'g> {
dst
}
/// The last submission of this encoder's work (what a readback after it waits for).
pub(crate) fn last_submission(&self) -> Option<wgpu::SubmissionIndex> {
self.last_submit.clone()
}
/// Submit recorded work.
pub fn submit(&mut self) {
if let Some((enc, id)) = self.enc.take() {
@@ -1970,6 +2151,7 @@ impl<'g> Enc<'g> {
return;
}
let allocated = self.g.allocated_bytes();
self.g.memcap.note(allocated);
if allocated <= self.g.memcap.cap() {
let _ = self.g.memcap.should_wait(allocated, 0);
return;
@@ -1994,13 +2176,13 @@ impl<'g> Enc<'g> {
let layout = crate::readback::Layout::new(img.width, img.height, 16, self.g.device.limits().max_buffer_size)
.map_err(|e| log::error!("gpu image readback: {e}"))
.ok()?;
let mut data = Vec::new();
data.try_reserve_exact(layout.len / 16).map_err(|e| log::error!("gpu image allocation: {e}")).ok()?;
data.resize(layout.len / 16, [0.0; 4]);
let mut out = Image { width: img.width, height: img.height, data };
let dst: &mut [u8] = bytemuck::cast_slice_mut(&mut out.data);
self.read_texture_into(&img.texture, [0, 0, img.width, img.height], 16, dst)?;
Some(out)
// The mapped rows go straight into the image's pixels: one copy (#3335).
let data: Vec<[f32; 4]> = self.read_texture_with(&img.texture, [0, 0, img.width, img.height], 16, crate::readback::rows::<[f32; 4]>)?;
if data.len() != layout.len / 16 {
log::error!("gpu image readback size mismatch");
return None;
}
Some(Image { width: img.width, height: img.height, data })
}
/// Tightly packed texel bytes of a texture (`bpp` bytes per pixel).
@@ -2013,27 +2195,32 @@ impl<'g> Enc<'g> {
pub fn read_rect(&mut self, texture: &wgpu::Texture, x: u32, y: u32, w: u32, h: u32, bpp: u32) -> Option<Vec<u8>> {
let layout =
crate::readback::Layout::new(w, h, bpp, self.g.device.limits().max_buffer_size).map_err(|e| log::error!("gpu texture readback: {e}")).ok()?;
let mut out = Vec::new();
out.try_reserve_exact(layout.len).map_err(|e| log::error!("gpu readback allocation: {e}")).ok()?;
out.resize(layout.len, 0);
self.read_texture_into(texture, [x, y, w, h], bpp, &mut out)?;
let out: Vec<u8> = self.read_texture_with(texture, [x, y, w, h], bpp, crate::readback::rows::<u8>)?;
if out.len() != layout.len {
log::error!("gpu texture readback size mismatch");
return None;
}
Some(out)
}
/// [`Enc::read_rect`] into `out` (`w × bpp × h` bytes), through a reused staging buffer.
/// `rect` is `[x, y, w, h]`.
fn read_texture_into(&mut self, texture: &wgpu::Texture, rect: [u32; 4], bpp: u32, out: &mut [u8]) -> Option<()> {
/// Read `rect` (`[x, y, w, h]`) of `texture` back through a reused staging buffer: `read`
/// gets the mapped bytes, the row length, the padded row stride and the row count, and
/// copies the rows into their destination itself (one copy, #3335). Waits only for this
/// encoder's own submission, so frames rendering on other threads don't delay it.
fn read_texture_with<R>(
&mut self,
texture: &wgpu::Texture,
rect: [u32; 4],
bpp: u32,
read: impl FnOnce(&[u8], usize, usize, usize) -> Result<R, String>,
) -> Option<R> {
let [x, y, w, h] = rect;
if !self.g.can_wait() {
return None;
}
self.g.transfers.count(false, out.len());
let layout =
crate::readback::Layout::new(w, h, bpp, self.g.device.limits().max_buffer_size).map_err(|e| log::error!("gpu texture readback: {e}")).ok()?;
if out.len() != layout.len {
log::error!("gpu readback destination size mismatch");
return None;
}
self.g.transfers.count(false, layout.len);
let row = layout.row as usize;
let padded = layout.stride;
let size = layout.size;
@@ -2051,12 +2238,11 @@ impl<'g> Enc<'g> {
wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
);
self.submit();
let bytes = self.g.read_buffer(&buf, row, padded as usize, h as usize).map_err(|e| log::error!("gpu texture readback: {e}")).ok()?;
if bytes.len() != out.len() {
log::error!("gpu texture readback size mismatch");
return None;
}
out.copy_from_slice(&bytes);
let out = self
.g
.read_mapped(&buf, self.last_submit.clone(), |view| read(view, row, padded as usize, h as usize))
.map_err(|e| log::error!("gpu texture readback: {e}"))
.ok()?;
// Keep a few staging buffers per size (frames read back at the same size every time).
// Layers whose bounds change every frame read back at a new size each time: drop the
// other sizes once over budget, or mapped host memory grows for the whole render (#2081).
@@ -2070,7 +2256,7 @@ impl<'g> Enc<'g> {
v.push(buf);
}
}
Some(())
Some(out)
}
/// [`Enc::read_texture`] without waiting for the GPU (the browser's main thread can't): `done`
+29 -3
View File
@@ -84,6 +84,7 @@ mod memcap;
mod ops;
mod particles;
mod readback;
mod upload;
mod walk;
mod yuv;
@@ -300,7 +301,12 @@ impl Gpu {
/// Browser callers use the host/owned-device failure bridge and bounded readbacks instead
/// of blocking their event loop on an error-scope future.
pub fn within_memory<T>(&self, f: impl FnOnce() -> T) -> Result<T, FrameError> {
self.ctx.check_health().map_err(FrameError::Retired)?;
if let Err(error) = self.ctx.check_health() {
// Queued jobs may still own this context after the UI detached it. Release its
// working caches before CPU fallback adds host-memory pressure (#3357).
self.ctx.discard_kept();
return Err(FrameError::Retired(error));
}
#[cfg(not(target_arch = "wasm32"))]
{
let oom = self.ctx.device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
@@ -314,7 +320,7 @@ impl Gpu {
// The allocation failure first: a validation error is often its consequence.
let errors: Vec<String> = [oom, internal, validation].into_iter().flatten().map(|e| e.to_string()).collect();
if errors.is_empty() {
return self.ctx.check_health().map(|()| out).map_err(FrameError::Retired);
return self.finish_memory_attempt(out);
}
// Its textures go back to the pool when dropped: drop them before emptying it.
drop(out);
@@ -325,7 +331,21 @@ impl Gpu {
#[cfg(target_arch = "wasm32")]
{
let out = f();
self.ctx.check_health().map(|()| out).map_err(FrameError::Retired)
self.finish_memory_attempt(out)
}
}
/// Retirement without a scoped error (for example, a readback timeout) must release
/// the same retained resources as a rejected allocation before the caller retries.
fn finish_memory_attempt<T>(&self, out: T) -> Result<T, FrameError> {
match self.ctx.check_health() {
Ok(()) => Ok(out),
Err(error) => {
// Dropping the result can return leased textures to the pool.
drop(out);
self.ctx.discard_kept();
Err(FrameError::Retired(error))
}
}
}
@@ -432,6 +452,11 @@ impl Accelerator for Gpu {
Some(&self.auto)
}
fn frame_memory(&self) -> Option<effectcraft_render::FrameMemory> {
self.ctx.check_health().ok()?;
self.ctx.frame_memory()
}
fn render_end(&self) {
let (hits, misses) = self.ctx.pool_stats();
let pct = hits.saturating_mul(100) / hits.saturating_add(misses).max(1);
@@ -439,6 +464,7 @@ impl Accelerator for Gpu {
target: "effectcraft_gpu",
"gpu pool: {hits} working textures reused, {misses} allocated ({pct}% reuse) so far"
);
self.ctx.trim_uploads();
self.ctx.trim_pool();
}
+19
View File
@@ -40,6 +40,8 @@ pub(crate) struct MemCap {
floor: AtomicU64,
/// Set after the first over-cap sample where non-reclaimable memory alone exceeds the cap.
warned: AtomicBool,
/// The most the allocator held in any sample (what frames in flight are sized by, #3335).
peak: AtomicU64,
#[cfg(test)]
timeout_ns: AtomicU64,
}
@@ -59,6 +61,7 @@ impl MemCap {
waits: AtomicU64::new(0),
floor: AtomicU64::new(0),
warned: AtomicBool::new(false),
peak: AtomicU64::new(0),
#[cfg(test)]
timeout_ns: AtomicU64::new(crate::readback::TIMEOUT.as_nanos() as u64),
}
@@ -74,6 +77,7 @@ impl MemCap {
waits: AtomicU64::new(0),
floor: AtomicU64::new(0),
warned: AtomicBool::new(false),
peak: AtomicU64::new(0),
timeout_ns: AtomicU64::new(self.timeout_ns.load(Relaxed)),
}
}
@@ -82,6 +86,21 @@ impl MemCap {
self.cap
}
/// Remember an allocator sample: the peak only grows.
pub(crate) fn note(&self, allocated: u64) {
self.peak.fetch_max(allocated, Relaxed);
}
/// The most the allocator held in any sample so far.
pub(crate) fn peak(&self) -> u64 {
self.peak.load(Relaxed)
}
/// Whether frames have had to wait for memory (the device ran over the cap).
pub(crate) fn pressed(&self) -> bool {
self.waits.load(Relaxed) > 0
}
/// Whether to sample the allocator on this call.
pub(crate) fn due(&self) -> bool {
self.cap != 0 && self.calls.fetch_add(1, Relaxed).is_multiple_of(EVERY)
+41 -5
View File
@@ -367,20 +367,32 @@ thread_local! {
/// Validate every row before publishing tightly packed bytes.
pub(crate) fn packed(view: &[u8], row: usize, stride: usize, height: usize) -> Result<Vec<u8>, Error> {
if row == 0 || height == 0 || stride < row {
rows(view, row, stride, height)
}
/// The `height` rows of `row` bytes each, `stride` bytes apart in the mapped `view`, as tightly
/// packed `T`s: one copy, straight from the mapped rows into a vector reserved once (no
/// zero-filled buffer first, no intermediate byte vector). Every row is validated before any
/// is copied. A row that is not aligned for `T` is decoded value by value (same bytes).
pub(crate) fn rows<T: bytemuck::Pod>(view: &[u8], row: usize, stride: usize, height: usize) -> Result<Vec<T>, Error> {
let unit = std::mem::size_of::<T>().max(1);
if row == 0 || height == 0 || stride < row || !row.is_multiple_of(unit) {
return Err("invalid GPU readback layout".into());
}
let size = row.checked_mul(height).ok_or("GPU readback output size overflow")?;
let size = row.checked_mul(height).ok_or("GPU readback output size overflow")? / unit;
let end = (height - 1).checked_mul(stride).and_then(|s| s.checked_add(row)).ok_or("GPU readback mapped size overflow")?;
if view.len() < end {
return Err(format!("short GPU readback: {} bytes, needs {end}", view.len()));
}
let mut out = Vec::new();
let mut out: Vec<T> = Vec::new();
out.try_reserve_exact(size).map_err(|e| format!("allocating GPU readback output: {e}"))?;
for y in 0..height {
let start = y.checked_mul(stride).ok_or("GPU readback row overflow")?;
let bytes = view.get(start..start + row).ok_or("short GPU readback row")?;
out.extend_from_slice(bytes);
let bytes = start.checked_add(row).and_then(|e| view.get(start..e)).ok_or("short GPU readback row")?;
match bytemuck::try_cast_slice::<u8, T>(bytes) {
Ok(values) => out.extend_from_slice(values),
Err(_) => out.extend(bytes.chunks_exact(unit).map(bytemuck::pod_read_unaligned::<T>)),
}
}
Ok(out)
}
@@ -658,4 +670,28 @@ mod tests {
assert!(packed(&[0; 7], 4, 4, 2).is_err());
assert_eq!(packed(&[1, 2, 0, 0, 3, 4], 2, 4, 2).unwrap(), vec![1, 2, 3, 4]);
}
/// #3335: rows go from the mapped buffer straight into the destination type, skipping the
/// row padding, whether or not the mapped bytes are aligned for it.
#[test]
fn rows_copy_once_into_the_destination_type() {
// Two rows of two RGBA f32 pixels, padded to a 48-byte stride.
let px = |i: f32| [i, i + 0.25, i + 0.5, 1.0];
let want = vec![px(1.0), px(2.0), px(3.0), px(4.0)];
let mut view = Vec::new();
for r in want.chunks(2) {
view.extend_from_slice(bytemuck::cast_slice(r));
view.extend_from_slice(&[0xAB; 16]);
}
assert_eq!(rows::<[f32; 4]>(&view, 32, 48, 2).unwrap(), want);
// The same bytes one byte off any f32 alignment decode the same.
let mut shifted = vec![0u8];
shifted.extend_from_slice(&view);
assert_eq!(rows::<[f32; 4]>(&shifted[1..], 32, 48, 2).unwrap(), want);
// A row that isn't whole pixels, a short last row and a stride under the row fail.
assert!(rows::<[f32; 4]>(&view, 30, 48, 2).is_err());
assert!(rows::<[f32; 4]>(&view[..view.len() - 17], 32, 48, 2).is_err());
assert!(rows::<[f32; 4]>(&view, 32, 16, 2).is_err());
assert_eq!(rows::<u8>(&[1, 2, 9, 3, 4], 2, 3, 2).unwrap(), vec![1, 2, 3, 4]);
}
}
+88
View File
@@ -28,8 +28,10 @@ use crate::Gpu;
mod adjust;
mod adv3d;
mod bench_share;
mod deferred;
mod deferred_jobs;
mod footage_share;
mod fx_color;
mod fx_depth;
mod fx_distort;
@@ -996,6 +998,49 @@ fn within_memory_returns_the_work_when_there_is_room() {
assert_eq!((img.width, img.height), (64, 32));
}
/// A timeout can retire a context without any device error scope reporting a failure.
/// Queued jobs keep that context alive, so CPU fallback must not wait for its last owner
/// to drop before releasing cached uploads and working textures (#3357).
fn retired_attempt_releases_kept_resources(retire_before: bool) {
hold_gpu_lock();
let made = Gpu::try_headless();
let Some(g) = require_gpu("retired memory attempt", made.as_ref()) else { return };
let buf = Arc::new(effectcraft_effects::Buf { img: Image::new(16, 16), offset: [0.0; 2], scale: 1.0 });
drop(g.ctx.upload_buf(&buf).expect("a small cached upload"));
drop(crate::context::Enc::new(&g.ctx).image(32, 16));
assert_eq!(g.ctx.upload_entries(), 1);
assert!(g.ctx.pool_retained_bytes() > 0);
// An earlier frame can return a lease after retirement; it must not refill the pool.
let earlier = crate::context::Enc::new(&g.ctx).image(48, 16);
let retire = g.ctx.device_failure_notifier();
if retire_before {
retire("synthetic readback retirement".into());
}
let called = std::cell::Cell::new(false);
let result = g.within_memory(|| {
called.set(true);
let out = crate::context::Enc::new(&g.ctx).image(64, 16);
retire("synthetic readback retirement".into());
out
});
assert!(matches!(result, Err(crate::FrameError::Retired(_))));
assert_eq!(called.get(), !retire_before);
assert_eq!(g.ctx.upload_entries(), 0, "CPU fallback must not retain uploaded layers");
assert_eq!(g.ctx.pool_retained_bytes(), 0, "CPU fallback must not retain working textures");
drop(earlier);
assert_eq!(g.ctx.pool_retained_bytes(), 0, "late leases cannot refill a retired pool");
}
#[test]
fn within_memory_releases_caches_when_already_retired() {
retired_attempt_releases_kept_resources(true);
}
#[test]
fn within_memory_releases_caches_when_retired_during_work() {
retired_attempt_releases_kept_resources(false);
}
/// A texture the device rejected (deliberately too wide), made outside any frame under a scope
/// of its own: the view a frame then makes of it is the reported validation error.
fn rejected_texture(g: &Gpu) -> wgpu::Texture {
@@ -1247,6 +1292,49 @@ fn readback_staging_stays_bounded_across_sizes() {
assert!(held <= (256 << 20) + 2 * 1024 * 16 * 376, "staging buffers hold {held} bytes");
}
/// #3335: a readback copies the mapped rows straight into the image (no zero-filled buffer,
/// no intermediate byte vector), skipping each row's padding: odd widths, sub-rectangles and
/// readbacks from several threads at once (each waiting for its own submission) return the
/// uploaded pixels exactly.
#[test]
fn single_copy_readback_returns_the_exact_pixels() {
let Some(g) = gpu() else { return };
if !g.ctx.can_wait() {
return;
}
let image = |w: u32, h: u32, seed: f32| {
let mut img = Image::new(w, h);
for (i, p) in img.data.iter_mut().enumerate() {
let v = i as f32 * 0.37 + seed;
*p = [v, -v, v * 1e-3, 1.0 / (1.0 + v)];
}
img
};
// 37 × 16 bytes = 592 per row: padded to 768 in the staging buffer.
let src = image(37, 5, 0.5);
let up = g.ctx.upload_image(&src).expect("upload");
let mut e = crate::context::Enc::new(&g.ctx);
let back = e.download(&up).expect("download");
assert_eq!((back.width, back.height), (37, 5));
assert_eq!(back.data, src.data);
let rect = e.read_rect(&up.texture, 3, 1, 5, 2, 16).expect("read_rect");
let want: Vec<[f32; 4]> = (1..3).flat_map(|y| (3..8).map(move |x| (x, y))).map(|(x, y)| src.data[src.idx(x, y)]).collect();
assert_eq!(rect, bytemuck::cast_slice::<[f32; 4], u8>(&want));
// Frames on several threads, each with its own encoder and submission.
std::thread::scope(|s| {
for t in 0..4 {
s.spawn(move || {
for k in 0..3 {
let src = image(61 + t, 7 + k, (t * 10 + k) as f32);
let up = g.ctx.upload_image(&src).expect("upload");
let back = crate::context::Enc::new(&g.ctx).download(&up).expect("download");
assert_eq!(back.data, src.data, "thread {t} frame {k}");
}
});
}
});
}
/// A static adjustment layer with a time-dependent effect (Noise) keeps its footprint key
/// across frames (the footprint is the source through the masks, not the effects), so the GPU
/// uploads nothing after the first frame; matches the CPU.
+83
View File
@@ -0,0 +1,83 @@
//! Warm-frame benchmark of #3333 (run by hand): three layers of the same 1080p footage frame,
//! 32 bpc, a 30-frame sequence already decoded (as the media pool serves a warm frame), with a
//! layer cache as the Render Queue has. Prints the median and mean ms per frame and a hash of
//! every frame (to compare the pixels of two builds).
//!
//! `cargo test -p effectcraft-gpu --release --lib bench_share -- --ignored --nocapture`
use std::collections::HashMap;
use std::sync::Mutex;
use super::*;
struct Decoded {
frames: Mutex<HashMap<i64, Arc<Image>>>,
}
impl FootageSource for Decoded {
fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option<Arc<Image>> {
let i = f.frame_rate.frame_at(t).clamp(0, 29);
let mut m = self.frames.lock().unwrap();
Some(m.entry(i).or_insert_with(|| Arc::new(pattern(item.0 as u32 + i as u32, f.width, f.height))).clone())
}
}
#[test]
#[ignore]
fn bench_share() {
let Some(g) = gpu() else { return };
let (w, h) = (1920, 1080);
let mut p = Project::default();
p.settings.bit_depth = BitDepth::Bpc32;
let comp = Comp::new(w, h, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into()));
let f = Footage {
path: "seq.exr".into(),
kind: FootageKind::Sequence,
width: w,
height: h,
pixel_aspect: 1.0,
frame_rate: FrameRate::FPS_30,
duration: Tick::from_seconds_f64(1.0),
has_video: true,
alpha: AlphaMode::Premultiplied,
..Default::default()
};
let fid = p.add_item("Sequence", Label::Lavender, None, ItemKind::Footage(f));
for i in 0..3 {
let mut l = build::layer(&mut p, &comp, "Sequence", LayerSource::Footage { item: fid }, (w, h), None);
if i > 0 {
l.blend_mode = BlendMode::Add;
set(&mut l, "transform/opacity", Value::Scalar(25.0));
}
p.comp_mut(cid).unwrap().layers.insert(0, l);
}
let src = Decoded { frames: Mutex::new(HashMap::new()) };
let frames = 30;
let mut runs = vec![];
let mut hashes = vec![];
for pass in 0..4 {
let cache = effectcraft_render::cache::LayerCache::new(1 << 30);
let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Gpu, ..Default::default() });
r.accel = Some(g);
r.cache = Some(&cache);
for k in 0..frames {
let t0 = std::time::Instant::now();
let img = g.render(&r, cid, Tick::from_seconds_f64(k as f64 / 30.0)).expect("the GPU renders the frame");
let ms = t0.elapsed().as_secs_f64() * 1e3;
if pass > 0 {
runs.push(ms);
}
if pass == 1 {
use std::hash::{Hash, Hasher};
let mut hs = std::collections::hash_map::DefaultHasher::new();
img.data.iter().for_each(|px| px.map(f32::to_bits).hash(&mut hs));
hashes.push(hs.finish());
}
}
}
runs.sort_by(f64::total_cmp);
let mean = runs.iter().sum::<f64>() / runs.len() as f64;
eprintln!("bench_share: {} frames, median {:.1} ms, mean {:.1} ms, min {:.1} ms", runs.len(), runs[runs.len() / 2], mean, runs[0]);
eprintln!("bench_share frame hashes: {hashes:016x?}");
}
+233
View File
@@ -0,0 +1,233 @@
//! #3333: a footage frame uploads once per frame and its texture is shared by every layer and
//! effect input that shows it; the frame is bit-identical to drawing each layer from its own copy.
use std::collections::HashMap;
use std::sync::Mutex;
use super::*;
use crate::walk::SHARED_PICTURES;
/// Keeps decoded frames as the media pool does: the same frame (at the same size) of the same
/// footage is the same `Arc` while kept. Counts the frames it decodes.
#[derive(Default)]
struct Kept {
frames: Mutex<HashMap<(ItemId, u32, u32, i64, Option<(u32, u32)>), Arc<Image>>>,
}
impl Kept {
fn get(&self, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option<Arc<Image>> {
let i = f.frame_rate.frame_at(t);
let mut m = self.frames.lock().unwrap();
let img = m.entry((item, f.width, f.height, i, size)).or_insert_with(|| {
let full = pattern(item.0 as u32 + i as u32 * 7 + f.width, f.width, f.height);
Arc::new(match size {
Some((w, h)) => effectcraft_raster::resample(&full, w, h),
None => full,
})
});
Some(img.clone())
}
}
impl FootageSource for Kept {
fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option<Arc<Image>> {
self.get(item, f, t, None)
}
fn frame_at_size(&self, item: ItemId, f: &Footage, t: Tick, width: u32, height: u32) -> Option<Arc<Image>> {
self.get(item, f, t, Some((width, height)))
}
}
/// One footage item (a sequence: a new picture every frame) shown by `layers` layers, each
/// placed differently (the first fills the comp).
fn shared_scene(depth: BitDepth, layers: usize) -> (Scene, ItemId) {
let mut s = Scene::new(depth);
let f = Footage {
path: "seq.exr".into(),
kind: FootageKind::Sequence,
width: 97,
height: 61,
pixel_aspect: 1.0,
frame_rate: FrameRate::FPS_30,
duration: Tick::from_seconds_f64(2.0),
has_video: true,
alpha: AlphaMode::Premultiplied,
..Default::default()
};
let fid = s.p.add_item("Sequence", Label::Lavender, None, ItemKind::Footage(f));
for i in 0..layers {
let mut l = build::layer(&mut s.p, &s.comp, "Sequence", LayerSource::Footage { item: fid }, (97, 61), None);
if i > 0 {
set(&mut l, "transform/rotation", Value::Scalar(11.0 * i as f64));
set(&mut l, "transform/scale", Value::Vec3([70.0 + 9.0 * i as f64, 64.0, 100.0]));
set(&mut l, "transform/position", v3(30.0 + 13.0 * i as f64, 25.0 + 4.0 * i as f64));
set(&mut l, "transform/opacity", Value::Scalar(80.0));
l.blend_mode = if i % 2 == 0 { BlendMode::Screen } else { BlendMode::Add };
}
s.push(l);
}
(s, fid)
}
/// The GPU frame with shared pictures on and off, and the number of uploads of each.
fn render_both(g: &crate::Gpu, s: &Scene, src: &Kept, opts: RenderOpts, t: Tick) -> (Image, Image, u64, u64) {
let mut r = Renderer::new(&s.p, src, RenderOpts { backend: Backend::Gpu, ..opts });
r.accel = Some(g);
let before = g.ctx.transfer_stats().uploads;
let on = g.render(&r, s.cid, t).expect("the GPU renders the frame");
let uploads = g.ctx.transfer_stats().uploads - before;
SHARED_PICTURES.store(false, std::sync::atomic::Ordering::Relaxed);
let before = g.ctx.transfer_stats().uploads;
let off = g.render(&r, s.cid, t);
let copied = g.ctx.transfer_stats().uploads - before;
SHARED_PICTURES.store(true, std::sync::atomic::Ordering::Relaxed);
(on, off.expect("the GPU renders the frame from per-layer copies"), uploads, copied)
}
fn check_cpu(label: &str, s: &Scene, src: &Kept, opts: RenderOpts, t: Tick, gpu_img: &Image) {
let cpu = Renderer::new(&s.p, src, RenderOpts { backend: Backend::Cpu, ..opts }).comp_frame_cpu(s.cid, t);
let mut d = diff(&cpu, gpu_img, tolerance(s.p.settings.bit_depth));
d.quantized = s.p.settings.bit_depth != BitDepth::Bpc32;
check(label, Some(d), 0.0);
}
/// Three layers showing the same frame upload it once; a fourth that runs an effect on it
/// (resident on the GPU) reads the same texture as its input. Every frame of the sequence
/// uploads its own picture once.
#[test]
fn three_layers_of_one_footage_frame_upload_once() {
let Some(g) = gpu() else { return };
let src = Kept::default();
let (mut s, fid) = shared_scene(BitDepth::Bpc32, 3);
for k in 0..3 {
let t = Tick::from_seconds_f64(k as f64 / 30.0);
let (on, off, uploads, copied) = render_both(g, &s, &src, opts(), t);
assert_eq!(uploads, 1, "frame {k}: three layers of one footage frame uploaded {uploads} times");
assert_eq!(copied, 3, "frame {k}: per-layer copies upload once per layer");
assert!(on.data == off.data, "frame {k}: the shared picture changed the frame");
check_cpu(&format!("shared picture frame {k}"), &s, &src, opts(), t, &on);
}
// An effect input: the blur reads the shared texture. Alone, the blurred layer uploads
// its picture (and whatever the blur itself uploads); the three plain layers showing the
// same frame add no upload.
let mut fx = build::layer(&mut s.p, &s.comp, "Blurred", LayerSource::Footage { item: fid }, (97, 61), None);
set(&mut fx, "transform/position", v3(20.0, 20.0));
s.effect(&mut fx, "ec.blur.gaussian", &[("blurriness", n(3.0))]);
let (mut alone, _) = shared_scene(BitDepth::Bpc32, 0);
alone.p = s.p.clone();
alone.p.comp_mut(alone.cid).unwrap().layers = vec![fx.clone()].into();
let (_, _, blur_uploads, _) = render_both(g, &alone, &src, opts(), Tick::from_seconds_f64(0.4));
s.push(fx);
let t = Tick::from_seconds_f64(0.5);
let (on, off, uploads, _) = render_both(g, &s, &src, opts(), t);
assert_eq!(uploads, blur_uploads, "three layers and an effect input of one footage frame uploaded {uploads} times, the effect layer alone {blur_uploads}");
assert!(on.data == off.data, "the shared picture changed the frame with an effect input");
check_cpu("shared picture with an effect input", &s, &src, opts(), t, &on);
}
/// The frame is bit-identical with shared pictures on and off wherever they apply or not: every
/// bit depth, colour management and linear blending, half resolution, a track matte, Preserve
/// Transparency, motion blur, masks, layer styles and a proxy.
#[test]
fn shared_pictures_do_not_change_the_frame() {
let Some(g) = gpu() else { return };
let src = Kept::default();
let t = Tick::from_seconds_f64(0.2);
for depth in [BitDepth::Bpc8, BitDepth::Bpc16, BitDepth::Bpc32] {
let mut cases: Vec<(String, Scene, RenderOpts)> = vec![];
cases.push(("plain".into(), shared_scene(depth, 3).0, opts()));
cases.push(("half resolution".into(), shared_scene(depth, 3).0, RenderOpts { scale: 0.5, ..opts() }));
for (ws, blend_linear) in [(Some(ColorSpace::Rec709), false), (None, true)] {
let (mut s, _) = shared_scene(depth, 3);
s.p.settings.working_space = ws;
s.p.settings.blend_linear = blend_linear;
cases.push((format!("colour {ws:?} linear blending {blend_linear}"), s, opts()));
}
{
let (mut s, _) = shared_scene(depth, 3);
let comp = s.p.comp_mut(s.cid).unwrap();
comp.enable_motion_blur = true;
let matte = comp.layers[0].id;
comp.layers[1].track_matte = Some(TrackMatte { layer: matte, kind: MatteKind::Luma });
comp.layers[2].preserve_transparency = true;
comp.layers[2].switches.motion_blur = true;
let pos = comp.layers[2].props.prop_mut("transform/position").unwrap();
pos.keys = vec![Keyframe::new(Tick::ZERO, v3(10.0, 20.0)), Keyframe::new(Tick::from_seconds_f64(0.5), v3(90.0, 45.0))];
s.comp.enable_motion_blur = true;
cases.push(("matte, preserve transparency, motion blur".into(), s, opts()));
}
{
let (mut s, _) = shared_scene(depth, 3);
let mut next = s.p.next_id;
let m = build::mask(&mut Ids(&mut next), "Mask 1", ShapePath::ellipse([40.0, 30.0], 60.0, 40.0), MaskMode::Add, [255, 255, 0]);
let comp = s.p.comp_mut(s.cid).unwrap();
comp.layers[0].props.sub_mut("masks").unwrap().children.push(m.into());
effectcraft_project::styles::add_style(&mut comp.layers[1], &mut Ids(&mut next), "dropShadow", &s.comp.global_light, true).unwrap();
s.p.next_id = next;
cases.push(("mask and layer style".into(), s, opts()));
}
{
let (mut s, fid) = shared_scene(depth, 3);
let proxy = Footage { path: "proxy.png".into(), kind: FootageKind::Still, width: 48, height: 30, has_video: true, ..Default::default() };
s.p.item_mut(fid).unwrap().proxy = Some(Box::new(effectcraft_project::Proxy { footage: proxy, enabled: true }));
cases.push(("proxy".into(), s, opts()));
}
for (label, s, o) in cases {
let (on, off, _, _) = render_both(g, &s, &src, o, t);
assert!(on.data == off.data, "{label} {depth:?}: the shared picture changed the frame");
check_cpu(&format!("{label} {depth:?}"), &s, &src, o, t, &on);
}
}
}
/// The picture cache keeps no texture of a picture the footage source dropped.
#[test]
fn picture_cache_drops_textures_of_dropped_pictures() {
let Some(g) = gpu() else { return };
let before = g.ctx.picture_entries();
for i in 0..6 {
let img = Arc::new(pattern(i, 64, 64));
let up = g.ctx.upload_picture(&img).expect("upload");
let again = g.ctx.upload_picture(&img).expect("upload");
assert!(up.texture == again.texture, "the same picture uploaded twice");
drop((up, again, img));
}
let kept = g.ctx.picture_entries();
assert!(kept <= before + 1, "the picture cache kept {kept} textures of dropped pictures (had {before})");
}
/// Large uploads copy through reused staging buffers (rows padded to the copy alignment) with
/// the same pixels as `Queue::write_texture`; the buffers stay within their budget, and a
/// clear while one is in flight forgets it.
#[test]
fn staging_uploads_keep_pixels_and_reuse_buffers() {
let Some(g) = gpu() else { return };
let wait = || {
let _ = g.ctx.poll_readbacks(wgpu::PollType::wait_indefinitely());
};
// 300 px: 4800-byte rows, padded to 4864.
for (w, h) in [(300u32, 300u32), (512, 600)] {
let before = g.ctx.upload_staging().copies();
for i in 0..4 {
let img = pattern(40 + i, w, h);
let up = g.ctx.upload_image(&img).expect("upload");
let mut e = crate::context::Enc::new(&g.ctx);
let back = e.download(&up).expect("readback");
assert!(back.data.iter().flatten().map(|v| v.to_bits()).eq(img.data.iter().flatten().map(|v| v.to_bits())), "{w}×{h} upload {i} changed pixels");
drop((up, e));
wait();
}
assert!(g.ctx.upload_staging().copies() - before >= 2, "{w}×{h}: no upload went through a reused staging buffer");
assert!(g.ctx.upload_staging().bytes() <= 192 << 20);
}
// A clear with a copy in flight: nothing kept, and later uploads still work.
let img = pattern(7, 300, 300);
let up = g.ctx.upload_image(&img).expect("upload");
g.ctx.upload_staging().clear();
wait();
assert_eq!(g.ctx.upload_staging().bytes(), 0, "a cleared staging kept its buffers");
let mut e = crate::context::Enc::new(&g.ctx);
assert!(e.download(&up).expect("readback").data == img.data);
let again = g.ctx.upload_image(&img).expect("upload after a clear");
assert!(e.download(&again).expect("readback").data == img.data);
}
+264
View File
@@ -0,0 +1,264 @@
//! Footage uploads (#3333): each decoded footage picture uploads once and its texture is shared
//! by every layer and effect input that shows it ([`Pictures`]), and large uploads copy through
//! staging buffers that are kept mapped and reused ([`Staging`]) instead of one that
//! `Queue::write_texture` allocates and frees per upload.
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError, Weak};
use effectcraft_raster::Image;
use crate::context::GpuImage;
/// Bytes of picture textures kept after their frame (a sequence's next frame takes the oldest
/// one's texture back from the pool), at most a quarter of the context's upload budget. The
/// newest picture is always kept, whatever its size.
pub(crate) const PICTURE_BUDGET: usize = 256 << 20;
/// Uploads from this size go through [`Staging`]; smaller ones through `Queue::write_texture`.
const STAGING_MIN: u64 = 1 << 20;
/// Bytes of staging buffers that may exist at once (mapped and ready, or in flight).
const STAGING_BUDGET: u64 = 192 << 20;
struct Picture {
img: Weak<Image>,
tex: GpuImage,
bytes: usize,
last_use: u64,
}
/// Uploaded footage pictures by the identity of the decoded frame (its `Arc`): the footage
/// source serves one `Arc` per (file, frame, size, colour curve) while it keeps the frame, and
/// a new one when the file changed, so the same `Arc` means the same pixels. The `Weak` keeps
/// the allocation, so a key cannot be reused by another picture while its entry exists.
#[derive(Default)]
pub(crate) struct Pictures {
map: HashMap<usize, Picture>,
bytes: usize,
clock: u64,
}
impl Pictures {
/// The texture of `img` if it was uploaded and is still kept.
pub(crate) fn get(&mut self, img: &Arc<Image>) -> Option<GpuImage> {
self.clock += 1;
let now = self.clock;
let e = self.map.get_mut(&(Arc::as_ptr(img) as usize))?;
if !e.img.upgrade().is_some_and(|i| Arc::ptr_eq(&i, img)) {
return None;
}
e.last_use = now;
Some(e.tex.clone())
}
/// Keep `tex`, the upload of `img`. Pictures the footage source no longer holds go at once
/// (nothing can ask for them again), then the least recently used over [`PICTURE_BUDGET`],
/// never `img` itself. A dropped texture goes back to the pool once no open encoder can
/// still read it.
pub(crate) fn insert(&mut self, img: &Arc<Image>, tex: GpuImage, budget: usize) {
let key = Arc::as_ptr(img) as usize;
let bytes = img.data.len().saturating_mul(16);
self.clock += 1;
let entry = Picture { img: Arc::downgrade(img), tex, bytes, last_use: self.clock };
if let Some(old) = self.map.insert(key, entry) {
self.bytes = self.bytes.saturating_sub(old.bytes);
}
self.bytes = self.bytes.saturating_add(bytes);
let dead: Vec<usize> = self.map.iter().filter(|(_, e)| e.img.strong_count() == 0).map(|(k, _)| *k).collect();
for k in dead {
if let Some(e) = self.map.remove(&k) {
self.bytes = self.bytes.saturating_sub(e.bytes);
}
}
while self.bytes > budget {
let Some(k) = self.map.iter().filter(|(k, _)| **k != key).min_by_key(|(_, e)| e.last_use).map(|(k, _)| *k) else { break };
if let Some(e) = self.map.remove(&k) {
self.bytes = self.bytes.saturating_sub(e.bytes);
}
}
}
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.map.len()
}
pub(crate) fn clear(&mut self) {
self.map.clear();
self.bytes = 0;
}
}
#[derive(Default)]
struct StagingState {
/// Buffers mapped for writing and not in use, each mapped whole.
ready: Vec<wgpu::Buffer>,
/// Bytes of every staging buffer that exists (ready, being mapped or in flight).
total: u64,
/// Raised by [`Staging::clear`]: a buffer of an earlier generation is dropped when its
/// mapping completes.
generation: u64,
/// Uploads copied through a staging buffer (diagnostics, tests).
copies: u64,
}
/// Reused upload staging buffers (`MAP_WRITE | COPY_SRC`). A buffer is written while mapped,
/// unmapped, copied into the texture by a command buffer submitted at once, and mapped again
/// when that submission is done (`CommandEncoder::map_buffer_on_submit`; the callback runs when
/// the device is polled, as every frame does). A new buffer is mapped asynchronously too (never
/// `mapped_at_creation`, which fails hard on drivers that report a failed allocation as an
/// internal error, #591), so the upload that made it, and any upload while no ready buffer fits
/// or the budget is spent, goes through `Queue::write_texture` instead.
#[derive(Default)]
pub(crate) struct Staging {
state: Arc<Mutex<StagingState>>,
}
/// `bytes_per_row` of a staging copy: rows padded to `COPY_BYTES_PER_ROW_ALIGNMENT`.
fn padded_row(width: u32) -> Option<u64> {
let row = u64::from(width).checked_mul(16)?;
let a = u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
row.div_ceil(a).checked_mul(a)
}
impl Staging {
fn lock(&self) -> std::sync::MutexGuard<'_, StagingState> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Bytes of staging buffers that exist (see [`STAGING_BUDGET`]).
pub(crate) fn bytes(&self) -> u64 {
self.lock().total
}
/// Uploads copied through a staging buffer so far.
#[cfg(test)]
pub(crate) fn copies(&self) -> u64 {
self.lock().copies
}
/// Forget every staging buffer (after a failed frame, #1319): those in flight are dropped
/// when their mapping completes.
pub(crate) fn clear(&self) {
let mut s = self.lock();
s.ready.clear();
// (buffers still in flight are no longer counted: they drop when their mapping ends)
s.total = 0;
s.generation = s.generation.wrapping_add(1);
}
/// The callback of a write mapping of `buffer`: once mapped it is ready for the next
/// upload; it is dropped (and its bytes uncounted) when mapping failed, and dropped when
/// the staging was cleared meanwhile (whose [`Staging::clear`] stopped counting it).
fn on_mapped(&self, buffer: &wgpu::Buffer, generation: u64) -> impl FnOnce(Result<(), wgpu::BufferAsyncError>) + wgpu::WasmNotSend + 'static {
let state = Arc::downgrade(&self.state);
let buffer = buffer.clone();
move |res: Result<(), wgpu::BufferAsyncError>| {
let Some(state) = state.upgrade() else { return };
let mut s = state.lock().unwrap_or_else(PoisonError::into_inner);
if s.generation != generation {
return;
}
if res.is_ok() {
s.ready.push(buffer);
} else {
s.total = s.total.saturating_sub(buffer.size());
}
}
}
/// Copy `img` into `texture` (its size) through a ready staging buffer, recorded and
/// submitted at once: before anything an open encoder records later, as
/// `Queue::write_texture` would. `false`: not done (small, no ready buffer fits, or the
/// mapping failed); the caller writes it through the queue. Starts mapping a new buffer for
/// later uploads when none fits and the budget allows.
pub(crate) fn write(&self, device: &wgpu::Device, queue: &wgpu::Queue, img: &Image, texture: &wgpu::Texture) -> bool {
let (w, h) = (img.width, img.height);
let row = w as usize * 16;
let Some(stride) = padded_row(w) else { return false };
let (Some(need), Ok(stride32)) = (stride.checked_mul(u64::from(h)), u32::try_from(stride)) else { return false };
if need < STAGING_MIN || img.data.len() != w as usize * h as usize {
return false;
}
let (buffer, generation) = {
let mut s = self.lock();
let generation = s.generation;
// The smallest ready buffer that holds it.
let best = s.ready.iter().enumerate().filter(|(_, b)| b.size() >= need).min_by_key(|(_, b)| b.size()).map(|(i, _)| i);
match best {
Some(i) => (Some(s.ready.swap_remove(i)), generation),
None => {
// Ready buffers too small for it make room (a size that no longer occurs).
while s.total.saturating_add(need) > STAGING_BUDGET
&& let Some(b) = s.ready.pop()
{
s.total = s.total.saturating_sub(b.size());
}
if s.total.saturating_add(need) <= STAGING_BUDGET {
s.total += need;
drop(s);
let b = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("upload staging"),
size: need,
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let done = self.on_mapped(&b, generation);
b.map_async(wgpu::MapMode::Write, .., done);
}
(None, generation)
}
}
};
let Some(buffer) = buffer else { return false };
self.lock().copies += 1;
{
let Ok(mut view) = buffer.get_mapped_range_mut(..need) else {
// Not mapped after all: give it up (its bytes too).
let mut s = self.lock();
if s.generation == generation {
s.total = s.total.saturating_sub(buffer.size());
}
return false;
};
let src: &[u8] = bytemuck::cast_slice(&img.data);
let stride = stride as usize;
if stride == row {
view.slice(..src.len()).copy_from_slice(src);
} else {
for (y, line) in src.chunks_exact(row).enumerate() {
let at = y * stride;
view.slice(at..at + row).copy_from_slice(line);
}
}
}
buffer.unmap();
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("upload") });
enc.copy_buffer_to_texture(
wgpu::TexelCopyBufferInfo {
buffer: &buffer,
layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(stride32), rows_per_image: Some(h) },
},
wgpu::TexelCopyTextureInfo { texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },
wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
);
let done = self.on_mapped(&buffer, generation);
enc.map_buffer_on_submit(&buffer, wgpu::MapMode::Write, .., done);
queue.submit([enc.finish()]);
true
}
}
#[cfg(test)]
mod tests {
use super::padded_row;
#[test]
fn staging_rows_are_padded_to_the_copy_alignment() {
assert_eq!(padded_row(1920), Some(30720));
assert_eq!(padded_row(97), Some(1792));
assert_eq!(padded_row(16), Some(256));
assert_eq!(padded_row(1), Some(256));
}
}
+69 -6
View File
@@ -241,6 +241,9 @@ fn draw_layer_on(e: &mut Enc, r: &Renderer, ctx: &EvalCtx, layer: &Layer, canvas
if let Some(done) = draw_resident_fx(e, r, ctx, layer, canvas, opacity) {
return done;
}
if let Some(done) = draw_picture(e, r, ctx, layer, canvas, opacity) {
return done;
}
let Some(st) = r.styled_layer(ctx, layer) else { return Some(()) };
if st.plain {
return composite_layer(e, r, ctx, layer, &st.body, canvas, opacity);
@@ -286,14 +289,28 @@ fn draw_resident_fx(e: &mut Enc, r: &Renderer, ctx: &EvalCtx, layer: &Layer, can
{
return None;
}
let input = r.layer_input(ctx, layer, 0)?;
if input.img.is_empty() {
return Some(Some(()));
}
let Some(src) = e.g.upload_buf(&input) else { return Some(None) };
// The decoded footage picture itself when it is the layer's input (uploaded once per frame
// and shared with every layer that shows it), else the cached input buffer.
let (src, offset, scale) = match picture(r, ctx, layer) {
Some(p) => {
if p.img.is_empty() {
return Some(Some(()));
}
let Some(src) = e.g.upload_picture(&p.img) else { return Some(None) };
(src, p.effect_offset, p.scale)
}
None => {
let input = r.layer_input(ctx, layer, 0)?;
if input.img.is_empty() {
return Some(Some(()));
}
let Some(src) = e.g.upload_buf(&input) else { return Some(None) };
(src, input.offset, input.scale)
}
};
let pipe = r.pipe();
// `layer_input` is in effect space: run the stack there, then back to layer space.
let mut fx = crate::effects::GpuFx::new(e, src, input.offset, input.scale, pipe.levels);
let mut fx = crate::effects::GpuFx::new(e, src, offset, scale, pipe.levels);
let origin = r.run_layer_effects_on(ctx, layer, &mut fx);
let Some((mut img, mut offset, scale)) = fx.finish() else { return Some(None) };
offset = [offset[0] - origin[0] * scale, offset[1] - origin[1] * scale];
@@ -308,6 +325,52 @@ fn draw_resident_fx(e: &mut Enc, r: &Renderer, ctx: &EvalCtx, layer: &Layer, can
Some(place_src(e, r, ctx, layer, &shape, img, canvas, layer.blend_mode, opacity, pipe.levels).map(|c| *canvas = c))
}
/// Shared footage pictures ([`picture`]); tests turn it off to compare.
pub(crate) static SHARED_PICTURES: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
/// The layer's source as the decoded footage picture (`Renderer::footage_picture`), unless
/// sharing is off or per-layer timings are being recorded (those time the layer-cache path).
fn picture(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> Option<effectcraft_render::FootagePicture> {
if !SHARED_PICTURES.load(std::sync::atomic::Ordering::Relaxed) || r.profile.is_some() {
return None;
}
r.footage_picture(ctx, layer)
}
/// A footage layer without effects or layer styles whose source is the decoded picture as it
/// is (`Renderer::footage_picture`) and needs no conversion to the blending space: placed
/// straight from the picture's texture, uploaded once per frame and shared by every layer that
/// shows the same frame, instead of from a copy per layer in the layer cache, uploaded per
/// layer. The pixels are the same: the layer-cache buffer is that picture, copied. `None`: not
/// this kind of layer; `Some(None)`: the GPU failed.
fn draw_picture(e: &mut Enc, r: &Renderer, ctx: &EvalCtx, layer: &Layer, canvas: &mut GpuImage, opacity: f32) -> Option<Option<()>> {
if (layer.switches.effects && layer.effects().is_some_and(|f| !f.children.is_empty())) || styles::active(ctx, layer) || r.pipe().to_blend().is_some() {
return None;
}
let p = picture(r, ctx, layer)?;
if p.img.is_empty() {
return Some(Some(()));
}
let Some(src) = e.g.upload_picture(&p.img) else { return Some(None) };
// The placement only reads the buffer's size, offset and scale.
let shape = Buf { img: Image { width: p.img.width, height: p.img.height, data: Vec::new() }, offset: [0.0; 2], scale: p.scale };
Some(composite_src(e, r, ctx, layer, &shape, src, canvas, opacity))
}
/// [`composite_layer`] of a GPU image `src` shaped like `shape`.
#[allow(clippy::too_many_arguments)]
fn composite_src(e: &mut Enc, r: &Renderer, ctx: &EvalCtx, layer: &Layer, shape: &Buf, src: GpuImage, canvas: &mut GpuImage, opacity: f32) -> Option<()> {
if opacity <= 0.0 {
return Some(());
}
if r.track_matte(ctx, layer).is_none() && !layer.preserve_transparency {
*canvas = place_src(e, r, ctx, layer, shape, src, canvas, layer.blend_mode, opacity, r.pipe().levels)?;
return Some(());
}
let iso = place_src(e, r, ctx, layer, shape, src, &e.zeros(canvas.width, canvas.height), BlendMode::Normal, 1.0, None)?;
composite_iso(e, r, ctx, layer, iso, canvas, opacity)
}
/// A 2D precomp layer, its nested comp rendered resident on the GPU (`walk::render` one level
/// down) instead of by the CPU compositor and uploaded every frame (`Renderer::layer_source`
/// renders nested comps on the CPU: depth > 0). Then the layer's effects (resident), its
+1 -1
View File
@@ -147,7 +147,7 @@ pub(crate) fn record(e: &mut Enc, yp: &YuvPipe, img: &GpuImage, bg: [f32; 3]) ->
pub(crate) fn convert(e: &mut Enc, yp: &YuvPipe, img: &GpuImage, bg: [f32; 3]) -> Option<Vec<u8>> {
let stg = record(e, yp, img, bg)?;
let n = planes_len(img.width, img.height) as usize;
e.g.read_buffer(&stg, n, n, 1).map_err(|err| log::error!(target: "effectcraft_gpu", "yuv420 readback: {err}")).ok()
e.g.read_buffer_after(&stg, e.last_submission(), n, n, 1).map_err(|err| log::error!(target: "effectcraft_gpu", "yuv420 readback: {err}")).ok()
}
#[cfg(test)]
+3
View File
@@ -62,6 +62,9 @@ impl Exporter for FileExporter {
label: job.label.clone(),
storage: job.storage,
overflow: job.project.render_prefs.overflow_folders.clone(),
caches: job.caches.map(|c| effectcraft_export::JobCaches { gpu: &c.gpu, cpu: &c.cpu }),
renderer: job.renderer,
gpu_frames: job.gpu_frames,
},
nested_switches: job.nested_switches,
};
+129 -2
View File
@@ -281,7 +281,7 @@ fn plugin_loading_reports_bad_modules_and_survives_failing_renders() {
let files = [
("a-good.wasm", wat_plugin("org.test.host.good", 1, "i32.const 0").into_bytes()),
("b-garbage.wasm", b"\0asm\x01\0\0\0garbage".to_vec()),
("c-v2.wasm", wat_plugin("org.test.host.v2", 2, "i32.const 0").into_bytes()),
("c-v3.wasm", wat_plugin("org.test.host.v3", 3, "i32.const 0").into_bytes()),
("d-empty.wasm", vec![]),
("e-trap.wasm", wat_plugin("org.test.host.trap", 1, "unreachable").into_bytes()),
("notes.txt", b"not a plug-in".to_vec()),
@@ -296,7 +296,7 @@ fn plugin_loading_reports_bad_modules_and_survives_failing_renders() {
let errs: Vec<String> = r["errors"].as_array().unwrap().iter().map(|e| e["path"].as_str().unwrap().to_string()).collect();
assert_eq!(errs.len(), 3, "{r}");
assert!(errs.iter().all(|p| !p.ends_with(".txt")));
assert!(r["errors"].to_string().contains("API 2"), "{r}");
assert!(r["errors"].to_string().contains("ABI 3"), "{r}");
// A single file: errors come back as command errors; loading twice is refused.
let good = dir.join("a-good.wasm").to_string_lossy().to_string();
assert!(s.execute("effect.plugins.load", json!({"path": good})).unwrap_err().to_string().contains("already registered"));
@@ -311,3 +311,130 @@ fn plugin_loading_reports_bad_modules_and_survives_failing_renders() {
assert_eq!(px(&s), before);
let _ = std::fs::remove_dir_all(&dir);
}
/// An ABI 2 plug-in (WebAssembly text) that shows the neighbouring frames it got: per pixel it
/// writes |next.red − prev.red| into red, `flags / 8` into green, `prev_time` into blue and 1
/// into alpha, so a render test can read the frames and their times back from the image.
fn wat_neighbour_plugin(id: &str) -> String {
let manifest = format!(r#"{{"api":2,"id":"{id}","name":"Host {id}","category":"Test Plug-ins","version":"1","params":[]}}"#);
let len = manifest.len();
let escaped = manifest.replace('"', "\\\"");
format!(
r#"(module
(memory (export "memory") 2)
(data (i32.const 16) "{escaped}")
(func (export "ec_api_version") (result i32) i32.const 2)
(func (export "ec_manifest_ptr") (result i32) i32.const 16)
(func (export "ec_manifest_len") (result i32) i32.const {len})
(func (export "ec_alloc") (param i32) (result i32) i32.const 1024)
(func (export "ec_render2")
(param $px i32) (param $w i32) (param $h i32) (param $params i32) (param $np i32)
(param $t f64) (param $scale f64)
(param $prev i32) (param $prev_t f64) (param $next i32) (param $next_t f64) (param $flags i32)
(result i32)
(local $i i32)
(local $n i32)
local.get $w
local.get $h
i32.mul
local.set $n
block $done
loop $per_pixel
local.get $i
local.get $n
i32.ge_u
br_if $done
;; red = |next.red - prev.red|
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $next
local.get $i
i32.const 16
i32.mul
i32.add
f32.load
local.get $prev
local.get $i
i32.const 16
i32.mul
i32.add
f32.load
f32.sub
f32.abs
f32.store
;; green = flags / 8
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $flags
f32.convert_i32_s
f32.const 8
f32.div
f32.store offset=4
;; blue = prev_time
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $prev_t
f32.demote_f64
f32.store offset=8
;; alpha = 1
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
f32.const 1
f32.store offset=12
local.get $i
i32.const 1
i32.add
local.set $i
br $per_pixel
end
end
i32.const 0))"#
)
}
/// The neighbouring frames are the layer's own pixels at other times: here a precomp whose
/// nested solid fades from 0 to 100 % over one second, at 4 fps. At 0.5 s the neighbours are
/// 0.25 s and 0.75 s, so red (the current frame) was 0.5 and |next − prev| is 0.5.
#[test]
fn abi_v2_plugins_render_with_real_neighbour_frames() {
let mut s = effectcraft_host::session();
let inner = s.execute("comp.new", json!({"name": "Inner", "width": 40, "height": 20, "frameRate": 4, "duration": 1})).unwrap()["comp"].as_u64().unwrap();
let solid = s.execute("layer.newSolid", json!({"color": [1.0, 0.0, 0.0], "width": 40, "height": 20})).unwrap()["layer"].as_u64().unwrap();
s.execute("prop.addKey", json!({"layer": solid, "path": "transform/opacity", "time": 0.0, "value": 0})).unwrap();
s.execute("prop.addKey", json!({"layer": solid, "path": "transform/opacity", "time": 1.0, "value": 100})).unwrap();
let outer = s.execute("comp.new", json!({"name": "Outer", "width": 40, "height": 20, "frameRate": 4, "duration": 1})).unwrap()["comp"].as_u64().unwrap();
let layer = s.execute("layer.addItem", json!({"comp": outer, "item": inner, "time": 0})).unwrap()["layer"].as_u64().unwrap();
let outer = effectcraft_engine::project::ItemId(outer);
let dir = tempfile::TempDir::new().unwrap();
let wasm = dir.path().join("neighbours.wasm");
std::fs::write(&wasm, wat_neighbour_plugin("org.test.host.neighbours")).unwrap();
let r = s.execute("effect.plugins.load", json!({"path": wasm.to_string_lossy()})).unwrap();
assert_eq!(r["loaded"][0]["id"], "org.test.host.neighbours", "{r}");
s.execute("effect.apply", json!({"layer": layer, "effect": "org.test.host.neighbours"})).unwrap();
let p = s.render(outer, Tick::from_seconds_f64(0.5), Default::default()).get(20, 10);
assert!((p[0] - 0.5).abs() < 0.02, "precomp neighbours: {p:?}");
// Both neighbours exist, the previous one is the 0.25 s frame, and the frame is opaque.
assert!((p[1] - 3.0 / 8.0).abs() < 0.01, "flags: {p:?}");
assert!((p[2] - 0.25).abs() < 0.01, "prev time: {p:?}");
assert_eq!(p[3], 1.0, "{p:?}");
// At the very start of the layer only the next frame exists (the previous one is zeroed);
// solids are filled with a quantised colour, hence the 8-bit step.
let p = s.render(outer, Tick::ZERO, Default::default()).get(20, 10);
assert!((p[0] - 0.25).abs() < 0.01, "the next frame's red, with no previous frame: {p:?}");
assert!((p[1] - 2.0 / 8.0).abs() < 0.01, "only the next frame is valid: {p:?}");
}
+1 -1
View File
@@ -144,7 +144,7 @@ ffmpeg elsewhere) to check the code tables against an independent implementation
## Shared parser setup and bounds
The workspace pins `craft-pdf` to `craft-libs` Git revision
`92d392cff132eaf53e13afff77f38b5c11b73aa0`, with `default-features = false` and only the explicit `read`
`e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545`, with `default-features = false` and only the explicit `read`
feature. This excludes the backend's bundled fonts and Adobe CMaps and preserves EffectCraft's
font and asset policies. The shared parser does not change the supported drawing operators,
image codecs, continuously rasterized SVG tree, editable shapes, or EPS interpreter.
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "effectcraft-plugin"
description = "EffectCraft effect plug-ins: loads sandboxed WebAssembly effect modules (plug-in API v1) into the effect registry"
description = "EffectCraft effect plug-ins: loads sandboxed WebAssembly effect modules (WebAssembly ABI v1 and v2) into the effect registry"
version.workspace = true
edition.workspace = true
license.workspace = true
+14 -8
View File
@@ -1,21 +1,25 @@
//! # effectcraft-plugin (L3)
//!
//! Loads **WebAssembly effect plug-ins** (plug-in ABI v1, see `docs/plugins.md`; the native
//! plug-in API 2 additions such as layer parameters and host services are not part of it) into the effect
//! registry ([`effectcraft_effects::plugin`]). A plug-in is a `.wasm` (or `.wat`) module with
//! no imports that exports:
//! Loads **WebAssembly effect plug-ins** (WebAssembly ABI v1 and v2, see `docs/plugins.md`; the
//! native plug-in API 2 additions such as layer parameters and host services are not part of it)
//! into the effect registry ([`effectcraft_effects::plugin`]). A plug-in is a `.wasm` (or
//! `.wat`) module with no imports that exports:
//!
//! | export | signature | |
//! |---|---|---|
//! | `memory` | memory | linear memory shared with the host |
//! | `ec_api_version` | `() -> i32` | must return 1 |
//! | `ec_api_version` | `() -> i32` | 1 for ABI 1, 2 for ABI 2 (neighbouring frames) |
//! | `ec_manifest_ptr`, `ec_manifest_len` | `() -> i32` | a UTF-8 JSON [`PluginManifest`] in memory |
//! | `ec_alloc` | `(bytes: i32) -> i32` | a buffer of at least `bytes` bytes, 8-byte aligned; it may reuse the previous one |
//! | `ec_render` | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; 0 = ok |
//! | `ec_render` (ABI 1) | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; 0 = ok |
//! | `ec_render2` (ABI 2) | `ec_render`'s arguments, then `prev: i32, prev_time: f64, next: i32, next_time: f64, flags: i32` | the same, with the neighbouring frames; 0 = ok |
//!
//! For each render the host asks `ec_alloc` for one buffer holding the parameters (`nparams`
//! little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s,
//! premultiplied RGBA, row-major), calls `ec_render`, and reads the pixels back.
//! premultiplied RGBA, row-major), calls the render export, and reads the pixels back. An ABI 2
//! module gets two more pixel buffers after the current frame: the layer's previous and next
//! frames, resampled onto the current grid, and their layer times. The buffers are always
//! allocated (zeroed when a frame does not exist); `flags` bit 0/1 says which are real.
//!
//! Modules run in the [wasmi](https://github.com/wasmi-labs/wasmi) interpreter (pure Rust,
//! MIT/Apache-2.0) with deterministic floats, no imports (no files, network or clock) and a
@@ -26,7 +30,9 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use effectcraft_effects::EffectSpec;
pub use effectcraft_effects::plugin::{EffectPlugin, PLUGIN_API_VERSION, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION, register_plugin};
pub use effectcraft_effects::plugin::{
EffectPlugin, MIN_WASM_API_VERSION, PLUGIN_API_VERSION, PluginFrame, PluginImage, PluginManifest, PluginParams, WASM_API_VERSION, register_plugin,
};
#[cfg(feature = "wasm")]
mod wasm;
+174 -5
View File
@@ -163,11 +163,15 @@ fn wasm_plugins_are_sandboxed() {
// No imports (files, clock, network…).
let imports = r#"(module (import "env" "now" (func $now (result f64))) (memory (export "memory") 1))"#;
assert!(load_err(load_wasm(imports.as_bytes(), "imports.wat")).contains("import"));
// Garbage, wrong API version, bad manifests.
// Garbage, an ABI this build doesn't implement, bad manifests. A module that claims ABI 2
// must export `ec_render2` (this one still has only `ec_render`).
assert!(load_wasm(b"\0asm nope", "bad.wasm").is_err());
let v2 = gain_module("org.test.v2", "")
.replace("(func (export \"ec_api_version\") (result i32) i32.const 1)", "(func (export \"ec_api_version\") (result i32) i32.const 2)");
assert!(load_err(load_wasm(v2.as_bytes(), "v2.wat")).contains("API 2"));
assert!(load_err(load_wasm(v2.as_bytes(), "v2.wat")).contains("ec_render2"));
let v3 = gain_module("org.test.v3", "")
.replace("(func (export \"ec_api_version\") (result i32) i32.const 1)", "(func (export \"ec_api_version\") (result i32) i32.const 3)");
assert!(load_err(load_wasm(v3.as_bytes(), "v3.wat")).contains("ABI 3"));
let reserved = gain_module("ec.blur.mine", "");
assert!(load_err(load_wasm(reserved.as_bytes(), "reserved.wat")).contains("reserved"));
}
@@ -194,14 +198,14 @@ fn abi_violations_fail_to_load_with_reasons() {
assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains("ec_api_version"));
let m = edit(&base, "(param $scale f64) (result i32)", "(param $scale f32) (result i32)");
assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains("ec_render"));
// API versions other than 1.
for v in ["0", "-1", "2147483647"] {
// WebAssembly ABIs other than 1 and 2.
for v in ["0", "-1", "3", "2147483647"] {
let m = edit(
&base,
"(func (export \"ec_api_version\") (result i32) i32.const 1)",
&format!("(func (export \"ec_api_version\") (result i32) i32.const {v})"),
);
assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains(&format!("API {v}")), "{v}");
assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains(&format!("ABI {v}")), "{v}");
}
// Manifest out of bounds, negative or absurd lengths, not JSON.
let ptr = "(func (export \"ec_manifest_ptr\") (result i32) i32.const 16)";
@@ -317,3 +321,168 @@ fn wasm_rendering_is_deterministic() {
let twin = load_wasm(gain_module("org.test.det2", body).as_bytes(), "det2.wat").unwrap();
assert_eq!(bits(&render(twin, 0.37, true)), first);
}
/// An ABI 2 module whose `ec_render2` writes what it got from the host into each pixel: red is
/// |next.red − prev.red|, green `flags / 8`, blue `prev_time`, alpha 1.
fn neighbour_module(id: &str) -> String {
let manifest = format!(r#"{{"api":2,"id":"{id}","name":"Neighbours {id}","category":"Test Plug-ins","version":"1","params":[]}}"#);
let len = manifest.len();
let escaped = manifest.replace('\\', "\\\\").replace('"', "\\\"");
format!(
r#"(module
(memory (export "memory") 2)
(data (i32.const 16) "{escaped}")
(func (export "ec_api_version") (result i32) i32.const 2)
(func (export "ec_manifest_ptr") (result i32) i32.const 16)
(func (export "ec_manifest_len") (result i32) i32.const {len})
(func (export "ec_alloc") (param i32) (result i32) i32.const 1024)
(func (export "ec_render2")
(param $px i32) (param $w i32) (param $h i32) (param $params i32) (param $np i32)
(param $t f64) (param $scale f64)
(param $prev i32) (param $prev_t f64) (param $next i32) (param $next_t f64) (param $flags i32)
(result i32)
(local $i i32) (local $n i32)
local.get $w
local.get $h
i32.mul
local.set $n
block $done
loop $per_pixel
local.get $i
local.get $n
i32.ge_u
br_if $done
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $next
local.get $i
i32.const 16
i32.mul
i32.add
f32.load
local.get $prev
local.get $i
i32.const 16
i32.mul
i32.add
f32.load
f32.sub
f32.abs
f32.store
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $flags
f32.convert_i32_s
f32.const 8
f32.div
f32.store offset=4
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
local.get $prev_t
f32.demote_f64
f32.store offset=8
local.get $px
local.get $i
i32.const 16
i32.mul
i32.add
f32.const 1
f32.store offset=12
local.get $i
i32.const 1
i32.add
local.set $i
br $per_pixel
end
end
i32.const 0))"#
)
}
fn neighbour_image(red: f32) -> effectcraft_effects::plugin::PluginImage {
effectcraft_effects::plugin::PluginImage { width: 2, height: 1, pixels: vec![[red, 0.0, 0.0, 1.0]; 2], origin: [0.0; 2], scale: 1.0 }
}
/// A host with frames at two layer times.
struct Host {
time: f64,
fps: f64,
prev: Option<(f64, effectcraft_effects::plugin::PluginImage)>,
next: Option<(f64, effectcraft_effects::plugin::PluginImage)>,
}
impl effectcraft_effects::plugin::PluginHost for Host {
fn input_at(&self, layer_time: f64) -> Option<effectcraft_effects::plugin::PluginImage> {
if let Some((t, img)) = &self.prev
&& (t - layer_time).abs() < 1e-9
{
return Some(img.clone());
}
if let Some((t, img)) = &self.next
&& (t - layer_time).abs() < 1e-9
{
return Some(img.clone());
}
None
}
fn layer_input(&self, _param_id: &str, _layer_time: f64) -> Option<effectcraft_effects::plugin::PluginImage> {
None
}
fn params_at(&self, _layer_time: f64) -> Option<(Vec<f64>, Vec<String>)> {
None
}
fn frame_rate(&self) -> f64 {
self.fps
}
fn time(&self) -> f64 {
self.time
}
}
#[test]
fn abi_v2_receives_neighbour_frames_and_times() {
load_wasm(neighbour_module("org.test.neighbours").as_bytes(), "neighbours.wat").unwrap();
let p = effectcraft_effects::plugin::plugin("org.test.neighbours").unwrap();
assert!(effectcraft_effects::plugin::reads_other_times("org.test.neighbours"));
assert!(effectcraft_effects::is_time_dependent("org.test.neighbours"));
// The module has no parameters, so the flattened vector is empty.
let params = effectcraft_effects::plugin::PluginParams { manifest: p.manifest(), flat: &[], texts: &[] };
let mut px = vec![[0.0f32; 4]; 2];
let host = Host { time: 0.5, fps: 4.0, prev: Some((0.25, neighbour_image(0.1))), next: Some((0.75, neighbour_image(0.6))) };
let mut frame = effectcraft_effects::plugin::PluginFrame { width: 2, height: 1, pixels: &mut px, scale: 1.0, origin: [0.0; 2], layer_size: [2.0, 1.0] };
p.render_v2(&mut frame, &params, 0.5, &host).unwrap();
assert!((px[0][0] - 0.5).abs() < 1e-6, "|next − prev|: {px:?}");
assert!((px[0][1] - 3.0 / 8.0).abs() < 1e-6, "both flags: {px:?}");
assert!((px[0][2] - 0.25).abs() < 1e-6, "previous frame's time: {px:?}");
assert_eq!(px[0][3], 1.0);
}
#[test]
fn abi_v2_without_a_host_gets_zeroed_neighbours() {
let spec = load_wasm(neighbour_module("org.test.neighbours.empty").as_bytes(), "empty.wat").unwrap();
let mut next_id = 1;
let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next_id), spec.name, [2.0, 1.0]);
let params = effectcraft_effects::flatten_params(&g, &mut |p| p.value.clone());
let ctx = EffectCtx { params: &params, time: 0.5, layer_size: [2.0, 1.0], seed: 1, adjustment: false, env: EffectEnv::default() };
let buf = Buf { img: Image::filled(2, 1, [0.5, 0.5, 0.5, 1.0]), offset: [0.0; 2], scale: 1.0 };
// No host: both neighbour buffers are zeroed and no flag is set.
assert_eq!((spec.render)(&ctx, buf).img.get(0, 0), [0.0, 0.0, 0.0, 1.0]);
// ABI 1 plug-ins keep the time-independent behaviour.
let gain = load_wasm(gain_module("org.test.neighbours.v1", "").as_bytes(), "v1.wat").unwrap();
assert!(!effectcraft_effects::plugin::reads_other_times("org.test.neighbours.v1"));
assert!(!effectcraft_effects::is_time_dependent("org.test.neighbours.v1"));
assert!(gain.id.starts_with("org.test."));
}
+244 -43
View File
@@ -1,10 +1,11 @@
//! The wasmi-backed [`EffectPlugin`] (plug-in API v1 ABI, see the crate docs).
//! The wasmi-backed [`EffectPlugin`] (WebAssembly ABI v1 and v2, see the crate docs).
use std::sync::Mutex;
use wasmi::{Config, Engine, Instance, Linker, Memory, Module, Store, StoreLimits, StoreLimitsBuilder, TypedFunc, WasmParams, WasmResults};
use crate::{EffectPlugin, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION};
use crate::{EffectPlugin, MIN_WASM_API_VERSION, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION};
use effectcraft_effects::plugin::{PluginHost, PluginImage};
/// Instruction budget per frame: a fixed allowance plus some per pixel. Generous for per-pixel
/// work, but a runaway loop stops instead of hanging the render.
@@ -15,32 +16,74 @@ const MAX_MEMORY: usize = 1 << 30;
/// The longest manifest read.
const MAX_MANIFEST: i32 = 1 << 20;
/// ABI 1: one buffer.
type RenderFn = TypedFunc<(i32, i32, i32, i32, i32, f64, f64), i32>;
/// ABI 2: the same call plus the previous and next frame buffers and their layer times.
type Render2Fn = TypedFunc<(i32, i32, i32, i32, i32, f64, f64, i32, f64, i32, f64, i32), i32>;
enum Renderer {
V1(RenderFn),
V2(Render2Fn),
}
/// One instantiated module (renders running on several threads each take their own).
struct Inst {
store: Store<StoreLimits>,
memory: Memory,
alloc: TypedFunc<i32, i32>,
render: Renderer,
}
/// An instance whose ABI version (and so its render export) is not selected yet.
struct Pending {
store: Store<StoreLimits>,
instance: Instance,
memory: Memory,
alloc: TypedFunc<i32, i32>,
render: RenderFn,
}
impl Inst {
impl Pending {
/// A typed export by name, reporting a missing or mistyped export like [`missing`].
fn func<P: WasmParams, R: WasmResults>(&self, name: &str) -> Result<TypedFunc<P, R>, String> {
self.instance.get_typed_func::<P, R>(&self.store, name).map_err(|_| missing(name))
}
/// Select the render export for `abi`.
fn finish(self, abi: u32) -> Result<Inst, String> {
let render = match abi {
1 => Renderer::V1(self.instance.get_typed_func::<_, i32>(&self.store, "ec_render").map_err(|_| missing("ec_render"))?),
2 => Renderer::V2(self.instance.get_typed_func::<_, i32>(&self.store, "ec_render2").map_err(|_| missing("ec_render2"))?),
v => return Err(unsupported(v as i32)),
};
Ok(Inst { store: self.store, memory: self.memory, alloc: self.alloc, render })
}
}
/// The error for a missing or mistyped export.
fn missing(name: &str) -> String {
format!("the module exports no `{name}` (or with the wrong signature)")
}
/// The error for an ABI this build does not implement.
fn unsupported(v: i32) -> String {
format!("the module implements WebAssembly ABI {v}, this build implements ABI {MIN_WASM_API_VERSION} to {WASM_API_VERSION}")
}
/// The module's `ec_api_version`, when this build implements it.
fn abi_of(v: i32) -> Result<u32, String> {
match u32::try_from(v) {
Ok(v) if (MIN_WASM_API_VERSION..=WASM_API_VERSION).contains(&v) => Ok(v),
_ => Err(unsupported(v)),
}
}
/// The error for a wasmi or trap failure.
fn err(e: impl std::fmt::Display) -> String {
e.to_string()
}
fn instantiate(engine: &Engine, module: &Module) -> Result<Inst, String> {
/// Instantiate `module` in a fresh, limited store and read its memory and allocator.
fn begin(engine: &Engine, module: &Module) -> Result<Pending, String> {
let mut store = Store::new(engine, StoreLimitsBuilder::new().memory_size(MAX_MEMORY).instances(1).build());
store.limiter(|l| l);
store.set_fuel(FUEL_BASE).map_err(err)?;
@@ -48,14 +91,20 @@ fn instantiate(engine: &Engine, module: &Module) -> Result<Inst, String> {
let instance = linker.instantiate_and_start(&mut store, module).map_err(|e| format!("instantiation failed: {e}"))?;
let memory = instance.get_memory(&store, "memory").ok_or("the module exports no `memory`")?;
let alloc = instance.get_typed_func::<i32, i32>(&store, "ec_alloc").map_err(|_| missing("ec_alloc"))?;
let render = instance.get_typed_func::<(i32, i32, i32, i32, i32, f64, f64), i32>(&store, "ec_render").map_err(|_| missing("ec_render"))?;
Ok(Inst { store, instance, memory, alloc, render })
Ok(Pending { store, instance, memory, alloc })
}
/// Instantiate `module` and select the render export for `abi`.
fn instantiate(engine: &Engine, module: &Module, abi: u32) -> Result<Inst, String> {
begin(engine, module)?.finish(abi)
}
/// A loaded WebAssembly effect plug-in.
pub struct WasmPlugin {
manifest: PluginManifest,
source: String,
/// The WebAssembly ABI the module implements (`ec_api_version`).
abi: u32,
engine: Engine,
module: Module,
/// Idle instances.
@@ -63,6 +112,7 @@ pub struct WasmPlugin {
}
impl WasmPlugin {
/// Load `bytes` and its manifest; errors on imports, a bad manifest or a missing export.
pub fn new(bytes: &[u8], source: &str) -> Result<WasmPlugin, String> {
let mut config = Config::default();
config.consume_fuel(true);
@@ -71,51 +121,42 @@ impl WasmPlugin {
if let Some(i) = module.imports().next() {
return Err(format!("plug-ins can't import anything (the module imports `{}.{}`)", i.module(), i.name()));
}
let mut inst = instantiate(&engine, &module)?;
let version = inst.func::<(), i32>("ec_api_version")?;
let v = version.call(&mut inst.store, ()).map_err(err)?;
if u32::try_from(v).ok() != Some(WASM_API_VERSION) {
return Err(format!("the module implements plug-in API {v}, this build implements API {WASM_API_VERSION} for WebAssembly"));
}
let ptr = inst.func::<(), i32>("ec_manifest_ptr")?.call(&mut inst.store, ()).map_err(err)?;
let len = inst.func::<(), i32>("ec_manifest_len")?.call(&mut inst.store, ()).map_err(err)?;
let mut pending = begin(&engine, &module)?;
let version = pending.func::<(), i32>("ec_api_version")?;
let abi = abi_of(version.call(&mut pending.store, ()).map_err(err)?)?;
let ptr = pending.func::<(), i32>("ec_manifest_ptr")?.call(&mut pending.store, ()).map_err(err)?;
let len = pending.func::<(), i32>("ec_manifest_len")?.call(&mut pending.store, ()).map_err(err)?;
if !(0..=MAX_MANIFEST).contains(&len) {
return Err(format!("bad manifest length {len}"));
}
let mut text = vec![0u8; len as usize];
inst.memory.read(&inst.store, ptr as u32 as usize, &mut text).map_err(|e| format!("manifest out of bounds: {e}"))?;
pending.memory.read(&pending.store, ptr as u32 as usize, &mut text).map_err(|e| format!("manifest out of bounds: {e}"))?;
let manifest: PluginManifest = serde_json::from_slice(&text).map_err(|e| format!("bad manifest JSON: {e}"))?;
manifest.validate()?;
if let Some(p) = manifest.params.iter().find(|p| p.kind.needs_v2()) {
return Err(format!("parameter `{}`: WebAssembly plug-ins use plug-in API 1 parameter types (no point3, layer, text or hidden)", p.id));
}
Ok(WasmPlugin { manifest, source: source.to_string(), engine, module, pool: Mutex::new(vec![inst]) })
let inst = pending.finish(abi)?;
Ok(WasmPlugin { manifest, source: source.to_string(), abi, engine, module, pool: Mutex::new(vec![inst]) })
}
/// An idle instance, or a fresh one when the pool is empty.
fn take(&self) -> Result<Inst, String> {
if let Some(i) = self.pool.lock().ok().and_then(|mut p| p.pop()) {
return Ok(i);
}
instantiate(&self.engine, &self.module)
instantiate(&self.engine, &self.module, self.abi)
}
/// Return a working instance to the pool.
fn give_back(&self, inst: Inst) {
if let Ok(mut p) = self.pool.lock() {
p.push(inst);
}
}
}
impl EffectPlugin for WasmPlugin {
fn manifest(&self) -> &PluginManifest {
&self.manifest
}
fn source(&self) -> String {
self.source.clone()
}
fn render(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> {
/// ABI 1: parameters, then the pixels; `ec_render`.
fn render_v1(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> {
let n = frame.pixels.len();
let params_bytes = params.flat.len() * 8;
let total = params_bytes + n * 16;
@@ -130,25 +171,19 @@ impl EffectPlugin for WasmPlugin {
for v in params.flat {
buf.extend_from_slice(&v.to_le_bytes());
}
for px in frame.pixels.iter() {
for c in px {
buf.extend_from_slice(&c.to_le_bytes());
}
}
push_pixels(&mut buf, frame.pixels);
inst.memory.write(&mut inst.store, base, &buf).map_err(|e| format!("ec_alloc returned a buffer out of bounds: {e}"))?;
let px_ptr = (base + params_bytes) as i32;
let r = inst
.render
.call(&mut inst.store, (px_ptr, frame.width as i32, frame.height as i32, base as i32, params.flat.len() as i32, time, frame.scale))
.map_err(|e| format!("ec_render trapped: {e}"));
let r = match &inst.render {
Renderer::V1(f) => f
.call(&mut inst.store, (px_ptr, frame.width as i32, frame.height as i32, base as i32, params.flat.len() as i32, time, frame.scale))
.map_err(|e| format!("ec_render trapped: {e}")),
Renderer::V2(_) => return Err("the module implements WebAssembly ABI 1 but exports ec_render2".into()),
};
let r = match r {
Ok(0) => {
inst.memory.read(&inst.store, base + params_bytes, &mut buf[params_bytes..]).map_err(err)?;
for (px, b) in frame.pixels.iter_mut().zip(buf[params_bytes..].as_chunks::<16>().0.iter()) {
for (c, v) in px.iter_mut().zip(b.as_chunks::<4>().0.iter()) {
*c = f32::from_le_bytes([v[0], v[1], v[2], v[3]]);
}
}
read_pixels(&buf[params_bytes..], frame.pixels);
Ok(())
}
Ok(code) => Err(format!("ec_render returned error {code}")),
@@ -159,4 +194,170 @@ impl EffectPlugin for WasmPlugin {
}
r
}
/// ABI 2: parameters, the current frame, then the previous and next frames; `ec_render2`.
/// The neighbour buffers are always allocated (zeroed when absent), so a module can read
/// both without a null check; `flags` says which hold real frames.
fn render_v2(
&self,
frame: &mut PluginFrame,
params: &PluginParams,
time: f64,
prev: Option<(f64, &PluginImage)>,
next: Option<(f64, &PluginImage)>,
) -> Result<(), String> {
let n = frame.pixels.len();
let params_bytes = params.flat.len() * 8;
let frame_bytes = n * 16;
let total = params_bytes + frame_bytes * 3;
if total > i32::MAX as usize {
return Err("frame too large for a 32-bit plug-in".into());
}
let prev_px = prev.map(|(t, img)| (t, img.on_grid(frame.width, frame.height, frame.origin, frame.scale)));
let next_px = next.map(|(t, img)| (t, img.on_grid(frame.width, frame.height, frame.origin, frame.scale)));
let prev = prev_px.as_ref().filter(|(_, px)| px.len() == n);
let next = next_px.as_ref().filter(|(_, px)| px.len() == n);
let mut inst = self.take()?;
inst.store.set_fuel(FUEL_BASE + FUEL_PER_PIXEL * (n as u64 * 3)).map_err(err)?;
let base = inst.alloc.call(&mut inst.store, total as i32).map_err(err)? as u32 as usize;
let mut buf: Vec<u8> = Vec::with_capacity(total);
for v in params.flat {
buf.extend_from_slice(&v.to_le_bytes());
}
push_pixels(&mut buf, frame.pixels);
push_neighbour(&mut buf, prev, frame_bytes);
push_neighbour(&mut buf, next, frame_bytes);
inst.memory.write(&mut inst.store, base, &buf).map_err(|e| format!("ec_alloc returned a buffer out of bounds: {e}"))?;
let px_ptr = (base + params_bytes) as i32;
let prev_ptr = (base + params_bytes + frame_bytes) as i32;
let next_ptr = (base + params_bytes + frame_bytes * 2) as i32;
let flags = i32::from(prev.is_some()) | (i32::from(next.is_some()) << 1);
let prev_time = prev.map_or(0.0, |(t, _)| *t);
let next_time = next.map_or(0.0, |(t, _)| *t);
let r = match &inst.render {
Renderer::V2(f) => f
.call(
&mut inst.store,
(
px_ptr,
frame.width as i32,
frame.height as i32,
base as i32,
params.flat.len() as i32,
time,
frame.scale,
prev_ptr,
prev_time,
next_ptr,
next_time,
flags,
),
)
.map_err(|e| format!("ec_render2 trapped: {e}")),
Renderer::V1(_) => return Err("the module implements WebAssembly ABI 2 but exports ec_render".into()),
};
let r = match r {
Ok(0) => {
let mut out = vec![0u8; frame_bytes];
inst.memory.read(&inst.store, base + params_bytes, &mut out).map_err(err)?;
read_pixels(&out, frame.pixels);
Ok(())
}
Ok(code) => Err(format!("ec_render2 returned error {code}")),
Err(e) => Err(e),
};
if r.is_ok() {
self.give_back(inst);
}
r
}
}
/// Append pixels to the little-endian buffer the module reads.
fn push_pixels(buf: &mut Vec<u8>, px: &[[f32; 4]]) {
for p in px {
for c in p {
buf.extend_from_slice(&c.to_le_bytes());
}
}
}
/// The neighbour's pixels, or `frame_bytes` zeroed bytes when there is no frame.
fn push_neighbour(buf: &mut Vec<u8>, neighbour: Option<&(f64, Vec<[f32; 4]>)>, frame_bytes: usize) {
match neighbour {
Some((_, px)) => push_pixels(buf, px),
None => buf.resize(buf.len() + frame_bytes, 0),
}
}
/// Read the little-endian pixels the module wrote back.
fn read_pixels(bytes: &[u8], pixels: &mut [[f32; 4]]) {
for (px, b) in pixels.iter_mut().zip(bytes.as_chunks::<16>().0.iter()) {
for (c, v) in px.iter_mut().zip(b.as_chunks::<4>().0.iter()) {
*c = f32::from_le_bytes([v[0], v[1], v[2], v[3]]);
}
}
}
/// The layer times of the frames around `time` on a `fps` grid: the frame before and after it
/// at a frame boundary, the two frames that bracket it between frames.
fn neighbour_times(time: f64, fps: f64) -> (f64, f64) {
if !fps.is_finite() || fps <= 0.0 {
return (time - 1.0 / 30.0, time + 1.0 / 30.0);
}
let step = 1.0 / fps;
let at = time * fps;
// Frame times are not exact in floating point: at NTSC rates `time * fps` often lands a few
// ulps above or below an integer. Treat a near-integer as a frame boundary, so the current
// frame is never handed back as its own neighbour.
if (at - at.round()).abs() < 1e-6 { (time - step, time + step) } else { (at.floor() / fps, at.ceil() / fps) }
}
impl EffectPlugin for WasmPlugin {
fn manifest(&self) -> &PluginManifest {
&self.manifest
}
fn source(&self) -> String {
self.source.clone()
}
fn reads_other_times(&self) -> bool {
self.abi >= 2
}
fn render(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> {
if self.abi == 1 { self.render_v1(frame, params, time) } else { self.render_v2(frame, params, time, None, None) }
}
fn render_v2(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64, host: &dyn PluginHost) -> Result<(), String> {
if self.abi == 1 {
return self.render_v1(frame, params, time);
}
let (prev_time, next_time) = neighbour_times(time, host.frame_rate());
let prev = host.input_at(prev_time);
let next = host.input_at(next_time);
let prev = prev.as_ref().map(|img| (prev_time, img));
let next = next.as_ref().map(|img| (next_time, img));
self.render_v2(frame, params, time, prev, next)
}
}
#[cfg(test)]
mod tests {
use super::neighbour_times;
/// NTSC frame times do not multiply back exactly; every frame boundary must still get the
/// frames either side of it, never the frame itself.
#[test]
fn frame_boundaries_get_their_neighbours_at_ntsc_rates() {
let fps = 24000.0 / 1001.0;
for n in 0..2400 {
// 24000/1001 fps at 254 016 000 000 ticks/s, as `Tick` stores it.
let time = (n * 10_594_584_000_i64) as f64 / 254_016_000_000.0;
let (prev, next) = neighbour_times(time, fps);
assert!((prev - (time - 1.0 / fps)).abs() < 1e-9, "frame {n}: prev {prev}, current {time}");
assert!((next - (time + 1.0 / fps)).abs() < 1e-9, "frame {n}: next {next}, current {time}");
}
}
}
+23 -1
View File
@@ -34,6 +34,22 @@ impl Backend {
}
}
/// An accelerator's device memory, to size how many frames an export renders on it at once
/// (see [`Accelerator::frame_memory`]).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FrameMemory {
/// Bytes the accelerator may use (a conservative share of the device, not its free memory).
pub budget: u64,
/// The most its allocator has held while frames rendered (one frame's working memory
/// included).
pub peak: u64,
/// Bytes of that peak one frame needed (what it released again), or the whole budget once
/// frames have had to wait for memory.
pub frame: u64,
/// Bytes long-lived caches (uploaded layers) may still grow by.
pub reserve: u64,
}
/// One effect of a GPU effect chain (see [`Accelerator::effects`]).
pub struct FxStep<'x> {
pub spec: &'static effectcraft_effects::EffectSpec,
@@ -94,6 +110,12 @@ pub trait Accelerator: Send + Sync {
fn auto_pick(&self) -> Option<&AutoPick> {
None
}
/// The device memory budget and use, after at least one frame rendered: what an export
/// sizes its frames in flight by. `None` where it isn't known (no allocator report, no
/// budget): the export then renders one frame at a time.
fn frame_memory(&self) -> Option<FrameMemory> {
None
}
/// Whether the accelerator can still render frames. `false` once it has stopped for good
/// (its device was lost or retired): every frame it is asked for falls back to the CPU, so
/// callers that limit frames in flight for it can stop doing so.
@@ -352,7 +374,7 @@ impl<'a> Renderer<'a> {
pub fn adjustment_footprint(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
// Cached, so an accelerator sees the same buffer (and uploads it once) every frame.
let key = self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)));
let key = self.mask_gate_key(ctx, layer, key);
let key = self.mask_gate_key(ctx, layer, self.clipped_key(ctx, layer, key));
if let Some((b, _)) = self.cached(key) {
return Some(b);
}
+8 -1
View File
@@ -230,6 +230,11 @@ impl LayerCache {
/// used. (A render that started before a drop may publish its buffer after the drop's
/// purge, Reload Footage clearing the cache while the buffer was being written: the check
/// here, not the order of the two, keeps it from being served.)
/// Whether a buffer is kept in memory under `key` (a peek: no hit, no miss, no disk read).
pub(crate) fn holds(&self, key: u64) -> bool {
self.kept(key, false).is_some()
}
fn kept(&self, key: u64, hit: bool) -> Option<(Arc<Buf>, Arc<ReadFiles>)> {
let found = {
let mut g = self.inner.lock().ok()?;
@@ -689,7 +694,9 @@ fn reads_other_times(layer: &Layer, effects: Option<usize>) -> bool {
fx.groups().take(effects.unwrap_or(usize::MAX)).any(|g| {
g.enabled
&& matches!(&g.kind, effectcraft_project::GroupKind::Effect { effect }
if effect.starts_with("ec.time.") || effectcraft_effects::reads_param_history(effect))
if effect.starts_with("ec.time.")
|| effectcraft_effects::reads_param_history(effect)
|| (!effect.starts_with("ec.") && effectcraft_effects::plugin::reads_other_times(effect)))
})
})
}
+8
View File
@@ -295,7 +295,15 @@ impl<'a> Renderer<'a> {
/// in this render ([`Self::mask_blur`]'s gate: the render options, the precomp layers above).
/// The key has the layer's own motion blur state, but a mask set to On blurs without it.
pub(crate) fn mask_gate_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option<u64>) -> Option<u64> {
// Queue jobs share this cache but may override Proxy Use differently. The item's
// proxy switch alone does not describe the source a renderer actually reads.
let k = key?;
let k = match self.opts.proxy {
effectcraft_project::render_queue::ProxyUse::CurrentSettings => k,
effectcraft_project::render_queue::ProxyUse::UseAll => cache::derive(k, 0x7072_6f78_7901),
effectcraft_project::render_queue::ProxyUse::UseCompOnly => cache::derive(k, 0x7072_6f78_7902),
effectcraft_project::render_queue::ProxyUse::UseNone => cache::derive(k, 0x7072_6f78_7903),
};
let masked = layer.masks().is_some_and(|m| !m.children.is_empty());
Some(if masked && !self.mask_blur(ctx, layer).allowed { cache::derive(k, 0x6d62_6f66) } else { k })
}
+4 -2
View File
@@ -46,7 +46,7 @@ mod layers;
mod opts;
mod pipeline;
pub use accel::{Accelerator, Backend, FxStep, FxTarget};
pub use accel::{Accelerator, Backend, FrameMemory, FxStep, FxTarget};
pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage, missing_item_problem, vector_fit};
pub use frames::{
adaptive_samples, blend_frames, cineon_frame, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path,
@@ -61,7 +61,7 @@ pub use layers::{Placement, StyledLayer};
pub(crate) use opts::Collapse3d;
pub use opts::{LayerTiming, RenderOpts};
use opts::{MAX_THROUGH, Through};
pub use pipeline::{Collapsed, MediaOverride, PropertyOverride};
pub use pipeline::{Collapsed, FootagePicture, MediaOverride, PropertyOverride};
pub struct Renderer<'a> {
pub project: &'a Project,
@@ -102,6 +102,8 @@ pub struct Renderer<'a> {
through: [Option<Through<'a>>; MAX_THROUGH],
/// More collapsed levels than [`MAX_THROUGH`]: their motion is not blurred.
through_lost: bool,
/// Layer-space consumers must not use the viewport raster optimization.
pub(crate) whole_raster: bool,
}
#[cfg(test)]
+207 -27
View File
@@ -19,6 +19,24 @@ pub(crate) struct EffectStack {
pub origin: [f64; 2],
}
/// A footage layer's source as the decoded picture itself (see [`Renderer::footage_picture`]):
/// its buffer in layer space is `img` at offset `[0, 0]` and `scale`; in effect space (what
/// [`Renderer::layer_input`] gives) at `effect_offset`.
#[derive(Clone, Debug)]
pub struct FootagePicture {
pub img: Arc<Image>,
pub scale: f64,
pub effect_offset: [f64; 2],
}
/// How a decoded footage frame becomes a layer buffer ([`Renderer::footage_buf`]).
enum FootageLayout {
/// Its own pixels, at this buffer scale.
Native { scale: f64 },
/// Resampled to `width`×`height` at this buffer scale.
Resample { width: u32, height: u32, scale: f64 },
}
/// A property value an Essential Properties override gives a layer of a collapsed precomp.
#[derive(Clone, Debug)]
pub struct PropertyOverride {
@@ -123,6 +141,7 @@ impl<'a> Renderer<'a> {
top: true,
through: [None; MAX_THROUGH],
through_lost: false,
whole_raster: false,
}
}
@@ -258,6 +277,8 @@ impl<'a> Renderer<'a> {
top: false,
through: [None; MAX_THROUGH],
through_lost: false,
// Effects and nested renders need whole layer-space pixels.
whole_raster: true,
..*self
}
}
@@ -398,8 +419,9 @@ impl<'a> Renderer<'a> {
}
let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer));
let prev = ctx.time - ctx.comp.frame_rate.frame_duration();
let now = cache::layer_key(ctx, layer, scale, draft, blur);
now.is_some() && now == cache::layer_key(&ctx.at(prev), layer, scale, draft, blur)
let before = ctx.at(prev);
let now = self.clipped_key(ctx, layer, cache::layer_key(ctx, layer, scale, draft, blur));
now.is_some() && now == self.clipped_key(&before, layer, cache::layer_key(&before, layer, scale, draft, blur))
}
/// Rasterisation scale of a layer's source: the output scale, or — for text and shape layers,
@@ -433,14 +455,73 @@ impl<'a> Renderer<'a> {
// a whole number maps the unit vectors to lengths a hair over 1, which must stay 1 (#390:
// rounded up to 2^¼, unscaled text was drawn larger and resampled, softer).
let mut k = 2f64.powf((k.log2() * 4.0 - 1e-6).ceil() / 4.0).clamp(1.0 / 16.0, 64.0);
// Keep the buffer within about 16 million pixels.
// Keep the buffer within about 16 million pixels (of the part the output shows, when
// only that is rasterised: see `raster_clip`).
if let Some(b) = content_bounds(ctx, layer) {
let b = match self.raster_clip(ctx, layer) {
Some(c) => {
let r = kurbo::Rect::new(b[0], b[1], b[2], b[3]).intersect(c);
[r.x0, r.y0, r.x1, r.y1]
}
None => b,
};
let area = ((b[2] - b[0]) * (b[3] - b[1])).max(1.0) * s * s;
k = k.min((16.0e6 / area).sqrt().max(1.0));
}
s * k
}
/// The layer-space part of a 3D text or shape layer the output shows, when only that part
/// needs rasterising (#1388: text close to the camera was rasterised whole at up to 16
/// million pixels, a quarter gigabyte per frame worker, though the frame showed a fraction
/// of it; behind the camera it was rasterised and not drawn). `None` rasterises it whole:
/// whenever pixels outside the frame can matter — effects, layer styles, masks, motion blur,
/// depth of field, lights (shadows and transmission), Advanced 3D, per-character 3D, a
/// collapsed 2D transform — or the visible part can't be bounded (see
/// [`three_d::compose::visible_clip`]).
pub(crate) fn raster_clip(&self, ctx: &EvalCtx, layer: &Layer) -> Option<kurbo::Rect> {
if self.whole_raster || !layer.is_3d() || !matches!(layer.source, LayerSource::Text | LayerSource::Shape) || self.outer.is_some() {
return None;
}
if (layer.switches.effects && layer.effects().is_some_and(|fx| !fx.children.is_empty()))
|| layer.masks().is_some_and(|m| !m.children.is_empty())
|| styles::active(ctx, layer)
|| ctx.comp.layers.iter().any(|l| matches!(l.source, LayerSource::Light { .. }))
|| self.is_adv3d(ctx)
|| (matches!(layer.source, LayerSource::Text) && (text::per_char_3d(ctx, layer) || text::animator_blur(ctx, layer)))
{
return None;
}
let v = three_d::compose::visible_clip(self, ctx, layer)?;
if v.area() <= 0.0 {
// Behind the camera.
return Some(v);
}
let b = content_bounds(ctx, layer).map(|b| kurbo::Rect::new(b[0], b[1], b[2], b[3]))?;
if v.x0 <= b.x0 && v.y0 <= b.y0 && v.x1 >= b.x1 && v.y1 >= b.y1 {
return None;
}
// Whole cells of a layer-space grid whose cell is about a quarter of the part that shows,
// rounded down to a power of two: a moving camera keeps one raster (and its cache entry)
// while that part stays within the same cells, and a dolly changes the grid only at each
// doubling. The cells cover at most about 2.25 times the part that shows. Worth it only
// when it leaves out at least half of the content.
let c = 2f64.powf(((v.width().max(v.height()) / 4.0).max(1e-6)).log2().floor());
let snap = |v: f64, up: bool| if up { (v / c).ceil() * c } else { (v / c).floor() * c };
// (Not cut to the content bounds: a stroke or a glyph's padding reaches past them.)
let cells = kurbo::Rect::new(snap(v.x0, false), snap(v.y0, false), snap(v.x1, true), snap(v.y1, true));
(cells.is_finite() && cells.intersect(b).area() <= b.area() * 0.5).then_some(cells)
}
/// A layer content cache key that also names the part rasterised ([`Self::raster_clip`]).
pub(crate) fn clipped_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option<u64>) -> Option<u64> {
let k = key?;
Some(match self.raster_clip(ctx, layer) {
Some(c) => raster_clip_key(k, c),
None => k,
})
}
/// Draw a Wireframe-quality layer: its bounds as a one-pixel outline.
pub(crate) fn draw_wireframe(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
for (x, y) in self.wireframe_pixels(ctx, layer, (canvas.width, canvas.height)) {
@@ -482,6 +563,22 @@ impl<'a> Renderer<'a> {
/// Render a composition at comp time `t` (transparent background, comp size × scale).
/// Top-level frames go to the GPU when [`Self::active_accel`] is set and handles them.
pub fn comp_frame(&self, comp_id: ItemId, t: Tick) -> Image {
self.comp_frame_with(comp_id, t, false, |a| Ok::<_, std::convert::Infallible>(a.comp_frame(self, comp_id, t))).unwrap_or_else(|never| match never {})
}
/// Render a frame, validating the accelerated attempt before starting any CPU fallback.
/// The caller's scope must run only the accelerator, not this whole method: a GPU fault
/// must release failed resources before a potentially large CPU render (#3357).
/// A scope error returns immediately; the caller can then retry with a fresh CPU renderer
/// without accelerator-produced cache entries. Healthy declines and Auto timing retain
/// the same behavior as [`Self::comp_frame`]. Its fallback disables acceleration for
/// effects too: GPU work must never escape the supplied error scope.
pub fn comp_frame_scoped<E>(&self, comp_id: ItemId, t: Tick, scope: impl FnOnce(&dyn Accelerator) -> Result<Option<Image>, E>) -> Result<Image, E> {
self.comp_frame_with(comp_id, t, true, scope)
}
fn comp_frame_with<E>(&self, comp_id: ItemId, t: Tick, scoped: bool, scope: impl FnOnce(&dyn Accelerator) -> Result<Option<Image>, E>) -> Result<Image, E> {
let cpu = if scoped { self.with_accel(None) } else { self.with_accel(self.accel) };
// Classic 3D runs composite on the GPU too (M12.7), so Auto sends 3D comps there as well;
// Auto renders each comp on whichever compositor measured faster ([`AutoPick`]).
if self.depth == 0
@@ -490,24 +587,24 @@ impl<'a> Renderer<'a> {
let auto = self.frame_auto(a, comp_id, false);
if auto.is_none_or(|(p, key)| p.choose(key)) {
let t0 = web_time::Instant::now();
if let Some(img) = a.comp_frame(self, comp_id, t) {
if let Some(img) = scope(a)? {
if let Some((p, key)) = auto {
p.record(key, true, t0.elapsed().as_secs_f64() * 1e3);
}
return img;
return Ok(img);
}
if let Some((p, key)) = auto {
p.declined(key);
}
}
let t0 = web_time::Instant::now();
let img = self.comp_frame_cpu(comp_id, t);
let img = cpu.comp_frame_cpu(comp_id, t);
if let Some((p, key)) = auto {
p.record(key, false, t0.elapsed().as_secs_f64() * 1e3);
}
return img;
return Ok(img);
}
self.comp_frame_cpu(comp_id, t)
Ok(cpu.comp_frame_cpu(comp_id, t))
}
/// The Auto timing history and key for a top-level frame of `comp` (`None` unless the
@@ -861,12 +958,7 @@ impl<'a> Renderer<'a> {
return Some(buf);
}
}
let lt = ctx.source_time(layer);
// The proxy is decoded instead, at its own size, and fills the footage's frame.
let proxy = self.proxy_for(item);
let pf = proxy.unwrap_or(f);
let size = self.reduced_size(f.width, f.height).filter(|_| proxy.is_none());
let img = self.footage_frame(ctx, layer, item, pf, lt, size)?;
let (img, pf) = self.footage_picture_of(ctx, layer, item, f)?;
Some(self.footage_buf(&img, pf, f.width, f.height))
}
// (the font files go to this render's log, which `ctx` may not carry: see
@@ -881,9 +973,12 @@ impl<'a> Renderer<'a> {
{
log.mark_unkeyed();
}
Some(self.authored(text::render(&self.with_reads(ctx), layer, self.raster_scale(ctx, layer))))
Some(self.authored(text::render_in(&self.with_reads(ctx), layer, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer))))
}
LayerSource::Shape => layer.props.sub("contents").map(|c| self.authored(shapes::render(ctx, layer, c, self.raster_scale(ctx, layer)))),
LayerSource::Shape => layer
.props
.sub("contents")
.map(|c| self.authored(shapes::render_in(ctx, layer, c, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer)))),
_ => None,
}
}
@@ -898,14 +993,9 @@ impl<'a> Renderer<'a> {
/// A decoded frame of `f` as a layer buffer covering `w`×`h` layer pixels (the footage's
/// nominal size; proxies are scaled up to it), converted from its colour profile.
fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32) -> Buf {
let s = self.opts.scale;
let k = if img.width > 0 { w.max(1) as f64 / img.width as f64 } else { 1.0 };
// Keep native pixels when downsampling is small; resample otherwise.
let mut buf = match self.reduced_size(w, h) {
// The source resampled it already (`FootageSource::frame_at_size`).
Some(size) if (img.width, img.height) == size => Buf { img: img.clone(), offset: [0.0; 2], scale: s },
Some((rw, rh)) if k <= 1.0 + 1e-9 => Buf { img: effectcraft_raster::resample(img, rw, rh), offset: [0.0; 2], scale: s },
_ => Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 / k },
let mut buf = match self.footage_layout(img, w, h) {
FootageLayout::Native { scale } => Buf { img: img.clone(), offset: [0.0; 2], scale },
FootageLayout::Resample { width, height, scale } => Buf { img: effectcraft_raster::resample(img, width, height), offset: [0.0; 2], scale },
};
if let Some(c) = self.pipe.media_in(f) {
color::convert(&mut buf.img, &c);
@@ -913,6 +1003,79 @@ impl<'a> Renderer<'a> {
buf
}
/// How [`Renderer::footage_buf`] lays out a decoded frame `img` of footage `w`×`h` layer
/// pixels: its own pixels, or resampled.
fn footage_layout(&self, img: &Image, w: u32, h: u32) -> FootageLayout {
let s = self.opts.scale;
let k = if img.width > 0 { w.max(1) as f64 / img.width as f64 } else { 1.0 };
// Keep native pixels when downsampling is small; resample otherwise.
match self.reduced_size(w, h) {
// The source resampled it already (`FootageSource::frame_at_size`).
Some(size) if (img.width, img.height) == size => FootageLayout::Native { scale: s },
Some((width, height)) if k <= 1.0 + 1e-9 => FootageLayout::Resample { width, height, scale: s },
_ => FootageLayout::Native { scale: 1.0 / k },
}
}
/// A footage layer's decoded frame at the context time (the proxy's when one is used) and
/// the footage it was decoded from: what [`Renderer::source`] makes the layer's buffer of.
fn footage_picture_of(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &'a Footage) -> Option<(Arc<Image>, &'a Footage)> {
let lt = ctx.source_time(layer);
// The proxy is decoded instead, at its own size, and fills the footage's frame.
let proxy = self.proxy_for(item);
let pf = proxy.unwrap_or(f);
let size = self.reduced_size(f.width, f.height).filter(|_| proxy.is_none());
let img = self.footage_frame(ctx, layer, item, pf, lt, size)?;
Some((img, pf))
}
/// The decoded footage picture a layer's masked source is, bit for bit, with no copy: a
/// footage layer (not continuously rasterised vector footage) without masks whose frame
/// needs no colour conversion, no resampling and no quantisation (32 bpc). The buffer
/// [`Renderer::layer_input`] gives for no effects is this picture at `effect_offset` and
/// `scale`.
/// Layers showing the same frame get the same `Arc` (the footage source's cached frame), so
/// an accelerator uploads it once and shares the texture. `None`: the layer's buffer is
/// made the usual way (any of the above applies, or the frame is missing), and also when the
/// frame is made per call rather than served by the footage source (frame blending,
/// separate fields, Invert Alpha, Interpret As Linear Light, Cineon conversion: a new
/// picture each time, nothing to share) or when the layer cache already holds the layer's
/// buffer (served from memory as before, without asking the footage source).
pub fn footage_picture(&self, ctx: &EvalCtx, layer: &Layer) -> Option<FootagePicture> {
let LayerSource::Footage { item } = &layer.source else { return None };
if self.pipe.levels.is_some() || layer.masks().is_some_and(|m| !m.children.is_empty()) || frame_blend_mode(ctx, layer) != FrameBlend::Off {
return None;
}
let item = ctx.media_item(layer, *item);
let ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None };
if !f.has_video || (layer.switches.collapse && is_vector_footage(f)) {
return None;
}
let pf = self.proxy_for(item).unwrap_or(f);
if self.pipe.media_in(pf).is_some()
|| pf.fields != effectcraft_project::FieldOrder::Off
|| pf.invert_alpha
|| pf.linear_light_applies()
|| pf.cineon_applies()
{
return None;
}
if let Some(cache) = self.cache {
let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer));
let content = self.content_key(ctx, layer, cache::layer_key(ctx, layer, scale, draft, blur));
let input = self.content_key(ctx, layer, cache::input_key(ctx, layer, scale, draft, blur, 0));
if [content, input].into_iter().flatten().any(|k| cache.holds(k)) {
return None;
}
}
let (img, _) = self.footage_picture_of(ctx, layer, item, f)?;
let FootageLayout::Native { scale } = self.footage_layout(&img, f.width, f.height) else { return None };
// `layer_input`'s rebase into effect space (`Buf::rebase` of a buffer at the origin).
let mut at = Buf { img: Image { width: 0, height: 0, data: Vec::new() }, offset: [0.0; 2], scale };
at.rebase(ctx.effect_bounds(layer).1);
Some(FootagePicture { img, scale, effect_offset: at.offset })
}
/// The size of `w` × `h` layer pixels in this render when it is reduced enough that footage
/// is resampled to it (below 3/4 scale).
fn reduced_size(&self, w: u32, h: u32) -> Option<(u32, u32)> {
@@ -1052,7 +1215,13 @@ impl<'a> Renderer<'a> {
/// (what the motion tracker analyses, like After Effects' Layer panel). `None` for layers
/// without pixels.
pub fn layer_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
self.source(ctx, layer)
Renderer { whole_raster: true, ..*self }.source(ctx, layer)
}
/// Layer-space pixels after masks and effects, including content outside the viewport.
/// Expression sampleImage reads this rather than the frame-oriented clipped buffer.
pub fn layer_content(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
Renderer { whole_raster: true, ..*self }.content_buf(ctx, layer)
}
/// A 3D model or primitive layer rendered on its own, through the comp's camera: what an
@@ -1092,7 +1261,7 @@ impl<'a> Renderer<'a> {
mut timing: Option<&mut LayerTiming>,
) -> Option<(Arc<Buf>, Option<u64>, Arc<ReadFiles>)> {
let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)));
let key = self.content_key(ctx, layer, key);
let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key));
if let Some((b, files)) = self.cached(key) {
if let Some(t) = timing.as_deref_mut() {
t.cached = true;
@@ -1114,8 +1283,13 @@ impl<'a> Renderer<'a> {
/// what effect `effects` sees. Served from / stored in the layer cache under its own key,
/// so scrubbing reuses frames rendered for earlier output frames.
pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option<Arc<Buf>> {
// The Layer panel, analysis tools and effect inputs read the whole layer, not the viewport.
Renderer { whole_raster: true, ..*self }.whole_layer_input(ctx, layer, effects)
}
fn whole_layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option<Arc<Buf>> {
let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects));
let key = self.content_key(ctx, layer, key);
let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key));
if let Some((b, _)) = self.cached(key) {
return Some(b);
}
@@ -1132,3 +1306,9 @@ impl<'a> Renderer<'a> {
Some(self.keep(key, Arc::new(buf?), &files).0)
}
}
/// Snap cells can be finer than 1/16 unit: preserve every coordinate bit so visible
/// clips cannot collide with another cell or the empty behind-camera raster.
pub(crate) fn raster_clip_key(key: u64, clip: kurbo::Rect) -> u64 {
[clip.x0, clip.y0, clip.x1, clip.y1].iter().fold(cache::derive(key, 0xc11b), |key, coordinate| cache::derive(key, coordinate.to_bits()))
}
+7 -3
View File
@@ -686,6 +686,12 @@ const BATCH_PIXELS: usize = 8 << 20;
/// Each draw rasterises and composites only its own pixel rectangle; coverage masks of
/// consecutive draws are rasterised in parallel, then composited in stacking order.
pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf {
render_in(ctx, layer, contents, s, None)
}
/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows,
/// see `Renderer::raster_clip`).
pub fn render_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64, clip: Option<kurbo::Rect>) -> Buf {
let mut arena = Vec::new();
let (draws, _) = collect(ctx, layer, contents, &mut arena);
// Bottom-most draw first.
@@ -702,9 +708,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf
let Some(b) = resolved.iter().map(|r| r.2).reduce(|a, b| a.union(b)) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } };
// Keep buffers sane for runaway geometry.
let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0));
let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384);
let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384);
let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0];
let Some((w, h, offset)) = crate::text::raster_window(b, s, clip) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } };
let m = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s));
let px_rect = |r: kurbo::Rect| -> Option<PxRect> {
let x0 = (r.x0 * s + offset[0]).floor().max(0.0);
+3
View File
@@ -11,6 +11,7 @@ mod auto;
mod aux_channels;
mod collapse;
mod color;
mod drop_shadow_cover;
mod footage_size;
mod frame_blend;
mod guides;
@@ -20,6 +21,8 @@ mod mask_feather;
mod motion_tile;
mod nested;
mod paint;
mod proxy_cache;
mod raster_clip;
mod remap;
mod roi;
mod stretch_limits;
+87 -1
View File
@@ -14,12 +14,13 @@ struct MockAccel {
handle: bool,
calls: AtomicUsize,
pick: AutoPick,
effect_calls: AtomicUsize,
}
impl MockAccel {
/// Construct a renderer stand-in without making assumptions about scheduling or speed.
fn new(handle: bool) -> MockAccel {
MockAccel { handle, calls: Default::default(), pick: Default::default() }
MockAccel { handle, calls: Default::default(), pick: Default::default(), effect_calls: Default::default() }
}
/// Number of full-frame requests dispatched to this accelerator.
fn calls(&self) -> usize {
@@ -42,6 +43,7 @@ impl Accelerator for MockAccel {
})
}
fn supports_effect(&self, _id: &str) -> bool {
self.effect_calls.fetch_add(1, Ordering::SeqCst);
false
}
fn effects(&self, _chain: &[crate::FxStep], _buf: &crate::Buf, _levels: Option<f32>) -> Option<crate::Buf> {
@@ -160,3 +162,87 @@ fn auto_pick_follows_changing_costs() {
assert!(pick.stats(AutoKey::new(ItemId(7), 1.0, true)).is_none());
assert!(pick.stats(AutoKey::new(ItemId(7), 0.5, false)).is_none());
}
/// Counts actual CPU source reads and rejects any CPU fallback inside a GPU error scope.
struct ScopedFootage {
in_scope: std::sync::atomic::AtomicBool,
reads: AtomicUsize,
}
impl crate::FootageSource for ScopedFootage {
fn frame(&self, _: ItemId, f: &effectcraft_project::Footage, _: Tick) -> Option<std::sync::Arc<crate::Image>> {
assert!(!self.in_scope.load(Ordering::SeqCst), "CPU fallback ran before GPU validation/cleanup");
self.reads.fetch_add(1, Ordering::SeqCst);
Some(std::sync::Arc::new(crate::Image::filled(f.width, f.height, [1.0, 0.0, 0.0, 1.0])))
}
}
fn scoped_project() -> (Project, ItemId) {
let (mut p, cid) = project();
let comp = p.comp(cid).unwrap().clone();
let footage = effectcraft_project::Footage {
path: "synthetic.png".into(),
kind: effectcraft_project::FootageKind::Still,
width: 50,
height: 50,
has_video: true,
duration: Tick::from_seconds_f64(2.0),
..Default::default()
};
let item = p.add_item("Source", Label::Red, None, ItemKind::Footage(footage));
let mut layer = build::layer(&mut p, &comp, "Source", LayerSource::Footage { item }, (50, 50), None);
super::add_effect_200(&mut p, &mut layer, "ec.blur.gaussian", &[]);
p.comp_mut(cid).unwrap().layers = vec![layer].into();
(p, cid)
}
/// #3357: a declined GPU frame must not allocate a CPU frame only to discard it when the
/// enclosing GPU scope reports a fault. The caller retries on a fresh CPU renderer once.
#[test]
fn failed_gpu_scope_returns_before_cpu_fallback() {
let (p, cid) = scoped_project();
for handled in [false, true] {
let a = MockAccel::new(handled);
let src = ScopedFootage { in_scope: false.into(), reads: 0.into() };
let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Gpu, ..Default::default() });
r.accel = Some(&a);
let result = r.comp_frame_scoped(cid, Tick::ZERO, |a| {
src.in_scope.store(true, Ordering::SeqCst);
let frame = a.comp_frame(&r, cid, Tick::ZERO);
drop(frame);
src.in_scope.store(false, Ordering::SeqCst);
Err::<Option<crate::Image>, _>("device retired")
});
assert_eq!(result.unwrap_err(), "device retired");
assert_eq!(src.reads.load(Ordering::SeqCst), 0);
let frame = r.with_accel(None).comp_frame(cid, Tick::ZERO);
assert!(frame.get(100, 50)[3] > 0.99);
assert_eq!(src.reads.load(Ordering::SeqCst), 1);
assert_eq!(a.calls(), 1);
}
}
/// A healthy decline closes its scope before CPU work, and still records Auto timings.
#[test]
fn healthy_gpu_decline_renders_cpu_once_outside_scope() {
let (p, cid) = scoped_project();
let a = MockAccel::new(false);
let src = ScopedFootage { in_scope: false.into(), reads: 0.into() };
let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Auto, ..Default::default() });
r.accel = Some(&a);
let frame = r
.comp_frame_scoped(cid, Tick::ZERO, |a| {
src.in_scope.store(true, Ordering::SeqCst);
let frame = a.comp_frame(&r, cid, Tick::ZERO);
src.in_scope.store(false, Ordering::SeqCst);
Ok::<_, ()>(frame)
})
.unwrap();
assert!(frame.get(100, 50)[3] > 0.99);
assert_eq!(src.reads.load(Ordering::SeqCst), 1);
assert_eq!(a.calls(), 1);
assert_eq!(a.pick.stats(AutoKey::new(cid, 1.0, false)).unwrap().cpu_frames, 1);
// Auto's next frame stays on CPU, without entering a GPU scope at all.
r.comp_frame_scoped(cid, Tick::ZERO, |_| -> Result<Option<crate::Image>, ()> { panic!("Auto CPU choice must not enter GPU error scopes") }).unwrap();
assert_eq!(src.reads.load(Ordering::SeqCst), 2);
assert_eq!(a.effect_calls.load(Ordering::SeqCst), 0, "CPU effects must not use the GPU outside its scope");
}
@@ -0,0 +1,142 @@
use super::*;
use crate::{EvalCtx, Image, NoFootage, RenderOpts, Renderer};
use effectcraft_effects::Buf;
// A synthetic painted source avoids installed-font dependencies and the multi-gigabyte
// allocation in #3357 while exercising the same text/shape effect-stack bounds.
fn source(scale: f64) -> Buf {
let mut img = Image::new(20, 12);
for y in 2..10 {
for x in 2..18 {
img.set(x, y, [0.8, 0.2, 0.1, 1.0]);
}
}
Buf { img, offset: [7.0, -3.0], scale }
}
#[test]
fn drop_shadow_does_not_cover_the_zoomed_layer_rectangle() {
let (mut p, cid, comp) = setup();
let mut layer = build::layer(&mut p, &comp, "Shape", LayerSource::Shape, (0, 0), None);
add_effect_200(&mut p, &mut layer, "ec.perspective.dropshadow", &[("distance", Value::Scalar(2.0)), ("softness", Value::Scalar(1.0))]);
let r = Renderer::new(&p, &NoFootage, RenderOpts::default());
let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO);
let out = r.apply_effects(&ctx, &layer, source(4.0), false);
// The old cover makes this at least 800 x 400. Alignment and the shadow's
// own padding need less than 128 pixels per side for this source.
assert!(out.img.width < 128 && out.img.height < 128, "{} x {}", out.img.width, out.img.height);
}
#[test]
fn drop_shadow_matches_the_covered_reference() {
for scale in [0.5, 1.0, 4.0] {
for only in [false, true] {
for direction in [0.0, 135.0, 270.0] {
let (mut p, cid, comp) = setup();
let mut layer = build::layer(&mut p, &comp, "Text", LayerSource::Text, (0, 0), None);
add_effect_200(
&mut p,
&mut layer,
"ec.perspective.dropshadow",
&[
("distance", Value::Scalar(3.5)),
("softness", Value::Scalar(2.0)),
("direction", Value::Scalar(direction)),
("shadowOnly", Value::Bool(only)),
],
);
let r = Renderer::new(&p, &NoFootage, RenderOpts::default());
let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO);
let input = source(scale);
let mut covered = input.clone();
let (size, origin) = ctx.effect_bounds(&layer);
covered.rebase(origin);
covered.cover(size);
covered.rebase([-origin[0], -origin[1]]);
let actual = r.apply_effects(&ctx, &layer, input, false);
let reference = r.apply_effects(&ctx, &layer, covered, false);
// Compare the union in layer-pixel coordinates, including the shadow outside
// the original content. Buffer origins differ by integral pixels.
let x0 = (-actual.offset[0]).min(-reference.offset[0]) as i32;
let y0 = (-actual.offset[1]).min(-reference.offset[1]) as i32;
let x1 = (f64::from(actual.img.width) - actual.offset[0]).max(f64::from(reference.img.width) - reference.offset[0]) as i32;
let y1 = (f64::from(actual.img.height) - actual.offset[1]).max(f64::from(reference.img.height) - reference.offset[1]) as i32;
for y in y0..y1 {
for x in x0..x1 {
let sample = |b: &Buf| b.img.sample_bilinear(f64::from(x) + b.offset[0] + 0.5, f64::from(y) + b.offset[1] + 0.5);
let (a, b) = (sample(&actual), sample(&reference));
for c in 0..4 {
assert!(
(a[c] - b[c]).abs() < 1e-5,
"scale {scale}, only {only}, direction {direction}, ({x}, {y}), channel {c}: {} != {}",
a[c],
b[c]
);
}
}
}
}
}
}
}
#[test]
fn drop_shadow_with_glow_stays_local_but_other_effects_keep_cover() {
for other in ["ec.stylize.glow", "ec.color.tint"] {
let (mut p, cid, comp) = setup();
let mut layer = build::layer(&mut p, &comp, "Shape", LayerSource::Shape, (0, 0), None);
add_effect_200(&mut p, &mut layer, "ec.perspective.dropshadow", &[("distance", Value::Scalar(2.0)), ("softness", Value::Scalar(1.0))]);
let values = if other == "ec.stylize.glow" { vec![("radius", Value::Scalar(1.0))] } else { vec![] };
add_effect_200(&mut p, &mut layer, other, &values);
let r = Renderer::new(&p, &NoFootage, RenderOpts::default());
let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO);
let out = r.apply_effects(&ctx, &layer, source(4.0), false);
if other == "ec.stylize.glow" {
assert!(out.img.width < 192 && out.img.height < 192, "{} x {}", out.img.width, out.img.height);
} else {
assert!(out.img.width >= 800 && out.img.height >= 400);
}
}
}
#[test]
fn parented_text_and_shape_keep_small_working_images_at_2200_percent() {
for text in [false, true] {
let mut p = Project::default();
p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32;
let comp = Comp::new(80, 40, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into()));
let mut parent = build::layer(&mut p, &comp, "Null", LayerSource::Null, (100, 100), None);
parent.props.prop_mut("transform/anchor").unwrap().value = Value::Vec3([0.0; 3]);
parent.props.prop_mut("transform/position").unwrap().value = Value::Vec3([40.0, 20.0, 0.0]);
parent.props.prop_mut("transform/scale").unwrap().value = Value::Vec3([2200.0, 2200.0, 100.0]);
let source = if text { LayerSource::Text } else { LayerSource::Shape };
let mut layer = build::layer(&mut p, &comp, "Painted", source, (80, 40), None);
layer.parent = Some(parent.id);
layer.props.prop_mut("transform/anchor").unwrap().value = Value::Vec3([0.0; 3]);
layer.props.prop_mut("transform/position").unwrap().value = Value::Vec3([0.0; 3]);
if text {
layer.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "I".into(), size: 8.0, ..Default::default() }));
} else {
let mut next = p.next_id;
let mut ids = Ids(&mut next);
let rect = build::shape_rect(&mut ids, [4.0, 4.0], [0.0, 0.0], 0.0);
let fill = build::shape_fill(&mut ids, [1.0; 4]);
let group = build::shape_group(&mut ids, "Rectangle", vec![rect, fill]);
p.next_id = next;
layer.props.sub_mut("contents").unwrap().children.push(group.into());
}
add_effect(&mut p, &mut layer, "ec.stylize.glow", [80.0, 40.0], &[("radius", Value::Scalar(0.1))]);
add_effect(&mut p, &mut layer, "ec.perspective.dropshadow", [80.0, 40.0], &[("distance", Value::Scalar(0.1)), ("softness", Value::Scalar(0.1))]);
p.comp_mut(cid).unwrap().layers = vec![layer, parent].into();
let comp = p.comp(cid).unwrap();
let layer = &comp.layers[0];
for scale in [1.0, 0.5] {
let r = Renderer::new(&p, &NoFootage, RenderOpts { scale, ..Default::default() });
let ctx = EvalCtx::new(&p, cid, comp, Tick::ZERO);
let input = r.layer_input(&ctx, layer, usize::MAX).unwrap();
assert!(input.img.width < 512 && input.img.height < 512, "text={text}, output scale={scale}: {} x {}", input.img.width, input.img.height);
assert!(input.img.data.iter().any(|pixel| pixel[3] > 0.5), "the painted source is not empty");
let frame = r.comp_frame(cid, Tick::ZERO);
assert!(frame.data.iter().any(|pixel| pixel[3] > 0.5), "the layer still draws in the composition");
}
}
}
+45
View File
@@ -0,0 +1,45 @@
use super::setup;
use crate::eval::EvalCtx;
use crate::{FootageSource, Image, LayerCache, RenderOpts, Renderer};
use effectcraft_color::Label;
use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind, LayerSource, Proxy, build, render_queue::ProxyUse};
use effectcraft_time::Tick;
use std::sync::Arc;
/// A source and its proxy deliberately have different pixels.
struct Pictures;
impl FootageSource for Pictures {
fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option<Arc<Image>> {
let color = if footage.path == "proxy" { [0.0, 1.0, 0.0, 1.0] } else { [1.0, 0.0, 0.0, 1.0] };
Some(Arc::new(Image::filled(16, 16, color)))
}
}
/// A queue run reuses its layer cache across jobs with different Proxy Use settings.
/// Time effects read layer_input, which must never serve another job's source pixels.
#[test]
fn shared_layer_cache_separates_proxy_use() {
let (mut p, cid, comp) = setup();
let footage = Footage { path: "original".into(), kind: FootageKind::Still, width: 16, height: 16, has_video: true, ..Default::default() };
let item = p.add_item("Footage", Label::Red, None, ItemKind::Footage(footage.clone()));
p.item_mut(item).unwrap().proxy = Some(Box::new(Proxy { footage: Footage { path: "proxy".into(), ..footage }, enabled: false }));
let layer = build::layer(&mut p, &comp, "Footage", LayerSource::Footage { item }, (16, 16), None);
p.comp_mut(cid).unwrap().layers.push(layer);
for enabled in [false, true] {
p.item_mut(item).unwrap().proxy.as_mut().unwrap().enabled = enabled;
let cache = LayerCache::default();
let ctx = EvalCtx::new(&p, cid, p.comp(cid).unwrap(), Tick::ZERO);
let layer = &ctx.comp.layers[0];
for proxy in [ProxyUse::UseAll, ProxyUse::UseNone, ProxyUse::CurrentSettings, ProxyUse::UseCompOnly, ProxyUse::UseAll] {
let mut r = Renderer::new(&p, &Pictures, RenderOpts { proxy, ..Default::default() });
let input = r.layer_input(&ctx, layer, 0).unwrap();
let content = r.content_buf(&ctx, layer).unwrap();
r.cache = Some(&cache);
assert_eq!(r.layer_input(&ctx, layer, 0).unwrap().img.data, input.img.data, "{proxy:?}, enabled={enabled}");
assert_eq!(r.content_buf(&ctx, layer).unwrap().img.data, content.img.data, "{proxy:?}, enabled={enabled}");
let hits = cache.stats().hits;
r.layer_input(&ctx, layer, 0).unwrap();
assert!(cache.stats().hits > hits, "unchanged proxy settings still reuse inputs");
}
}
}
+360
View File
@@ -0,0 +1,360 @@
//! #1388: 3D text and shape layers rasterise only the part of the layer the output shows. Where
//! the whole raster fits its pixel budget the frame is identical to rasterising the whole layer;
//! close to the camera the raster stays small instead of growing to ~16 million pixels.
use effectcraft_color::Label;
use effectcraft_keyframe::{TextDoc, Value};
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, Project};
use effectcraft_time::{FrameRate, Tick};
use crate::{NoFootage, RenderOpts, Renderer};
const W: u32 = 400;
const H: u32 = 300;
/// The default camera's distance to the z = 0 plane.
fn zoom() -> f64 {
W as f64 * 50.0 / 36.0
}
fn setup() -> (Project, ItemId, Comp) {
let mut p = Project::default();
p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32;
let comp = Comp::new(W, H, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into()));
(p, cid, comp)
}
fn set(l: &mut Layer, path: &str, v: Value) {
l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).value = v;
}
/// A 3D text layer at `pos`, turned `rot_y` degrees about Y.
fn text3(p: &mut Project, comp: &Comp, pos: [f64; 3], rot_y: f64) -> Layer {
let mut l = build::layer(p, comp, "Text", LayerSource::Text, (W, H), None);
l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA TEXT".into(), size: 60.0, ..Default::default() }));
l.switches.three_d = true;
set(&mut l, "transform/position", Value::Vec3(pos));
set(&mut l, "transform/rotationY", Value::Scalar(rot_y));
l
}
/// A 3D shape layer (a 300×120 rectangle with a stroke) at `pos`.
fn shape3(p: &mut Project, comp: &Comp, pos: [f64; 3]) -> Layer {
let mut l = build::layer(p, comp, "Shape", LayerSource::Shape, (W, H), None);
let mut next = p.next_id;
{
let mut ids = Ids(&mut next);
let r = build::shape_rect(&mut ids, [300.0, 120.0], [0.0, 0.0], 0.0);
let f = build::shape_fill(&mut ids, [0.9, 0.4, 0.1, 1.0]);
let s = build::shape_stroke(&mut ids, [1.0, 1.0, 1.0, 1.0], 6.0);
let g = build::shape_group(&mut ids, "Box", vec![r, f, s]);
l.props.sub_mut("contents").unwrap().children.push(g.into());
}
p.next_id = next;
l.switches.three_d = true;
set(&mut l, "transform/position", Value::Vec3(pos));
l
}
fn scene(l: impl FnOnce(&mut Project, &Comp) -> Layer) -> (Project, ItemId) {
let (mut p, cid, comp) = setup();
let layer = l(&mut p, &comp);
p.comp_mut(cid).unwrap().layers.push(layer);
(p, cid)
}
fn frame(p: &Project, cid: ItemId, whole: bool) -> effectcraft_raster::Image {
let mut r = Renderer::new(p, &NoFootage, RenderOpts::default());
r.whole_raster = whole;
r.comp_frame(cid, Tick::ZERO)
}
/// Pixels of the layer's processed buffer.
fn raster_px(p: &Project, cid: ItemId, whole: bool) -> u64 {
let mut r = Renderer::new(p, &NoFootage, RenderOpts::default());
r.whole_raster = whole;
let c = p.comp(cid).unwrap();
let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap();
let b = r.layer_buf(&ctx, &c.layers[0]).unwrap();
b.img.width as u64 * b.img.height as u64
}
#[test]
fn the_visible_part_renders_the_same_frame_as_the_whole_layer() {
let cx = W as f64 / 2.0;
let cy = H as f64 / 2.0;
let cases: Vec<(&str, (Project, ItemId))> = vec![
("far", scene(|p, c| text3(p, c, [cx, cy, 300.0], 0.0))),
("in the frame plane", scene(|p, c| text3(p, c, [cx, cy, 0.0], 0.0))),
("past the frame edge", scene(|p, c| text3(p, c, [60.0, cy, -200.0], 0.0))),
("turned", scene(|p, c| text3(p, c, [cx, cy, -150.0], 55.0))),
("close to the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 40.0], 0.0))),
("partly behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 30.0], 80.0))),
("behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() - 200.0], 0.0))),
("shape", scene(|p, c| shape3(p, c, [cx + 150.0, cy, -250.0]))),
];
for (label, (p, cid)) in &cases {
let clipped = frame(p, *cid, false);
let whole = frame(p, *cid, true);
assert_eq!(clipped.data, whole.data, "{label}");
}
}
/// Close to the camera only part of the text shows: its raster is a fraction of the whole one
/// (millions of pixels), and the frame still shows text.
#[test]
fn text_close_to_the_camera_rasterises_what_shows() {
let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0));
let clipped = raster_px(&p, cid, false);
let whole = raster_px(&p, cid, true);
assert!(whole > 5_000_000, "the whole raster is huge ({whole} px)");
eprintln!("near camera: {clipped} px rasterised, {whole} px whole");
assert!(clipped * 10 < whole, "{clipped} px of {whole}");
let img = frame(&p, cid, false);
let covered = img.data.iter().filter(|px| px[3] > 0.5).count();
assert!(covered > (W * H / 20) as usize, "text shows ({covered} px)");
// Behind the camera there is nothing to rasterise.
let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() - 200.0], 0.0));
assert!(raster_px(&p, cid, false) <= 1);
}
/// The #1388 case at full size: 200 pt text in a 1920 × 1080 comp, close to the camera. Rasterised
/// whole it fills the ~16 million pixel cap; clipped, it stays within a few frames' worth of pixels
/// (and the text still shows).
#[test]
fn hd_text_close_to_the_camera_stays_within_a_few_frames() {
let mut p = Project::default();
let comp = Comp::new(1920, 1080, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into()));
let mut l = build::layer(&mut p, &comp, "Text", LayerSource::Text, (1920, 1080), None);
l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA".into(), size: 200.0, ..Default::default() }));
l.switches.three_d = true;
set(&mut l, "transform/position", Value::Vec3([960.0, 540.0, -1920.0 * 50.0 / 36.0 + 60.0]));
p.comp_mut(cid).unwrap().layers.push(l);
let (clipped, whole) = (raster_px(&p, cid, false), raster_px(&p, cid, true));
eprintln!("HD near camera: {clipped} px rasterised, {whole} px whole");
assert!(whole > 12_000_000, "{whole}");
assert!(clipped <= 4 * 1920 * 1080, "{clipped} px");
assert!(frame(&p, cid, false).data.iter().any(|px| px[3] > 0.5), "the text shows");
}
/// A slow move of the layer against the camera (a camera move does the same) keeps the part
/// rasterised within the same grid cells for several frames: they reuse the cached raster instead
/// of drawing the text again each frame.
#[test]
fn a_slow_move_reuses_the_clipped_raster() {
let cache = crate::LayerCache::default();
let at = |x: f64| {
let (p, cid) = scene(|p, c| text3(p, c, [x, H as f64 / 2.0, -zoom() + 40.0], 0.0));
let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default());
r.cache = Some(&cache);
r.comp_frame(cid, Tick::ZERO);
};
at(W as f64 / 2.0);
let misses = cache.stats().misses;
// 16 frames, a quarter of a layer pixel apart (here about three output pixels).
for i in 1..=16 {
at(W as f64 / 2.0 + i as f64 * 0.25);
}
let rasters = cache.stats().misses - misses;
assert!(rasters <= 2, "{rasters} new rasters over 16 frames");
}
/// One keyframed motion: (seconds, value) pairs.
type Keys<'a> = &'a [(f64, [f64; 3])];
fn keyed(l: &mut Layer, path: &str, keys: Keys) {
l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).keys =
keys.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Vec3(*v))).collect();
}
/// A camera keyed through `pos` / `poi`, and the text keyed through `text_pos` (and turning about
/// Y through `text_rot`): every frame of a second, rendered with the clipped raster and a layer
/// cache shared across the frames, matches rasterising the whole layer without a cache (or differs
/// by an anti-aliasing sub-sample or two on a few edge pixels) — except where the whole raster is held at
/// its pixel cap (softer than the clipped one, which isn't): there the text shows and the clipped
/// raster stays small.
fn moving(label: &str, pos: Keys, poi: Keys, text_pos: Keys, text_rot: &[(f64, f64)]) {
let (mut p, cid, comp) = setup();
let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, 0.0], 0.0);
if !text_pos.is_empty() {
keyed(&mut text, "transform/position", text_pos);
}
if !text_rot.is_empty() {
text.props.prop_mut("transform/rotationY").unwrap().keys =
text_rot.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Scalar(*v))).collect();
}
let mut cam = build::layer(&mut p, &comp, "Camera", LayerSource::Camera, (W, H), None);
set(&mut cam, "cameraOptions/zoom", Value::Scalar(zoom()));
keyed(&mut cam, "transform/position", pos);
keyed(&mut cam, "transform/poi", poi);
p.comp_mut(cid).unwrap().layers = vec![cam, text].into();
let cache = crate::LayerCache::default();
let (mut exact, mut near, mut capped) = (0, 0, 0);
for f in 0..30 {
let t = Tick::from_seconds_f64(f as f64 / 30.0);
let mut clipped = Renderer::new(&p, &NoFootage, RenderOpts::default());
clipped.cache = Some(&cache);
let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default());
whole.whole_raster = true;
let ctx = whole.eval_ctx(cid, t).unwrap();
let layer = &p.comp(cid).unwrap().layers[1];
let a = clipped.comp_frame(cid, t);
if clipped.raster_scale(&ctx, layer) == whole.raster_scale(&ctx, layer) {
let b = whole.comp_frame(cid, t);
if a.data == b.data {
exact += 1;
} else {
// The clipped raster places the glyphs at other coordinates (smaller ones: the whole
// raster can be thousands of pixels wide), and tiny-skia transforms them in f32: its
// 4 × 4 anti-aliasing may land a sub-sample or two differently on a few edge pixels.
let (n, worst) = a.data.iter().zip(&b.data).fold((0usize, 0.0f32), |(n, w), (p, q)| {
let d = (0..4).map(|i| (p[i] - q[i]).abs()).fold(0.0f32, f32::max);
(n + usize::from(d > 0.0), w.max(d))
});
assert!(worst <= 2.0 / 16.0 + 1e-4 && n * 1000 <= (W * H) as usize, "{label}: frame {f}: {n} px differ, by up to {worst}");
near += 1;
}
} else {
// Held at the cap whole: the clipped raster is sharper, and small.
let buf = clipped.layer_buf(&ctx, layer).unwrap();
assert!(buf.img.width as u64 * buf.img.height as u64 <= 16_500_000, "{label}: frame {f}");
let shows = |img: &effectcraft_raster::Image| img.data.iter().filter(|px| px[3] > 0.5).count();
let (sa, sb) = (shows(&a), shows(&whole.comp_frame(cid, t)));
// The same text shows (or none, the camera past it); coverage within a few percent.
assert!(sa.abs_diff(sb) * 50 <= sb.max(1) + 50, "{label}: frame {f}: {sa} px of text, {sb} rasterised whole");
capped += 1;
}
}
eprintln!("{label}: {exact} frames identical, {near} within anti-aliasing noise, {capped} sharper than the capped whole raster");
assert!(exact + near >= 10, "{label}: {exact} identical, {near} close, {capped} at the cap");
}
#[test]
fn a_camera_moving_any_way_renders_every_frame_as_the_whole_layer() {
let (cx, cy, z) = (W as f64 / 2.0, H as f64 / 2.0, -zoom());
let still = [(0.0, [cx, cy, 0.0])];
// In towards the text, through it and past it; and back out.
moving("dolly in through the text", &[(0.0, [cx, cy, 2.0 * z]), (1.0, [cx, cy, 150.0])], &[(0.0, [cx, cy, 400.0]), (1.0, [cx, cy, 700.0])], &[], &[]);
moving("dolly out", &[(0.0, [cx, cy, -60.0]), (1.0, [cx, cy, 3.0 * z])], &still, &[], &[]);
// Sideways and up and down close to the text, so it leaves the frame on both sides.
moving(
"pan x",
&[(0.0, [cx - 500.0, cy, -120.0]), (1.0, [cx + 500.0, cy, -120.0])],
&[(0.0, [cx - 500.0, cy, 0.0]), (1.0, [cx + 500.0, cy, 0.0])],
&[],
&[],
);
moving("pan y", &[(0.0, [cx, cy - 300.0, -90.0]), (1.0, [cx, cy + 300.0, -90.0])], &[(0.0, [cx, cy - 300.0, 0.0]), (1.0, [cx, cy + 300.0, 0.0])], &[], &[]);
// Around the text, close, looking at it.
let orbit: Vec<(f64, [f64; 3])> = (0..=8)
.map(|i| {
let a = i as f64 / 8.0 * std::f64::consts::TAU * 0.4 - 0.8;
(i as f64 / 8.0, [cx + 150.0 * a.sin(), cy, -150.0 * a.cos()])
})
.collect();
moving("orbit", &orbit, &still, &[], &[]);
// The text moves instead: towards the camera, past it, and across.
moving(
"text towards and past the camera",
&[(0.0, [cx, cy, z])],
&still,
&[(0.0, [cx + 200.0, cy - 80.0, 400.0]), (1.0, [cx - 150.0, cy + 60.0, z - 100.0])],
&[],
);
// Turning close to the camera.
moving("text turning near the camera", &[(0.0, [cx, cy, z])], &still, &[(0.0, [cx, cy, z + 70.0])], &[(0.0, -70.0), (1.0, 70.0)]);
}
/// Reads of a layer in its own space get the whole layer, not the part the frame shows: an effect
/// taking another layer as a parameter (Set Matte, stretched to fit, from hidden text close to the
/// camera), and the source the motion tracker analyses.
#[test]
fn layer_space_reads_get_the_whole_layer() {
let (mut p, cid, comp) = setup();
let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0);
text.switches.video = false;
let text_id = text.id;
let sid =
p.add_item("Solid", Label::Red, None, ItemKind::Solid(effectcraft_project::Solid { color: [0.2, 0.6, 1.0], width: W, height: H, pixel_aspect: 1.0 }));
let mut solid = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (W, H), None);
let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap();
let mut next = p.next_id;
let mut g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]);
p.next_id = next;
g.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(text_id.0));
solid.props.sub_mut("effects").unwrap().children.push(g.into());
p.comp_mut(cid).unwrap().layers = vec![text, solid].into();
assert_eq!(frame(&p, cid, false).data, frame(&p, cid, true).data, "Set Matte from the text");
let r = Renderer::new(&p, &NoFootage, RenderOpts::default());
let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap();
let layer = &p.comp(cid).unwrap().layers[0];
let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default());
whole.whole_raster = true;
assert_eq!(r.layer_source(&ctx, layer).unwrap().img, whole.layer_source(&ctx, layer).unwrap().img, "the tracker's source");
}
/// A layer whose pixels outside the frame can matter (an effect here) is rasterised whole.
#[test]
fn layers_with_effects_rasterise_whole() {
let (mut p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0));
let spec = effectcraft_effects::find("ec.blur.gaussian").unwrap();
let mut next = p.next_id;
let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]);
p.next_id = next;
p.comp_mut(cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.push(g.into());
assert_eq!(raster_px(&p, cid, false), raster_px(&p, cid, true));
}
/// The Layer panel and analysis tools consume layer-space inputs, even with no effects.
#[test]
fn layer_panel_input_reads_the_whole_layer_behind_the_camera() {
let (p, cid) = scene(|p, c| shape3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() - 200.0]));
let r = Renderer::new(&p, &NoFootage, RenderOpts::default());
let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap();
let layer = &p.comp(cid).unwrap().layers[0];
let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default());
whole.whole_raster = true;
let expected = whole.layer_input(&ctx, layer, 0).unwrap();
assert!(expected.img.data.iter().any(|pixel| pixel[3] > 0.5));
assert_eq!(r.layer_input(&ctx, layer, 0).unwrap().img, expected.img);
}
#[test]
fn fine_clip_cells_have_distinct_cache_keys() {
let a = kurbo::Rect::new(0.0, 0.0, 1.0 / 128.0, 1.0 / 128.0);
let b = kurbo::Rect::new(1.0 / 128.0, 0.0, 2.0 / 128.0, 1.0 / 128.0);
let key = |clip| crate::pipeline::raster_clip_key(42, clip);
// All three clips rounded to identical coordinates in the old 1/16-unit key.
assert_ne!(key(a), key(b));
assert_ne!(key(a), key(kurbo::Rect::ZERO));
assert_ne!(key(b), key(kurbo::Rect::ZERO));
}
/// Text animator Blur reads beyond the fixed anti-aliasing padding of a clipped window.
#[test]
fn animated_text_blur_keeps_offscreen_glyph_pixels_while_panning() {
let (mut p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0));
let mut next = p.next_id;
let mut ids = Ids(&mut next);
let props = build::text_anim_props(&mut ids, "blur", false);
let mut animator = build::text_animator(&mut ids, "Blur", props);
animator.prop_mut("properties/blur").unwrap().value = Value::Vec2([40.0, 40.0]);
p.next_id = next;
p.comp_mut(cid).unwrap().layers[0].props.group_mut("text/animators").unwrap().children.push(animator.into());
let opts = RenderOpts { scale: 0.125, ..Default::default() };
let cache = crate::LayerCache::default();
for dx in [-10.0, 0.0, 10.0] {
set(&mut p.comp_mut(cid).unwrap().layers[0], "transform/position", Value::Vec3([W as f64 / 2.0 + dx, H as f64 / 2.0, -zoom() + 40.0]));
let mut r = Renderer::new(&p, &NoFootage, opts);
r.cache = Some(&cache);
let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap();
let layer = &p.comp(cid).unwrap().layers[0];
assert!(r.raster_clip(&ctx, layer).is_none(), "animator blur needs off-window pixels");
let mut whole = Renderer::new(&p, &NoFootage, opts);
whole.whole_raster = true;
assert_eq!(r.comp_frame(cid, Tick::ZERO).data, whole.comp_frame(cid, Tick::ZERO).data);
}
}
+89
View File
@@ -1,7 +1,10 @@
//! Time effects through the renderer: neighbouring frames via `EffectHost::self_at`, and the
//! layer cache staying correct while scrubbing.
use std::sync::Arc;
use effectcraft_color::Label;
use effectcraft_effects::plugin::{EffectPlugin, PluginFrame, PluginManifest, PluginParams, register_plugin};
use effectcraft_keyframe::{Keyframe, ShapePath, Value};
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, MaskMode, Project, Solid};
@@ -157,3 +160,89 @@ fn input_keys_ignore_later_effects() {
set(&mut p, "effects/#1/blurriness", 8.0);
assert_ne!(key(&p).0, one);
}
/// A plug-in that reads the layer's neighbouring frames (WebAssembly ABI 2) makes its layer's
/// cache key depend on the layer time, even when the source and every property are static. A
/// plug-in that does not keeps the old key (#1977: a static layer stays cached across a scrub).
#[test]
fn abi2_plugin_layers_key_on_layer_time() {
struct NeighbourReader {
m: PluginManifest,
}
impl EffectPlugin for NeighbourReader {
fn manifest(&self) -> &PluginManifest {
&self.m
}
fn render(&self, _frame: &mut PluginFrame, _params: &PluginParams, _time: f64) -> Result<(), String> {
Ok(())
}
fn reads_other_times(&self) -> bool {
true
}
}
struct Plain {
m: PluginManifest,
}
impl EffectPlugin for Plain {
fn manifest(&self) -> &PluginManifest {
&self.m
}
fn render(&self, _frame: &mut PluginFrame, _params: &PluginParams, _time: f64) -> Result<(), String> {
Ok(())
}
}
/// A static white solid with one plug-in effect, registered by `plugin`.
fn plugin_scene(plugin: Arc<dyn EffectPlugin>) -> (Project, ItemId) {
let mut p = Project::default();
let comp = Comp::new(200, 100, FrameRate::FPS_30, Tick::from_seconds_f64(2.0));
let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into()));
let sid = p.add_item("Solid", Label::Red, None, ItemKind::Solid(Solid { color: [1.0, 1.0, 1.0], width: 200, height: 100, pixel_aspect: 1.0 }));
let mut l = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (200, 100), None);
let spec = register_plugin(plugin).unwrap();
let mut next = p.next_id;
let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [200.0, 100.0]);
p.next_id = next;
l.props.sub_mut("effects").unwrap().children.push(g.into());
p.comp_mut(cid).unwrap().layers.push(l);
(p, cid)
}
fn key(p: &Project, cid: ItemId, t: f64) -> Option<u64> {
let comp = p.comp(cid).unwrap();
let ctx = crate::EvalCtx {
project: p,
comp_id: cid,
comp,
time: Tick::from_seconds_f64(t),
expr: None,
footage: None,
property_overrides: &[],
media_overrides: &[],
reads: None,
};
crate::cache::layer_key(&ctx, &comp.layers[0], 1.0, false, false)
}
let manifest = |id: &str| PluginManifest {
api: 2,
id: id.into(),
name: "Key".into(),
category: "Test Plug-ins".into(),
version: "1".into(),
author: String::new(),
description: String::new(),
params: vec![],
};
let (plain, c1) = plugin_scene(Arc::new(Plain { m: manifest("org.test.render.time-key.plain") }));
assert_eq!(key(&plain, c1, 0.25), key(&plain, c1, 0.5), "a plain plug-in keeps the old key");
let (reader, c2) = plugin_scene(Arc::new(NeighbourReader { m: manifest("org.test.render.time-key.reader") }));
assert_ne!(key(&reader, c2, 0.25), key(&reader, c2, 0.5), "an ABI 2 plug-in keys on the layer time");
}
+49 -3
View File
@@ -509,6 +509,16 @@ pub fn per_char_3d(ctx: &EvalCtx, layer: &Layer) -> bool {
layer.is_3d() && layer.props.sub("text").is_some_and(|t| ctx.b(layer, t, "perChar3d"))
}
/// Animator blur can read glyph pixels beyond a viewport raster window. Conservatively
/// keep the whole raster even when the current selector happens to select no characters.
pub(crate) fn animator_blur(ctx: &EvalCtx, layer: &Layer) -> bool {
layer.props.group("text/animators").is_some_and(|anims| {
anims.groups().filter(|anim| anim.enabled).any(|anim| {
anim.sub("properties").and_then(|props| props.get("blur")).is_some_and(|blur| ctx.value(layer, blur).as_vec2().iter().any(|value| *value != 0.0))
})
})
}
/// One drawn character.
#[derive(Clone, Debug)]
pub struct PlacedGlyph {
@@ -1122,8 +1132,46 @@ fn draw_glyphs(geom: &TextGeom, img: &mut Image, to_px: &Mat3, s: f64, only: Opt
}
}
/// Buffer pixels kept around a clip (see [`raster_window`]).
const CLIP_PAD: f64 = 24.0;
/// The buffer for content bounds `b` (layer space) at scale `s`: (width, height, layer → buffer
/// offset), two pixels of padding around it. With `clip`, only the pixels of that grid inside
/// `clip`, padded by [`CLIP_PAD`]: a whole-pixel window of the whole buffer's grid. Its pixels are
/// the whole buffer's, except that a glyph cut by the window is rasterised from the window's edge:
/// tiny-skia's 4 × 4 anti-aliasing then lands a sub-sample differently on a few edge pixels, up to
/// a dozen rows in from that edge — inside the padding, out of the frame. `None` when `clip`
/// misses the content.
pub(crate) fn raster_window(b: kurbo::Rect, s: f64, clip: Option<kurbo::Rect>) -> Option<(u32, u32, [f64; 2])> {
let (full_w, full_h) = ((b.width() * s).ceil() + 4.0, (b.height() * s).ceil() + 4.0);
let full = [-b.x0 * s + 2.0, -b.y0 * s + 2.0];
let (x0, y0, x1, y1) = match clip {
None => (0.0, 0.0, full_w, full_h),
// Nothing shows (behind the camera).
Some(c) if c.area() <= 0.0 => return None,
Some(c) => (
(c.x0 * s + full[0] - CLIP_PAD).floor().max(0.0),
(c.y0 * s + full[1] - CLIP_PAD).floor().max(0.0),
(c.x1 * s + full[0] + CLIP_PAD).ceil().min(full_w),
(c.y1 * s + full[1] + CLIP_PAD).ceil().min(full_h),
),
};
if !(x1 > x0 && y1 > y0) {
return None;
}
let w = ((x1 - x0) as u32).clamp(1, 16384);
let h = ((y1 - y0) as u32).clamp(1, 16384);
Some((w, h, [full[0] - x0, full[1] - y0]))
}
/// Render a text layer into a layer-space buffer at scale `s`.
pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf {
render_in(ctx, layer, s, None)
}
/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows,
/// see `Renderer::raster_clip`).
pub fn render_in(ctx: &EvalCtx, layer: &Layer, s: f64, clip: Option<kurbo::Rect>) -> Buf {
let empty = || Buf { img: Image::new(1, 1), offset: [0.0; 2], scale: s };
let Some(geom) = text_geom(ctx, layer) else { return empty() };
let mut bounds: Option<kurbo::Rect> = None;
@@ -1137,9 +1185,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf {
}
let Some(b) = bounds else { return empty() };
let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0));
let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384);
let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384);
let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0];
let Some((w, h, offset)) = raster_window(b, s, clip) else { return empty() };
let mut img = Image::new(w, h);
let base = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s));
draw_glyphs(&geom, &mut img, &base, s, None);
+46
View File
@@ -185,6 +185,52 @@ pub(crate) fn view_size(r: &Renderer, ctx: &EvalCtx) -> (f64, f64) {
(c.width as f64, c.height as f64)
}
/// The layer-space rectangle of a 3D layer that this render's output can show: the output frame
/// (padded by two pixels) projected back onto the layer's plane, the way [`geo`] draws it. `None`
/// when that can't be bounded: an orthographic camera, depth of field (its blur reads past the
/// frame), motion blur (each sub-sample sees another part), or a frame corner whose ray misses the
/// plane in front of the camera (a plane seen towards its horizon). An empty rectangle when the
/// whole content is behind the camera.
pub(crate) fn visible_clip(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> Option<kurbo::Rect> {
let cam = camera_for(r, ctx);
if cam.ortho || cam.dof.is_some() || mb_samples(r, ctx, layer) > 1 {
return None;
}
let comp = view_size(r, ctx);
let s = r.opts.scale;
let outer = r.collapse3d.map_or(Mat4::IDENTITY, |c| c.world);
let world = outer * ctx.world_matrix(layer);
let (off, w, h) = match r.roi_offset() {
Some((x, y, w, h)) => ((x, y), w as f64, h as f64),
None => ((0.0, 0.0), (comp.0 * s).round(), (comp.1 * s).round()),
};
let proj = Mat4::translate(vec3(-off.0, -off.1, 0.0)) * Mat4::scale(vec3(s, s, 1.0)) * cam.projection(comp.0, comp.1);
let hinv = (proj * world).plane_to_mat3().inverse()?;
let cv = cam.view * world;
let depth = |u: f64, v: f64| cv.0[2][0] * u + cv.0[2][1] * v + cv.0[2][3];
if let Some(b) = crate::content_bounds(ctx, layer)
&& [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])].iter().all(|&(u, v)| depth(u, v) < NEAR)
{
return Some(kurbo::Rect::ZERO);
}
const PAD: f64 = 2.0;
let mut rect: Option<kurbo::Rect> = None;
for (sx, sy) in [(-PAD, -PAD), (w + PAD, -PAD), (w + PAD, h + PAD), (-PAD, h + PAD)] {
let m = &hinv.0;
let qz = m[2][0] * sx + m[2][1] * sy + m[2][2];
if qz <= 1e-300 {
return None;
}
let (u, v) = ((m[0][0] * sx + m[0][1] * sy + m[0][2]) / qz, (m[1][0] * sx + m[1][1] * sy + m[1][2]) / qz);
if !u.is_finite() || !v.is_finite() || depth(u, v) < NEAR {
return None;
}
let p = kurbo::Rect::new(u, v, u, v);
rect = Some(rect.map_or(p, |r| r.union(p)));
}
rect
}
/// Number of motion-blur sub-samples for a layer.
fn mb_samples(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> usize {
if r.opts.motion_blur && ctx.comp.enable_motion_blur && r.layer_motion_blur(layer) {
+149
View File
@@ -0,0 +1,149 @@
//! Render Queue benchmark (#3335): a 1080p 32 bpc job on the GPU, the CPU or the measured choice.
//!
//! `cargo run --release -p effectcraft-ui-egui --example rq_bench -- [options]`
//!
//! It builds a comp like a compositing shot: a 1920 × 1080 OpenEXR sequence (float RGBA with a
//! Cryptomatte ID channel and manifest, generated once into `target/rq-bench/media`, no
//! committed media) under Gaussian Blur, Curves and the Cryptomatte effect and a moving
//! translucent solid, at 32 bpc. It queues the comp twice (the second item shows a warm start) and
//! renders the queue as the app does, printing each item's milliseconds per frame and a hash of
//! the written pixels (equal hashes: identical output).
//!
//! Options: `--renderer gpu|cpu` (the project's renderer, default gpu; with gpu the Render
//! Queue measures both unless `EFFECTCRAFT_RQ_RENDERER` says otherwise), `--frames N` (default
//! 60), `--format png|exr|tiff` (default png), `--out DIR` (default `target/rq-bench/out`).
use std::sync::Arc;
use effectcraft_engine::Session;
use effectcraft_engine::raster::channels3d::{crypto_float, crypto_hash};
use serde_json::{Value, json};
const W: usize = 1920;
const H: usize = 1080;
fn arg(name: &str) -> Option<String> {
let a: Vec<String> = std::env::args().collect();
a.iter().position(|x| x == name).and_then(|i| a.get(i + 1).cloned())
}
fn run(s: &mut Session, cmd: &str, p: Value) -> Value {
s.execute(cmd, p).unwrap_or_else(|e| panic!("{cmd}: {e}"))
}
/// The plate's object at pixel (x, y) in frame `f`: 24 bands that drift sideways.
fn object(x: usize, y: usize, f: usize) -> String {
format!("obj{}", ((x + f * 7) / 80 + y / 270 * 24) % 96)
}
/// Frame `f` of the plate: float colour over 1.0 (32 bpc), alpha, and Cryptomatte IDs.
fn plate(path: &std::path::Path, f: usize) {
use exr::prelude::*;
let px = |c: usize| -> Vec<f32> {
(0..W * H)
.map(|i| {
let (x, y) = (i % W, i / W);
let v = ((x as f32 * 0.003 + f as f32 * 0.05 + c as f32).sin() * 0.5 + 0.5) * (1.0 + y as f32 / H as f32);
if c == 3 { 1.0 } else { v }
})
.collect()
};
let ids: Vec<f32> = (0..W * H).map(|i| crypto_float(crypto_hash(&object(i % W, i / W, f)))).collect();
let ch = |n: &str, v: Vec<f32>| AnyChannel::new(n, FlatSamples::F32(v));
let channels = AnyChannels::sort(
vec![ch("R", px(0)), ch("G", px(1)), ch("B", px(2)), ch("A", px(3)), ch("CryptoObject00.R", ids), ch("CryptoObject00.G", vec![1.0; W * H])].into(),
);
let mut layer = Layer::new((W, H), LayerAttributes::default(), Encoding::FAST_LOSSLESS, channels);
let names: serde_json::Map<String, Value> = (0..96).map(|k| format!("obj{k}")).map(|n| (n.clone(), json!(format!("{:08x}", crypto_hash(&n))))).collect();
for (key, v) in [("name", "CryptoObject".to_string()), ("manifest", Value::Object(names).to_string())] {
layer.attributes.other.insert(Text::from(format!("cryptomatte/f834d0a/{key}").as_str()), AttributeValue::Text(Text::from(v.as_str())));
}
Image::from_layer(layer).write().to_file(path).expect("write plate");
}
/// FNV-1a over every written frame file, in name order (equal: identical output).
fn hash_dir(dir: &std::path::Path) -> (usize, u64) {
let mut names: Vec<_> = std::fs::read_dir(dir).map(|d| d.filter_map(|e| e.ok()).map(|e| e.path()).collect()).unwrap_or_default();
names.retain(|p| !p.to_string_lossy().ends_with(".txt"));
names.sort();
let mut h = 0xcbf2_9ce4_8422_2325u64;
for p in &names {
for b in std::fs::read(p).unwrap_or_default() {
h = (h ^ b as u64).wrapping_mul(0x100_0000_01b3);
}
}
(names.len(), h)
}
fn main() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../target/rq-bench");
let frames: usize = arg("--frames").and_then(|n| n.parse().ok()).unwrap_or(60);
let format = arg("--format").unwrap_or_else(|| "png".into());
let gpu = arg("--renderer").as_deref() != Some("cpu");
let out = arg("--out").map(std::path::PathBuf::from).unwrap_or_else(|| root.join("out"));
let media = root.join("media");
std::fs::create_dir_all(&media).expect("media folder");
let first = media.join("plate_0000.exr");
for f in 0..frames {
let p = media.join(format!("plate_{f:04}.exr"));
if !p.exists() {
if f == 0 {
eprintln!("writing the {frames}-frame EXR plate (once)…");
}
plate(&p, f);
}
}
let mut s = effectcraft_host::session();
let accel = effectcraft_gpu::Gpu::try_headless().unwrap_or_else(|e| panic!("no GPU: {e}"));
eprintln!("GPU: {}", effectcraft_engine::render::Accelerator::name(&accel));
s.accel = Some(Arc::new(accel));
let r = run(&mut s, "file.import", json!({"paths": [first.to_string_lossy()], "sequence": true}));
let footage = r["items"][0].as_u64().expect("the plate");
let secs = frames as f64 / 30.0;
run(&mut s, "comp.new", json!({"name": "Shot", "width": W, "height": H, "frameRate": 30, "duration": secs}));
let plate = run(&mut s, "layer.addItem", json!({"item": footage}))["layer"].clone();
run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Gaussian Blur"}));
run(&mut s, "prop.set", json!({"layer": plate, "path": "effects/#1/blurriness", "value": 12.0}));
run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Curves"}));
run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Cryptomatte"}));
let solid = run(&mut s, "layer.newSolid", json!({"color": "#d04020", "width": 400, "height": 300}))["layer"].clone();
run(&mut s, "prop.addKey", json!({"layer": solid, "path": "transform/position", "time": 0.0, "value": [200.0, 540.0]}));
run(&mut s, "prop.addKey", json!({"layer": solid, "path": "transform/position", "time": secs, "value": [1720.0, 540.0]}));
run(&mut s, "prop.set", json!({"layer": solid, "path": "transform/opacity", "value": 70.0}));
let mut p = (*s.project).clone();
p.settings.bit_depth = effectcraft_engine::project::BitDepth::Bpc32;
p.settings.gpu_acceleration = gpu;
s.project = Arc::new(p);
let _ = std::fs::remove_dir_all(&out);
let dirs = [out.join("a"), out.join("b")];
for d in &dirs {
std::fs::create_dir_all(d).expect("output folder");
let path = d.join(format!("f_[####].{format}"));
run(&mut s, "renderQueue.add", json!({"comp": "Shot", "format": format, "output": path.to_string_lossy(), "log": "plusSettings"}));
}
let t0 = std::time::Instant::now();
run(&mut s, "renderQueue.render", json!({"wait": true}));
let total = t0.elapsed().as_secs_f64();
println!(
"renderer {} (EFFECTCRAFT_RQ_RENDERER={:?}, EFFECTCRAFT_GPU_INFLIGHT={:?}), {frames} frames {W}x{H} 32 bpc, {format}",
if gpu { "gpu" } else { "cpu" },
std::env::var("EFFECTCRAFT_RQ_RENDERER").ok(),
std::env::var("EFFECTCRAFT_GPU_INFLIGHT").ok()
);
for (i, (item, d)) in s.project.render_queue.iter().zip(&dirs).enumerate() {
let secs = item.render_time.unwrap_or(f64::NAN);
let (n, h) = hash_dir(d);
println!(" item {}: {:?}, {:.1} ms/frame, {n} files, hash {h:016x}", i + 1, item.status, secs * 1000.0 / frames as f64);
// The render log's account of the compositor and the GPU frames in flight.
let logs =
std::fs::read_dir(d).map(|r| r.filter_map(|e| e.ok()).map(|e| e.path()).filter(|p| p.to_string_lossy().ends_with(".txt")).collect::<Vec<_>>());
for log in logs.unwrap_or_default() {
for line in std::fs::read_to_string(log).unwrap_or_default().lines().filter(|l| l.starts_with("Renderer:") || l.starts_with("GPU frames")) {
println!(" {line}");
}
}
}
println!(" queue: {total:.2} s");
}
+248 -19
View File
@@ -33,7 +33,7 @@
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use effectcraft_engine::project::{Comp, Heard, ItemId, ItemKind, Node, Project, PropGroup};
use effectcraft_engine::render::disk_cache::{self, DiskCache};
@@ -54,6 +54,92 @@ const GPU_BUDGET: usize = 1 << 30;
const MIN_GPU_BUDGET: usize = 64 << 20;
const GPU_PRESSURE_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(5);
const GPU_RECOVERY_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30);
/// GPU frames that render at once, however many frame threads there are. Each one holds its own
/// working textures, so 16 at once ran an RTX 3090 out of video memory (#3028).
const MAX_GPU_RENDERS: usize = 2;
/// Counts the GPU renders in flight; the rest of the frame threads wait their turn.
struct GpuRenderSlots {
#[cfg(test)]
entered: std::sync::atomic::AtomicUsize,
busy: Mutex<usize>,
freed: Condvar,
}
/// One GPU render slot, given back on drop.
struct GpuRenderSlot<'a>(&'a GpuRenderSlots);
static GPU_RENDER_SLOTS: GpuRenderSlots = GpuRenderSlots::new();
impl GpuRenderSlots {
const fn new() -> Self {
Self {
#[cfg(test)]
entered: std::sync::atomic::AtomicUsize::new(0),
busy: Mutex::new(0),
freed: Condvar::new(),
}
}
/// The UI-thread display path must hand a frame to the remote worker instead of waiting
/// behind preview lanes. Never block the window to enforce the native memory cap.
fn try_acquire(&self, max: usize) -> Option<GpuRenderSlot<'_>> {
let mut busy = match self.busy.try_lock() {
Ok(busy) => busy,
Err(std::sync::TryLockError::Poisoned(error)) => error.into_inner(),
Err(std::sync::TryLockError::WouldBlock) => return None,
};
if !cfg!(target_arch = "wasm32") && *busy >= max {
return None;
}
*busy += 1;
#[cfg(test)]
self.entered.fetch_add(1, Ordering::SeqCst);
Some(GpuRenderSlot(self))
}
/// A Rayon helper may be executing a nested frame while an admitted render waits for
/// its work. Blocking that helper on a permit would deadlock the render (#3028).
fn for_render(&self, max: usize) -> Option<GpuRenderSlot<'_>> {
// Dedicated preview pools have one thread, protected from nested drains. They cannot
// be helpers of another admitted frame, so waiting preserves GPU preview throughput.
#[cfg(not(target_arch = "wasm32"))]
if DRAINING_LANE.get() {
return Some(self.acquire(max));
}
if rayon::current_thread_index().is_some() { self.try_acquire(max) } else { Some(self.acquire(max)) }
}
/// Wait until fewer than `max` renders are in flight, then take a slot. The browser has no
/// threads to wait on: it never blocks.
fn acquire(&self, max: usize) -> GpuRenderSlot<'_> {
let mut busy = self.busy.lock().unwrap_or_else(PoisonError::into_inner);
while !cfg!(target_arch = "wasm32") && *busy >= max {
busy = self.freed.wait(busy).unwrap_or_else(PoisonError::into_inner);
}
*busy += 1;
#[cfg(test)]
self.entered.fetch_add(1, Ordering::SeqCst);
GpuRenderSlot(self)
}
}
impl Drop for GpuRenderSlot<'_> {
fn drop(&mut self) {
let mut busy = self.0.busy.lock().unwrap_or_else(PoisonError::into_inner);
*busy = busy.saturating_sub(1);
self.0.freed.notify_one();
}
}
/// The window's frame gaps that adapt [`Queue::gpu_limit`] while a preview plays: a longer one
/// is a stall, shorter ones for a while are smooth.
const UI_STALL: std::time::Duration = std::time::Duration::from_millis(100);
const UI_SMOOTH: std::time::Duration = std::time::Duration::from_millis(50);
/// How long the window must stay smooth before one more lane composites on the GPU, and before it
/// tries again a limit that stalled.
const GPU_LANE_STEP: std::time::Duration = std::time::Duration::from_millis(500);
const GPU_LANE_RETRY: std::time::Duration = std::time::Duration::from_secs(10);
mod f16;
pub use f16::{F16_PIXEL_BYTES, F16Frame};
@@ -639,7 +725,7 @@ impl Default for Frames {
})
.collect();
#[cfg(not(target_arch = "wasm32"))]
let queue = Queue { idle_lanes: (0..lanes.len()).collect(), lanes: lanes.len(), ..Default::default() };
let queue = Queue { idle_lanes: (0..lanes.len()).collect(), lanes: lanes.len(), gpu_limit: 1, ..Default::default() };
#[cfg(target_arch = "wasm32")]
let queue = Queue::default();
Frames {
@@ -1282,6 +1368,8 @@ impl Frames {
#[cfg(not(target_arch = "wasm32"))]
let moved = q.playhead.is_some() != playhead.is_some();
q.playhead = playhead;
// (called once per frame of the window while a preview plays)
let more_gpu = q.ui_frame(playhead.map(|_| web_time::Instant::now()));
if let Some(p) = playhead
&& let Ok(mut inf) = self.inflight.lock()
{
@@ -1293,6 +1381,13 @@ impl Frames {
!gone
});
}
// A lane more may composite: one that stopped at the limit starts again.
#[cfg(not(target_arch = "wasm32"))]
if more_gpu && !moved {
self.start_lanes(&mut q, 1);
}
#[cfg(target_arch = "wasm32")]
let _ = more_gpu;
#[cfg(not(target_arch = "wasm32"))]
if moved {
self.start_lanes(&mut q, usize::MAX);
@@ -1597,19 +1692,44 @@ struct Worker {
thread_local! {
/// This pool thread is inside [`Worker::drain`] (rendering a frame).
static DRAINING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
/// Only the single thread of a dedicated preview lane may wait for GPU admission.
static DRAINING_LANE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// Clears [`DRAINING`] when the drain ends, even by a panic.
#[cfg(not(target_arch = "wasm32"))]
struct Draining;
#[cfg(not(target_arch = "wasm32"))]
impl Draining {
fn enter(lane: Option<usize>) -> Option<Self> {
if DRAINING.replace(true) {
return None;
}
DRAINING_LANE.set(lane.is_some());
Some(Self)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Drop for Draining {
fn drop(&mut self) {
DRAINING_LANE.set(false);
DRAINING.set(false);
}
}
/// A lane's hold on one of the [`Queue::gpu_limit`] GPU slots (see [`Queue::gpu_lanes`]), given back when dropped,
/// even by a panic.
struct GpuSlot<'a>(&'a Mutex<Queue>);
impl Drop for GpuSlot<'_> {
fn drop(&mut self) {
let mut q = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
q.gpu_lanes = q.gpu_lanes.saturating_sub(1);
}
}
impl Worker {
/// Render queued jobs, most important first, until the queue is empty.
///
@@ -1623,11 +1743,7 @@ impl Worker {
/// `lane`: the lane this runs on (see [`Queue::idle_lanes`]), `None` on the pool.
#[cfg(not(target_arch = "wasm32"))]
fn drain(&self, lane: Option<usize>) {
if DRAINING.get() {
return;
}
DRAINING.set(true);
let _reset = Draining;
let Some(_reset) = Draining::enter(lane) else { return };
while self.run_next(lane) {}
}
@@ -1636,19 +1752,25 @@ impl Worker {
/// idle again.
fn run_next(&self, lane: Option<usize>) -> bool {
let Worker { queue, content_keys, .. } = self;
let job = {
// (asked before the queue's lock: it takes the cache's)
let gpu = lane.is_some() && self.gpu_ready();
let (job, mut slot) = {
let Ok(mut q) = queue.lock() else { return false };
let Some(i) = q.next(lane.is_some()) else {
let Some(i) = q.next(lane.is_some(), gpu) else {
q.idle_lanes.extend(lane);
return false;
};
let job = q.jobs.swap_remove(i);
let slot = (gpu && q.gpu_job(&job)).then(|| {
q.gpu_lanes += 1;
GpuSlot(queue)
});
q.running.insert(job.key);
if job.urgent {
q.urgent_running += 1;
q.urgent_cancels.push((job.key, job.cancel.clone()));
}
job
(job, slot)
};
let t0 = web_time::Instant::now();
// (the drop generation before the frame reads anything: File ▸ Reload Footage meanwhile
@@ -1657,6 +1779,10 @@ impl Worker {
job.src.disk.as_ref().map(|dc| (dc.clone(), frame_disk_key(content_keys, &job.src.project, &job.key, opts_hash(&job.opts)), dc.drop_generation()));
let from_disk = disk.as_ref().and_then(|(dc, k, _)| self.disk_frame(dc, *k, &job));
let disk_hit = from_disk.is_some();
if disk_hit {
// Nothing for the GPU to do: another lane may composite meanwhile.
slot = None;
}
// `files`: the source files the frame was made of (see `render::reads`).
let (ci, files) = match from_disk {
Some(hit) => hit,
@@ -1683,6 +1809,8 @@ impl Worker {
// and compression. Persistence still runs on this worker, using the same snapshot.
let since = disk.as_ref().map(|(dc, _, since)| DiskSince::of(dc, *since));
self.finish(&job, ci.clone(), Some(files.clone()), since, t0);
// The GPU's part is done; storing it on disk is the CPU's.
drop(slot);
if !disk_hit
&& let Some((dc, k, since)) = &disk
&& put_disk_frame(dc, *k, &ci, &files, self.virtual_id.as_deref(), *since)
@@ -1812,7 +1940,10 @@ impl Worker {
r.reads = Some(&files);
r.accel = Some(g as &dyn effectcraft_engine::render::Accelerator);
r.active_accel()?;
let f = g.within_memory(|| g.render_display(&r, job.comp, job.t));
let f = {
let _slot = GPU_RENDER_SLOTS.try_acquire(MAX_GPU_RENDERS)?;
g.within_memory(|| g.render_display(&r, job.comp, job.t))
};
effectcraft_engine::render::Accelerator::frame_end(g);
match f {
Ok(f) => {
@@ -1853,7 +1984,7 @@ impl Worker {
let auto = if cfg!(target_arch = "wasm32") { None } else { r.frame_auto(a, job.comp, true) };
if auto.is_none_or(|(p, key)| p.choose(key)) {
let t0 = web_time::Instant::now();
match g.within_memory(|| g.render_display(&r, job.comp, job.t)) {
match gpu_attempt(g, || g.render_display(&r, job.comp, job.t)) {
Ok(Some(f)) => {
self.gpu_succeeded();
if let Some((p, key)) = auto {
@@ -1872,13 +2003,21 @@ impl Worker {
}
}
Err(e) => {
self.gpu_attempt_failed(g, &e);
self.gpu_attempt_rejected(g, &e);
return self.render_job(job, false, files);
}
}
}
let t0 = web_time::Instant::now();
let img = cpu_frame(&r.comp_frame_cpu(job.comp, job.t), &job.src.project, &job.opts);
// CPU compositing in Auto can still run GPU layer effects. Keep those inside the
// same admission and error scope as a GPU-composited frame (#3028).
let img = match gpu_attempt(g, || r.comp_frame_cpu(job.comp, job.t)) {
Ok(img) => cpu_frame(&img, &job.src.project, &job.opts),
Err(e) => {
self.gpu_attempt_rejected(g, &e);
return self.render_job(job, false, files);
}
};
if let Some((p, key)) = auto {
p.record(key, false, t0.elapsed().as_secs_f64() * 1000.0);
}
@@ -1886,19 +2025,37 @@ impl Worker {
}
if let Some(g) = gpu {
// The GPU composites and the frame is read back.
return match g.within_memory(|| r.comp_frame(job.comp, job.t)) {
return match r.comp_frame_scoped(job.comp, job.t, |a| gpu_attempt(g, || a.comp_frame(&r, job.comp, job.t))) {
Ok(img) => {
self.gpu_succeeded();
cpu_frame(&img, &job.src.project, &job.opts)
}
Err(e) => {
self.gpu_attempt_failed(g, &e);
self.gpu_attempt_rejected(g, &e);
self.render_job(job, false, files)
}
};
}
cpu_frame(&r.comp_frame(job.comp, job.t), &job.src.project, &job.opts)
}
/// Busy shared-pool workers retry on the CPU without treating contention as a GPU fault.
fn gpu_attempt_rejected(&self, gpu: &Gpu, error: &GpuAttemptError) {
if let GpuAttemptError::Failed(error) = error {
self.gpu_attempt_failed(gpu, error);
}
}
}
enum GpuAttemptError {
Busy,
Failed(FrameError),
}
/// Bound all preview GPU attempts, including effects in an Auto CPU composite. The permit
/// ends before the caller retries on the CPU, so failed frames cannot block GPU admissions.
fn gpu_attempt<T>(gpu: &Gpu, render: impl FnOnce() -> T) -> Result<T, GpuAttemptError> {
let _slot = GPU_RENDER_SLOTS.for_render(MAX_GPU_RENDERS).ok_or(GpuAttemptError::Busy)?;
gpu.within_memory(render).map_err(GpuAttemptError::Failed)
}
/// Who asked for a frame (see [`Frames::request_with`]).
@@ -1951,6 +2108,24 @@ struct Queue {
/// caching left the same gaps, and held up the viewer's frame when it shared a piece.
lanes: usize,
idle_lanes: Vec<usize>,
/// Lanes compositing a frame on the GPU, at most [`Queue::gpu_limit`]. The GPU is the
/// viewer's device: each lane records, submits and reads back a whole frame on it, and the
/// window can't draw until that work is done. With a lane per core on a GPU that can't keep
/// up, the window froze for hundreds of milliseconds at a time while a preview played, and
/// the preview jumped ahead when it came back (#3345). The other lanes go on with the frames
/// that don't need the GPU (the disk cache's, or all of them while the GPU is out of memory
/// or retired); the pool and the viewer's frame are not counted.
gpu_lanes: usize,
/// How many lanes may composite on the GPU at a time (at least 1, see [`Queue::ui_frame`]).
gpu_limit: usize,
/// The limit that last stalled the window: tried again only after [`GPU_LANE_RETRY`].
gpu_stalled_at: usize,
/// While a preview plays: the window's last frame, and since when it has been smooth.
ui_last: Option<web_time::Instant>,
ui_smooth_since: Option<web_time::Instant>,
/// Tests: as if there were a GPU compositor (no adapter is there).
#[cfg(test)]
fake_gpu: bool,
}
impl Queue {
@@ -1959,9 +2134,63 @@ impl Queue {
self.lanes > 0 && !j.urgent && (j.background || self.playhead.is_some())
}
/// The job a lane (`lane`) or the pool renders next: the [`Queue::best`] of its own.
fn next(&self, lane: bool) -> Option<usize> {
self.best_of(|j| self.on_lane(j) == lane)
/// The job a lane (`lane`) or the pool renders next: the [`Queue::best`] of its own. `gpu`:
/// the GPU composites (a lane's frames), so a lane takes no GPU frame while
/// [`Queue::gpu_limit`] lanes are compositing.
fn next(&self, lane: bool, gpu: bool) -> Option<usize> {
let gpu_full = gpu && self.gpu_lanes >= self.gpu_limit.max(1);
self.best_of(|j| self.on_lane(j) == lane && !(gpu_full && self.gpu_job(j)))
}
/// The window drew a frame at `now` (`None`: no preview plays). A window that stalls with
/// lanes compositing gets fewer of them: the GPU can't keep up with them and the window. One
/// that stays smooth while every allowed lane composites gets one more, up to all of them: a
/// GPU that keeps up renders the preview on every lane. A limit that stalled is tried again
/// only after a longer smooth spell, so the window doesn't stall over and over. `true` when
/// one more lane may composite.
fn ui_frame(&mut self, now: Option<web_time::Instant>) -> bool {
let Some(now) = now else {
self.ui_last = None;
return false;
};
let limit = self.gpu_limit.max(1);
let gap = self.ui_last.map(|last| now.saturating_duration_since(last));
self.ui_last = Some(now);
let Some(gap) = gap else {
self.ui_smooth_since = Some(now);
return false;
};
if gap > UI_STALL && self.gpu_lanes > 0 {
self.gpu_stalled_at = limit;
self.gpu_limit = (limit / 2).max(1);
self.ui_smooth_since = Some(now);
} else if gap > UI_SMOOTH {
self.ui_smooth_since = Some(now);
} else {
let since = *self.ui_smooth_since.get_or_insert(now);
let wait = if limit + 1 >= self.gpu_stalled_at && self.gpu_stalled_at > 0 { GPU_LANE_RETRY } else { GPU_LANE_STEP };
if self.gpu_lanes >= limit && limit < self.lanes && now.saturating_duration_since(since) >= wait {
self.gpu_limit = limit + 1;
self.ui_smooth_since = Some(now);
return true;
}
}
false
}
/// Whether `j` is composited on the GPU when the GPU is ready: there is a GPU compositor and
/// the frame's renderer uses it (as `Renderer::active_accel` decides: Software Only keeps
/// every frame on the CPU).
fn gpu_job(&self, j: &Job) -> bool {
let adapter = j.src.gpu.is_some();
#[cfg(test)]
let adapter = adapter || self.fake_gpu;
adapter
&& match j.opts.backend {
effectcraft_engine::render::Backend::Cpu => false,
effectcraft_engine::render::Backend::Gpu => true,
effectcraft_engine::render::Backend::Auto => j.src.project.settings.gpu_acceleration,
}
}
/// The job that matters most: urgent first (newest), then, while a preview plays, the
+409
View File
@@ -610,15 +610,21 @@ struct ParallelClip {
started: std::sync::atomic::AtomicUsize,
/// Frames started by the time frame 1 was done.
started_at_first: std::sync::atomic::AtomicUsize,
/// Frames decoding now, and the most at a time.
active: std::sync::atomic::AtomicUsize,
most_active: std::sync::atomic::AtomicUsize,
}
impl FootageSource for ParallelClip {
fn frame(&self, _: ItemId, _: &effectcraft_engine::project::Footage, t: Tick) -> Option<Arc<effectcraft_engine::render::Image>> {
self.started.fetch_add(1, Ordering::SeqCst);
let active = self.active.fetch_add(1, Ordering::SeqCst) + 1;
self.most_active.fetch_max(active, Ordering::SeqCst);
(0..16).into_par_iter().for_each(|_| (0..4).into_par_iter().for_each(|_| std::thread::sleep(std::time::Duration::from_millis(1))));
if effectcraft_engine::time::FrameRate::FPS_30.frame_at(t) == 1 {
self.started_at_first.store(self.started.load(Ordering::SeqCst), Ordering::SeqCst);
}
self.active.fetch_sub(1, Ordering::SeqCst);
Some(Arc::new(effectcraft_engine::render::Image::new(4, 4)))
}
}
@@ -729,6 +735,166 @@ fn queued_frames_render_when_a_preview_starts_or_stops() {
assert_eq!(f.cached_frames(&series).len(), 119);
}
/// Waits for the lanes to be idle (after their last frame they give back their GPU slot) and
/// checks every GPU slot was given back.
fn wait_for_idle_lanes(f: &Frames) {
for _ in 0..2000 {
let q = f.queue.lock().unwrap();
if q.idle_lanes.len() == f.lanes.len() {
break;
}
drop(q);
std::thread::sleep(std::time::Duration::from_millis(5));
}
let q = f.queue.lock().unwrap();
assert_eq!(q.idle_lanes.len(), f.lanes.len(), "every lane is idle again");
assert_eq!(q.gpu_lanes, 0, "every lane gave its GPU slot back");
}
/// Plays a preview of `frames` frames of a [`parallel_clip`] as the viewer renders them (the
/// project's renderer: `backend` Auto) or with `backend`, as if there were a GPU compositor
/// (`gpu`), `retired` or with the project on Mercury Software Only (`!gpu_acceleration`), and
/// waits for them: the frame cache and the most frames that rendered at a time.
fn play_preview(gpu: bool, retired: bool, backend: effectcraft_engine::render::Backend, gpu_acceleration: bool) -> (Frames, usize) {
use effectcraft_engine::time::FrameRate;
let (f, footage, mut src, cid, series) = parallel_clip();
Arc::make_mut(&mut src.project).settings.gpu_acceleration = gpu_acceleration;
f.queue.lock().unwrap().fake_gpu = gpu;
if retired {
f.retire_gpu();
}
let opts = RenderOpts { backend, ..RenderOpts::default() };
f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false }));
let frames = 24;
for frame in 1..=frames {
f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts);
}
wait_for_renders(&f);
assert_eq!(f.cached_frames(&series).len(), frames as usize);
wait_for_idle_lanes(&f);
let most = footage.most_active.load(Ordering::SeqCst);
(f, most)
}
/// Every lane composited a playing preview's frame on the GPU at the same time, on the device
/// the window draws with: the window froze for hundreds of milliseconds and the preview jumped
/// ahead (#3345). A preview starts on one GPU lane (see [`Queue::ui_frame`]).
#[test]
fn a_preview_composites_on_a_few_lanes_at_a_time() {
use effectcraft_engine::render::Backend;
for backend in [Backend::Auto, Backend::Gpu] {
let (_, most) = play_preview(true, false, backend, true);
assert!(most <= 1, "{most} lanes composited on the GPU at once ({backend:?})");
}
}
/// Frames that render on the CPU use every lane: with no GPU compositor, with the GPU retired,
/// and with Mercury Software Only (the project's renderer, or the CPU backend).
#[test]
fn frames_on_the_cpu_render_on_every_lane() {
use effectcraft_engine::render::Backend;
for (gpu, retired, backend, gpu_acceleration) in
[(false, false, Backend::Auto, true), (true, true, Backend::Auto, true), (true, false, Backend::Auto, false), (true, false, Backend::Cpu, true)]
{
let (f, most) = play_preview(gpu, retired, backend, gpu_acceleration);
if f.lanes.len() > 2 {
assert!(most > 2, "frames on the CPU rendered on {most} lanes at a time ({gpu} {retired} {backend:?} {gpu_acceleration})");
}
}
}
/// A queue of `lanes` lanes, `busy` of them compositing on the GPU.
fn gpu_queue(lanes: usize, busy: usize) -> Queue {
Queue { lanes, gpu_lanes: busy, gpu_limit: 1, ..Default::default() }
}
/// Window frames every `gap` ms from `t` for `ms` ms; the time after the last.
fn ui_frames(q: &mut Queue, t: web_time::Instant, gap: u64, ms: u64) -> web_time::Instant {
let mut t = t;
for _ in 0..ms / gap {
t += std::time::Duration::from_millis(gap);
q.ui_frame(Some(t));
}
t
}
/// A GPU that keeps up with the window gets every lane, one more each half second the window
/// stays smooth with every allowed lane compositing.
#[test]
fn a_smooth_window_gives_the_gpu_every_lane() {
let mut q = gpu_queue(8, 0);
let t = web_time::Instant::now();
q.ui_frame(Some(t));
// Nothing compositing: the limit isn't what holds the preview back.
let t = ui_frames(&mut q, t, 16, 2000);
assert_eq!(q.gpu_limit, 1);
let mut t = t;
for _ in 0..20 {
q.gpu_lanes = q.gpu_limit;
t = ui_frames(&mut q, t, 16, 520);
}
assert_eq!(q.gpu_limit, 8, "no more than the lanes");
}
/// A window that stalls while lanes composite halves the limit, and the limit that stalled is
/// tried again only after a long smooth spell; a window that is only slow keeps the limit.
#[test]
fn a_stalling_window_takes_lanes_from_the_gpu() {
let mut q = gpu_queue(8, 4);
q.gpu_limit = 4;
let t = web_time::Instant::now();
q.ui_frame(Some(t));
let t = ui_frames(&mut q, t, 300, 300);
assert_eq!((q.gpu_limit, q.gpu_stalled_at), (2, 4));
let t = ui_frames(&mut q, t, 80, 5000);
assert_eq!(q.gpu_limit, 2, "a slow (not stalling) window keeps the limit");
q.gpu_lanes = 2;
let t = ui_frames(&mut q, t, 16, 600);
assert_eq!(q.gpu_limit, 3);
q.gpu_lanes = 3;
let t = ui_frames(&mut q, t, 16, 2000);
assert_eq!(q.gpu_limit, 3, "4 stalled: it waits longer");
ui_frames(&mut q, t, 16, 10_000);
assert_eq!(q.gpu_limit, 4);
// A stall with nothing compositing isn't the GPU's.
let mut q = gpu_queue(8, 0);
q.gpu_limit = 4;
let t = web_time::Instant::now();
q.ui_frame(Some(t));
ui_frames(&mut q, t, 500, 500);
assert_eq!(q.gpu_limit, 4);
// Never below one lane; a stopped preview's pause is no stall.
let mut q = gpu_queue(8, 1);
let t = web_time::Instant::now();
q.ui_frame(Some(t));
ui_frames(&mut q, t, 400, 400);
assert_eq!(q.gpu_limit, 1);
q.gpu_limit = 2;
q.ui_frame(None);
q.ui_frame(Some(t + std::time::Duration::from_secs(5)));
assert_eq!(q.gpu_limit, 2);
}
/// A preview that stops while lanes composite: their frames finish, the queued ones move to the
/// pool and render, the viewer's frame is not held up, and every GPU slot is given back.
#[test]
fn a_preview_stopping_mid_render_gives_the_gpu_slots_back() {
use effectcraft_engine::time::FrameRate;
let (f, _, src, cid, series) = parallel_clip();
f.queue.lock().unwrap().fake_gpu = true;
let opts = RenderOpts { backend: effectcraft_engine::render::Backend::Auto, ..RenderOpts::default() };
f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false }));
for frame in 1..=24 {
f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts);
}
std::thread::sleep(std::time::Duration::from_millis(20));
f.set_playhead(None);
f.request_urgent(&src, FrameKey { frame: 100, ..series }, cid, FrameRate::FPS_30.tick_of(100), opts);
wait_for_renders(&f);
assert_eq!(f.cached_frames(&series).len(), 25);
wait_for_idle_lanes(&f);
}
/// The cache is full. A zoom (or preview-resolution) change left the frames of the old
/// size to the plain least-recently-shown order, so the frames of the zoom the viewer is back
/// at went before the ones of the abandoned zoom. The other size's frames go first, then the
@@ -2957,3 +3123,246 @@ mod frame_files {
assert_eq!(healed.hits(), (1, 0), "the rewritten entry is used");
}
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn gpu_renders_in_flight_stay_within_the_cap() {
use std::sync::atomic::AtomicUsize;
let slots = GpuRenderSlots::new();
let now = AtomicUsize::new(0);
let peak = AtomicUsize::new(0);
std::thread::scope(|s| {
for _ in 0..16 {
s.spawn(|| {
let _slot = slots.acquire(2);
let n = now.fetch_add(1, Ordering::SeqCst) + 1;
peak.fetch_max(n, Ordering::SeqCst);
std::thread::sleep(std::time::Duration::from_millis(5));
now.fetch_sub(1, Ordering::SeqCst);
});
}
});
assert!(peak.load(Ordering::SeqCst) <= 2);
assert_eq!(*slots.busy.lock().unwrap_or_else(PoisonError::into_inner), 0);
}
/// #3028: exercise the actual worker routes, not only the admission counter. The Auto CPU
/// compositor still has a live accelerator for its layer effects.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn gpu_worker_routes_admit_display_readback_and_auto_effects() {
use effectcraft_engine::render::{Accelerator, Backend};
let Some(gpu) = Gpu::headless() else {
eprintln!("GPU worker route test skipped: no adapter");
return;
};
let (frames, mut src, key, _, _, _) = disk_fixture(&[]);
let mut session = effectcraft_engine::Session::default();
session.execute("comp.new", serde_json::json!({"width": 16, "height": 16, "duration": 1})).unwrap();
let comp = session.active_comp_id().unwrap();
let solid = session.execute("layer.newSolid", serde_json::json!({"color": "#ff0000"})).unwrap()["layer"].clone();
session.execute("effect.apply", serde_json::json!({"layers": [solid], "effect": "Gaussian Blur"})).unwrap();
src.project = session.project;
src.gpu = Some(gpu.clone());
src.gpu_display = true;
let mut job = Job {
key,
src,
comp,
t: Tick::ZERO,
opts: RenderOpts { backend: Backend::Gpu, ..Default::default() },
urgent: true,
background: false,
seq: 0,
cancel: Arc::default(),
};
let worker = frames.worker();
let admissions = || GPU_RENDER_SLOTS.entered.load(Ordering::SeqCst);
// This path is called on the UI thread by dispatch_remote: a full GPU must decline
// immediately so the frame can go to the remote worker and the window keeps painting.
let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let (tx, rx) = std::sync::mpsc::channel();
let declined = std::thread::scope(|scope| {
scope.spawn(|| tx.send(worker.render_gpu(&job).is_none()).unwrap());
let result = rx.recv_timeout(std::time::Duration::from_secs(2));
drop((first, second));
result
});
assert_eq!(declined, Ok(true), "UI display must not wait for GPU admission");
let before = admissions();
assert!(worker.render_gpu(&job).is_some(), "direct display path renders");
assert!(admissions() > before, "direct display bypassed GPU admission");
let before = admissions();
assert!(worker.render_job(&job, true, &ReadLog::new()).is_gpu());
assert!(admissions() > before, "worker display bypassed GPU admission");
job.src.gpu_display = false;
job.src.layer_cache.clear();
let before = admissions();
assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu());
assert!(admissions() > before, "readback bypassed GPU admission");
job.src.gpu_display = true;
job.src.layer_cache.clear();
job.opts.backend = Backend::Auto;
let renderer = Renderer::new(&job.src.project, job.src.footage.as_ref(), job.opts);
let (pick, key) = renderer.frame_auto(&gpu, comp, true).unwrap();
pick.declined(key); // Select CPU compositing while retaining GPU-capable layer effects.
let before = admissions();
assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu());
assert!(admissions() > before, "Auto CPU effects bypassed GPU admission");
let before = admissions();
assert!(!worker.render_job(&job, false, &ReadLog::new()).is_gpu());
assert_eq!(admissions(), before, "CPU retry must not hold a GPU slot");
assert_eq!(*GPU_RENDER_SLOTS.busy.lock().unwrap(), 0);
// Saturation in the shared Rayon pool is contention, not a GPU fault. It must finish
// through the fresh CPU route without admitting GPU effects or retiring the device.
let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let before = admissions();
let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let cpu = std::thread::scope(|scope| {
scope.spawn(|| pool.install(|| tx.send(!worker.render_job(&job, true, &ReadLog::new()).is_gpu()).unwrap()));
let result = rx.recv_timeout(std::time::Duration::from_secs(2));
drop((first, second));
result
});
assert_eq!(cpu, Ok(true), "busy pool worker must complete on the CPU");
assert_eq!(admissions(), before);
assert!(worker.gpu_ready());
assert!(gpu.context().check_health().is_ok());
// The real dedicated-lane drain waits, then caches a GPU frame instead of silently
// switching a healthy preview to CPU rendering while the other lanes are busy.
frames.queue.lock().unwrap().jobs.push(Job {
key: job.key,
src: job.src.clone(),
comp,
t: job.t,
opts: RenderOpts { backend: Backend::Gpu, ..job.opts },
urgent: false,
background: true,
seq: 1,
cancel: Arc::default(),
});
let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS);
let pool = rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let (started, ready) = std::sync::mpsc::channel();
let (tx, rx) = std::sync::mpsc::channel();
let (early, finished) = std::thread::scope(|scope| {
scope.spawn(|| {
pool.install(|| {
started.send(()).unwrap();
worker.drain(Some(0));
tx.send(()).unwrap();
})
});
ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
let early = rx.recv_timeout(std::time::Duration::from_millis(100));
drop((first, second));
let finished = early.or_else(|_| rx.recv_timeout(std::time::Duration::from_secs(2)));
(early, finished)
});
assert!(early.is_err(), "preview lane must wait for an available GPU permit");
assert_eq!(finished, Ok(()));
assert!(frames.cache.lock().unwrap().map.get(&job.key).unwrap().img.is_gpu());
// A retirement found inside the scope releases the permit before the fresh CPU retry.
gpu.context().device_failure_notifier()("synthetic worker retirement".into());
let before = admissions();
assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu());
assert_eq!(admissions(), before + 1, "failed GPU attempt must retry without a second admission");
assert_eq!(*GPU_RENDER_SLOTS.busy.lock().unwrap(), 0);
gpu.frame_end();
}
/// Early returns and unwinding release admission even before another render starts.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn gpu_render_slots_return_after_failure_and_unwind() {
let slots = GpuRenderSlots::new();
let failed = || -> Result<(), ()> {
let _slot = slots.acquire(2);
Err(())
};
assert!(failed().is_err());
assert_eq!(*slots.busy.lock().unwrap(), 0);
let result = std::panic::catch_unwind(|| {
let _slot = slots.acquire(2);
panic!("synthetic render panic");
});
assert!(result.is_err());
assert_eq!(*slots.busy.lock().unwrap(), 0);
}
/// A Rayon helper must decline a full gate rather than wait for a render that can itself be
/// waiting for that helper. Release the permits on timeout so a regression fails, not hangs.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn gpu_render_slots_never_block_rayon_helpers() {
let slots = GpuRenderSlots::new();
let first = slots.acquire(2);
let second = slots.acquire(2);
let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let declined = std::thread::scope(|scope| {
scope.spawn(|| {
pool.install(|| {
rayon::join(|| {}, || tx.send(slots.for_render(2).is_none()).unwrap());
});
});
let result = rx.recv_timeout(std::time::Duration::from_secs(2));
drop((first, second));
result
});
assert_eq!(declined, Ok(true), "Rayon helpers must not wait on GPU permits");
assert_eq!(*slots.busy.lock().unwrap(), 0);
}
/// Dedicated preview lanes have one protected thread and can wait without creating the
/// shared-pool helper dependency cycle. They must keep rendering on a healthy GPU.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn gpu_preview_lane_waits_for_admission_and_resets_its_marker() {
let slots = GpuRenderSlots::new();
let first = slots.acquire(2);
let second = slots.acquire(2);
let pool = rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let (started, ready) = std::sync::mpsc::channel();
let (tx, rx) = std::sync::mpsc::channel();
let (early, admitted) = std::thread::scope(|scope| {
scope.spawn(|| {
pool.install(|| {
let _drain = Draining::enter(Some(0)).unwrap();
assert!(Draining::enter(None).is_none(), "a lane cannot start a nested drain");
assert!(DRAINING_LANE.get(), "nested rejection must not clear the lane marker");
started.send(()).unwrap();
tx.send(slots.for_render(2).is_some()).unwrap();
});
});
ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
let early = rx.recv_timeout(std::time::Duration::from_millis(100));
drop((first, second));
let admitted = early.or_else(|_| rx.recv_timeout(std::time::Duration::from_secs(2)));
(early, admitted)
});
assert!(early.is_err(), "busy dedicated lane must wait instead of falling back to CPU");
assert_eq!(admitted, Ok(true));
pool.install(|| {
assert!(!DRAINING.get());
assert!(!DRAINING_LANE.get());
let panic = std::panic::catch_unwind(|| {
let _drain = Draining::enter(Some(0)).unwrap();
panic!("synthetic lane failure");
});
assert!(panic.is_err());
assert!(!DRAINING.get());
assert!(!DRAINING_LANE.get());
});
}
+30 -28
View File
@@ -506,8 +506,8 @@ const SIMPLIFIED_CHINESE: &[(&str, &str, &str)] = &[
("file.import", "File...", "文件..."),
("file.importMultiple", "Multiple Files...", "多个文件..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro 项目..."),
("file.importAep", "After Effects Project...", "After Effects 项目..."),
("file.exportAep", "After Effects Project...", "After Effects 项目..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effects 项目..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects 项目..."),
("file.importProject", "EffectCraft Project...", "EffectCraft 项目..."),
("file.importPlaceholder", "Placeholder...", "占位符..."),
("file.importSolid", "Solid...", "纯色..."),
@@ -1146,8 +1146,8 @@ const SWEDISH: &[(&str, &str, &str)] = &[
("file.import", "File...", "Fil..."),
("file.importMultiple", "Multiple Files...", "Flera filer..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-projekt..."),
("file.importAep", "After Effects Project...", "After Effects-projekt..."),
("file.exportAep", "After Effects Project...", "After Effects-projekt..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-projekt..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-projekt..."),
("file.importProject", "EffectCraft Project...", "EffectCraft-projekt..."),
("file.importPlaceholder", "Placeholder...", "Platshållare..."),
("file.importSolid", "Solid...", "Enfärgad..."),
@@ -1788,8 +1788,8 @@ const TRADITIONAL_CHINESE: &[(&str, &str, &str)] = &[
("file.import", "File...", "檔案..."),
("file.importMultiple", "Multiple Files...", "多個檔案..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro 專案..."),
("file.importAep", "After Effects Project...", "After Effects 專案..."),
("file.exportAep", "After Effects Project...", "After Effects 專案..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effects 專案..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects 專案..."),
("file.importProject", "EffectCraft Project...", "EffectCraft 專案..."),
("file.importPlaceholder", "Placeholder...", "預留位置..."),
("file.importSolid", "Solid...", "純色..."),
@@ -2427,8 +2427,8 @@ const JAPANESE: &[(&str, &str, &str)] = &[
("file.import", "File...", "ファイル..."),
("file.importMultiple", "Multiple Files...", "複数のファイル..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Proプロジェクト..."),
("file.importAep", "After Effects Project...", "After Effectsプロジェクト..."),
("file.exportAep", "After Effects Project...", "After Effectsプロジェクト..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effectsプロジェクト..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effectsプロジェクト..."),
("file.importPlaceholder", "Placeholder...", "プレースホルダー..."),
("file.importSolid", "Solid...", "平面..."),
("file.importLottie", "Lottie...", "Lottie..."),
@@ -3069,8 +3069,8 @@ const UKRAINIAN: &[(&str, &str, &str)] = &[
("file.import", "File...", "Файл..."),
("file.importMultiple", "Multiple Files...", "Кілька файлів..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Проєкт Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Проєкт After Effects..."),
("file.exportAep", "After Effects Project...", "Проєкт After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Проєкт Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Проєкт Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Проєкт EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Заповнювач..."),
("file.importSolid", "Solid...", "Суцільний колір..."),
@@ -3712,8 +3712,8 @@ const SPANISH: &[(&str, &str, &str)] = &[
("file.import", "File...", "Archivo..."),
("file.importMultiple", "Multiple Files...", "Varios archivos..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Proyecto de Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Proyecto de After Effects..."),
("file.exportAep", "After Effects Project...", "Proyecto de After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Proyecto de Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Proyecto de Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Proyecto de EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Marcador de posición..."),
("file.importSolid", "Solid...", "Sólido..."),
@@ -4353,8 +4353,8 @@ const PORTUGUESE: &[(&str, &str, &str)] = &[
("file.import", "File...", "Arquivo..."),
("file.importMultiple", "Multiple Files...", "Vários arquivos..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Projeto do Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Projeto do After Effects..."),
("file.exportAep", "After Effects Project...", "Projeto do After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Projeto do Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Projeto do Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Projeto do EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Espaço reservado..."),
("file.importSolid", "Solid...", "Sólido..."),
@@ -5002,8 +5002,8 @@ const RUSSIAN: &[(&str, &str, &str)] = &[
("file.import", "File...", "Файл..."),
("file.importMultiple", "Multiple Files...", "Несколько файлов..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Проект Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Проект After Effects..."),
("file.exportAep", "After Effects Project...", "Проект After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Проект Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Проект Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Проект EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Заполнитель..."),
("file.importSolid", "Solid...", "Сплошной цвет..."),
@@ -5643,8 +5643,8 @@ const FRENCH: &[(&str, &str, &str)] = &[
("file.import", "File...", "Fichier..."),
("file.importMultiple", "Multiple Files...", "Plusieurs fichiers..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Projet Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Projet After Effects..."),
("file.exportAep", "After Effects Project...", "Projet After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Projet Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Projet Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Projet EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Espace réservé..."),
("file.importSolid", "Solid...", "Solide..."),
@@ -6284,8 +6284,8 @@ const GERMAN: &[(&str, &str, &str)] = &[
("file.import", "File...", "Datei..."),
("file.importMultiple", "Multiple Files...", "Mehrere Dateien..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-Projekt..."),
("file.importAep", "After Effects Project...", "After Effects-Projekt..."),
("file.exportAep", "After Effects Project...", "After Effects-Projekt..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-Projekt..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-Projekt..."),
("file.importProject", "EffectCraft Project...", "EffectCraft-Projekt..."),
("file.importPlaceholder", "Placeholder...", "Platzhalter..."),
("file.importSolid", "Solid...", "Farbfläche..."),
@@ -6925,8 +6925,8 @@ const POLISH: &[(&str, &str, &str)] = &[
("file.import", "File...", "Plik..."),
("file.importMultiple", "Multiple Files...", "Wiele plików..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Projekt Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Projekt After Effects..."),
("file.exportAep", "After Effects Project...", "Projekt After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Projekt EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Element zastępczy..."),
("file.importSolid", "Solid...", "Plansza..."),
@@ -7563,8 +7563,8 @@ const CZECH: &[(&str, &str, &str)] = &[
("file.import", "File...", "Soubor..."),
("file.importMultiple", "Multiple Files...", "Více souborů..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Projekt Adobe Premiere Pro..."),
("file.importAep", "After Effects Project...", "Projekt After Effects..."),
("file.exportAep", "After Effects Project...", "Projekt After Effects..."),
("file.importAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."),
("file.exportAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."),
("file.importProject", "EffectCraft Project...", "Projekt EffectCraft..."),
("file.importPlaceholder", "Placeholder...", "Zástupný záznam..."),
("file.importSolid", "Solid...", "Plná vrstva..."),
@@ -8201,8 +8201,8 @@ const GEORGIAN: &[(&str, &str, &str)] = &[
("file.import", "File...", "ფაილი..."),
("file.importMultiple", "Multiple Files...", "რამდენიმე ფაილი..."),
("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-ს პროექტი..."),
("file.importAep", "After Effects Project...", "After Effects-ის პროექტი..."),
("file.exportAep", "After Effects Project...", "After Effects-ის პროექტი..."),
("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-ის პროექტი..."),
("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-ის პროექტი..."),
("file.importProject", "EffectCraft Project...", "EffectCraft-ის პროექტი..."),
("file.importPlaceholder", "Placeholder...", "ჩანაცვლების ობიექტი..."),
("file.importSolid", "Solid...", "სოლიდი..."),
@@ -9208,7 +9208,8 @@ mod tests {
assert!(!japanese(&app));
}
/// File ▸ Import ▸ After Effects Project… has its row in every menu catalog.
/// File ▸ Import / Export ▸ Adobe After Effects Project… has its row in every menu catalog,
/// under the label the menu draws (a row under an older label would fall back to English).
#[test]
fn the_aep_import_and_export_entries_are_in_every_menu_catalog() {
for (code, catalog) in [
@@ -9227,7 +9228,8 @@ mod tests {
("sv", SWEDISH),
] {
for id in ["file.importAep", "file.exportAep"] {
assert!(catalog.iter().any(|(command, _, _)| *command == id), "{code} has no {id} row");
let drawn = effectcraft_engine::menus::entries().into_iter().find(|(_, e)| e.command == id).map(|(_, e)| e.label.clone()).unwrap_or_default();
assert!(catalog.iter().any(|(command, english, _)| *command == id && *english == drawn), "{code} has no {id} row for {drawn:?}");
}
}
}
+32 -11
View File
@@ -242,7 +242,7 @@ const JAPANESE: &[(&str, &str)] = &[
("Brush Dynamics", "ブラシのダイナミクス"),
("Caching frames", "フレームをキャッシュ中"),
("Cancel", "キャンセル"),
("Import After Effects Project", "After Effectsプロジェクトを読み込み"),
("Import Adobe After Effects Project", "Adobe After Effectsプロジェクトを読み込み"),
("Some parts of this project cannot be imported exactly (left out or changed):", "このプロジェクトの一部は正確に読み込めません(省略または変更されます):"),
("Import Anyway", "このまま読み込み"),
("Casts Shadows", "シャドウを落とす"),
@@ -891,6 +891,7 @@ const JAPANESE: &[(&str, &str)] = &[
("Footage Frame Cache", "フッテージフレームキャッシュ"),
("Preview (RAM) Cache", "プレビュー(RAM)キャッシュ"),
("Reduce Cache Size When System Is Low on Memory", "システムのメモリが不足しているときにキャッシュサイズを減らす"),
("GPU Frames Rendered at Once", "一度にレンダリングするGPUフレーム数"),
("Video", "ビデオ"),
("Enable Video Preview Output", "ビデオプレビュー出力を有効にする"),
("Video Device", "ビデオデバイス"),
@@ -1382,7 +1383,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[
("Brush Dynamics", "画笔动态"),
("Caching frames", "正在缓存帧"),
("Cancel", "取消"),
("Import After Effects Project", "导入 After Effects 项目"),
("Import Adobe After Effects Project", "导入 Adobe After Effects 项目"),
("Some parts of this project cannot be imported exactly (left out or changed):", "此项目的某些部分无法准确导入(将被省略或更改):"),
("Import Anyway", "仍然导入"),
("Casts Shadows", "投射阴影"),
@@ -2023,6 +2024,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[
("Footage Frame Cache", "素材帧缓存"),
("Preview (RAM) Cache", "预览(内存)缓存"),
("Reduce Cache Size When System Is Low on Memory", "系统内存不足时减少缓存大小"),
("GPU Frames Rendered at Once", "GPU 同时渲染的帧数"),
("Video", "视频"),
("Enable Video Preview Output", "启用视频预览输出"),
("Video Device", "视频设备"),
@@ -2497,7 +2499,7 @@ const SWEDISH: &[(&str, &str)] = &[
("Brush Dynamics", "Penseldynamik"),
("Caching frames", "Cachelagrar bildrutor"),
("Cancel", "Avbryt"),
("Import After Effects Project", "Importera After Effects-projekt"),
("Import Adobe After Effects Project", "Importera Adobe After Effects-projekt"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Vissa delar av projektet kan inte importeras exakt (utelämnas eller ändras):",
@@ -3135,6 +3137,7 @@ const SWEDISH: &[(&str, &str)] = &[
("Footage Frame Cache", "Bildrutecache för material"),
("Preview (RAM) Cache", "Förhandsgranskningscache (RAM)"),
("Reduce Cache Size When System Is Low on Memory", "Minska cachestorleken när systemet har ont om minne"),
("GPU Frames Rendered at Once", "GPU-bildrutor som renderas samtidigt"),
("Video", "Video"),
("Enable Video Preview Output", "Aktivera videoutgång för förhandsgranskning"),
("Video Device", "Videoenhet"),
@@ -3185,6 +3188,14 @@ const SWEDISH: &[(&str, &str)] = &[
("A built-in preset has this name", "En inbyggd förinställning har det här namnet"),
("Cannot import dropped files: {}", "Kan inte importera släppta filer: {}"),
("Cannot open dropped project {}: {}", "Kan inte öppna det släppta projektet {}: {}"),
(
"Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…",
"Importerat material har en omärkt alfakanal: välj hur den ska tolkas i Arkiv ▸ Tolka material ▸ Huvud…",
),
(
"Skipped {} more file(s) while a dialog is open; import them again after it",
"Hoppade över {} fil(er) till medan en dialogruta är öppen; importera dem igen efteråt",
),
("Click in the Composition panel to sample text {}", "Klicka i panelen Komposition för att hämta text {}"),
("Click to sample text {}", "Klicka för att hämta text {}"),
("Comment of {}: {}", "Kommentar till {}: {}"),
@@ -3765,7 +3776,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[
("Brush Dynamics", "筆刷動態"),
("Caching frames", "正在快取幀"),
("Cancel", "取消"),
("Import After Effects Project", "匯入 After Effects 專案"),
("Import Adobe After Effects Project", "匯入 Adobe After Effects 專案"),
("Some parts of this project cannot be imported exactly (left out or changed):", "此專案的部分內容無法準確匯入(將被省略或變更):"),
("Import Anyway", "仍要匯入"),
("Casts Shadows", "投射陰影"),
@@ -4352,6 +4363,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[
("Footage Frame Cache", "素材影格快取"),
("Preview (RAM) Cache", "預覽(記憶體)快取"),
("Reduce Cache Size When System Is Low on Memory", "系統記憶體不足時減少快取大小"),
("GPU Frames Rendered at Once", "GPU 同時算圖的影格數"),
("Video", "視訊"),
("Enable Video Preview Output", "啟用視訊預覽輸出"),
("Video Device", "視訊裝置"),
@@ -4830,7 +4842,7 @@ const UKRAINIAN: &[(&str, &str)] = &[
("Brush Dynamics", "Динаміка пензля"),
("Caching frames", "Кешування кадрів"),
("Cancel", "Скасувати"),
("Import After Effects Project", "Імпортувати проєкт After Effects"),
("Import Adobe After Effects Project", "Імпортувати проєкт Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Деякі частини цього проєкту неможливо імпортувати точно (їх пропущено або змінено):",
@@ -5411,7 +5423,7 @@ const PORTUGUESE: &[(&str, &str)] = &[
("Brush Dynamics", "Dinâmica do pincel"),
("Caching frames", "Armazenando quadros em cache"),
("Cancel", "Cancelar"),
("Import After Effects Project", "Importar projeto do After Effects"),
("Import Adobe After Effects Project", "Importar projeto do Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Algumas partes deste projeto não podem ser importadas exatamente (omitidas ou alteradas):",
@@ -5917,6 +5929,7 @@ const PORTUGUESE: &[(&str, &str)] = &[
("Recent Projects Shown", "Projetos recentes exibidos"),
("Red", "Vermelho"),
("Reduce Cache Size When System Is Low on Memory", "Reduzir o tamanho do cache quando o sistema estiver com pouca memória"),
("GPU Frames Rendered at Once", "Quadros da GPU renderizados de uma vez"),
("Remove from favorites", "Remover dos favoritos"),
("Repair", "Reparo"),
("Report Missing Frames", "Relatar quadros ausentes"),
@@ -6183,7 +6196,7 @@ const POLISH: &[(&str, &str)] = &[
("Brush Dynamics", "Dynamika pędzla"),
("Caching frames", "Buforowanie klatek"),
("Cancel", "Anuluj"),
("Import After Effects Project", "Importuj projekt After Effects"),
("Import Adobe After Effects Project", "Importuj projekt Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Niektórych części tego projektu nie można zaimportować dokładnie (zostaną pominięte lub zmienione):",
@@ -6939,7 +6952,7 @@ const CZECH: &[(&str, &str)] = &[
("Brush Dynamics", "Dynamika štětce"),
("Caching frames", "Ukládání snímků do mezipaměti"),
("Cancel", "Zrušit"),
("Import After Effects Project", "Importovat projekt After Effects"),
("Import Adobe After Effects Project", "Importovat projekt Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Některé části tohoto projektu nelze importovat přesně (budou vynechány nebo změněny):",
@@ -7514,7 +7527,7 @@ const SPANISH: &[(&str, &str)] = &[
("Brush Dynamics", "Dinámica del pincel"),
("Caching frames", "Almacenando fotogramas en caché"),
("Cancel", "Cancelar"),
("Import After Effects Project", "Importar proyecto de After Effects"),
("Import Adobe After Effects Project", "Importar proyecto de Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Algunas partes de este proyecto no se pueden importar con exactitud (se omiten o cambian):",
@@ -8080,7 +8093,7 @@ const GEORGIAN: &[(&str, &str)] = &[
("Brush Dynamics", "ფუნჯის დინამიკა"),
("Caching frames", "კადრების ქეშირება"),
("Cancel", "გაუქმება"),
("Import After Effects Project", "After Effects-ის პროექტის იმპორტი"),
("Import Adobe After Effects Project", "Adobe After Effects-ის პროექტის იმპორტი"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"ამ პროექტის ზოგიერთი ნაწილის ზუსტად იმპორტი შეუძლებელია (გამოტოვებული ან შეცვლილი):",
@@ -9053,7 +9066,7 @@ const FRENCH: &[(&str, &str)] = &[
("Brush Dynamics", "Dynamique du pinceau"),
("Caching frames", "Mise en cache des images"),
("Cancel", "Annuler"),
("Import After Effects Project", "Importer un projet After Effects"),
("Import Adobe After Effects Project", "Importer un projet Adobe After Effects"),
(
"Some parts of this project cannot be imported exactly (left out or changed):",
"Certaines parties de ce projet ne peuvent pas être importées à l’identique (omises ou modifiées) :",
@@ -9796,6 +9809,14 @@ const FRENCH: &[(&str, &str)] = &[
),
("Only one project can be opened per drop; skipped {} more", "Un seul projet peut être ouvert par dépôt ; {} de plus ignoré(s)"),
("Cannot open dropped project {}: {}", "Impossible d’ouvrir le projet déposé {} : {}"),
(
"Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…",
"Le métrage importé a une couche alpha non étiquetée : choisissez son interprétation dans Fichier ▸ Interpréter le métrage ▸ Principal…",
),
(
"Skipped {} more file(s) while a dialog is open; import them again after it",
"{} fichier(s) de plus ignoré(s) pendant qu’une boîte de dialogue est ouverte ; importez-les ensuite",
),
("Cannot import dropped files: {}", "Impossible d’importer les fichiers déposés : {}"),
("A built-in preset has this name", "Un préréglage intégré porte ce nom"),
("Replaces your preset with this name", "Remplace votre préréglage portant ce nom"),
+2 -2
View File
@@ -27,8 +27,8 @@ pub(crate) use entries::{entry_checked, entry_enabled, entry_label, entry_shortc
pub use frontend::frontend;
#[cfg(test)]
use frontend::zoom_step;
pub(crate) use frontend::{dialog_answered, run_engine};
pub use invoke::{invoke, panel_command_id, reveal};
pub(crate) use frontend::{dialog_answered, import_aep_projects, run_engine};
pub use invoke::{invoke, panel_command_id, reveal, waits_on_dialog};
#[cfg(test)]
use shortcuts::modifiers_at_start;
pub use shortcuts::{Binding, bindings, chord_matches, forwardable_during_text_edit, handle_shortcuts, handle_tab_shortcut, parse_shortcut, shortcut_text};
+91 -10
View File
@@ -502,6 +502,15 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
return run_engine(app, ctx, "window.assignWorkspaceShortcut", q);
}
"file.interpretFootage" => {
// A background import finishing while another dialog waits (the After Effects losses
// prompt of the same drop) does not replace it: that dialog's work would be lost. The
// footage keeps the default interpretation; the note says where to change it.
if app.dialog.is_some() {
app.ui.status =
crate::i18n::tr("Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…")
.to_string();
return Ok(Value::Null);
}
// Import ▸ Interpret Unlabeled Alpha As: Ask User.
if let Some(i) = p.get("item").and_then(Value::as_u64) {
app.session.state.project_selection = vec![effectcraft_engine::project::ItemId(i)];
@@ -601,7 +610,8 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option<Result<Value, String>> {
enum Ask {
Folder,
Import,
/// Footage, or a file with one of these extensions.
Import(&'static [&'static str]),
OpenProject,
Save(std::borrow::Cow<'static, str>),
Open(&'static [&'static str]),
@@ -617,7 +627,9 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) ->
path.with_file_name(format!("{stem}{suffix}.{ext}")).to_string_lossy().into_owned().into()
};
let (key, ask) = match id {
"file.import" | "file.importMultiple" => ("paths", Ask::Import),
// Projects too, as After Effects' File… takes an .aep: they import as projects
// (`run_with_picked`, and `file.import` for EffectCraft projects).
"file.import" | "file.importMultiple" => ("paths", Ask::Import(&["aep", "aepx", "ecproj", "ecprojx"])),
"file.open" => ("path", Ask::OpenProject),
"file.save" if app.session.path.is_none() => ("path", Ask::Save(project_as("", "ecproj"))),
"file.saveAs" => ("path", Ask::Save(project_as("", "ecproj"))),
@@ -644,7 +656,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) ->
"effect.plugins.install" if params.get("bundle").is_some() => ("folder", Ask::Folder),
"effect.plugins.install" if params.get("folder").is_none() && params.get("path").is_none() => ("path", Ask::Open(&["wasm"])),
"effect.helpers.load" => ("path", Ask::Folder),
"file.replaceFootage" => ("path", Ask::Import),
"file.replaceFootage" => ("path", Ask::Import(&[])),
"file.relinkFootage" => ("folder", Ask::Folder),
"file.collectFiles" => ("folder", Ask::Save("Collected Files".into())),
// An open XML project gets ` copy` too: the copy never writes over the open file.
@@ -672,6 +684,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) ->
"eps", "psd", "psb", "pcraft", "tga", "hdr", "gltf", "glb", "obj", "stl", "fbx", "json", "csv", "tsv",
];
let footage = |more: &[&'static str]| effectcraft_project::VIDEO_EXTENSIONS.iter().chain(more).copied().collect::<Vec<_>>();
let importable = |more: &[&'static str]| footage(MEDIA).into_iter().chain(more.iter().copied()).collect::<Vec<_>>();
// A host whose blocking dialogs are unsafe inside this frame shows the dialog without
// blocking — macOS: the nested event loop re-entered winit's handler on a drag over the
// window and aborted the app (#511); Linux: the desktop portal's answer never gets pumped,
@@ -680,7 +693,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) ->
if let Some(spawn) = app.hooks.file_dialog_async.as_ref() {
let request = match ask {
Ask::Folder => crate::FileDialog::Folder,
Ask::Import => crate::FileDialog::Open { exts: footage(MEDIA).iter().map(|e| e.to_string()).collect(), many: key == "paths" },
Ask::Import(more) => crate::FileDialog::Open { exts: importable(more).iter().map(|e| e.to_string()).collect(), many: key == "paths" },
Ask::OpenProject => crate::FileDialog::OpenProject,
Ask::Open(exts) => crate::FileDialog::Open { exts: exts.iter().map(|e| e.to_string()).collect(), many: false },
Ask::OpenFootage(more) => crate::FileDialog::Open { exts: footage(more).iter().map(|e| e.to_string()).collect(), many: false },
@@ -702,9 +715,9 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) ->
let Some(f) = app.hooks.pick_folder.as_ref() else { return Some(Err("no folder dialog available (pass `folder`)".into())) };
f().map(Value::from)
}
Ask::Import => {
Ask::Import(more) => {
let Some(f) = app.hooks.pick_files.as_ref() else { return Some(Err("no file dialog available (pass `paths`)".into())) };
picked_value(key, f(&footage(MEDIA)))
picked_value(key, f(&importable(more)))
}
Ask::OpenProject => {
let Some(f) = app.hooks.pick_open_project.as_ref() else { return Some(Err("no file dialog available (pass `path`)".into())) };
@@ -758,7 +771,8 @@ pub(crate) fn show_result_errors(app: &mut EffectcraftApp, result: &Value) {
.map(|e| match (e.get("file").and_then(Value::as_str), e.get("error").and_then(Value::as_str)) {
(Some(file), Some(error)) => format!("{file}: {error}"),
(_, Some(error)) => error.to_string(),
_ => e.to_string(),
// A plain message (`file.import`'s errors) reads as itself, not as quoted JSON.
_ => e.as_str().map_or_else(|| e.to_string(), str::to_string),
})
.collect();
let mut msg = parts.iter().take(3).cloned().collect::<Vec<_>>().join("; ");
@@ -770,6 +784,14 @@ pub(crate) fn show_result_errors(app: &mut EffectcraftApp, result: &Value) {
/// Run command `id` with the path picked for it in parameter `key`.
fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value, v: Value) -> Result<Value, String> {
if matches!(id, "file.import" | "file.importMultiple")
&& let Some(picked) = v.as_array()
&& picked.iter().filter_map(Value::as_str).any(effectcraft_engine::commands::is_aep_path)
{
let (aep, rest): (Vec<String>, Vec<String>) =
picked.iter().filter_map(Value::as_str).map(str::to_string).partition(|p| effectcraft_engine::commands::is_aep_path(p));
return import_picked_with_aep(app, id, key, params, aep, rest);
}
let mut p = params.as_object().cloned().unwrap_or_default();
p.insert(key.to_string(), v);
// Picked files import in the background, with the Importing card showing progress (#270).
@@ -777,10 +799,13 @@ fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value
p.insert("background".into(), Value::Bool(true));
}
// Photoshop files ask how to import them first; numbered stills whether to import them as an
// image sequence.
// image sequence. Not while the losses prompt of an After Effects project picked with them
// shows: one dialog at a time, so they import with the defaults.
let picked = Value::Object(p.clone());
if id == "file.import" && crate::panels::dialogs::open_form(app, id, &picked)
|| matches!(id, "file.import" | "file.importMultiple") && crate::panels::forms::open_import_sequence(app, &picked)
let asking = crate::panels::aep_losses::asking(app);
if !asking
&& (id == "file.import" && crate::panels::dialogs::open_form(app, id, &picked)
|| matches!(id, "file.import" | "file.importMultiple") && crate::panels::forms::open_import_sequence(app, &picked))
{
return Ok(json!({"dialog": id}));
}
@@ -804,6 +829,62 @@ fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value
r
}
/// Import After Effects projects dropped or picked together, as File ▸ Import ▸ Adobe After
/// Effects Project… does each: one that would lose parts asks first (the losses prompt), and the
/// ones after it wait in its queue until it is answered (`panels::aep_losses`). Returns the errors
/// of those that could not be imported; each names its file.
pub(crate) fn import_aep_projects(app: &mut EffectcraftApp, paths: Vec<String>) -> Vec<String> {
let mut errors = Vec::new();
// An unanswered prompt's projects go first.
let mut paths = crate::panels::aep_losses::take_unanswered(app).into_iter().chain(paths);
while let Some(path) = paths.next() {
if crate::panels::aep_losses::asking(app) {
let queue = &mut app.dialog_state.aep_losses.queue;
queue.push(path);
queue.extend(paths);
break;
}
if let Err(e) = run_with_picked(app, "file.importAep", "path", &json!({}), json!(path)) {
errors.push(e);
}
}
errors
}
/// File ▸ Import ▸ File… with After Effects projects picked: they import first, as projects
/// ([`import_aep_projects`]), then the other files as footage. Their errors lead the result's
/// `errors` and the status line (callers of the blocking dialog drop the result), or the message
/// when the footage import failed.
fn import_picked_with_aep(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value, aep: Vec<String>, rest: Vec<String>) -> Result<Value, String> {
let errors = import_aep_projects(app, aep);
let r = if !rest.is_empty() {
run_with_picked(app, id, key, params, json!(rest))
} else if crate::panels::aep_losses::asking(app) {
Ok(json!({"dialog": "aepLosses"}))
} else {
Ok(json!({}))
};
if errors.is_empty() {
return r;
}
match r {
Ok(mut result) => {
if !result.is_object() {
result = json!({"result": result});
}
let own = result.get("errors").and_then(Value::as_array).cloned().unwrap_or_default();
result["errors"] = Value::Array(errors.into_iter().map(Value::from).chain(own).collect());
show_result_errors(app, &result);
Ok(result)
}
Err(e) => {
let message = format!("{}; {e}", errors.join("; "));
app.ui.status = message.clone();
Err(message)
}
}
}
/// View ▸ Zoom In / Zoom Out: the next standard magnification (as a share of screen pixels, so
/// 100 % is one comp pixel per screen pixel at any display scaling) above or below `cur`, which is
/// in points per comp pixel like the result (#882).
+6
View File
@@ -183,6 +183,12 @@ fn only_comp(p: &Value) -> Option<effectcraft_engine::project::ItemId> {
m.get("comp")?.as_u64().map(effectcraft_engine::project::ItemId)
}
/// Whether [`invoke`]'s result says the command waits on a dialog (a guard or a form deferred it
/// until the user answers, `{"dialog": …}`).
pub fn waits_on_dialog(result: &Value) -> bool {
result.get("dialog").is_some()
}
/// Execute a UI or engine command by id.
pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
crate::control::check_script_render(app, id)?;
+123
View File
@@ -733,3 +733,126 @@ fn effect_plugin_install_dialog_router() {
assert!(r.is_err(), "{r:?}");
assert!(pending.lock().unwrap().is_empty(), "no dialog for an inline folder");
}
/// File ▸ Import ▸ File… in the non-blocking dialog (macOS, Linux) offers After Effects projects
/// next to footage, and a picked one imports as File ▸ Import ▸ Adobe After Effects Project… does: its
/// folder, its composition open.
#[test]
fn import_file_dialog_offers_and_imports_after_effects_projects() {
use std::sync::{Arc, Mutex};
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let ctx = egui::Context::default();
let dir = std::env::temp_dir().join(format!("ec-import-aep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let aep = dir.join("Intro.aep").to_string_lossy().to_string();
let mut source = effectcraft_engine::Session::default();
source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap();
source.execute("file.exportAep", json!({"path": aep, "allowLosses": true})).unwrap();
let pending: Arc<Mutex<Vec<crate::FileDialog>>> = Arc::default();
let queue = pending.clone();
app.hooks.file_dialog_async = Some(Box::new(move |dialog, _done| queue.lock().unwrap().push(dialog)));
invoke(&mut app, &ctx, "file.import", json!({})).unwrap();
let Some(crate::FileDialog::Open { exts, many }) = pending.lock().unwrap().pop() else { panic!("an Open dialog was shown") };
assert!(many && exts.iter().any(|e| e == "aep") && exts.iter().any(|e| e == "aepx"), "{exts:?}");
assert!(exts.iter().any(|e| e == "png"), "footage too");
super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths: vec![aep] });
let comp = app.session.project.item(app.session.active_comp_id().expect("a composition is open")).unwrap();
assert_eq!(comp.name, "Intro");
assert_eq!(app.session.project.item(comp.parent.expect("in a folder")).unwrap().name, "Intro.aep");
let _ = std::fs::remove_dir_all(&dir);
}
/// Footage with unlabeled alpha asks how to interpret it (Interpret Unlabeled Alpha As: Ask
/// User), but not over a dialog that already waits (the After Effects losses prompt of the same
/// drop, which a background import can finish behind): that dialog stays, and a note says where
/// to choose the interpretation.
#[test]
fn the_unlabeled_alpha_question_does_not_replace_an_open_dialog() {
use effectcraft_engine::project::{Footage, FootageKind, ItemKind};
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let ctx = egui::Context::default();
let footage = Footage { path: "/m/matte.tga".into(), kind: FootageKind::Still, width: 4, height: 4, has_video: true, ..Default::default() };
let item = std::sync::Arc::make_mut(&mut app.session.project).add_item("matte.tga", Default::default(), None, ItemKind::Footage(footage)).0;
app.dialog = Some(crate::Dialog::AepLosses);
super::frontend(&mut app, &ctx, "file.interpretFootage", json!({"item": item, "reason": "unlabeledAlpha"})).unwrap();
assert_eq!(app.dialog, Some(crate::Dialog::AepLosses), "the waiting dialog stays");
assert!(app.ui.status.contains("Interpret Footage"), "{}", app.ui.status);
assert!(app.session.state.project_selection.is_empty(), "the selection is left alone");
app.dialog = None;
super::frontend(&mut app, &ctx, "file.interpretFootage", json!({"item": item, "reason": "unlabeledAlpha"})).unwrap();
assert_eq!(app.dialog, Some(crate::Dialog::Form), "with no dialog open it asks");
}
/// File ▸ Import ▸ File… and Multiple Files… offer projects next to footage; File ▸ Replace
/// Footage offers footage only.
#[test]
fn only_the_import_dialogs_offer_projects() {
use std::sync::{Arc, Mutex};
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let pending: Arc<Mutex<Vec<crate::FileDialog>>> = Arc::default();
let queue = pending.clone();
app.hooks.file_dialog_async = Some(Box::new(move |dialog, _done| queue.lock().unwrap().push(dialog)));
let projects = ["aep", "aepx", "ecproj", "ecprojx"];
for (id, offered) in [("file.import", true), ("file.importMultiple", true), ("file.replaceFootage", false)] {
assert!(super::frontend::file_dialog(&mut app, id, &json!({})).is_some(), "{id} asks for a file");
let Some(crate::FileDialog::Open { exts, .. }) = pending.lock().unwrap().pop() else { panic!("{id}: an Open dialog") };
assert!(exts.iter().any(|e| e == "png"), "{id} offers footage");
assert_eq!(projects.iter().all(|p| exts.iter().any(|e| e == p)), offered, "{id}: {exts:?}");
assert!(offered || !projects.iter().any(|p| exts.iter().any(|e| e == p)), "{id} offers no project: {exts:?}");
}
}
/// Picked After Effects projects that cannot be imported keep their errors next to the other
/// files': a broken `.aep` and a broken `.ecproj` are both named in the status line.
#[test]
fn picked_projects_report_every_error() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let dir = std::env::temp_dir().join(format!("ec-picked-errors-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let aep = dir.join("broken.aep");
let ecproj = dir.join("broken.ecproj");
std::fs::write(&aep, b"not an aep").unwrap();
std::fs::write(&ecproj, b"not a project").unwrap();
let paths = vec![aep.to_string_lossy().to_string(), ecproj.to_string_lossy().to_string()];
super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths: paths.clone() });
let status = &app.ui.status;
assert!(status.contains("broken.aep") && status.contains("broken.ecproj"), "{status}");
assert!(!status.contains('"'), "messages, not quoted JSON: {status}");
assert!(app.dialog.is_none() && app.session.project.items.is_empty());
// The blocking dialog (Windows), whose callers drop the result: the status line still has both.
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let ctx = egui::Context::default();
app.hooks.pick_files = Some(Box::new(move |_: &[&str]| paths.clone()));
let _ = invoke(&mut app, &ctx, "file.import", json!({}));
let status = &app.ui.status;
assert!(status.contains("broken.aep") && status.contains("broken.ecproj"), "{status}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A picked After Effects project with parts that cannot come over asks first; one picked with
/// it waits for the answer.
#[test]
fn a_picked_project_with_losses_asks_and_the_next_waits() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let dir = std::env::temp_dir().join(format!("ec-picked-losses-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let lossy = dir.join("Synthetic.aep");
std::fs::copy(concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep"), &lossy).unwrap();
let intro = dir.join("Intro.aep").to_string_lossy().to_string();
let mut source = effectcraft_engine::Session::default();
source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap();
source.execute("file.exportAep", json!({"path": intro, "allowLosses": true})).unwrap();
let paths = vec![lossy.to_string_lossy().to_string(), intro.clone()];
super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths });
assert_eq!(app.dialog, Some(crate::Dialog::AepLosses));
assert_eq!(app.dialog_state.aep_losses.queue, [intro]);
assert!(app.session.project.items.is_empty(), "nothing imported before the answer");
let _ = std::fs::remove_dir_all(&dir);
}
+96 -7
View File
@@ -1,8 +1,10 @@
//! File ▸ Import / Export ▸ After Effects Project… from the menu: what the conversion cannot
//! File ▸ Import / Export ▸ Adobe After Effects Project… from the menu: what the conversion cannot
//! carry over is shown first (the command's dry run), and the user chooses to cancel or to go on
//! with those losses (`allowLosses`). Cancel changes nothing; a conversion without losses runs at
//! once. Agents running `file.importAep` / `file.exportAep` with `engine.execute` are never asked:
//! they pass `allowLosses` or get the refusal.
//! they pass `allowLosses` or get the refusal. After Effects projects dropped or picked together
//! (File ▸ Import ▸ File…) are asked about one at a time: the others wait in the queue until the
//! prompt is answered, and Cancel skips only the one it asks about.
//!
//! Automation ids: `dialog.aepLosses.continue`, `dialog.aepLosses.cancel`.
@@ -17,11 +19,32 @@ use crate::{Dialog, EffectcraftApp};
#[derive(Clone, Debug, Default)]
pub struct Pending {
pub command: Option<(String, Value, Value)>,
/// After Effects projects dropped or picked with it, imported once it is answered.
pub queue: Vec<String>,
}
/// How many detail lines of a loss category the prompt shows.
const DETAILS: usize = 3;
/// Whether the prompt is showing with a conversion waiting on it.
pub fn asking(app: &EffectcraftApp) -> bool {
app.dialog == Some(Dialog::AepLosses) && app.dialog_state.aep_losses.command.is_some()
}
/// The projects of a prompt another dialog replaced (Save changes? on quitting) before it was
/// answered: its import's file, then its queue. Taken, so they are tried again instead of waiting
/// for an answer that never comes (a replaced export is dropped: it wrote nothing).
pub fn take_unanswered(app: &mut EffectcraftApp) -> Vec<String> {
if app.dialog == Some(Dialog::AepLosses) {
return Vec::new();
}
let losses = &mut app.dialog_state.aep_losses;
let Some((id, params, _)) = losses.command.take() else { return std::mem::take(&mut losses.queue) };
let mut paths: Vec<String> = params.get("path").and_then(Value::as_str).filter(|_| id == "file.importAep").map(str::to_string).into_iter().collect();
paths.append(&mut losses.queue);
paths
}
/// Whether command `id` with `params` needs the prompt first. When it does, the prompt opens
/// holding the command and the caller must not run it. `Err` when the dry run fails (the file
/// cannot be read or is not a project), with the message.
@@ -39,6 +62,9 @@ pub fn guard(app: &mut EffectcraftApp, id: &str, params: &Value) -> Result<bool,
if report["losses"].as_array().is_none_or(|l| l.is_empty()) {
return Ok(false);
}
// An unanswered prompt's projects wait behind this one.
let unanswered = take_unanswered(app);
app.dialog_state.aep_losses.queue.extend(unanswered);
app.dialog_state.aep_losses.command = Some((id.to_string(), params.clone(), report));
app.dialog = Some(Dialog::AepLosses);
Ok(true)
@@ -64,13 +90,15 @@ pub fn lines(report: &Value) -> Vec<(String, Vec<String>)> {
.collect()
}
/// Draw the prompt for the conversion waiting on it: its losses by category, Cancel and Import /
/// Export Anyway (Escape cancels).
pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
let Some((id, _, report)) = app.dialog_state.aep_losses.command.clone() else {
app.dialog = None;
return;
};
let import = id == "file.importAep";
let title = if import { "Import After Effects Project" } else { "Export After Effects Project" };
let title = if import { "Import Adobe After Effects Project" } else { "Export Adobe After Effects Project" };
let mut answer: Option<bool> = None;
super::dialogs::modal(ctx, title, vec2(520.0, 340.0), t, |ui| {
let text = if import {
@@ -114,11 +142,25 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
/// The prompt's answer: Cancel (`false`) drops the conversion, Import / Export Anyway runs it
/// with `allowLosses`.
fn finish(app: &mut EffectcraftApp, ctx: &egui::Context, go: bool) {
app.dialog = None;
let Some((id, params, _)) = app.dialog_state.aep_losses.command.take() else { return };
let Some((id, params, _)) = app.dialog_state.aep_losses.command.take() else {
app.dialog = None;
return;
};
let mut errors = Vec::new();
if go {
// An error is already in the status bar.
let _ = crate::menus::run_engine(app, ctx, &id, with_losses(&params));
// The prompt stays up while the conversion runs: a question queued by a footage import
// that finished meanwhile (Interpret Footage for unlabeled alpha, drained by `run_engine`)
// leaves a note instead of opening a dialog the next queued prompt would replace.
if let Err(e) = crate::menus::run_engine(app, ctx, &id, with_losses(&params)) {
errors.push(e);
}
}
app.dialog = None;
// The projects dropped or picked with this one: Cancel skips only this one.
let queue = std::mem::take(&mut app.dialog_state.aep_losses.queue);
errors.extend(crate::menus::import_aep_projects(app, queue));
if !errors.is_empty() {
app.ui.status = errors.join("; ");
}
}
@@ -142,6 +184,53 @@ mod tests {
assert!(lines(&json!({})).is_empty());
}
/// Projects dropped or picked together wait only behind a prompt that shows; with none
/// showing they are tried at once (a missing file is an error naming it).
#[test]
fn projects_wait_only_behind_a_showing_prompt() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/a/One.aep"}), json!({})));
app.dialog = Some(Dialog::AepLosses);
assert!(crate::menus::import_aep_projects(&mut app, vec!["/no/such/Two.aep".into(), "/no/such/Three.aep".into()]).is_empty());
assert_eq!(app.dialog_state.aep_losses.queue, ["/no/such/Two.aep", "/no/such/Three.aep"]);
// Cancel: the queued ones are tried next (both missing here, so both are reported).
let ctx = egui::Context::default();
finish(&mut app, &ctx, false);
assert!(app.dialog_state.aep_losses.queue.is_empty());
assert!(app.ui.status.contains("Two.aep") && app.ui.status.contains("Three.aep"), "{}", app.ui.status);
}
/// A prompt another dialog replaced (Save changes? on quitting) holds nothing back: the next
/// import tries its project and queue first, then the new one.
#[test]
fn a_replaced_prompt_holds_nothing_back() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/no/such/One.aep"}), json!({})));
app.dialog_state.aep_losses.queue = vec!["/no/such/Two.aep".into()];
app.dialog = Some(Dialog::UnsavedChanges);
let errors = crate::menus::import_aep_projects(&mut app, vec!["/no/such/Three.aep".into()]);
let named = |n: &str| errors.iter().position(|e| e.contains(n));
assert_eq!((named("One.aep"), named("Two.aep"), named("Three.aep")), (Some(0), Some(1), Some(2)), "{errors:?}");
assert!(app.dialog_state.aep_losses.command.is_none() && app.dialog_state.aep_losses.queue.is_empty());
assert_eq!(app.dialog, Some(Dialog::UnsavedChanges), "the other dialog stays");
}
/// A new prompt keeps an unanswered one's projects: they wait behind it.
#[test]
fn a_new_prompt_keeps_an_unanswered_ones_projects() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/a/One.aep"}), json!({})));
app.dialog_state.aep_losses.queue = vec!["/a/Two.aep".into()];
app.dialog = Some(Dialog::UnsavedChanges);
// Rec. 2020 has no After Effects counterpart: exporting asks.
std::sync::Arc::make_mut(&mut app.session.project).settings.working_space = Some(effectcraft_engine::color::ColorSpace::Rec2020);
let dir = TempDir::new();
assert_eq!(guard(&mut app, "file.exportAep", &json!({"path": dir.0.join("out.aep").to_string_lossy()})), Ok(true));
assert_eq!(app.dialog, Some(Dialog::AepLosses));
assert_eq!(app.dialog_state.aep_losses.queue, ["/a/One.aep", "/a/Two.aep"]);
}
#[test]
fn agents_and_consented_calls_are_not_asked() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
+3 -1
View File
@@ -475,13 +475,15 @@ fn home_page(
}
}
/// Run the commands the Home screen's buttons and recent files asked for, and leave Home once one
/// opened something (not while a dialog it opened waits, nor when only the list changed).
fn run_actions(app: &mut EffectcraftApp, ctx: &egui::Context, actions: Vec<(&str, serde_json::Value)>) {
for (id, params) in actions {
let before = (app.session.path.clone(), app.session.revision);
match crate::menus::invoke(app, ctx, id, params) {
Err(e) => app.ui.status = e,
// Asking to save the open project first: Home stays until it is answered.
Ok(r) if r.get("dialog").is_some() => {}
Ok(r) if crate::menus::waits_on_dialog(&r) => {}
// Leave Home unless only the list changed or a file dialog was cancelled.
Ok(_) => {
let cancelled = matches!(id, "file.open" | "file.import") && (app.session.path.clone(), app.session.revision) == before;
+11 -81
View File
@@ -22,7 +22,9 @@ use crate::icons::{self, Icon};
use crate::theme::Tokens;
use crate::{EffectcraftApp, widgets};
#[derive(Clone, Debug)]
mod marquee;
#[derive(Clone, Debug, PartialEq)]
enum RowKind {
Layer,
Group {
@@ -328,7 +330,7 @@ fn focus_expression(ctx: &egui::Context, prop: u64, select: [usize; 2]) {
ctx.data_mut(|d| d.insert_temp(expr_focus_id(), ExprFocus { prop, select, pass }));
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
struct Row {
layer: LayerId,
depth: usize,
@@ -1969,8 +1971,6 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
g.max_scroll = (total_h - g.rows_rect.height() + g.rh).max(0.0);
g.rows = rows;
reveal_scroll(app, g);
wheel_zoom_scroll(app, ui, g);
pick_whip_edge_scroll(app, g);
let snap_project = app.session.project.clone();
let snap_expr = app.session.expr.clone();
let ectx = EvalCtx {
@@ -1986,6 +1986,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
};
let mut rw = begin_rows(app, ui, tl);
let g = &mut rw;
marquee::prepare(app, ui, g);
wheel_zoom_scroll(app, ui, &g.tl);
pick_whip_edge_scroll(app, &g.tl);
marquee::scroll(app, g);
draw_rows(app, ui, g, &ectx);
ui.set_clip_rect(g.full_clip);
background_menus(app, ui, g);
@@ -1997,7 +2001,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
group_reorder_drag(ui, g);
quick_offset_follow(g);
key_drag_follow(app, g);
box_select(app, ui, g);
marquee::finish(app, ui, g);
current_time_indicator(app, g);
let footer = footer_bar(app, ui, g);
footer_zoom(app, ui, g, footer);
@@ -4460,7 +4464,7 @@ fn background_menus(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw) {
}
}
/// Click on the empty outline (its drag is the Timeline's one box, [`box_select`]).
/// Click on the empty outline (its drag is the Timeline's one box, [`marquee`]).
fn outline_marquee(ui: &mut egui::Ui, g: &mut Rw) {
let actions = &mut g.tl.actions;
let outline_empty = g.outline_empty.clone();
@@ -4578,7 +4582,7 @@ fn alt_scale_and_graph(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw,
// below the last row) deselects the layers and their keys and properties, as in After
}
/// Click on empty track area (its drag is the Timeline's one box, [`box_select`]).
/// Click on empty track area (its drag is the Timeline's one box, [`marquee`]).
fn graph_empty_area(ui: &mut egui::Ui, g: &mut Rw) {
let actions = &mut g.tl.actions;
let empty = g.empty.clone();
@@ -4797,80 +4801,6 @@ fn keys_in_box(comp: &Comp, rows: &[Row], tm: &TMap, top: f32, rh: f32, br: Rect
keys
}
/// What a box over the Timeline selects: the layer rows it crosses (locked layers stay out) and
/// the keys whose markers it touches. A box that reaches the outline (started on either side)
/// selects the rows it crosses; one kept inside the time graph selects keys only, as before.
/// Keys exist only in the time graph, so the box is cut at its left edge (`graph_x0`; a key
/// scrolled out under the outline isn't touched).
/// `graph_on`: the Graph Editor shows no key markers here.
#[allow(clippy::too_many_arguments)]
fn box_targets(
comp: &Comp,
rows: &[Row],
hit_rows: &[(Rect, Row)],
tm: &TMap,
top: f32,
rh: f32,
graph_x0: f32,
graph_on: bool,
br: Rect,
) -> (Vec<u64>, Vec<effectcraft_engine::KeyRef>) {
let layers = hit_rows
.iter()
.filter(|(r, row)| br.min.x < graph_x0 && matches!(row.kind, RowKind::Layer) && r.intersects(br))
.filter(|(_, row)| comp.layer(row.layer).is_some_and(|l| !l.switches.locked))
.map(|(_, row)| row.layer.0)
.collect();
let in_graph = Rect::from_min_max(pos2(br.min.x.max(graph_x0), br.min.y), pos2(br.max.x, br.max.y));
let keys = if graph_on || in_graph.width() < 0.0 { vec![] } else { keys_in_box(comp, rows, tm, top, rh, in_graph) };
(layers, keys)
}
/// The Timeline's one selection box, from the empty outline or the empty time graph: it spans both
/// columns as it is dragged (the pointer may cross the boundary either way) and on release selects
/// the layer rows it crosses and the keys inside it, as in After Effects.
fn box_select(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw) {
let t = g.tl.t;
let tm = g.tl.tm;
let rows_rect = g.tl.rows_rect;
let rh = g.tl.rh;
let graph_x0 = g.tl.graph_x0;
let graph_on = g.graph_on;
let ctx = g.tl.ctx.clone();
let comp = g.tl.comp.clone();
let rows = g.tl.rows.clone();
let origin = [&g.outline_empty, &g.empty].into_iter().find(|r| r.dragged() || r.drag_stopped()).cloned();
let Some(origin) = origin else { return };
let box_of = |o: &egui::Response| {
let start = ctx.input(|i| i.pointer.press_origin());
start.zip(o.interact_pointer_pos().or_else(|| ctx.input(|i| i.pointer.latest_pos()))).map(|(a, b)| Rect::from_two_pos(a, b).intersect(rows_rect))
};
let id = egui::Id::unique("tl-box");
if origin.dragged()
&& let Some(br) = box_of(&origin)
{
let bp = g.tl.p.with_clip_rect(rows_rect);
bp.rect_filled(br, 0.0, t.accent.gamma_multiply(0.12));
bp.rect_stroke(br, 0.0, Stroke::new(1.0, t.accent), StrokeKind::Inside);
ctx.data_mut(|d| d.insert_temp(id, br));
}
if origin.drag_stopped()
&& let Some(br) = ctx.data(|d| d.get_temp::<Rect>(id))
{
ctx.data_mut(|d| d.remove::<Rect>(id));
let (layers, touched) = box_targets(&comp, &rows, &g.hit_rows, &tm, rows_rect.min.y - app.ui.timeline.scroll_y, rh, graph_x0, graph_on, br);
let mods = ui.input(|i| i.modifiers);
let add = mods.shift || mods.command;
let actions = &mut g.tl.actions;
// A box that catches nothing leaves nothing selected (layers included), unless it adds.
actions.push(("layer.select".into(), json!({"layers": layers, "add": add})));
if !touched.is_empty() {
let keys: Vec<serde_json::Value> = touched.iter().map(|k| json!({"layer": k.layer.0, "prop": k.prop, "time": k.time.seconds()})).collect();
actions.push(("keys.select".into(), json!({"keys": keys, "add": true, "selectProperties": true})));
}
}
}
fn current_time_indicator(app: &mut EffectcraftApp, g: &Rw) {
let t = g.tl.t;
let time = g.tl.time;
@@ -0,0 +1,192 @@
//! A marquee owns content coordinates; clipping belongs only to its painter.
use std::sync::Arc;
use effectcraft_engine::project::Comp;
use egui::{Id, Pos2, Rect, Stroke, StrokeKind, pos2};
use serde_json::json;
use super::{Row, RowKind, Rw, TMap, edge_scroll_speed, keys_in_box, row_height};
use crate::EffectcraftApp;
#[derive(Clone)]
struct Marquee {
owner: Id,
comp: Arc<Comp>,
rows: Arc<Vec<Row>>,
graph_on: bool,
layout: (Rect, f32, f32),
anchor: Anchor,
dragging: bool,
}
/// The pressed corner in content coordinates: a time (or an outline x) and a scroll-free y.
#[derive(Clone, Copy)]
pub(super) struct Anchor {
time: f64,
y: f32,
outline_x: Option<f32>,
}
/// The timeline layout a marquee is projected through.
#[derive(Clone, Copy)]
pub(super) struct View {
pub tm: TMap,
/// Screen y of the first row at scroll 0.
pub top: f32,
pub graph_x0: f32,
pub graph_on: bool,
}
impl View {
fn of(g: &Rw) -> Self {
Self { tm: g.tl.tm, top: g.tl.rows_rect.min.y, graph_x0: g.tl.graph_x0, graph_on: g.graph_on }
}
}
fn id() -> Id {
Id::unique("tl-marquee")
}
/// Capture on press, before wheel handling, even if the drag threshold is not crossed yet.
pub(super) fn prepare(app: &EffectcraftApp, ui: &egui::Ui, g: &Rw) {
let ctx = &g.tl.ctx;
let (pressed, down, released, escape, focused) =
ctx.input(|i| (i.pointer.primary_pressed(), i.pointer.primary_down(), i.pointer.primary_released(), i.key_pressed(egui::Key::Escape), i.focused));
if let Some(mut drag) = ctx.data(|d| d.get_temp::<Marquee>(id())) {
let owned = ctx.dragged_id() == Some(drag.owner);
let dropped = released && ctx.drag_stopped_id() == Some(drag.owner);
let valid = ui.is_enabled()
&& focused
&& !escape
&& Arc::ptr_eq(&drag.comp, &g.tl.comp)
&& *drag.rows == *g.tl.rows
&& drag.graph_on == g.graph_on
&& drag.layout == (g.tl.rect, g.tl.rh, g.tl.graph_x0)
&& (dropped || (down && (owned || (!drag.dragging && ctx.dragged_id().is_none()))));
drag.dragging |= owned;
ctx.data_mut(|d| {
if valid {
d.insert_temp(id(), drag);
} else {
d.remove::<Marquee>(id());
}
});
}
if pressed && ui.is_enabled() && focused && !escape {
let owner = [&g.outline_empty, &g.empty].into_iter().find(|r| r.is_pointer_button_down_on());
if let Some(owner) = owner
&& let Some(p) = ctx.input(|i| i.pointer.press_origin())
{
ctx.data_mut(|d| {
d.insert_temp(
id(),
Marquee {
owner: owner.id,
comp: g.tl.comp.clone(),
rows: g.tl.rows.clone(),
graph_on: g.graph_on,
layout: (g.tl.rect, g.tl.rh, g.tl.graph_x0),
anchor: Anchor::press(&View::of(g), app.ui.timeline.scroll_y, p),
dragging: false,
},
);
});
}
}
}
/// Only the marquee owner scrolls, with the same bounded edge speed as a pick whip.
pub(super) fn scroll(app: &mut EffectcraftApp, g: &Rw) {
let ctx = &g.tl.ctx;
if let Some(drag) = ctx.data(|d| d.get_temp::<Marquee>(id()))
&& ctx.dragged_id() == Some(drag.owner)
&& ctx.input(|i| i.pointer.primary_down())
&& let Some(p) = ctx.input(|i| i.pointer.latest_pos())
{
let speed = edge_scroll_speed(g.tl.rows_rect, p.y, g.tl.rh);
let old = app.ui.timeline.scroll_y;
app.ui.timeline.scroll_y = (old + speed * ctx.input(|i| i.stable_dt).min(0.1)).clamp(0.0, g.tl.max_scroll);
if app.ui.timeline.scroll_y != old {
ctx.request_repaint();
}
}
}
pub(super) struct SelectionBox {
pub keys: Rect,
pub outline: bool,
}
impl Anchor {
/// Anchor a press at screen point `p` with the rows scrolled by `scroll`.
pub(super) fn press(v: &View, scroll: f32, p: Pos2) -> Self {
Self { time: v.tm.t(p.x.max(v.graph_x0)), y: p.y - v.top + scroll, outline_x: (p.x < v.graph_x0).then_some(p.x) }
}
/// Reproject the content anchor without clipping the logical selection bounds.
pub(super) fn selection(&self, v: &View, scroll: f32, pointer: Pos2) -> (Rect, SelectionBox) {
let tm = v.tm;
let anchor = pos2(self.outline_x.unwrap_or_else(|| tm.x(self.time)), v.top + self.y - scroll);
let paint = Rect::from_two_pos(anchor, pointer);
let graph = self.outline_x.is_none() || pointer.x >= v.graph_x0;
let keys =
if graph && !v.graph_on { Rect::from_two_pos(pos2(tm.x(self.time), anchor.y), pos2(pointer.x.max(v.graph_x0), pointer.y)) } else { Rect::NOTHING };
(paint, SelectionBox { keys, outline: self.outline_x.is_some() || pointer.x < v.graph_x0 })
}
}
/// Query all logical rows; the renderer's visible hit rows are not selection data.
pub(super) fn targets(
comp: &Comp,
rows: &[Row],
tm: &TMap,
top: f32,
rh: f32,
paint: Rect,
selection: &SelectionBox,
) -> (Vec<u64>, Vec<effectcraft_engine::KeyRef>) {
let mut layers = vec![];
let mut y = top;
for row in rows {
let bottom = y + row_height(row, rh);
if selection.outline
&& matches!(row.kind, RowKind::Layer)
&& y <= paint.max.y
&& bottom >= paint.min.y
&& comp.layer(row.layer).is_some_and(|l| !l.switches.locked)
{
layers.push(row.layer.0);
}
y = bottom;
}
(layers, keys_in_box(comp, rows, tm, top, rh, selection.keys))
}
/// Commit an owned primary release; otherwise paint without changing selection.
pub(super) fn finish(app: &EffectcraftApp, ui: &egui::Ui, g: &mut Rw) {
let ctx = &g.tl.ctx;
let Some(drag) = ctx.data(|d| d.get_temp::<Marquee>(id())) else { return };
let released = ctx.input(|i| i.pointer.primary_released());
let dropped = released && ctx.drag_stopped_id() == Some(drag.owner);
if ctx.dragged_id() != Some(drag.owner) && !dropped {
return;
}
let Some(pointer) = ctx.input(|i| i.pointer.latest_pos()) else { return };
let (paint, selection) = drag.anchor.selection(&View::of(g), app.ui.timeline.scroll_y, pointer);
if dropped {
ctx.data_mut(|d| d.remove::<Marquee>(id()));
let (layers, keys) = targets(&g.tl.comp, &g.tl.rows, &g.tl.tm, g.tl.rows_rect.min.y - app.ui.timeline.scroll_y, g.tl.rh, paint, &selection);
let add = ui.input(|i| i.modifiers.shift || i.modifiers.command);
g.tl.actions.push(("layer.select".into(), json!({"layers": layers, "add": add})));
if !keys.is_empty() {
let keys: Vec<_> = keys.iter().map(|k| json!({"layer": k.layer.0, "prop": k.prop, "time": k.time.seconds()})).collect();
g.tl.actions.push(("keys.select".into(), json!({"keys": keys, "add": true, "selectProperties": true})));
}
} else {
let clip = if selection.outline { g.tl.rows_rect } else { g.right_clip };
let painter = g.tl.p.with_clip_rect(clip);
painter.rect_filled(paint, 0.0, g.tl.t.accent.gamma_multiply(0.12));
painter.rect_stroke(paint, 0.0, Stroke::new(1.0, g.tl.t.accent), StrokeKind::Inside);
}
}
+17 -9
View File
@@ -959,25 +959,33 @@ fn a_box_over_both_columns_selects_the_rows_it_crosses_and_the_keys_inside() {
rows.push(Row { layer: LayerId(*id), depth: 1, kind: RowKind::Prop { uid: uid(*id) } });
}
// Rows are 20 pt and 400 pt wide: the outline is left of x 200, a key at 0 s sits at x 205.
let hit_rows: Vec<(Rect, Row)> =
rows.iter().enumerate().map(|(i, r)| (Rect::from_min_size(pos2(0.0, 20.0 * i as f32), vec2(400.0, 20.0)), r.clone())).collect();
let tm = TMap { x0: 205.0, start: 0.0, pps: 100.0 };
let go = |br: Rect| box_targets(comp, &rows, &hit_rows, &tm, 0.0, 20.0, 200.0, false, br);
let view = marquee::View { tm, top: 0.0, graph_x0: 200.0, graph_on: false };
// Press at `from` with the rows scrolled by `s0`, release at `to` scrolled by `s1`.
let drag = |from: Pos2, s0: f32, to: Pos2, s1: f32| {
let (paint, sel) = marquee::Anchor::press(&view, s0, from).selection(&view, s1, to);
marquee::targets(comp, &rows, &tm, -s1, 20.0, paint, &sel)
};
let go = |from: Pos2, to: Pos2| drag(from, 0.0, to, 0.0);
let keyed = |ks: &[effectcraft_engine::KeyRef]| ks.iter().map(|k| k.layer.0).collect::<Vec<_>>();
// From the outline across the boundary: layer A's row (y 0..20) and B's row, and their keys.
let (ls, ks) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(260.0, 65.0)));
let (ls, ks) = go(pos2(10.0, 5.0), pos2(260.0, 65.0));
assert_eq!(ls, [layers[0], layers[1]]);
assert_eq!(keyed(&ks), [layers[0], layers[1]]);
// The same box dragged the other way is the same rectangle: the same rows, the same keys.
let (ls, ks) = go(Rect::from_two_pos(pos2(260.0, 65.0), pos2(10.0, 5.0)));
// The same box dragged the other way: the same rows, the same keys.
let (ls, ks) = go(pos2(260.0, 65.0), pos2(10.0, 5.0));
assert_eq!((ls, keyed(&ks)), (vec![layers[0], layers[1]], vec![layers[0], layers[1]]));
// Only over the outline: rows, no keys; only over the graph: keys, and the rows crossed.
let (ls, ks) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(150.0, 65.0)));
let (ls, ks) = go(pos2(10.0, 5.0), pos2(150.0, 65.0));
assert_eq!((ls.len(), ks.len()), (2, 0), "a key under the outline isn't touched");
// Kept inside the time graph: keys only, no rows (a marquee over empty track area).
let (ls, ks) = go(Rect::from_min_max(pos2(204.0, 5.0), pos2(260.0, 65.0)));
let (ls, ks) = go(pos2(204.0, 5.0), pos2(260.0, 65.0));
assert_eq!((ls.len(), keyed(&ks)), (0, vec![layers[0], layers[1]]));
// A locked layer is not selected as a row (the engine drops its keys in keys.select).
let (ls, _) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(260.0, 119.0)));
let (ls, _) = go(pos2(10.0, 5.0), pos2(260.0, 119.0));
assert_eq!(ls, [layers[0], layers[1]]);
// Scrolled 20 pt mid-drag: the pressed corner stays on layer A's row (#2859), so A is still
// selected; a screen-fixed anchor would start the box below A and select only B.
let (ls, _) = drag(pos2(10.0, 5.0), 0.0, pos2(260.0, 45.0), 20.0);
assert_eq!(ls, [layers[0], layers[1]]);
}
+42 -7
View File
@@ -75,12 +75,45 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) {
if proj.len() > 1 {
errors.push(crate::i18n::tr_args("Only one project can be opened per drop; skipped {} more", &[&proj.len().saturating_sub(1)]));
}
// While the After Effects losses prompt waits, opening a project would replace it (and the
// project the answer imports into): the project waits for the answer, dropped again after it.
let proj = if crate::panels::aep_losses::asking(app) && !proj.is_empty() {
errors.push(crate::i18n::tr_args("Skipped {} more file(s) while a dialog is open; import them again after it", &[&1]));
Vec::new()
} else {
proj
};
if let Some(p) = proj.first()
&& let Err(e) = crate::menus::invoke(app, &ctx, "file.open", json!({"path": p}))
{
errors.push(crate::i18n::tr_args("Cannot open dropped project {}: {}", &[&p, &e]));
// After Effects projects import as projects (File ▸ Import ▸ Adobe After Effects Project…),
// each into a folder named after its file, opening its first composition.
let (aep, mut rest): (Vec<String>, Vec<String>) = rest.into_iter().partition(|p| effectcraft_engine::commands::is_aep_path(p));
// A project in the drop changes the active composition: the other files go to the Project
// panel only, not into a composition.
let has_project = !proj.is_empty() || !aep.is_empty();
// Opened first, so the other files land in the project it opens. While it waits on Save
// changes? the rest of the drop is skipped with a note: it would land in the project about
// to be replaced.
let mut waiting = false;
if let Some(p) = proj.first() {
match crate::menus::invoke(app, &ctx, "file.open", json!({"path": p})) {
Ok(r) => waiting = crate::menus::waits_on_dialog(&r),
Err(e) => errors.push(crate::i18n::tr_args("Cannot open dropped project {}: {}", &[&p, &e])),
}
}
let mut aep = aep;
if waiting {
let skipped = rest.len() + aep.len();
if skipped > 0 {
errors.push(crate::i18n::tr_args("Skipped {} more file(s) while a dialog is open; import them again after it", &[&skipped]));
}
rest.clear();
aep.clear();
}
// One with losses asks first; the projects after it wait for the answer, the other files
// import meanwhile. The errors name their files.
errors.extend(crate::menus::import_aep_projects(app, aep));
if !rest.is_empty() {
let viewer = app
@@ -101,7 +134,7 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) {
.is_some_and(|e| Rect::from_min_size(egui::pos2(e.rect[0], e.rect[1]), egui::vec2(e.rect[2], e.rect[3])).contains(p))
});
let at = if proj.is_empty() && !over_timeline {
let at = if !has_project && !over_timeline {
ctx.input(|i| i.pointer.hover_pos()).filter(|p| viewer.is_some_and(|v| v.contains(*p))).and_then(|p| crate::panels::viewer::screen_to_comp(&ctx, p))
} else {
None
@@ -116,11 +149,13 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) {
let mut params = json!({
"paths": rest,
"drag": true,
"addToComp": at.is_some() || over_timeline,
"addToComp": !has_project && (at.is_some() || over_timeline),
"position": at,
"background": true
});
if !layered {
// While the losses prompt of a project dropped with it shows, a Photoshop file imports
// with Default Drag Import As: one dialog at a time.
if !layered || crate::panels::aep_losses::asking(app) {
params["importAs"] = json!(app.session.prefs.drag_import_as());
}
if alt {
+162
View File
@@ -722,3 +722,165 @@ fn illustrator_import_offers_and_applies_retain_layer_sizes() {
assert_eq!((f.width, f.height), (30, 20));
assert!(f.layer.as_ref().unwrap().layer_size);
}
/// An After Effects project, written by File ▸ Export ▸ Adobe After Effects Project… to a folder of its
/// own: composition "Intro" with a solid.
fn aep_file(tag: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("ec-ui-aep-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("Intro.aep");
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Intro", "width": 320, "height": 180, "frameRate": 30, "duration": 2})).unwrap();
s.execute("layer.newSolid", json!({"name": "Backdrop", "color": "#204060"})).unwrap();
s.execute("file.exportAep", json!({"path": path.to_string_lossy(), "allowLosses": true})).unwrap();
path
}
/// The open composition is "Intro", in the folder named after the imported file.
fn intro_is_open_in_its_folder(h: &Harness<'_, EffectcraftApp>) {
let s = &h.state().session;
let comp = s.project.item(s.active_comp_id().expect("a composition is open")).unwrap();
assert_eq!(comp.name, "Intro");
let folder = s.project.item(comp.parent.expect("in a folder")).unwrap();
assert_eq!(folder.name, "Intro.aep");
assert!(matches!(folder.kind, effectcraft_engine::project::ItemKind::Folder));
}
/// Dropped After Effects projects import as File ▸ Import ▸ Adobe After Effects Project… does
/// (they were refused as unsupported footage): each in a folder named after its file, the last
/// one's composition open. With nothing lost, nothing is asked.
#[test]
fn dropped_after_effects_projects_import_and_open_their_composition() {
let (mut h, _) = harness();
let aep = aep_file("drop");
h.event(Event::PointerMoved(rect(&h, "viewer.area").center()));
h.step();
let outro = aep.with_file_name("Outro.aep");
std::fs::copy(&aep, &outro).unwrap();
h.input_mut().dropped_files.push(Arc::new(Dropped(outro)));
h.input_mut().dropped_files.push(Arc::new(Dropped(aep)));
h.run_steps(3);
assert_eq!(h.state().dialog, None, "nothing is lost, so nothing to ask");
assert!(h.state().session.project.items.values().any(|i| i.name == "Outro.aep"), "the first project too");
intro_is_open_in_its_folder(&h);
assert_eq!(h.state().ui.status, "", "no errors");
}
/// File ▸ Import ▸ File… offers After Effects projects; a picked one imports as a project, and
/// the other files picked with it as footage.
#[test]
fn import_file_dialog_imports_a_picked_after_effects_project_with_the_other_files() {
let (mut h, _) = harness();
let aep = aep_file("pick");
let still = aep.with_file_name("still.png");
std::fs::write(&still, b"").unwrap();
let offered = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
let seen = offered.clone();
let picked = vec![aep.to_string_lossy().to_string(), still.to_string_lossy().to_string()];
h.state_mut().session.importer = Some(Arc::new(Stills));
h.state_mut().hooks.pick_files = Some(Box::new(move |exts: &[&str]| {
*seen.lock().unwrap() = exts.iter().map(|e| e.to_string()).collect();
picked.clone()
}));
let ctx = h.ctx.clone();
effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "file.import", json!({})).unwrap();
assert!(["aep", "aepx"].iter().all(|e| offered.lock().unwrap().iter().any(|o| o == e)), "{:?}", offered.lock().unwrap());
step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png"));
intro_is_open_in_its_folder(&h);
}
/// Footage dropped with an After Effects project goes to the Project panel only, also over the
/// Timeline: the project opens its own composition, which the footage does not join.
#[test]
fn footage_dropped_with_an_after_effects_project_stays_in_the_project_panel() {
let (mut h, _) = harness();
let aep = aep_file("with-footage");
let still = aep.with_file_name("still.png");
std::fs::write(&still, b"").unwrap();
h.state_mut().session.importer = Some(Arc::new(Stills));
h.event(Event::PointerMoved(rect(&h, "panel.Timeline").center()));
h.step();
h.input_mut().dropped_files.push(Arc::new(Dropped(still)));
h.input_mut().dropped_files.push(Arc::new(Dropped(aep)));
h.run_steps(3);
step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png"));
h.run_steps(2);
intro_is_open_in_its_folder(&h);
let s = &h.state().session;
assert!(s.project.comps().all(|(_, c)| c.layers.iter().all(|l| l.name != "still.png")), "no composition got a still.png layer");
}
/// A dropped After Effects project with parts that cannot come over asks first (the losses
/// prompt). The projects dropped with it wait for the answer; the other files import meanwhile,
/// a Photoshop file with Default Drag Import As (one dialog at a time). Cancel skips only the
/// project it asks about; Import Anyway imports it.
#[test]
fn a_dropped_after_effects_project_with_losses_asks_first() {
let (mut h, _) = harness();
let intro = aep_file("losses");
let lossy = intro.with_file_name("Synthetic.aep");
std::fs::copy(concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep"), &lossy).unwrap();
h.state_mut().session.importer = Some(Arc::new(Gated { started: AtomicUsize::new(0), allow: Arc::new(AtomicUsize::new(usize::MAX)) }));
h.state_mut().session.prefs.import.drag_import_as = "footage".into();
for path in [lossy.clone(), intro.clone(), intro.with_file_name("Layers.PSD"), intro.with_file_name("still.png")] {
h.input_mut().dropped_files.push(Arc::new(Dropped(path)));
}
h.run_steps(3);
assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses), "the drop asks first");
assert_eq!(h.state_mut().dialog_state_mut().aep_losses.queue, [intro.to_string_lossy().to_string()], "the other project waits");
let named = |h: &Harness<'_, EffectcraftApp>, name: &str| h.state().session.project.items.values().any(|i| i.name == name);
assert!(!named(&h, "Synthetic.aep") && !named(&h, "Intro.aep"), "no project imported before the answer");
step_until(&mut h, |h| ["Layers.PSD", "still.png"].iter().all(|n| footage_items(h).iter().any(|(f, _)| f == n)));
assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses), "the footage did not replace the prompt");
click_id(&mut h, "dialog.aepLosses.cancel");
h.run_steps(2);
assert_eq!(h.state().dialog, None);
assert!(!named(&h, "Synthetic.aep"), "Cancel skips the project it asked about");
intro_is_open_in_its_folder(&h);
h.input_mut().dropped_files.push(Arc::new(Dropped(lossy)));
h.run_steps(3);
assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses));
click_id(&mut h, "dialog.aepLosses.continue");
h.run_steps(2);
assert_eq!(h.state().dialog, None);
assert!(named(&h, "Synthetic.aep"), "Import Anyway imports it");
}
/// An After Effects project with nothing lost imports at once, so a Photoshop file dropped with
/// it still asks how to import it (Import Photoshop File).
#[test]
fn a_photoshop_file_dropped_with_an_after_effects_project_still_asks() {
let (mut h, _) = harness();
h.state_mut().session.importer = Some(Arc::new(Gated { started: AtomicUsize::new(0), allow: Arc::new(AtomicUsize::new(usize::MAX)) }));
let aep = aep_file("with-psd");
h.input_mut().dropped_files.push(Arc::new(Dropped(aep.with_file_name("Layers.PSD"))));
h.input_mut().dropped_files.push(Arc::new(Dropped(aep)));
h.run_steps(2);
intro_is_open_in_its_folder(&h);
assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::Form), "Import Photoshop File asks");
}
/// A dropped file that is not an After Effects project reports why, changes nothing, and the
/// other files of the drop still import.
#[test]
fn a_corrupt_dropped_after_effects_project_reports_and_keeps_the_other_files() {
let (mut h, _) = harness();
let dir = aep_file("corrupt").parent().unwrap().to_path_buf();
let broken = dir.join("Broken.aep");
std::fs::write(&broken, b"not an After Effects project").unwrap();
let still = dir.join("still.png");
std::fs::write(&still, b"").unwrap();
h.state_mut().session.importer = Some(Arc::new(Stills));
let comp = h.state().session.active_comp_id();
h.input_mut().dropped_files.push(Arc::new(Dropped(broken)));
h.input_mut().dropped_files.push(Arc::new(Dropped(still)));
h.run_steps(3);
step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png"));
let status = h.state().ui.status.clone();
assert!(status.contains("Broken.aep"), "the error names the file: {status}");
assert!(!h.state().session.project.items.values().any(|i| i.name == "Broken.aep"));
assert_eq!(h.state().session.active_comp_id(), comp, "the open composition stays");
}
+268 -2
View File
@@ -13,9 +13,9 @@ use serde_json::json;
const REACH: f32 = 5.5;
/// A comp with one solid, its Opacity keys at 1 s and 2 s (linear) revealed.
fn harness() -> (Harness<'static, EffectcraftApp>, u64) {
fn harness_for(duration: f64) -> (Harness<'static, EffectcraftApp>, u64) {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Box", "width": 320, "height": 180, "frameRate": 30, "duration": 4})).unwrap();
s.execute("comp.new", json!({"name": "Box", "width": 320, "height": 180, "frameRate": 30, "duration": duration})).unwrap();
let id = s.execute("layer.newSolid", json!({"name": "L", "color": "#e04020", "width": 80, "height": 80})).unwrap()["layer"].as_u64().unwrap();
for (t, v) in [(1.0, 0.0), (2.0, 100.0)] {
s.execute("prop.addKey", json!({"layer": id, "path": "transform/opacity", "time": t, "value": v})).unwrap();
@@ -29,6 +29,10 @@ fn harness() -> (Harness<'static, EffectcraftApp>, u64) {
(h, id)
}
fn harness() -> (Harness<'static, EffectcraftApp>, u64) {
harness_for(4.0)
}
/// The center of the key at `secs`: the middle of its hit rect, which is centered on the glyph.
fn key_center(h: &Harness<'_, EffectcraftApp>, id: u64, secs: f64) -> Pos2 {
let comp = h.state().session.active_comp().unwrap();
@@ -122,3 +126,265 @@ fn a_box_dragged_across_the_outline_and_the_graph_selects_rows_and_keys() {
assert_eq!(keys, [1.0, 1.0]);
assert_eq!(selected(&h), (both.clone(), both), "graph → outline");
}
/// Thirty expanded layers force row virtualization and leave room for wheel and edge scrolling.
fn rows_harness() -> (Harness<'static, EffectcraftApp>, Vec<u64>) {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Rows", "width": 320, "height": 180, "frameRate": 30, "duration": 4})).unwrap();
let mut ids = vec![];
for i in 0..30 {
let id = s.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#406080"})).unwrap()["layer"].as_u64().unwrap();
s.execute("prop.addKey", json!({"layer": id, "path": "transform/opacity", "time": 1.0, "value": 50})).unwrap();
ids.push(id);
}
s.execute("layer.select", json!({"layers": ids})).unwrap();
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s));
h.run_steps(3);
let ctx = h.ctx.clone();
effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "timeline.reveal.opacity", json!({})).unwrap();
h.run_steps(4);
(h, ids)
}
/// The originally enclosed key stays in the box when wheel scrolling takes its row offscreen.
#[test]
fn scrolling_rows_during_a_marquee_keeps_offscreen_keys_in_the_box() {
let (mut h, ids) = rows_harness();
let top = *ids.last().unwrap();
let c = key_center(&h, top, 1.0);
let from = c - vec2(20.0, 5.0);
let to = c + vec2(20.0, 30.0);
h.event(Event::PointerMoved(from));
h.step();
h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.step();
h.event(Event::PointerMoved(to));
h.run_steps(2);
h.event(Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, -400.0),
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
});
h.run_steps(20);
assert!(h.state().ui.timeline.scroll_y > 300.0, "the real wheel handler scrolled rows");
h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
h.run_steps(2);
let selected: Vec<_> = h.state().session.state.selected_keys.iter().map(|k| k.layer.0).collect();
assert!(selected.contains(&top), "the key on layer {top} scrolled offscreen but remains inside the content-anchored marquee; actual layers {selected:?}");
}
fn press(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) {
h.event(Event::PointerMoved(from));
h.step();
h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.step();
h.event(Event::PointerMoved(to));
h.run_steps(2);
}
fn release(h: &mut Harness<'_, EffectcraftApp>, to: Pos2) {
h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
h.run_steps(2);
}
fn wheel(h: &mut Harness<'_, EffectcraftApp>, delta: Vec2) {
h.event(Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta,
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
});
h.run_steps(20);
}
fn times(h: &Harness<'_, EffectcraftApp>) -> Vec<f64> {
h.state().session.state.selected_keys.iter().map(|k| k.time.seconds()).collect()
}
/// Panning time retains hidden keys while both painting and row selection respect the graph boundary.
#[test]
fn horizontal_scroll_keeps_hidden_keys_without_selecting_outline_layers() {
let (mut h, id) = harness_for(20.0);
h.state_mut().ui.timeline.pps = Some(200.0);
h.run_steps(3);
let c = key_center(&h, id, 1.0);
let to = c + vec2(20.0, 5.0);
press(&mut h, c - vec2(20.0, 5.0), to);
wheel(&mut h, vec2(-400.0, 0.0));
assert!(h.state().ui.timeline.start > 1.5, "time panned: {}", h.state().ui.timeline.start);
let graph_left = h.state().auto.find("timeline.ruler").unwrap().rect[0];
let box_shapes: Vec<_> =
h.output().shapes.iter().filter(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect.max == to && (r.rect.height() - 10.0).abs() < 0.01)).collect();
assert!(!box_shapes.is_empty(), "the held marquee is drawn");
assert!(box_shapes.iter().all(|s| s.clip_rect.min.x >= graph_left), "a graph-only box cannot paint over the outline when its anchor scrolls out");
release(&mut h, to);
assert_eq!(times(&h), [1.0, 2.0]);
// Keys select their owning layer, but panning the anchor under the outline must not select
// unrelated layer rows. The separate cross-column test below covers those rows.
assert_eq!(h.state().session.state.selected_layers, [LayerId(id)]);
}
/// Returning the viewport shrinks the box again without editing the document or its history.
#[test]
fn shrinking_after_scrolling_removes_keys_again_and_selection_does_not_edit_the_project() {
let (mut h, id) = harness_for(20.0);
h.state_mut().ui.timeline.pps = Some(200.0);
h.run_steps(3);
let before = h.state().session.project.to_json();
let revision = h.state().session.revision;
let undo = h.state().session.history.undo.len();
let c = key_center(&h, id, 1.0);
let to = c + vec2(20.0, 5.0);
press(&mut h, c - vec2(20.0, 5.0), to);
wheel(&mut h, vec2(-400.0, 0.0));
wheel(&mut h, vec2(400.0, 0.0));
assert!(h.state().ui.timeline.start < 0.01);
release(&mut h, to);
assert_eq!(times(&h), [1.0], "shrinking is not a union of every key passed");
assert_eq!(h.state().session.project.to_json(), before);
assert_eq!(h.state().session.revision, revision);
assert_eq!(h.state().session.history.undo.len(), undo);
}
/// Invalidated gestures leave the prior selection intact even if the pointer keeps moving.
#[test]
fn cancellation_preserves_selection_and_does_not_restart_until_a_new_press() {
for cancel in ["escape", "ownership", "focus", "layout", "resize", "edit", "comp"] {
let (mut h, id) = harness();
h.state_mut().session.execute("keys.select", json!({"keys": [{"layer":id,"path":"transform/opacity","time":2.0}]})).unwrap();
h.run_steps(2);
let c = key_center(&h, id, 1.0);
let to = c + vec2(10.0, 5.0);
press(&mut h, c - vec2(20.0, 5.0), to);
assert_eq!(times(&h), [2.0], "selection commits only on release");
match cancel {
"escape" => h.event(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }),
"ownership" => h.ctx.set_dragged_id(egui::Id::unique("another-owner")),
"focus" => h.input_mut().focused = false,
"layout" => h.state_mut().ui.timeline.search = "no matching rows".into(),
"resize" => {
h.set_size(vec2(1400.0, 900.0));
}
"edit" => {
h.state_mut().session.execute("prop.set", json!({"layer":id,"path":"transform/opacity","value":75})).unwrap();
}
"comp" => {
h.state_mut().session.execute("comp.new", json!({"name":"Other"})).unwrap();
}
_ => unreachable!(),
}
h.run_steps(2);
let expected = times(&h);
h.event(Event::PointerMoved(to + vec2(1.0, 0.0)));
h.step();
release(&mut h, to);
assert_eq!(times(&h), expected, "{cancel} cancelled without applying the box");
if !matches!(cancel, "comp" | "edit") {
assert_eq!(times(&h), [2.0], "{cancel} preserves the prior selection");
}
}
}
/// A release-frame pointer move takes effect, modifiers add, and locked keys remain excluded.
#[test]
fn release_uses_the_release_position_and_additive_modifiers_and_excludes_locked_keys() {
for modifiers in [Modifiers::NONE, Modifiers::SHIFT, Modifiers::COMMAND] {
let (mut h, id) = harness();
h.state_mut().session.execute("keys.select", json!({"keys": [{"layer":id,"path":"transform/opacity","time":2.0}]})).unwrap();
h.run_steps(2);
let c = key_center(&h, id, 1.0);
press(&mut h, c - vec2(25.0, 5.0), c - vec2(15.0, 5.0));
h.event(Event::ModifiersChanged(modifiers));
h.event(Event::PointerMoved(c + vec2(10.0, 5.0)));
h.event(Event::PointerButton { pos: c + vec2(10.0, 5.0), button: egui::PointerButton::Primary, pressed: false, modifiers });
h.run_steps(2);
let mut actual = times(&h);
actual.sort_by(f64::total_cmp);
assert_eq!(actual, if modifiers == Modifiers::NONE { vec![1.0] } else { vec![1.0, 2.0] });
h.state_mut().session.execute("layer.setSwitch", json!({"layers":[id],"switch":"lock","value":true})).unwrap();
h.event(Event::ModifiersChanged(Modifiers::NONE));
h.run_steps(2);
assert!(marquee(&mut h, c - vec2(20.0, 5.0), c + vec2(10.0, 5.0)).is_empty(), "locked keys stay excluded");
}
}
/// Cross-column selection uses logical headers, including those no longer rendered after scrolling.
#[test]
fn crossing_into_the_outline_after_scrolling_selects_offscreen_layer_rows() {
let (mut h, ids) = rows_harness();
let c = key_center(&h, *ids.last().unwrap(), 1.0);
let outline = h.state().auto.find("timeline.layerMarquee").unwrap().rect;
let to = pos2(outline[0] + 40.0, c.y + 30.0);
press(&mut h, c + vec2(20.0, -5.0), to);
wheel(&mut h, vec2(0.0, -400.0));
assert!(h.state().ui.timeline.scroll_y > 300.0);
release(&mut h, to);
let selected = &h.state().session.state.selected_layers;
assert!(selected.contains(&LayerId(ids[ids.len() - 2])), "the second header is inside the box although it scrolled offscreen");
assert!(selected.len() > 5, "all intervening rows are selected");
}
/// Edge scrolling continues without pointer motion and stops as soon as the gesture is cancelled.
#[test]
fn a_stationary_pointer_at_either_edge_scrolls_only_until_release_or_cancel() {
let (mut h, ids) = rows_harness();
let c = key_center(&h, *ids.last().unwrap(), 1.0);
let area = h.state().auto.find("timeline.layerMarquee").unwrap().rect;
let bottom = pos2(c.x + 20.0, area[1] + area[3] - 2.0);
press(&mut h, c - vec2(20.0, 5.0), bottom);
let before = h.state().ui.timeline.scroll_y;
h.run_steps(30);
assert!(h.state().ui.timeline.scroll_y > before + 50.0, "a stationary drag requests more frames and scrolls");
let top = pos2(bottom.x, area[1] + 2.0);
h.event(Event::PointerMoved(top));
h.step();
let before = h.state().ui.timeline.scroll_y;
h.run_steps(10);
assert!(h.state().ui.timeline.scroll_y < before, "the top edge scrolls back");
h.event(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.step();
let before = h.state().ui.timeline.scroll_y;
h.run_steps(10);
assert_eq!(h.state().ui.timeline.scroll_y, before, "Escape stops edge scrolling");
release(&mut h, top);
assert!(times(&h).is_empty(), "cancelled box selects nothing");
}
/// Batched wheel events cannot move the press anchor or make release query stale row positions.
#[test]
fn wheel_on_press_captures_the_old_anchor_and_wheel_on_release_uses_the_new_rows() {
for scroll_on_press in [true, false] {
let (mut h, ids) = rows_harness();
let top = *ids.last().unwrap();
let c = key_center(&h, top, 1.0);
let from = c - vec2(20.0, 5.0);
let to = c + vec2(20.0, 30.0);
if scroll_on_press {
h.event(Event::PointerMoved(from));
h.step();
h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
} else {
press(&mut h, from, to);
}
h.event(Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, -400.0),
modifiers: Modifiers::NONE,
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
});
if !scroll_on_press {
h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
}
h.step();
if scroll_on_press {
h.event(Event::PointerMoved(to));
h.run_steps(20);
release(&mut h, to);
}
assert!(h.state().session.state.selected_keys.iter().any(|k| k.layer == LayerId(top)), "scroll_on_press={scroll_on_press}");
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-11-05 · **Change:** a 3D layer's changed Material Options are reported, shape and text animator colours outside 0 to 1 are clamped and reported, and a popup expression is no longer reported where the option order is the same (2026-11-05); before that: composition settings the base writes at their defaults (Preserve Frame Rate / Resolution, motion blur samples, adaptive sample limit) and render queue items are reported, and a sequence's folder time is read only when its file passes the document-path policy (2026-11-04); before that: every chunk is built in code, no template is read (2026-10-29); before that: Curves and Mesh Warp are written (R53); before that: a switched-off shape item or layer style is written switched off (R52, 986-988); before that: a reversed layer (negative stretch) is written as After Effects stores it (R36, 981-983); before that: an explicit None (`NO_LAYER`) on Compound Blur, Displacement Map or Set Matte is written as None, and Layer Control on a named layer no longer reports a source choice; before that: layer styles are written (R51, 950-980; read back in After Effects: `export-tests\styles`); before that: an image sequence's folder modification time is written when the caller knows it (`sspc@118`, R50: without it After Effects rescans the folder on open and warns about every gap of a sparse sequence in a modal dialog); before that: an output space equal to the working space is not reported (After Effects renders in the working space, 902; the importer sets it so for Rec.709 Gamma 2.4); before that: one effect table for both directions again: the 27 effects of the importer round 3 and the Layer Control are written too (prototypes from 903 and 076, layer parameters by layer id; not read back in After Effects yet); before that: the importer round 3 is merged: Offset Paths' Copy Offset is written as EffectCraft's value + 1 (826-828, the importer's inverse); before that: sparse sequences checked (860, written as After Effects stores them); before that: the Rec.709 Gamma 2.4 working space is written with a profile authored here (R49, g5); before that: text animators with range selectors are written (R48, t8); before that: a footage layer running past its source is ended there on import, as After Effects does, and less than a frame past is not a loss (LayerTiming); before that: Anchor Point and mask path ease are written (R47); before that: shape operators (polystar, gradients, trim paths, round corners, merge / offset paths, repeater) and keys and expressions on shape properties are written (R46, o2); before that: every effect of the importer's table is written, through one shared table and records built per parameter type (R45, e2); before that: time remapping is written (319, 354, r1, R44); before that: the character and paragraph settings are read back in After Effects (t6) and right-to-left justification is written relative to the direction (t7, R43); before that: the character and paragraph settings the importer decodes are written (scales, baseline shift, kerning, ligatures, tsume, fill / stroke switches and width, justified variants, indents, spacing, composer, direction); before that: track mattes, markers, cameras, lights and shape layers (R39-R42), keys on stretched layers, conformed clips, tga / psd / ai / eps, project settings (620-634, R37), superscript / subscript (526-528); before: precomposition layers (124, 257, 258, 600-602), the effects Tint, Gradient Ramp and Exposure (356-397, 610), footage (351, 353, 603-607, 611-613), text layers (110, 245, t1-t4), the 3D switch (015) and time stretch (018, 316, 317) are exported · **Target:** Adobe After Effects 2026
File ▸ Export ▸ After Effects Project… (`file.exportAep`) writes the open project as an `.aep` that
File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`) writes the open project as an `.aep` that
After Effects 2026 opens. It is deliberately narrow: what it writes is exact, tested and checked
against After Effects itself; everything else is **reported** and never dropped silently. The
importer ([aep-import.md](aep-import.md)) is the mirror image; both rest on the clean-room format
+10 -3
View File
@@ -1,9 +1,16 @@
# After Effects project import
> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** After Effects' Puppet arrives as EffectCraft's Puppet without its meshes and pins, which are reported mesh by mesh (#3093; the storage is not sampled yet, aep-format 3.7); before that: Lumetri Color's master RGB curve, Hue vs Sat curve and colour wheels are imported (1200 presets made by hand); before that: Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: Line truncated
> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-10 · **Change:** dropped projects and projects picked in File ▸ Import ▸ File… import as projects, several asked about one at a time; `file.import` hands them to `file.importAep`; the menu entries are named Adobe After Effects Project… (#3103); before that: After Effects' Puppet arrives as EffectCraft's Puppet without its meshes and pins, which are reported mesh by mesh (#3093; the storage is not sampled yet, aep-format 3.7); before that: Lumetri Color's master RGB curve, Hue vs Sat curve and colour wheels are imported (1200 presets made by hand); before that: Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/SaturatLine truncated
File ▸ Import ▸ After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx`
File ▸ Import ▸ Adobe After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx`
and adds its items to the open project, in a folder named after the file (as After Effects does when it imports a project).
A project dropped on the window, or picked in an Import File dialog (File ▸ Import ▸ File… or
Multiple Files…, the Home screen, the Project panel), imports the same way. Several are asked
about one at a time: while the losses prompt shows, the others wait, and Cancel skips only the
one it asks about. The other files of the drop or pick import as footage, into the Project panel
only (not into a composition, as the project opens its own); while the prompt shows, a Photoshop file or image sequence imports with its defaults instead of
opening its own dialog. Agents giving an `.aep` to `file.import` get the refusal when parts would
be lost (`file.importAep` takes `allowLosses` and `dryRun`).
It is deliberately narrow: what it brings over is exact and tested, everything else is **reported**
and never dropped silently. Wider coverage grows with the reader's verified field layouts
([aep-format.md](aep-format.md)); this page lists what exists today.
@@ -496,7 +503,7 @@ generated corpus and its case numbers, not a claim about what was or was not see
## Export
The other direction, File ▸ Export ▸ After Effects Project… (`file.exportAep`), is described in
The other direction, File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`), is described in
[aep-export.md](aep-export.md).
## Not done yet
+4
View File
@@ -322,6 +322,10 @@ build real projects through these interfaces; they are worked examples of everyt
then `keys.velocity {"outSpeed":0,"outInfluence":33.33}`.
* **Gradients**: `set_property {"path":"contents/gfill/colors","value":["#0080ff","#ffff00"]}` or
`{"colors":[[0,"#0080ff"],[1,[1,1,0]]],"opacities":[[0,1],[1,0.5]]}`.
* **Projects**: `file.import` takes `.ecproj` / `.ecprojx` and After Effects projects (`.aep`, `.aepx`)
too, each into a folder named after it (`projects` in the result). An After Effects project that
would lose parts is refused (an `errors` entry, nothing imported): `file.importAep {"path": …,
"dryRun": true}` lists the losses, `"allowLosses": true` imports the rest.
* **Image sequences**: `file.import {"paths":["/r/shot_0001.png"], "sequence": true}` imports the
file's whole numbered run as one item (several picked frames: that range; a folder: each run in
it, also without `sequence`). Without `"sequence": true` a picked or dropped numbered file imports
+7 -2
View File
@@ -43,7 +43,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for
| L3 | `interchange` | Premiere Pro interop through timeline interchange (FCP7 XML, FCPXML, OTIO, EDL, AAF, OMF via FilmCraft's `filmcraft-interchange`): sequences ↔ compositions (§6a) |
| L3 | `aep` | After Effects projects in one crate (like `lottie`): `container` reads and writes the `.aep` container (RIFX `Egg!`) as a lossless chunk tree with a byte-identical round trip, bounded for hostile input, reads the `.aepx` XML form into the same tree, and has a typed view of items, layers, properties, effects, masks, shapes, text, markers, footage, cameras, lights and layer styles, from our own observations of generated files ([aep-format.md](aep-format.md)); `import` maps the typed view into a project (items, settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text, shapes, cameras, lights, mattes, markers, time remapping, layer styles, the effects of a shared table, footage) with a report of everything not imported ([aep-import.md](aep-import.md)); `export` builds every chunk in code (prototypes in the layout After Effects writes, cloned and edited only at verified fields) for the same scope, with a report of everything not exported, verified by opening the output in After Effects ([aep-export.md](aep-export.md)) |
| L3 | `lottie` | Lottie JSON / dotLottie import and export (layers, precomps, eased and spatial keyframes, shapes, masks, mattes) with a warnings list for what Lottie cannot express |
| L3 | `plugin` | Effect plug-ins: loads sandboxed WebAssembly effect modules (plug-in API v1; the wasmi interpreter behind the `wasm` feature, on for native hosts) into the effect registry ([plugins.md](plugins.md)) |
| L3 | `plugin` | Effect plug-ins: loads sandboxed WebAssembly effect modules (WebAssembly ABI v1 and v2, the latter with neighbouring frames; the wasmi interpreter behind the `wasm` feature, on for native hosts) into the effect registry ([plugins.md](plugins.md)) |
| L3 | `helper-host` | External effect helpers: finds helper programs, runs each as a child process and registers its effects (plug-in API 2) over a versioned pipe + shared-file protocol; a crashing helper is an error on the effect, never a crash ([external-helpers.md](external-helpers.md), [ADR 0001](adr/0001-external-effect-helpers.md)). Native only |
| L4 | `engine` | `Session`: project, branching undo history, editor state, the command registry and menus, the ScriptUI window model (`scriptui`) |
| L4 | `script` | Scripting: JavaScript (boa) with an After Effects-style object model (`app.project`, comps, layers, properties, render queue) and ScriptUI (dialogs, palettes, dockable panels; script-host threads so dialogs block `show()`), whose edits run engine commands; AE match names |
@@ -412,7 +412,12 @@ as NaNs (a kernel that skips pixels fails the oracle) and validation errors pani
(`ui-egui::frames`); headless renders, the CLI (unless `--gpu`) and CI use the CPU. Outside
tests, device errors are logged rather than fatal. A viewer frame that runs out of video memory
(`Gpu::within_memory`) renders on the CPU, the uploaded layers and pooled textures are freed, and
the RAM preview keeps half as many GPU frames from then on. The same guard catches a frame's
the RAM preview keeps half as many GPU frames from then on. However many frame threads the viewer
runs, at most two of them start GPU render attempts at once. Shared-pool workers use the CPU
when both slots are busy rather than blocking Rayon helpers; dedicated single-thread preview
lanes wait for admission and remain on the GPU, so
working-texture pressure is bounded by concurrent attempts rather than the thread count (#3028).
A single large frame or retained frames can still exceed the available video memory. The same guard catches a frame's
validation errors (an invalid texture, often left by a failed allocation): that frame renders on
the CPU and the kept textures are freed, but the compositor stays on and, with no allocation
failure before it, the RAM preview keeps its GPU frames (#1319). A frame whose render panics is
+1 -1
View File
@@ -22,7 +22,7 @@ trips and renders, checked by tests, not against After Effects), **partial** (na
| Format | After Effects | EffectCraft read | Write | Fidelity / notes | Tested by |
|---|---|---|---|---|---|
| Project `.aep` | R/W (native) | partial | partial | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table shared with the exporter (Lumetri Color, Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). File ▸ Export ▸ After Effects Project… (`file.exportAep`, `effectcraft_aep::export`) writes the same scope with every chunk built in code; exports were opened and read back in After Effects 2026 ([aep-export.md](aep-export.md)); everything else is reported. Effects outside the table (plug-ins, …) are not converted | `crates/aep` tests (`tests/container`, `tests/import`, `tests/export`) |
| Project `.aep` | R/W (native) | partial | partial | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ Adobe After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table shared with the exporter (Lumetri Color, Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`, `effectcraft_aep::export`) writes the same scope with every chunk built in code; exports were opened and read back in After Effects 2026 ([aep-export.md](aep-export.md)); everything else is reported. Effects outside the table (plug-ins, …) are not converted | `crates/aep` tests (`tests/container`, `tests/import`, `tests/export`) |
| XML project `.aepx` | R/W | partial | none | Read into the same chunk tree as `.aep` by `crates/aep` (`read_aepx`; mapping in [aep-format.md](aep-format.md) section 1), so `file.importAep` imports it with the same coverage as `.aep`; not written | `crates/aep` `aepx` tests (container and import) |
| Templates `.aet` / `.aetx` | R/W | none | none | Own `.ectemplate` (Home ▸ Templates, Save as Template) | `templates.rs` tests |
| Motion Graphics template `.mogrt` | W (and R in Premiere) | none | none | Own `.ectemplate` Essential Graphics templates; FilmCraft rejects our `.fcgt` export (#611) | `essential.rs` tests |
+3
View File
@@ -70,6 +70,9 @@ next to it.
Agents: `file.importProject {"path": "/projects/titles.ecproj"}` returns the comps, the root comps,
`primaryComp` (the only root comp, if there is one) and any missing media.
After Effects projects (`.aep`, `.aepx`) given to File ▸ Import ▸ File… or dropped on the window
import as File ▸ Import ▸ Adobe After Effects Project… does ([aep-import.md](aep-import.md)).
## Finding a layer's source in the Project panel
Right-click a layer in the Timeline or Composition panel and choose **Reveal Layer Source in
+34 -15
View File
@@ -11,7 +11,7 @@ Guide):
| After Effects | EffectCraft | |
|---|---|---|
| Effect plug-ins (C/C++ SDK, `.aex` / `.plugin`) | [Effect plug-ins](#effect-plug-ins): WebAssembly modules (plug-in API v1) or Rust; the `Plug-ins` folder next to the settings loads at start-up | Different ABI: After Effects SDK plug-ins can't run (G8) |
| Effect plug-ins (C/C++ SDK, `.aex` / `.plugin`) | [Effect plug-ins](#effect-plug-ins): WebAssembly modules (WebAssembly ABI 1 and 2) or Rust; the `Plug-ins` folder next to the settings loads at start-up | Different ABI: After Effects SDK plug-ins can't run (G8) |
| Scripts folder: its scripts are listed in File ▸ Scripts | `Scripts/` in the settings folder (File ▸ Scripts ▸ Install Script File… copies a script there), listed in File ▸ Scripts with the bundled samples; a subfolder (one level) is a submenu named after it | Same |
| File ▸ Scripts ▸ Run Script File… (`.jsx`, `.jsxbin`) | `.jsx` / `.js` | Gap: `.jsxbin` (an undocumented binary encoding) |
| File ▸ Scripts ▸ Recent Script Files: the 10 most recently run scripts; Cmd+Option+Shift+D (Ctrl+Alt+Shift+D on Windows) reruns one | File ▸ Scripts ▸ Recent Script Files lists the 10 scripts run last (script files and File ▸ Scripts entries, newest first, kept in the settings); Cmd+Alt+Shift+D runs the most recent one again (`file.runScript {"recent": 0}`, a key that can be changed in Edit ▸ Keyboard Shortcuts) | Same |
@@ -152,7 +152,7 @@ example of a keyframe tool.
## Effect plug-ins
EffectCraft loads third-party effects through a small, versioned plug-in API (**API version 2**; WebAssembly ABI 1).
EffectCraft loads third-party effects through a small, versioned plug-in API (**API version 2**; WebAssembly ABI 1 and 2).
A plug-in effect behaves exactly like a built-in one: it is listed in Effects & Presets (and the
Effect menu) under its category, applied by id (`effect.apply {"effect": "org.example.posterize"}`)
or display name, its parameters are ordinary properties (keyframes, expressions, Effect Controls,
@@ -178,7 +178,8 @@ author, source, params}]}`.
* **Parameters** arrive flattened to `f64`s in the order they are declared: slider, angle,
checkbox (0 / 1) and popup (0-based index) take one value, point two (already in **buffer
pixels**), colour four (RGBA 0–1).
* **Time** is layer time in seconds.
* **Time** is layer time in seconds. A WebAssembly module written for ABI 2 also gets the
layer's neighbouring frames at other times (see [WebAssembly ABI](#webassembly-abi)).
* Rendering must be **deterministic** and may run on several threads at once (a WebAssembly
plug-in gets one instance per thread).
* A failed render (an error code, a trap, running out of fuel) leaves the frame unchanged.
@@ -216,22 +217,36 @@ Every plug-in describes itself with a JSON manifest:
parameters; a slider needs `min ≤ default ≤ max` (and `sliderMin ≤ sliderMax`); the manifest
is at most 1 MiB.
### WebAssembly ABI (v1)
### WebAssembly ABI
A module with **no imports** that exports:
A module with **no imports** exports:
| export | signature | |
|---|---|---|
| `memory` | memory | linear memory shared with the host |
| `ec_api_version` | `() -> i32` | must return `1` |
| `ec_api_version` | `() -> i32` | `1` for ABI 1, `2` for ABI 2 (neighbouring frames) |
| `ec_manifest_ptr`, `ec_manifest_len` | `() -> i32` | the UTF-8 JSON manifest in memory |
| `ec_alloc` | `(bytes: i32) -> i32` | a buffer of at least `bytes` bytes, 8-byte aligned; it may reuse the previous one |
| `ec_render` | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; return 0 on success |
| `ec_render` (ABI 1) | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; return 0 on success |
| `ec_render2` (ABI 2) | `ec_render`'s arguments, then `prev: i32, prev_time: f64, next: i32, next_time: f64, flags: i32` | the same, with the neighbouring frames; return 0 on success |
For each frame the host calls `ec_alloc` once for a buffer holding the parameters (`nparams`
little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s), fills it,
calls `ec_render`, and reads the pixels back. Each call gets a fuel budget proportional to the
frame size; a module that loops forever stops with an error instead of hanging the app.
little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s,
premultiplied RGBA, row-major), fills it, calls the module's render export, and reads the pixels
back. An ABI 2 module gets two more pixel buffers after the current frame: the previous and next
frames, resampled onto the current buffer's grid. At a frame boundary they are the frames before
and after it (`time ± 1/fps`); between frames they are the two frames that bracket `time`.
The neighbour buffers are always allocated (zeroed when the layer has no pixels there, so a
module can read both without a null check). `flags` bit 0 is set when `prev` holds a real frame,
bit 1 when `next` does; `prev_time` / `next_time` are those frames' layer times in seconds (0
when absent). The ABI version is the module's `ec_api_version` and is independent of the
manifest's `api` (the native plug-in API): a module can use ABI 2 while its manifest says
`"api": 1`. Because an ABI 2 module reads other times, its layer is keyed on the layer time in
the render cache; ABI 1 modules keep the time-independent key.
Each call gets a fuel budget proportional to the frame size; a module that loops forever stops
with an error instead of hanging the app.
#### Example
@@ -324,8 +339,10 @@ register_plugin(Arc::new(Swap(manifest)))?; // before the UI builds its menus
## Plug-in API 2 (native plug-ins)
API 2 is for Rust plug-ins and for hosts that bring effects from elsewhere (such as
[external effect helpers](external-helpers.md)); the WebAssembly ABI stays at 1. Manifests with `"api": 1` or `2` are accepted,
`effect.plugins.list` reports `api: 2`, and up to 1024 plug-in effects can be registered.
[external effect helpers](external-helpers.md)); the WebAssembly ABI is versioned separately
(ABI 2 adds the neighbouring frames, see [WebAssembly ABI](#webassembly-abi)). Manifests with
`"api": 1` or `2` are accepted, `effect.plugins.list` reports `api: 2`, and up to 1024 plug-in
effects can be registered.
* **New parameter types**: `point3` (`default` `[x, y, z]`; x, y arrive in buffer pixels, z as is),
`layer` (another layer of the composition; flattens to its id, -1 for none, read with
@@ -354,9 +371,11 @@ API 2 is for Rust plug-ins and for hosts that bring effects from elsewhere (such
### Compatibility
The API version is checked when a plug-in loads: a manifest or module for another version is
refused with a message. Within a version the contract above does not change; additions (new
parameter types, new optional exports) bump the version.
The API version is checked when a plug-in loads: this build implements plug-in API 1 and 2 and
WebAssembly ABI 1 and 2; a manifest or module for another version is refused with a message.
ABI 1 modules load and render unchanged; ABI 2 modules add `ec_render2` and the neighbouring
frames. Within a version the contract above does not change; additions (new parameter types,
new optional exports) bump the version.
## Plug-in Library
+8
View File
@@ -82,6 +82,14 @@ Commands (CLI, MCP, control channel):
- `memory.layerCacheMb`: Layer Cache (in the browser at most 512 MB, see [web.md](web.md))
- `memory.mediaCacheMb`: Footage Frame Cache (in the browser at most 256 MB)
- `memory.previewCacheMb`: Preview (RAM) Cache
- `memory.gpuFramesInFlight`: GPU Frames Rendered at Once (`auto`, `1`, `2`, `3`): how many
frames the Render Queue renders on the GPU at once. Auto measures the GPU memory after the
job's first GPU frame and fits up to 3 frames into the GPU memory cap (by default a
conservative 3 GiB on a discrete card, less when its memory is known, 2 GiB on an integrated
one; `EFFECTCRAFT_GPU_MEMCAP_MB` sets it), keeping room for the upload cache to fill. It
renders one frame at a time where the memory can't be measured (Metal, WebGPU, GL, or the
cap turned off) and once frames have had to wait for memory. The render log names the count.
`EFFECTCRAFT_GPU_INFLIGHT` overrides it.
- `threeD.defaultRenderer`: Default 3D Renderer
- `labels.N.name` / `labels.N.color`: the 16 label names and colours, used by the Label menu,
the timeline, project panel, render queue and viewer handles
+25 -1
View File
@@ -2,7 +2,7 @@
```sh
cargo test --workspace # everything
cargo xtask ci # fmt, clippy -D warnings, tests, pdf-deps, layers, assets, wasm
cargo xtask ci # fmt, clippy -D warnings, tests, pdf-deps, lock-patches, layers, assets, wasm
```
The After Effects test projects of `crates/aep` are not in the repository; without them the
@@ -112,3 +112,27 @@ and line instead of being discarded. `--fail-over PCT` requires the same run key
successful latest runs, and at least one comparable resource metric per run. Failed latest runs,
missing coverage, empty results and wall-time-only measurements cannot pass the gate. Omit
`--fail-over` for an exploratory partial comparison; failed latest runs remain visible.
## Unexpected desktop exit diagnostics
The desktop starts a separate, lightweight reporter before loading fonts, projects or the GPU.
A normal exit (including a handled startup error) sends a clean marker only after desktop
teardown. macOS finishes at the end of its successful exit hook because its native event loop
terminates without returning to main; other platforms finish after the event loop returns.
A teardown panic leaves reporting armed. An abort or other unexpected process termination
closes the pipe without that marker.
The helper saves a local report under the session log folder's `Crash Reports` directory and
shows a native error dialog. Control-server launches save the report without a modal dialog.
Reports contain version/platform and at most the last 64 KiB of the matching session's log.
Logging filters and the session file size cap can omit later events from that tail.
They are never uploaded automatically. Review logs for private project paths before sharing.
Run `cargo test -p effectcraft crash_report::tests` for bounded protocol, normal shutdown,
actual subprocess abort, unrelated child handles, concurrent sessions, missing log/helper,
log rotation and report-write failure coverage. The three ignored tests are subprocess entry
points launched by these tests, not manual tests.
This is diagnosis, not memory recovery: a pipe closing does not prove an out-of-memory failure
or provide the process's exit code. System-wide exhaustion, killing the whole process tree,
or an unavailable native dialog service can also prevent the helper from reporting. Existing
auto-save recovery remains responsible for restoring previously saved work.
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "posterize-bands"
description = "Example EffectCraft effect plug-in (WebAssembly, plug-in API v1): posterizes luminance into tinted bands"
description = "Example EffectCraft effect plug-in (WebAssembly ABI 1): posterizes luminance into tinted bands"
version = "1.0.0"
edition = "2024"
license = "MIT OR Apache-2.0"
+1 -1
View File
@@ -1,4 +1,4 @@
//! Posterize Bands: an example EffectCraft effect plug-in (plug-in API v1, see
//! Posterize Bands: an example EffectCraft effect plug-in (WebAssembly ABI 1, see
//! `docs/plugins.md`). It quantizes each pixel's luminance into `levels` bands and mixes a tint
//! colour in by `mix` percent, optionally only above a `threshold`.
//!
+71
View File
@@ -0,0 +1,71 @@
//! Fail when `Cargo.lock` records a `[patch]` entry nothing uses.
//!
//! An unused patch only makes cargo warn, but the Nix package builds offline from vendored
//! sources, which hold only the packages the build uses: it cannot find the patched package and
//! refuses to build (#3403). Patch a crate together with the dependency that needs it.
use std::path::Path;
pub fn run(root: &Path) -> Result<(), String> {
let lock = std::fs::read_to_string(root.join("Cargo.lock")).map_err(|e| format!("Cargo.lock: {e}"))?;
let unused = unused_patches(&lock);
if unused.is_empty() {
println!("Cargo.lock: every [patch] entry is used");
return Ok(());
}
Err(format!(
"Cargo.lock has [[patch.unused]] entries: {}. Remove them from [patch] in Cargo.toml (add a patch together with the dependency that needs it); the Nix build, which uses vendored sources, fails on them (#3403)",
unused.join(", ")
))
}
/// The package names of the `[[patch.unused]]` entries in the text of a `Cargo.lock`.
fn unused_patches(lock: &str) -> Vec<String> {
let mut names = Vec::new();
let mut in_unused = false;
for line in lock.lines() {
let line = line.trim();
if line.starts_with('[') {
in_unused = line == "[[patch.unused]]";
} else if let Some(name) = line.strip_prefix("name = ").filter(|_| in_unused) {
names.push(name.trim_matches('"').to_string());
}
}
names
}
#[cfg(test)]
mod tests {
use super::*;
const LOCK: &str = r#"
[[package]]
name = "egui"
version = "0.37.0"
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc#6d518fc"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
"#;
#[test]
fn lists_the_unused_patches_and_only_those() {
assert_eq!(unused_patches(LOCK), ["egui_demo_lib", "egui_inspection"]);
}
#[test]
fn a_lock_without_unused_patches_is_accepted() {
assert!(unused_patches("[[package]]\nname = \"egui\"\nversion = \"0.37.0\"\n").is_empty());
}
#[test]
fn the_repository_lock_has_no_unused_patches() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
assert!(run(&root).is_ok(), "{:?}", run(&root));
}
}
+7 -3
View File
@@ -1,4 +1,4 @@
//! Workspace automation: `cargo xtask <layers|assets|pdf-deps|aep-corpus|wasm|web|ico|ci>`.
//! Workspace automation: `cargo xtask <layers|assets|pdf-deps|lock-patches|aep-corpus|wasm|web|ico|ci>`.
//!
//! - `layers`: enforces the dependency layering of `docs/architecture.md` §1 (downward-only edges,
//! listed same-layer edges, no UI/OS crates below L5).
@@ -6,6 +6,7 @@
//! and the web app.
//! - `web [--dev] [--serve PORT]`: build the web app (`apps/effectcraft-web`) into
//! `<target>/web/dist` with `wasm-bindgen` (docs/web.md); `--serve` serves it on localhost.
//! - `lock-patches`: `Cargo.lock` records no unused `[patch]` entry (the Nix build cannot vendor one).
//! - `assets`: every asset file (image, icon, font, LUT, audio, video…) has a complete
//! `<file>.attribution` sidecar and an entry in `ATTRIBUTION.md` (AGENTS.md §1).
//! - `aep-corpus [--dir DIR]`: fetch the After Effects test corpus at the commit pinned in
@@ -19,6 +20,7 @@
mod aep_corpus;
mod bench;
mod ico;
mod lock_patches;
mod pdf_deps;
mod version;
@@ -534,12 +536,13 @@ type Stage = fn() -> Result<(), String>;
/// Every stage runs even when an earlier one fails, so one run reports all the problems.
fn ci() -> Result<(), String> {
let stages: [(&str, Stage); 8] = [
let stages: [(&str, Stage); 9] = [
("audio backend", audio_backend),
("fmt", || run(Command::new(env!("CARGO")).args(["fmt", "--check"]))),
("clippy", || run(Command::new(env!("CARGO")).args(["clippy", "--workspace", "--all-targets", "--", "-D", "warnings"]))),
("tests", tests),
("pdf-deps", || pdf_deps::run(&root())),
("lock-patches", || lock_patches::run(&root())),
("layers", layers),
("assets", assets),
("wasm", wasm),
@@ -555,6 +558,7 @@ fn main() -> ExitCode {
"wasm" => wasm(),
"assets" => assets(),
"pdf-deps" => pdf_deps::run(&root()),
"lock-patches" => lock_patches::run(&root()),
"aep-corpus" => aep_corpus::run(&root(), &std::env::args().skip(2).collect::<Vec<_>>()),
"ci" => ci(),
"audio-backend" => audio_backend(),
@@ -568,7 +572,7 @@ fn main() -> ExitCode {
let rest: Vec<String> = std::env::args().skip(2).collect();
version::run(&root(), &rest.iter().map(String::as_str).collect::<Vec<_>>())
}
_ => Err("usage: cargo xtask <layers|assets|pdf-deps|aep-corpus [--dir DIR]|wasm|web [--dev] [--serve PORT]|ico OUT.ico IN.png…|version [set X.Y.Z]|ci|audio-backend|bench [--bin CLI] [--quick] [--project F --comp C] [--compare OLD.jsonl] (--help)>".into()),
_ => Err("usage: cargo xtask <layers|assets|pdf-deps|lock-patches|aep-corpus [--dir DIR]|wasm|web [--dev] [--serve PORT]|ico OUT.ico IN.png…|version [set X.Y.Z]|ci|audio-backend|bench [--bin CLI] [--quick] [--project F --comp C] [--compare OLD.jsonl] (--help)>".into()),
};
match r {
Ok(()) => ExitCode::SUCCESS,
+1 -1
View File
@@ -5,7 +5,7 @@ use std::process::Command;
use serde_json::Value;
const REV: &str = "92d392cff132eaf53e13afff77f38b5c11b73aa0";
const REV: &str = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545";
const PACKAGES: &[(&str, &str)] = &[("craft-pdf", "0.1.0"), ("hayro-syntax", "0.7.2"), ("hayro-jbig2", "0.3.0")];
pub fn run(root: &Path) -> Result<(), String> {