Skip to content

armgit / raea

Public Imported from GitHub
0

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.

render --alpha: transparent PNGs for overlays

Leonardo Gonzalez30c80d9a1f96
diff --git a/core/k/kernels/composite.k b/core/k/kernels/composite.k
index 01e739f2..b03db4cc 100644
--- a/core/k/kernels/composite.k
+++ b/core/k/kernels/composite.k
@@ -9,6 +9,7 @@ bg:tex[T;x;y]
r:and[bg;255]
g:and[shr[bg;8];255]
b:and[shr[bg;16];255]
+a:and[shr[bg;24];255]
/ clip slot 0
on0:word[C;0]
bx0:word[C;1]
@@ -62,6 +63,7 @@ bb0:clamp[shr[k0+(541*cu0)+128;8];0;255]
r:shr[(am0*rr0)+((256-am0)*r);8]
g:shr[(am0*gg0)+((256-am0)*g);8]
b:shr[(am0*bb0)+((256-am0)*b);8]
+a:shr[(am0*255)+((256-am0)*a);8]
end
/ clip slot 1
on1:word[C;12]
@@ -116,6 +118,7 @@ bb1:clamp[shr[k1+(541*cu1)+128;8];0;255]
r:shr[(am1*rr1)+((256-am1)*r);8]
g:shr[(am1*gg1)+((256-am1)*g);8]
b:shr[(am1*bb1)+((256-am1)*b);8]
+a:shr[(am1*255)+((256-am1)*a);8]
end
/ clip slot 2
on2:word[C;24]
@@ -170,6 +173,7 @@ bb2:clamp[shr[k2+(541*cu2)+128;8];0;255]
r:shr[(am2*rr2)+((256-am2)*r);8]
g:shr[(am2*gg2)+((256-am2)*g);8]
b:shr[(am2*bb2)+((256-am2)*b);8]
+a:shr[(am2*255)+((256-am2)*a);8]
end
/ clip slot 3
on3:word[C;36]
@@ -224,6 +228,7 @@ bb3:clamp[shr[k3+(541*cu3)+128;8];0;255]
r:shr[(am3*rr3)+((256-am3)*r);8]
g:shr[(am3*gg3)+((256-am3)*g);8]
b:shr[(am3*bb3)+((256-am3)*b);8]
+a:shr[(am3*255)+((256-am3)*a);8]
end
/ the overlay, premultiplied: o + below * (255 - a) / 255
o:word[O;i]
@@ -231,4 +236,5 @@ oa:and[shr[o;24];255]
r:and[o;255]+((r*(255-oa))+127)%255
g:and[shr[o;8];255]+((g*(255-oa))+127)%255
b:and[shr[o;16];255]+((b*(255-oa))+127)%255
-px:pack[r;g;b;255]
+a:oa+((a*(255-oa))+127)%255
+px:pack[r;g;b;a]
diff --git a/core/k/kernels/composite_tex.k b/core/k/kernels/composite_tex.k
index 514d12f8..9e624b9a 100644
--- a/core/k/kernels/composite_tex.k
+++ b/core/k/kernels/composite_tex.k
@@ -8,6 +8,7 @@ bg:tex[T;x;y]
r:and[bg;255]
g:and[shr[bg;8];255]
b:and[shr[bg;16];255]
+a:and[shr[bg;24];255]
/ clip slot 0
on0:word[C;0]
bx0:word[C;1]
@@ -60,6 +61,7 @@ bb0:clamp[shr[k0+(541*cu0)+128;8];0;255]
r:shr[(am0*rr0)+((256-am0)*r);8]
g:shr[(am0*gg0)+((256-am0)*g);8]
b:shr[(am0*bb0)+((256-am0)*b);8]
+a:shr[(am0*255)+((256-am0)*a);8]
end
/ clip slot 1
on1:word[C;12]
@@ -113,6 +115,7 @@ bb1:clamp[shr[k1+(541*cu1)+128;8];0;255]
r:shr[(am1*rr1)+((256-am1)*r);8]
g:shr[(am1*gg1)+((256-am1)*g);8]
b:shr[(am1*bb1)+((256-am1)*b);8]
+a:shr[(am1*255)+((256-am1)*a);8]
end
/ clip slot 2
on2:word[C;24]
@@ -166,6 +169,7 @@ bb2:clamp[shr[k2+(541*cu2)+128;8];0;255]
r:shr[(am2*rr2)+((256-am2)*r);8]
g:shr[(am2*gg2)+((256-am2)*g);8]
b:shr[(am2*bb2)+((256-am2)*b);8]
+a:shr[(am2*255)+((256-am2)*a);8]
end
/ clip slot 3
on3:word[C;36]
@@ -219,6 +223,7 @@ bb3:clamp[shr[k3+(541*cu3)+128;8];0;255]
r:shr[(am3*rr3)+((256-am3)*r);8]
g:shr[(am3*gg3)+((256-am3)*g);8]
b:shr[(am3*bb3)+((256-am3)*b);8]
+a:shr[(am3*255)+((256-am3)*a);8]
end
/ the overlay, premultiplied: o + below * (255 - a) / 255
o:word[O;i]
@@ -226,4 +231,5 @@ oa:and[shr[o;24];255]
r:and[o;255]+((r*(255-oa))+127)%255
g:and[shr[o;8];255]+((g*(255-oa))+127)%255
b:and[shr[o;16];255]+((b*(255-oa))+127)%255
-px:pack[r;g;b;255]
+a:oa+((a*(255-oa))+127)%255
+px:pack[r;g;b;a]
diff --git a/core/k/kernels/gen.py b/core/k/kernels/gen.py
index 2682976c..d3a7a562 100644
--- a/core/k/kernels/gen.py
+++ b/core/k/kernels/gen.py
@@ -9,7 +9,10 @@ L = ["/ The frame on the GPU: the camera's target T is the background (clear col
"/ Luma and chroma are bilinear; YUV is BT.709 limited range in 8.8 fixed point. One RGBA word per pixel.",
"x:i!w", "y:i%w",
"bg:tex[T;x;y]",
- "r:and[bg;255]", "g:and[shr[bg;8];255]", "b:and[shr[bg;16];255]"]
+ "r:and[bg;255]", "g:and[shr[bg;8];255]", "b:and[shr[bg;16];255]",
+ # coverage, carried so a transparent clear (render --alpha) stays transparent where nothing is drawn; over an
+ # opaque background every step below keeps it 255, so those frames are the same pixels as before
+ "a:and[shr[bg;24];255]"]
for s in range(SLOTS):
P, c = f"P{s}", lambda k: f"word[C;{s * 12 + k}]"
L += [f"/ clip slot {s}",
@@ -46,13 +49,15 @@ for s in range(SLOTS):
f"r:shr[(am{s}*rr{s})+((256-am{s})*r);8]",
f"g:shr[(am{s}*gg{s})+((256-am{s})*g);8]",
f"b:shr[(am{s}*bb{s})+((256-am{s})*b);8]",
+ f"a:shr[(am{s}*255)+((256-am{s})*a);8]",
"end"]
L += ["/ the overlay, premultiplied: o + below * (255 - a) / 255",
"o:word[O;i]", "oa:and[shr[o;24];255]",
"r:and[o;255]+((r*(255-oa))+127)%255",
"g:and[shr[o;8];255]+((g*(255-oa))+127)%255",
"b:and[shr[o;16];255]+((b*(255-oa))+127)%255",
- "px:pack[r;g;b;255]"]
+ "a:oa+((a*(255-oa))+127)%255",
+ "px:pack[r;g;b;a]"]
open("kernels/composite.k", "w").write("\n".join(L) + "\n")
# composite_tex.k: the same, with each slot's planes as textures, as VideoToolbox's IOSurface-backed buffers wrap
diff --git a/engine/cli/src/cli.rs b/engine/cli/src/cli.rs
index b5acb40e..942ed113 100644
--- a/engine/cli/src/cli.rs
+++ b/engine/cli/src/cli.rs
@@ -255,7 +255,7 @@ pub fn run(args: &[String]) -> R<String> {
}
if args.first().map(String::as_str) != Some("render") || args.len() < 2 {
return Err(format!(
- "raea render EDIT.tsx -o OUT.mp4|OUT.png|DIR [--frame N | --at A,B] [--composition ID] [--w W] [--h H] [--fps FPS] [--frames N] [--words W.json] [--perception P.json] [--input I.json|inputs.rrl] [--manifest render.json] [--stage WxH] [--10bit] [--kbps N | --quality draft|high|master|0..1] (also: --lite --comps)\n raea render PAGE.html -o OUT.mp4|DIR [--at A,B] (an HTML composition, the HTML composition format format)\n raea render --bundle NAME.comp.js -o OUT.png|DIR [--dir DIR] [--frame N] (a bundle from raea bundle, as render_bundle draws it)
+ "raea render EDIT.tsx -o OUT.mp4|OUT.png|DIR [--frame N | --at A,B] [--composition ID] [--w W] [--h H] [--fps FPS] [--frames N] [--words W.json] [--perception P.json] [--input I.json|inputs.rrl] [--manifest render.json] [--stage WxH] [--10bit] [--alpha] [--kbps N | --quality draft|high|master|0..1] (also: --lite --comps)\n raea render PAGE.html -o OUT.mp4|DIR [--at A,B] (an HTML composition, the HTML composition format format)\n raea render --bundle NAME.comp.js -o OUT.png|DIR [--dir DIR] [--frame N] (a bundle from raea bundle, as render_bundle draws it)
raea media probe|still|sheet|audio|loudness|motion|cuts|freezes|proxy|trim|concat FILE ... | ffmpeg ARGS | ffprobe ARGS (media work, native: media help)
raea bundle EDIT.tsx --composition-bundle -o OUT.js [--portable DIR] (a composition for render_bundle, on the Mac or the iPhone)\n raea add PKG[@RANGE]... [--dev] | remove NAME... | install [--offline] [--in DIR] (npm packages for the project: raea.lock, node_modules)\n {} (foley, local)\n {} (an instrument's notes, local)\n raea setup-audio [--check] [sao|tokensynth] (install or check the local audio models)\n {} (the song's energy map: beats, sections, builds, drops, stops, hits, cuts per bar; also: --backend NAME)\n raea sounds [WORDS] | sounds get PACK/INSTRUMENT [--notes ..|--all] [-o DIR] (open sample libraries for a Sampler)\n {} (the clip's camera solved, for <Matchmove>)",
tools::sound::SAO_USAGE, tools::sound::TOKENSYNTH_USAGE, tools::song::USAGE, motion::track::USAGE
diff --git a/runtime/encode/src/render.rs b/runtime/encode/src/render.rs
index ab86f6e6..e05c451a 100644
--- a/runtime/encode/src/render.rs
+++ b/runtime/encode/src/render.rs
@@ -337,7 +337,23 @@ pub fn render_with(prog: &Program, rows: &[Tup], dir: &Path, out: &Path, ro: &Re
} else {
std::fs::create_dir_all(&out).map_err(|e| e.to_string())?;
let nv12 = e.nv12;
- let rgba_of = |px: Vec<u8>| if nv12 { raea_io_media::yuv::nv12_to_rgba(&px, w as usize, h as usize) } else { px };
+ // the composite is premultiplied; a PNG holds straight colour. Only a transparent clear (render --alpha) leaves
+ // alpha under 255, so an opaque frame is written as it comes
+ let rgba_of = |px: Vec<u8>| {
+ if nv12 {
+ return raea_io_media::yuv::nv12_to_rgba(&px, w as usize, h as usize);
+ }
+ let mut px = px;
+ for p in px.chunks_exact_mut(4) {
+ let a = p[3] as u32;
+ if a > 0 && a < 255 {
+ for c in &mut p[..3] {
+ *c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8;
+ }
+ }
+ }
+ px
+ };
e.frames(&list, |f, px| save_png(&out.join(format!("frame-{f:05}.png")), &rgba_of(px), w, h))?;
}
eprintln!("{} ({} frames) in {:.2}s", out.display(), list.len(), t0.elapsed().as_secs_f64());
diff --git a/runtime/export/src/lib.rs b/runtime/export/src/lib.rs
index 2bea5bfb..8af4ed06 100644
--- a/runtime/export/src/lib.rs
+++ b/runtime/export/src/lib.rs
@@ -74,6 +74,10 @@ static STAGE: std::sync::Mutex<Option<(u32, u32)>> = std::sync::Mutex::new(None)
/// render exactly (8-bit limited-range H.264 errs up to 2/255 by its own round trip; lightspeed Q3.colour).
static TEN_BIT: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+/// `render --alpha`: PNGs (a frame or a folder) kept transparent where the composition draws nothing, the pages
+/// composited over a clear background instead of black: overlays for another editor's timeline.
+static ALPHA: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
/// `render --kbps N`: the video's average bitrate (raea_io_media::default_kbps when 0, from the size and frame rate).
static KBPS: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
diff --git a/runtime/export/src/program.rs b/runtime/export/src/program.rs
index 6abce85a..c6e3b9af 100644
--- a/runtime/export/src/program.rs
+++ b/runtime/export/src/program.rs
@@ -13,7 +13,8 @@ pub(crate) fn program(frames: &[(i64, Frame)], w: u32, h: u32, fps: f64) -> raea
r.insert("id".into(), V::S(id.into()));
r
};
- let clear = frames.first().and_then(|(_, f)| f.clear.clone()).and_then(|c| hex_of(&c)).unwrap_or("#000000".into());
+ // --alpha: nothing under the pages, so what the composition leaves empty stays transparent
+ let clear = if ALPHA.load(std::sync::atomic::Ordering::Relaxed) { "#00000000".into() } else { frames.first().and_then(|(_, f)| f.clear.clone()).and_then(|c| hex_of(&c)).unwrap_or("#000000".into()) };
let has3d = frames.iter().any(|(_, f)| !f.scene.is_empty());
let mut cam = row("cam", vec![("bg", V::S(clear))]);
if has3d {
diff --git a/runtime/export/src/render.rs b/runtime/export/src/render.rs
index d2c7ce93..6198bc8c 100644
--- a/runtime/export/src/render.rs
+++ b/runtime/export/src/render.rs
@@ -49,6 +49,11 @@ pub fn render(args: &[String]) -> R<String> {
};
*MANIFEST.lock().unwrap() = flag(args, "--manifest").map(PathBuf::from);
TEN_BIT.store(args.iter().any(|a| a == "--10bit"), std::sync::atomic::Ordering::Relaxed);
+ let alpha = args.iter().any(|a| a == "--alpha");
+ if alpha && !matches!(out_ext(&out).as_str(), "png" | "") {
+ return Err("--alpha writes PNGs (OUT.png or a folder): video here is H.264 or HEVC, which hold no alpha. Encode the folder with alpha after (ffmpeg -i DIR/frame-%05d.png -c:v prores_ks -profile:v 4444 -pix_fmt yuva444p10le overlay.mov)".into());
+ }
+ ALPHA.store(alpha, std::sync::atomic::Ordering::Relaxed);
let kbps = flag(args, "--kbps").map(|k| k.parse::<u32>().ok().filter(|k| *k > 0).ok_or("--kbps takes a bitrate in kbit/s")).transpose()?;
KBPS.store(kbps.unwrap_or(0), std::sync::atomic::Ordering::Relaxed);
let quality = flag(args, "--quality").map(|q| raea_io_media::parse_quality(&q)).transpose()?;
diff --git a/sdk/skills/raea/SKILL.md b/sdk/skills/raea/SKILL.md
index ef1d135c..bc167548 100644
--- a/sdk/skills/raea/SKILL.md
+++ b/sdk/skills/raea/SKILL.md
@@ -152,6 +152,7 @@ raea render piece.tsx -o piece.mp4 # the whole composition
raea render piece.tsx --frame 60 -o frame60.png # one frame, to look at
raea render piece.tsx --at 0,45,89 -o frames/ # a few frames
raea render piece.tsx --quality master -o piece.mp4 # a final export: constant quality
+raea render piece.tsx --alpha -o frames/ # transparent PNGs, for an overlay in another editor
raea play game.tsx --input # play a game live
```