Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
A render says how far it has got while it runs: ~/.raea/renders/<pid>-<n>.json, for the app's meter
shinyobjectz9a6cd12d5f55
diff --git a/runtime/encode/src/lib.rs b/runtime/encode/src/lib.rsindex ac59e419..d06a6c09 100644--- a/runtime/encode/src/lib.rs+++ b/runtime/encode/src/lib.rs@@ -14,6 +14,7 @@ pub use raea_render::{dir_of, inserted_of, save_png};pub mod html;mod sound;pub use sound::*;+mod progress;mod render;pub use render::*;diff --git a/runtime/encode/src/progress.rs b/runtime/encode/src/progress.rsnew file mode 100644index 00000000..3f211095--- /dev/null+++ b/runtime/encode/src/progress.rs@@ -0,0 +1,108 @@+//! What a render is doing while it runs, for whatever shows it: the raea app's chat draws a meter from it (the+//! developer, 11-16: "in the chat ui having something that if its rendering we can see a progress meter"). One small+//! JSON file per render, `<renders>/<pid>-<n>.json`:+//!+//! `{"pid", "out", "cwd", "frames", "done", "started", "updated"}` (times in ms since the epoch)+//!+//! It's written when the render starts, as its frames finish (at most four times a second, and at the last frame),+//! and taken away when the render ends, however it ends; one left by a process that died is a reader's to pass over+//! (its pid is gone). `<renders>` is $RAEA_RENDERS, else ~/.raea/renders; `RAEA_RENDERS=off` writes none.+use std::path::{Path, PathBuf};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};++static SEQ: AtomicUsize = AtomicUsize::new(0);+const EVERY: Duration = Duration::from_millis(250);++/// A render's progress, kept on disk while it lives.+pub struct Progress {+ file: Option<PathBuf>,+ out: String,+ cwd: String,+ frames: usize,+ done: usize,+ started: u64,+ last: Option<Instant>,+}++fn dir() -> Option<PathBuf> {+ match std::env::var_os("RAEA_RENDERS") {+ Some(v) if v == "off" => None,+ Some(v) if !v.is_empty() => Some(PathBuf::from(v)),+ _ => std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(|h| PathBuf::from(h).join(".raea").join("renders")),+ }+}++fn now_ms() -> u64 {+ SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)+}++impl Progress {+ /// A render of `frames` frames to `out`, starting.+ pub fn start(out: &Path, frames: usize) -> Self {+ Self::start_in(dir(), out, frames)+ }++ fn start_in(dir: Option<PathBuf>, out: &Path, frames: usize) -> Self {+ let file = dir.and_then(|d| std::fs::create_dir_all(&d).ok().map(|_| d.join(format!("{}-{}.json", std::process::id(), SEQ.fetch_add(1, Ordering::Relaxed)))));+ let out = std::path::absolute(out).unwrap_or_else(|_| out.to_path_buf()).display().to_string();+ let cwd = std::env::current_dir().map(|d| d.display().to_string()).unwrap_or_default();+ let mut p = Progress { file, out, cwd, frames, done: 0, started: now_ms(), last: None };+ p.write();+ p+ }++ /// One more frame done.+ pub fn frame(&mut self) {+ self.done += 1;+ if self.done >= self.frames || self.last.is_none_or(|t| t.elapsed() >= EVERY) {+ self.write();+ }+ }++ fn write(&mut self) {+ let Some(f) = &self.file else { return };+ self.last = Some(Instant::now());+ let body = serde_json::json!({"pid": std::process::id(), "out": self.out, "cwd": self.cwd, "frames": self.frames, "done": self.done, "started": self.started, "updated": now_ms()});+ // written whole beside it, then put in its place: a reader never sees half of one+ let part = f.with_extension("json.part");+ if std::fs::write(&part, body.to_string()).is_ok() {+ let _ = std::fs::rename(&part, f);+ }+ }+}++impl Drop for Progress {+ fn drop(&mut self) {+ if let Some(f) = &self.file {+ let _ = std::fs::remove_file(f);+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn written_as_frames_finish_and_gone_when_the_render_ends() {+ let d = std::env::temp_dir().join(format!("raea-renders-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&d);+ let read = || -> serde_json::Value {+ let f = std::fs::read_dir(&d).unwrap().filter_map(|e| e.ok()).map(|e| e.path()).find(|p| p.extension().is_some_and(|x| x == "json")).unwrap();+ serde_json::from_str(&std::fs::read_to_string(f).unwrap()).unwrap()+ };+ let mut p = Progress::start_in(Some(d.clone()), Path::new("film.mp4"), 3);+ let v = read();+ assert_eq!((v["frames"].as_u64(), v["done"].as_u64(), v["pid"].as_u64()), (Some(3), Some(0), Some(std::process::id() as u64)));+ assert!(v["out"].as_str().unwrap().ends_with("film.mp4") && Path::new(v["out"].as_str().unwrap()).is_absolute());+ p.frame();+ p.frame();+ p.frame();+ // the last frame is always written, however soon after the one before+ assert_eq!(read()["done"].as_u64(), Some(3));+ drop(p);+ assert_eq!(std::fs::read_dir(&d).unwrap().count(), 0);+ let _ = std::fs::remove_dir_all(&d);+ }+}diff --git a/runtime/encode/src/render.rs b/runtime/encode/src/render.rsindex db486834..261ab7a0 100644--- a/runtime/encode/src/render.rs+++ b/runtime/encode/src/render.rs@@ -103,8 +103,11 @@ pub fn render_fed(}let t_frames = std::time::Instant::now();let mut n = 0;+ // how far it's got, for whatever shows it (progress.rs)+ let mut meter = crate::progress::Progress::start(out, list.len());let dt = v.render_nv12_fed(&list, feed, |f, px| {n += 1;+ meter.frame();direct_sink(tx.as_ref(), &mut lines, f, px)});drop(tx);@@ -197,7 +200,9 @@ pub fn render_with(prog: &Program, rows: &[Tup], dir: &Path, out: &Path, ro: &Re}));}let t_frames = std::time::Instant::now();+ let mut meter = crate::progress::Progress::start(out, list.len());let tm = v.render_nv12(&list, |f, px| {+ meter.frame();if let Some(tx) = tx.as_ref() {return tx.send(px).map_err(|_| "the encoder stopped".to_string());}@@ -269,8 +274,10 @@ pub fn render_with(prog: &Program, rows: &[Tup], dir: &Path, out: &Path, ro: &Re}let t_frames = std::time::Instant::now();let mut n = 0;+ let mut meter = crate::progress::Progress::start(out, list.len());let dt = v.render_nv12(&list, |f, px| {+ meter.frame();match pngs {true => save_png(&if still { out.to_path_buf() } else { out.join(format!("frame-{f:05}.png")) }, &straight(px), w, h)?,false => direct_sink(tx.as_ref(), &mut lines, f, px)?,