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.

#159: --aov and RAEA_PASS keep the camera single-sampled on every frame. Both read the camera's prepasses (depth and motion vectors, depth and normals) through single-sampled bindings, so they put Msaa::Off on the camera once, with the prepasses. The camera row's post-processing then put multisampling back: raea-sys-post's sync, on the first frame whose post columns changed (a bloom ramp), restored the Msaa the camera was made with, and msaa_for_3d did it on every frame a live scene (a game) set rows. The prepasses became multisampled and the frame's bind group was refused ("Texture binding 0 expects multisampled = false, but given a view with samples = 4"), at frame 2 of a render with frames 0 and 1 written. A camera whose prepasses a pass reads now carries raea_sys_frame::camera::PrepassRead, put on with the prepasses by --aov (aov_prepasses) and RAEA_PASS (compose's mark). sync and msaa_for_3d leave such a camera's MSAA off, and sync no longer takes off a depth or normal prepass that AO brought when the AO goes. aov_prepasses finds its cameras by PrepassRead, not by the motion-vector prepass, which a motion-blurred camera already had, so that camera was never made single-sampled. The readback refuses a multisampled prepass with a message instead of handing it to the GPU, whose validation error stopped the render. Tests: raea-sys-post's a_camera_whose_prepasses_are_read_stays_single_sampled (before: Sample4 once the bloom changes); bench/aov-msaa/check.sh renders frames 0 to 5 of a bloom ramp and of a game with --aov, and the ramp with RAEA_PASS=depth (before: each refused at frame 2).

