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
The window's two CPU switches, off by default and set in the environment or raea's settings file (~/.raea/env), where the app's window has no shell: RAEA_ST=1 runs every schedule on Bevy's single-threaded executor (34% less CPU at rest, 8% in a drag, 4% playing, frames as long; open: a seek while playing shows one more frame from before it), and RAEA_IDLE_GATE=1 lets a window with nothing drawn for 30 frames wait for input, 50 ms focused and 250 ms behind other windows, until the next draw (raea-sys-page counts its draws). The rest/wake rule is unit-tested; its on-screen check is still to do
shinyobjectzb4aede8ae61e
diff --git a/Cargo.lock b/Cargo.lockindex a178e32a..67a936d1 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -14731,6 +14731,7 @@ dependencies = ["blitz-traits","image","keyboard-types",+ "raea-io-home","raea-io-media","raea-io-net-calls","raea-io-os",diff --git a/engine/page/host/Cargo.toml b/engine/page/host/Cargo.tomlindex 89648cd5..2cada488 100644--- a/engine/page/host/Cargo.toml+++ b/engine/page/host/Cargo.toml@@ -33,6 +33,8 @@ raea-page-window = { path = "../window" }raea-sys-page = { path = "../../../systems/page" }# headless, the system calls are scripted (docs/targets.feature T2d)raea-io-os = { path = "../../../io/os" }+# raea's settings file (~/.raea/env): the window's perf switches, set where the app's window has no shell (main.rs)+raea-io-home = { path = "../../../io/home" }raea-sys-fonts = { path = "../../../systems/fonts" }raea-runtime-dom = { path = "../../../runtime/dom" }raea-runtime-input = { path = "../../../runtime/input" }diff --git a/engine/page/host/src/idle.rs b/engine/page/host/src/idle.rsnew file mode 100644index 00000000..7bed266e--- /dev/null+++ b/engine/page/host/src/idle.rs@@ -0,0 +1,75 @@+//! The window at rest (RAEA_IDLE_GATE=1 while it's checked on screen). Bevy's default runs the whole frame 60 times a+//! second, focused or behind other windows, though a page painted the same isn't drawn again (raea-sys-page draw.rs):+//! the page's step, its restyle and paint, and Bevy's render and present of a picture that didn't change were 0.29 of a+//! core with the app's feed open and still (11-16). After half a second in which no page was drawn and none is+//! animating, the window waits for input before its next frame (or 50 ms when focused, 250 ms behind other windows, so+//! a page's timers and answers still arrive); the first frame that draws a page puts it back to every frame. A playing+//! video, a game on the stage and any view draw their page every frame, so they never rest.++use std::time::Duration;++use bevy::prelude::*;+use bevy::winit::{UpdateMode, WinitSettings};++/// Frames in a row with nothing drawn before the window rests.+const QUIET: u32 = 30;++pub struct IdlePlugin;++impl Plugin for IdlePlugin {+ fn build(&self, app: &mut App) {+ app.add_systems(Last, rest);+ }+}++fn rest(settings: Option<ResMut<WinitSettings>>, docs: Option<NonSend<raea_sys_page::Docs>>, mut last: Local<u64>, mut quiet: Local<u32>) {+ let Some(mut settings) = settings else { return };+ let drawn = raea_sys_page::draws();+ let moved = drawn != *last || docs.is_some_and(|d| d.0.values().any(|d| d.is_animating()));+ *last = drawn;+ *quiet = quieter(*quiet, moved);+ let (focused, unfocused) = modes(*quiet);+ if settings.focused_mode != focused || settings.unfocused_mode != unfocused {+ settings.focused_mode = focused;+ settings.unfocused_mode = unfocused;+ }+}++/// The frames in a row with nothing drawn after this one: none when a page was drawn or one is animating.+fn quieter(quiet: u32, moved: bool) -> u32 {+ if moved { 0 } else { quiet.saturating_add(1) }+}++/// The window's update modes, focused and behind other windows, `quiet` frames into a run with nothing drawn: every+/// frame until QUIET, then waiting for input (or 50 ms focused, 250 ms behind, for a page's timers and answers).+fn modes(quiet: u32) -> (UpdateMode, UpdateMode) {+ if quiet >= QUIET {+ (UpdateMode::reactive(Duration::from_millis(50)), UpdateMode::reactive_low_power(Duration::from_millis(250)))+ } else {+ (UpdateMode::Continuous, UpdateMode::Continuous)+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn the_window_rests_after_half_a_second_with_nothing_drawn_and_wakes_on_the_next_draw() {+ let mut quiet = 0;+ // a page drawn every frame (a video playing, a game): never rests+ for _ in 0..100 {+ quiet = quieter(quiet, true);+ assert_eq!(modes(quiet).0, UpdateMode::Continuous);+ }+ // nothing drawn: every frame for QUIET frames, then waiting+ for n in 1..=QUIET {+ quiet = quieter(quiet, false);+ assert_eq!(modes(quiet).0 == UpdateMode::Continuous, n < QUIET, "frame {n} of the quiet run");+ }+ assert_eq!(modes(quiet), (UpdateMode::reactive(Duration::from_millis(50)), UpdateMode::reactive_low_power(Duration::from_millis(250))));+ // the first frame that draws (a key, an answer, a timer's change) puts it back to every frame at once+ quiet = quieter(quiet, true);+ assert_eq!(modes(quiet), (UpdateMode::Continuous, UpdateMode::Continuous));+ }+}diff --git a/engine/page/host/src/lib.rs b/engine/page/host/src/lib.rsindex efbbc43e..54cb50a3 100644--- a/engine/page/host/src/lib.rs+++ b/engine/page/host/src/lib.rs@@ -9,6 +9,8 @@ pub mod brp;pub use raea_page_window::{chrome, native};/// the live stage, in raea-page-stagepub use raea_page_stage as stage;+#[cfg(feature = "window")]+pub mod idle;pub mod inject;pub mod mcp;pub mod page;diff --git a/engine/page/host/src/main.rs b/engine/page/host/src/main.rsindex 90911a09..932c937f 100644--- a/engine/page/host/src/main.rs+++ b/engine/page/host/src/main.rs@@ -105,6 +105,24 @@ fn main() {// each update draws its own frame: the frame `ui` reports is the one rendered.disable::<PipelinedRenderingPlugin>(),);+ // every schedule on one thread: the window's ~290 systems a frame are each a few µs, and the multi-threaded executor+ // spent more waking its workers for them than they took (35% of the window's busy time at rest). On one thread, the+ // app's window used 34% less CPU at rest, 8% less through a drag and 4% less playing a film, its frames as long+ // (11-16, three rounds each). Systems' own par_iter still use the task pools. RAEA_ST=1 (the environment, or raea's+ // settings file: ~/.raea/env, read where the app's window has no shell to set it) until the window specs pass with+ // it, then on by default (RAEA_ST=0 for Bevy's multi-threaded one). Open: playback's "a seek while it plays" shows+ // one more frame from where it was after the seek (3 runs of 3; the multi-threaded run shows two, as the spec allows)+ if raea_io_home::setting("RAEA_ST").as_deref() == Some("1") {+ // a schedule that's running is out of Schedules: First sets the rest, Last sets First (Main is one system)+ app.add_systems(First, single_threaded).add_systems(Last, single_threaded);+ app.sub_app_mut(bevy::render::RenderApp).add_systems(bevy::render::ExtractSchedule, single_threaded).add_systems(bevy::render::Render, single_threaded);+ }+ // on screen, a window with nothing changing waits for input (raea_page_host::idle); RAEA_IDLE_GATE=1 (the+ // environment or ~/.raea/env, as RAEA_ST) while it's checked on screen+ #[cfg(feature = "window")]+ if a.window && raea_io_home::setting("RAEA_IDLE_GATE").as_deref() == Some("1") {+ app.add_plugins(raea_page_host::idle::IdlePlugin);+ }// on screen, bevy_winit's event loop is the runner (a ScheduleRunner here replaced it: no window, 11-04)if !a.window && !app.is_plugin_added::<ScheduleRunnerPlugin>() {// 60 frames a second, as most screens; RAEA_PAGE_FPS stands in for another screen's rate (a 120 Hz one, to@@ -149,3 +167,15 @@ fn main() {app.add_systems(Last, page::tree.before(tree::answer));app.run();}++/// Every schedule of this world that isn't running now, on the single-threaded executor (once, in each schedule it's in).+fn single_threaded(world: &mut World, mut done: Local<bool>) {+ if std::mem::replace(&mut *done, true) {+ return;+ }+ if let Some(mut schedules) = world.get_resource_mut::<bevy::ecs::schedule::Schedules>() {+ for (_, s) in schedules.iter_mut() {+ s.set_executor(bevy::ecs::schedule::SingleThreadedExecutor::new());+ }+ }+}diff --git a/systems/page/src/draw.rs b/systems/page/src/draw.rsindex 36be75ba..8873e17a 100644--- a/systems/page/src/draw.rs+++ b/systems/page/src/draw.rs@@ -13,6 +13,15 @@ pub struct Scenes {pub todo: Vec<Drawn>,}+/// Pages drawn so far (each one Vello drew, not one skipped as painted the same): what the window reads to rest (draws).+static DRAWS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);++/// How many pages have been drawn since the process started: unchanged from one frame to the next, nothing on any page+/// changed (a playing video, a stage or a view draws its page every frame).+pub fn draws() -> u64 {+ DRAWS.load(std::sync::atomic::Ordering::Relaxed)+}+/// The one Vello renderer on Bevy's device (not `Sync`: its shader cache holds cells, so it's held in a SyncCell and the/// render world can run on a thread of its own, as the engine's pipelined updates run it), made on first use; each page's/// storage texture Vello draws into; and the pass that premultiplies it into the page's image.@@ -171,6 +180,7 @@ pub fn draw(mut scenes: ResMut<Scenes>, mut vello: ResMut<Vello>, device: Res<Recontinue;}prints.insert(handle.id(), print);+ DRAWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);// a bevy_ui page's renders (its layers' and its own) blend in linear light (vendor/vello_shaders fine.wgsl)vello::low_level::vello_encoding::set_blend_linear(linear);// each filtered layer drawn by Vello, filtered, and handed to Vello as its placeholder's pixels, an innerdiff --git a/systems/page/src/lib.rs b/systems/page/src/lib.rsindex 27b97add..5e802e85 100644--- a/systems/page/src/lib.rs+++ b/systems/page/src/lib.rs@@ -31,7 +31,7 @@ mod snapshot;pub use snapshot::{OFFSET_BODY, OFFSET_NONE, OFFSETS, STRIDE, snapshot, snapshot_by};pub mod view3d;-pub use draw::{Placed, view, view_at};+pub use draw::{Placed, draws, view, view_at};use std::collections::HashMap;use std::sync::{Arc, Mutex};