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.

A 3D render draws the same pixels every time it runs (closes #48). Two renders of sdk/react/bench/games/room3d with one binary differed in 147 (RAEA_3D=program) to 369 (the live route) of their 7200 frames, by one pixel a frame: a single MSAA sample where a wall's foot meets the floor. There the two surfaces tie in depth, and the draw order decides the tie. Within a Bevy multidraw batch set, draws go in the order their bins were added, with removed bins swap-removed. await_models ran full updates while glTFs loaded, so the render world binned each mesh at the update its load landed on, and the trees' meshes joined at a different update each run. Bisected: without <Outside> (the glTF garden) 0 of 30 stills differ; with the trees alone they differ. Waiting for the GPU each frame (RAEA_SERIAL), the single-threaded executor and RGBA readback all still differ.

shinyobjectz53b0ce3fbbd0
diff --git a/bench/room3d-repeat/check.sh b/bench/room3d-repeat/check.sh
new file mode 100755
index 00000000..58fc56bf
--- /dev/null
+++ b/bench/room3d-repeat/check.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+# #48: a 3D game rendered again gives the same frames. sdk/react/bench/games/room3d (a room whose walls meet its floor,
+# and glTF trees outside) is rendered RUNS times (8) at frames where a wall's foot and the floor tie in depth, and
+# every run must match the first. Before await_models held the views off while models loaded, the render world binned
+# each mesh at the update its load landed on, so the multidraw's order, and which surface won the tie, changed from
+# run to run: about one run in three differed from the first, by one pixel a frame on the floor's seam.
+# bench/room3d-repeat/check.sh (RAEA_BIN: the raea to run, default target/release/raea; RUNS: how many runs)
+cd "$(dirname "$0")/../.." || exit 2
+BIN=${RAEA_BIN:-$PWD/target/release/raea}
+RUNS=${RUNS:-8}
+AT=35,90,210,258,278,295
+export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0
+OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-room3d-repeat.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+status=0
+for i in $(seq 1 "$RUNS"); do
+ "$BIN" render sdk/react/bench/games/room3d/edit.tsx --at "$AT" -o "$OUT/$i/" > "$OUT/$i.log" 2>&1 || { echo "FAIL: run $i failed"; tail -5 "$OUT/$i.log"; exit 1; }
+ [ "$i" = 1 ] && continue
+ d=$(for f in "$OUT/1"/*.png; do cmp -s "$f" "$OUT/$i/$(basename "$f")" || printf '%s ' "$(basename "$f" .png)"; done)
+ [ -z "$d" ] || { echo "FAIL: run $i differs from run 1 at $d(of --at $AT)"; status=1; }
+done
+[ $status = 0 ] && echo "PASS: $RUNS runs of room3d, every frame the same"
+exit $status
diff --git a/runtime/scene3d/src/model.rs b/runtime/scene3d/src/model.rs
index b01b8e50..672d69cf 100644
--- a/runtime/scene3d/src/model.rs
+++ b/runtime/scene3d/src/model.rs
@@ -337,6 +337,8 @@ pub fn await_models(_app: &mut App) -> Result<(), String> {
pub fn await_models(app: &mut App) -> Result<(), String> {
let mut t0 = bevy::platform::time::Instant::now();
let mut most = 0usize;
+ // the cameras off while models load, so the draws are binned once, whole (#48): raea-sys-post's views.rs
+ let mut off: Option<Vec<Entity>> = None;
loop {
let world = app.world_mut();
if let Some(e) = world.resource_mut::<Error>().0.take() {
@@ -360,6 +362,7 @@ pub fn await_models(app: &mut App) -> Result<(), String> {
done += s;
}
if waiting == 0 {
+ if let Some(cams) = off { raea_sys_post::views::on(app, cams); }
return Ok(());
}
if done > most {
@@ -370,6 +373,7 @@ pub fn await_models(app: &mut App) -> Result<(), String> {
let [l, s, r] = stages;
return Err(format!("{waiting} model(s) didn't load: none moved a stage in 60 s ({l} loading, {s} spawning, {r} ready without their surfaces)"));
}
+ if off.is_none() { off = Some(raea_sys_post::views::off(app.world_mut())); }
app.update();
std::thread::sleep(std::time::Duration::from_millis(1));
}
diff --git a/systems/post/src/lib.rs b/systems/post/src/lib.rs
index b5f9b9ee..1eef6d46 100644
--- a/systems/post/src/lib.rs
+++ b/systems/post/src/lib.rs
@@ -9,6 +9,7 @@ use raea_sys_frame::*;
use raea_sys_frame::bevy_api;
pub mod lens;
+pub mod views;
pub mod motion_blur;
pub mod dof;
pub mod effect_stack;
diff --git a/systems/post/src/views.rs b/systems/post/src/views.rs
new file mode 100644
index 00000000..287408e2
--- /dev/null
+++ b/systems/post/src/views.rs
@@ -0,0 +1,43 @@
+//! The views off while a scene's glTF models load, and on again before its first frame (raea #48): runtime/scene3d's
+//! await_models.
+
+use bevy::prelude::*;
+
+/// The active cameras turned off while models load, so the render world bins no draws until the scene is whole
+/// (#48): it still prepares meshes, textures and pipelines. Binned as each mesh turned ready, the scene's draws went
+/// into a multidraw in the order loads landed (Bevy draws a batch set's bins in the order they were added and
+/// swap-removes them), and that order decides a depth tie, as where a wall meets the floor: two renders of room3d
+/// differed in 147-369 of their 7200 frames. Turned on again (views::on) before the first frame, every view bins the
+/// whole scene into new phases in queue order, the same every run. (Turning views off after a draw was binned
+/// instead loses what Bevy extracts only on a change.)
+pub fn off(world: &mut World) -> Vec<Entity> {
+ let mut cams = world.query::<(Entity, &mut Camera)>();
+ let mut off = vec![];
+ for (e, mut c) in cams.iter_mut(world) {
+ if c.is_active {
+ c.is_active = false;
+ off.push(e);
+ }
+ }
+ off
+}
+
+/// The cameras `off` turned off on again, and one update, then each morphed mesh's Mesh3d marked changed so the
+/// first frame collects it again. Bevy 0.20 gives a morph mesh its descriptor (prepare_morph_descriptors) only once its
+/// instance was collected (collect_meshes_for_gpu_building, which runs after it), and collects an instance only when it
+/// changes: one first seen by a view on the update after loading would keep "no morph" for good (bevy-ts morph drew its
+/// card unmorphed), where the loading updates used to collect it again by chance.
+pub fn on(app: &mut App, cams: Vec<Entity>) {
+ let world = app.world_mut();
+ for e in cams {
+ if let Some(mut c) = world.get_mut::<Camera>(e) {
+ c.is_active = true;
+ }
+ }
+ app.update();
+ let world = app.world_mut();
+ let mut morphed = world.query_filtered::<&mut Mesh3d, With<bevy::mesh::morph::MeshMorphWeights>>();
+ for mut m in morphed.iter_mut(world) {
+ m.set_changed();
+ }
+}