shinyobjectz57e0737edd2f
diff --git a/bench/aov-msaa/check.sh b/bench/aov-msaa/check.sh
new file mode 100755
index 00000000..1d16006c
--- /dev/null
+++ b/bench/aov-msaa/check.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+# #159: --aov and RAEA_PASS on a multisampled 3D camera write every frame. Both read the camera's prepasses as
+# single-sampled textures, so they turn its multisampling off; the camera row's post-processing turned it back on: in
+# a render, on the first frame whose post changed (ramp.tsx's bloom ramps); in a game, on every frame its rows set
+# (game.tsx). Frame 2 was then refused ("Texture binding 0 expects multisampled = false, but given a view with
+# samples = 4"), with frames 0 and 1 written. Here frames 0 to 5 of each are rendered: --aov must write each frame's
+# depth and motion at the frame's size (320 x 180 floats: 4 and 8 bytes a pixel), the depth not all zero (something
+# drawn), and RAEA_PASS=depth all six pictures. Exits 1 on a failure.
+# bench/aov-msaa/check.sh (RAEA_BIN: the raea to run, default target/release/raea)
+cd "$(dirname "$0")/../.." || exit 2
+BIN=${RAEA_BIN:-$PWD/target/release/raea}
+AT=0,1,2,3,4,5
+export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0 RAEA_AUDIO_MUTE=1
+OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-aov-msaa.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+refused() { grep -m1 REFUSED "$1" || tail -3 "$1"; }
+for c in ramp game; do
+ "$BIN" render "bench/aov-msaa/$c.tsx" --at "$AT" --aov -o "$OUT/$c" > "$OUT/$c.log" 2>&1 || { echo "FAIL: $c --aov: the render failed"; refused "$OUT/$c.log"; exit 1; }
+ for i in 0 1 2 3 4 5; do
+ n=$(printf '%05d' "$i")
+ [ "$(wc -c < "$OUT/$c/depth/frame-$n.f32" 2>/dev/null | tr -d ' ')" = 230400 ] || { echo "FAIL: $c --aov: frame $i's depth is missing or not 320 x 180"; exit 1; }
+ [ "$(wc -c < "$OUT/$c/motion/frame-$n.f32" 2>/dev/null | tr -d ' ')" = 460800 ] || { echo "FAIL: $c --aov: frame $i's motion is missing or not 320 x 180"; exit 1; }
+ [ "$(tr -d '\0' < "$OUT/$c/depth/frame-$n.f32" | head -c 1 | wc -c | tr -d ' ')" = 1 ] || { echo "FAIL: $c --aov: frame $i's depth is all zero"; exit 1; }
+ done
+ echo "PASS: $c --aov: frames 0 to 5, each with its depth and motion"
+done
+RAEA_PASS=depth RAEA_READBACK=rgba "$BIN" render bench/aov-msaa/ramp.tsx --at "$AT" -o "$OUT/pass" > "$OUT/pass.log" 2>&1 || { echo "FAIL: ramp RAEA_PASS=depth: the render failed"; refused "$OUT/pass.log"; exit 1; }
+[ "$(ls "$OUT/pass"/frame-*.png 2>/dev/null | wc -l | tr -d ' ')" = 6 ] || { echo "FAIL: ramp RAEA_PASS=depth: not six frames"; exit 1; }
+echo "PASS: ramp RAEA_PASS=depth: frames 0 to 5"
diff --git a/bench/aov-msaa/game.tsx b/bench/aov-msaa/game.tsx
new file mode 100644
index 00000000..75b4afd9
--- /dev/null
+++ b/bench/aov-msaa/game.tsx
@@ -0,0 +1,31 @@
+// #159: a game's 3D (its rows set on the live engine each frame, so raea-sys-post's msaa_for_3d sees them): a
+// multisampled camera, a still bloom and a box that moves, rendered with --aov (check.sh).
+import { AbsoluteFill, registerRoot, Composition, useGameState } from "raea";
+import { Bloom, Canvas3D, ToneMapping, manifold } from "raea/3d";
+import { Camera3d } from "raea/camera";
+import { AmbientLight, DirectionalLight } from "raea/light";
+import { Mesh3d } from "raea/mesh";
+import { StandardMaterial } from "raea/pbr";
+import { Transform } from "raea/transform";
+
+const GROUND = manifold.box(20, 1, 20).move(0, -0.5, 0);
+const BOX = manifold.box(1.5, 1.5, 1.5);
+
+function Game() {
+ const t = useGameState({ t: 0 }, (s) => ({ t: s.t + 1 })).t;
+ return (
+ <AbsoluteFill style={{ background: "#202830" }}>
+ <Canvas3D>
+ <Camera3d transform={Transform.fromXyz(6, 3, 8).lookingAt([0, 0.8, 0], [0, 1, 0])} projection={{ Perspective: { fov: 0.8, near: 0.1, far: 100 } }} />
+ <DirectionalLight name="sun" transform={Transform.fromXyz(10, 20, 5).lookingAt([0, 0, 0], [0, 1, 0])} illuminance={20000} shadow_maps_enabled />
+ <AmbientLight brightness={200} />
+ <Mesh3d name="ground" shape={GROUND}><StandardMaterial base_color="#6a7a50" perceptual_roughness={0.9} /></Mesh3d>
+ <Mesh3d name="box" shape={BOX} transform={Transform.fromXyz(-1.5 + t * 0.2, 0.75, 0)}><StandardMaterial base_color="#d03020" emissive="#ffe0a0" emissive_intensity={4} /></Mesh3d>
+ <ToneMapping mode="agx" />
+ <Bloom intensity={0.3} threshold={0.8} />
+ </Canvas3D>
+ </AbsoluteFill>
+ );
+}
+
+registerRoot(() => <Composition id="game" component={Game} width={320} height={180} fps={30} durationInFrames={10} />);
diff --git a/bench/aov-msaa/ramp.tsx b/bench/aov-msaa/ramp.tsx
new file mode 100644
index 00000000..f0f9025b
--- /dev/null
+++ b/bench/aov-msaa/ramp.tsx
@@ -0,0 +1,31 @@
+// #159: a multisampled 3D camera (no AO or <AntiAliasing>) whose post-processing changes on every frame (a bloom ramp),
+// rendered with --aov and with RAEA_PASS (check.sh). Its frames read the camera's rows through raea-sys-post's sync.
+import { AbsoluteFill, interpolate, registerRoot, Composition, useCurrentFrame } from "raea";
+import { Bloom, Canvas3D, ToneMapping, manifold } from "raea/3d";
+import { Camera3d } from "raea/camera";
+import { AmbientLight, DirectionalLight } from "raea/light";
+import { Mesh3d } from "raea/mesh";
+import { StandardMaterial } from "raea/pbr";
+import { Transform } from "raea/transform";
+
+const GROUND = manifold.box(20, 1, 20).move(0, -0.5, 0);
+const BOX = manifold.box(1.5, 1.5, 1.5);
+
+function Ramp() {
+ const f = useCurrentFrame();
+ return (
+ <AbsoluteFill style={{ background: "#202830" }}>
+ <Canvas3D>
+ <Camera3d transform={Transform.fromXyz(6, 3, 8).lookingAt([0, 0.8, 0], [0, 1, 0])} projection={{ Perspective: { fov: 0.8, near: 0.1, far: 100 } }} />
+ <DirectionalLight name="sun" transform={Transform.fromXyz(10, 20, 5).lookingAt([0, 0, 0], [0, 1, 0])} illuminance={20000} shadow_maps_enabled />
+ <AmbientLight brightness={200} />
+ <Mesh3d name="ground" shape={GROUND}><StandardMaterial base_color="#6a7a50" perceptual_roughness={0.9} /></Mesh3d>
+ <Mesh3d name="box" shape={BOX} transform={Transform.fromXyz(-1.5 + f * 0.2, 0.75, 0)}><StandardMaterial base_color="#d03020" emissive="#ffe0a0" emissive_intensity={4} /></Mesh3d>
+ <ToneMapping mode="agx" />
+ <Bloom intensity={interpolate(f, [0, 9], [0.1, 0.6])} threshold={0.8} />
+ </Canvas3D>
+ </AbsoluteFill>
+ );
+}
+
+registerRoot(() => <Composition id="ramp" component={Ramp} width={320} height={180} fps={30} durationInFrames={10} />);
diff --git a/engine/render/src/engine/aov.rs b/engine/render/src/engine/aov.rs
index e1b67151..e60d5cf4 100644
--- a/engine/render/src/engine/aov.rs
+++ b/engine/render/src/engine/aov.rs
@@ -1,11 +1,12 @@
//! RT1 (docs/raytracing.feature): each frame's motion vectors and depth, the guide data a neural or denoising pass
//! after the render needs (MLX-DLSS's run-sequence, MetalFX). Asked for with `want_aov`, before the first draw: the 3D
-//! cameras then draw Bevy's depth and motion-vector prepasses (MSAA off: a multisampled prepass can't be read), and
+//! cameras then draw Bevy's depth and motion-vector prepasses (MSAA off on every frame: they're read single-sampled), and
//! each frame's first draw reads both back (`take_aov`). A redraw of the same frame keeps the first draw's: drawn
//! again, every mesh's last pose is its pose now, and its motion would read zero.
use super::*;
use bevy::core_pipeline::prepass::{DepthPrepass, MotionVectorPrepass, ViewPrepassTextures};
+use raea_sys_frame::camera::PrepassRead;
/// One frame's guide data at the prepass's size (the frame's, times RAEA_SS).
pub struct Aov {
@@ -24,9 +25,13 @@ pub(crate) struct AovWanted {
last: Option<Aov>,
}
-fn aov_prepasses(mut cmd: Commands, q: Query<Entity, (With<Camera3d>, Without<MotionVectorPrepass>)>) {
+/// Each 3D camera draws both prepasses, single-sampled on every frame: PrepassRead keeps the row's post-processing from
+/// putting MSAA back (#159: raea-sys-post's sync did on the first frame whose post changed, msaa_for_3d on each frame
+/// of a live scene, and frame 2's readback was refused). Marked by PrepassRead, not by the motion-vector prepass, which
+/// a camera with motion blur has already.
+fn aov_prepasses(mut cmd: Commands, q: Query<Entity, (With<Camera3d>, Or<(Without<PrepassRead>, Without<DepthPrepass>, Without<MotionVectorPrepass>)>)>) {
for e in &q {
- cmd.entity(e).insert((DepthPrepass, MotionVectorPrepass, Msaa::Off));
+ cmd.entity(e).insert((DepthPrepass, MotionVectorPrepass, Msaa::Off, PrepassRead));
}
}
@@ -93,6 +98,12 @@ impl Engine {
.ok_or("no 3D camera drew a depth and motion-vector prepass")?;
// the target is a row taller than the frame (the frame tag's row, open.rs), at the frame's scale (RAEA_SS)
let (w, h) = (pick.size.width, pick.size.height.min(self.h * (pick.size.width / self.w.max(1)).max(1)));
+ // the bindings below are single-sampled: a multisampled prepass is said here, not given to the GPU, whose
+ // validation error stops the render
+ let samples = pick.motion_vectors.as_ref().unwrap().texture.texture.sample_count();
+ if samples > 1 {
+ return Err(format!("the prepass is multisampled ({samples} samples)"));
+ }
let depth = pick.depth_only_view().ok_or("the prepass has no depth view")?.clone();
let motion = pick.motion_vectors.as_ref().unwrap().texture.default_view.clone();
let device = rw.resource::<bevy::render::renderer::RenderDevice>().wgpu_device().clone();
diff --git a/systems/compose/src/passes.rs b/systems/compose/src/passes.rs
index 78442bcc..2add2fa1 100644
--- a/systems/compose/src/passes.rs
+++ b/systems/compose/src/passes.rs
@@ -78,7 +78,8 @@ pub fn install(app: &mut App) {
fn mark(pass: Pass, mut commands: Commands, cams: Query<Entity, (With<Camera3d>, Without<PassView>)>, mut mats: ResMut<Assets<StandardMaterial>>) {
for e in &cams {
let mut c = commands.entity(e);
- c.insert((PassView(pass as u32), DepthPrepass, NormalPrepass, Msaa::Off));
+ // single-sampled on every frame, whatever the camera's row asks (#159)
+ c.insert((PassView(pass as u32), DepthPrepass, NormalPrepass, Msaa::Off, raea_sys_frame::camera::PrepassRead));
if pass == Pass::Albedo {
c.insert((Tonemapping::None, super::bevy_api::Untonemapped)).remove::<DistanceFog>().remove::<bevy::post_process::bloom::Bloom>().remove::<bevy::render::view::ColorGrading>();
}
diff --git a/systems/frame/src/camera.rs b/systems/frame/src/camera.rs
index 0cbb5a00..4d2830f4 100644
--- a/systems/frame/src/camera.rs
+++ b/systems/frame/src/camera.rs
@@ -50,6 +50,14 @@ pub fn remove<B: Bundle>(world: &mut World, e: Entity) {
#[derive(Component, Clone, Copy, Default)]
pub struct IntoImage;
+/// A camera whose prepasses a pass after it reads (`--aov`'s motion vectors and depth, RAEA_PASS's depth and normals),
+/// bound as single-sampled textures: it draws with MSAA off on every frame, whatever its row asks, and keeps the depth
+/// and normal prepasses an effect brought when the effect goes (raea-sys-post's msaa_for_3d and sync). Put on with the
+/// prepasses, never taken off (#159: the row's post-processing turned MSAA back on under them, and the pass's bind
+/// group refused the frame).
+#[derive(Component, Clone, Copy, Default)]
+pub struct PrepassRead;
+
/// The image a camera row draws into (a rendercam's `into`, "render:NAME:WxH[:float]": docs/r3f.feature R3F23a), made
/// if it's new, as the camera's size and target; None for a screen camera.
pub fn image_of(world: &mut World, vals: &BTreeMap<String, V>) -> Result<Option<((u32, u32), RenderTarget)>, String> {
diff --git a/systems/post/src/camera.rs b/systems/post/src/camera.rs
index 90fd5568..a1da9455 100644
--- a/systems/post/src/camera.rs
+++ b/systems/post/src/camera.rs
@@ -9,7 +9,7 @@ use raea_core_program::ir::V;
use raea_sys_frame::*;
mod on;
-use raea_sys_frame::camera::{Group, remove};
+use raea_sys_frame::camera::{Group, PrepassRead, remove};
type Vals = BTreeMap<String, V>;
@@ -56,10 +56,12 @@ fn take_off_brought(world: &mut World, e: Entity, by: &mut [bool; 3]) {
use bevy::core_pipeline::prepass::{DeferredPrepass, DepthPrepass, NormalPrepass};
let w = world.entity(e);
let ao = w.contains::<bevy::pbr::ScreenSpaceAmbientOcclusion>() || w.contains::<crate::outline::InkOutline>();
+ // a pass after the camera reads its prepasses (--aov, RAEA_PASS): they stay
+ let read = w.contains::<PrepassRead>();
let need = [
w.contains::<bevy::post_process::bloom::Bloom>() || w.contains::<bevy::pbr::AtmosphereSettings>() || w.contains::<DeferredPrepass>(),
- ao || w.contains::<bevy::post_process::motion_blur::MotionBlur>() || w.contains::<DeferredPrepass>(),
- ao,
+ ao || read || w.contains::<bevy::post_process::motion_blur::MotionBlur>() || w.contains::<DeferredPrepass>(),
+ ao || read,
];
let mut em = world.entity_mut(e);
for (i, b) in by.iter_mut().enumerate() {
@@ -93,7 +95,8 @@ pub fn apply(world: &mut World, e: Entity, vals: &Vals, extra: &[Group]) {
/// Each camera's post-processing as this frame's row has it: a group whose columns changed since the camera last took
/// them is taken off and put on again (a frame's own sky, fog or anti-aliasing: frame_sets, raea-sys-frame tables.rs).
/// Multisampling goes off with AO or SMAA/FXAA and back to the camera's own without, except on a deferred camera (Solari),
-/// which can't multisample. `extra` as `apply` had it.
+/// which can't multisample, and on one whose prepasses a pass reads (PrepassRead: --aov, RAEA_PASS), which stays off
+/// (#159). `extra` as `apply` had it.
pub fn sync(world: &mut World, extra: &[Group]) {
let groups: Vec<&Group> = GROUPS.iter().chain(extra).collect();
let cams: Vec<Entity> = world.query_filtered::<Entity, With<Applied>>().iter(world).collect();
@@ -120,7 +123,7 @@ pub fn sync(world: &mut World, extra: &[Group]) {
by[i] |= now[i] && !had[i];
}
take_off_brought(world, e, &mut by);
- let single = vals.contains_key("ssao") || vals.contains_key("aa");
+ let single = vals.contains_key("ssao") || vals.contains_key("aa") || world.get::<PrepassRead>(e).is_some();
let want = if single { Msaa::Off } else { own };
let deferred = world.get::<bevy::core_pipeline::prepass::DeferredPrepass>(e).is_some();
if world.get::<Msaa>(e) != Some(&want) && (want == Msaa::Off || !deferred) {
@@ -210,6 +213,29 @@ mod tests {
assert_eq!(w.get::<bevy::post_process::bloom::Bloom>(e).unwrap().intensity, 0.5);
}
+ #[test]
+ fn a_camera_whose_prepasses_are_read_stays_single_sampled() {
+ use bevy::core_pipeline::prepass::{DepthPrepass, MotionVectorPrepass};
+ // made multisampled with AO on its first shot (AO brought the depth prepass), then given its prepasses as
+ // --aov gives them (#159)
+ let (mut w, e) = world(row(&[("bloom", V::N(0.1)), ("ssao", V::S("ultra".into()))]));
+ w.entity_mut(e).insert((DepthPrepass, MotionVectorPrepass, Msaa::Off, raea_sys_frame::camera::PrepassRead));
+ // the AO goes and the bloom ramps: still single-sampled, and the depth prepass stays
+ for b in [0.2, 0.3] {
+ set(&mut w, row(&[("bloom", V::N(b))]));
+ sync(&mut w, &[]);
+ assert_eq!(w.get::<Msaa>(e), Some(&Msaa::Off), "bloom {b}");
+ assert!(w.get::<DepthPrepass>(e).is_some() && w.get::<MotionVectorPrepass>(e).is_some(), "bloom {b}");
+ }
+ // a live scene's frame with 3D in it leaves it single-sampled too
+ crate::msaa_for_3d(&mut w, true);
+ assert_eq!(w.get::<Msaa>(e), Some(&Msaa::Off));
+ // a camera nothing reads the prepasses of still multisamples again (#38)
+ w.entity_mut(e).remove::<raea_sys_frame::camera::PrepassRead>();
+ crate::msaa_for_3d(&mut w, true);
+ assert_eq!(w.get::<Msaa>(e), Some(&Msaa::Sample4));
+ }
+
#[test]
fn depth_of_field_comes_with_the_shot_that_has_it() {
use bevy::post_process::dof::DepthOfField;
diff --git a/systems/post/src/lib.rs b/systems/post/src/lib.rs
index 467505c4..7138cb0a 100644
--- a/systems/post/src/lib.rs
+++ b/systems/post/src/lib.rs
@@ -22,7 +22,8 @@ pub mod outline;
/// 10 and had hard edges live). Not one whose row asks for a single-sampled frame (ssao, aa, outline, frame_looks,
/// passes: spawn_camera's rule), not in preview, and not a deferred camera (a ray-traced one): Bevy turns MSAA off
/// on those itself each frame and says so, so this turned it on again every frame, a warning a frame in a render's
-/// log and the camera's pipelines made again for nothing.
+/// log and the camera's pipelines made again for nothing. Nor one whose prepasses a pass reads single-sampled
+/// (PrepassRead: --aov, RAEA_PASS): turned on under them, a game's --aov was refused at its third frame (#159).
pub fn msaa_for_3d(world: &mut World, has_3d: bool) {
if !has_3d || preview() {
return;
@@ -31,7 +32,8 @@ pub fn msaa_for_3d(world: &mut World, has_3d: bool) {
for e in cams {
let single = world.resource::<FrameRows>().vals(e).is_none_or(|v| ["ssao", "aa", "outline", "frame_looks", "passes"].iter().any(|k| v.contains_key(*k)));
let deferred = world.entity(e).contains::<bevy::core_pipeline::prepass::DeferredPrepass>();
- if !single && !deferred && world.get::<Msaa>(e) == Some(&Msaa::Off) {
+ let read = world.entity(e).contains::<raea_sys_frame::camera::PrepassRead>();
+ if !single && !deferred && !read && world.get::<Msaa>(e) == Some(&Msaa::Off) {
world.entity_mut(e).insert(Msaa::default());
}
}