From 7bc06bb7775cbcb80dca27fe88ba6e6a8e46ee89 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Sat, 21 Nov 2026 19:38:57 -0800 Subject: [PATCH] SG4a: the clipping check holds each part over the frames it's in, not a slot for every frame A 13,175-frame film (mobile-ads film2) was killed at 27-33 GB the moment its last frame was drawn: the clipping check made tracks_of's dense tracks, a slot a frame for every part id across the whole render, and filled() copied them again. A long film's parts each live in one scene, so nearly all of those slots were empty. core/scene's spans_of keeps each part from its first frame to its last (tracks_of is built on it, so the ids stay one rule), and raea-sys-contact reads spans and a part's own world, with no filled copy. raea scene stage over the same film peaked at 3.1 GB, so the stage checks were never the cause. --- core/scene/src/lib.rs | 60 ++++++++++++++++++++++++++++++++------ systems/contact/src/lib.rs | 26 ++++++++--------- 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/core/scene/src/lib.rs b/core/scene/src/lib.rs index 776c9402..b328a381 100644 --- a/core/scene/src/lib.rs +++ b/core/scene/src/lib.rs @@ -90,11 +90,26 @@ fn surface_ident(it: &Item) -> String { } } -/// `tracks` over items already walked: only the parts of these kinds are copied -pub fn tracks_of(all: &[Vec], kinds: &[&str]) -> Vec<(String, Vec>)> { +/// A part's track over only the frames it's in: from its first to its last, None where it's out between. +#[derive(Clone)] +pub struct Span { + pub first: usize, + pub items: Vec>, +} + +impl Span { + /// Its item at frame `k`, when it's in that frame. + pub fn at(&self, k: usize) -> Option<&Item> { + self.items.get(k.checked_sub(self.first)?)?.as_ref() + } +} + +/// `tracks_of`'s tracks as spans: each part's slots from its first frame to its last, not one for every frame of the +/// render. A long film's parts each live in a scene; a slot a frame for every one of them made the clipping check of a +/// 13,175-frame film ask for 28 GB at its end (docs/stage-guards.feature SG4a). +pub fn spans_of(all: &[Vec], kinds: &[&str]) -> Vec<(String, Span)> { let mut order: Vec = Vec::new(); - let mut by: BTreeMap>> = BTreeMap::new(); - let frames = all; + let mut by: BTreeMap = BTreeMap::new(); for (i, its) in all.iter().enumerate() { let mut seen: BTreeMap = BTreeMap::new(); for it in its.iter().filter(|it| kinds.contains(&it.kind)).cloned() { @@ -102,19 +117,33 @@ pub fn tracks_of(all: &[Vec], kinds: &[&str]) -> Vec<(String, Vec], kinds: &[&str]) -> Vec<(String, Vec>)> { + spans_of(all, kinds) + .into_iter() + .map(|(id, s)| { + let mut v = vec![None; s.first]; + v.extend(s.items); + v.resize(all.len(), None); + (id, v) + }) + .collect() +} + /// Each frame's item, a missing frame holding the nearest present one (a hidden part stays where it was). pub fn filled(track: &[Option]) -> Vec { let first = track.iter().flatten().next().cloned().expect("a track has a frame"); @@ -163,6 +192,19 @@ mod surface_tracks { assert_eq!(t.len(), 2, "{:?}", t.iter().map(|x| &x.0).collect::>()); } + #[test] + fn a_span_covers_only_the_frames_a_part_is_in() { + let m = || mesh("box", "MeshStandardMaterial", None); + // in frames 2 and 4 of 6, out in 3 + let all = vec![vec![], vec![], vec![m()], vec![], vec![m()], vec![]]; + let s = &spans_of(&all, &["mesh"])[0].1; + assert_eq!((s.first, s.items.len()), (2, 3)); + assert!(s.at(1).is_none() && s.at(2).is_some() && s.at(3).is_none() && s.at(4).is_some() && s.at(5).is_none()); + // the dense tracks are the same parts, a slot a frame + let t = &tracks_of(&all, &["mesh"])[0].1; + assert_eq!(t.iter().map(Option::is_some).collect::>(), [false, false, true, false, true, false]); + } + #[test] fn a_shader_whose_uniforms_move_stays_one_part() { let src = "fn surface(in: VertexOutput, pbr: ptr) { }"; diff --git a/systems/contact/src/lib.rs b/systems/contact/src/lib.rs index 9ecc554f..d72a4a97 100644 --- a/systems/contact/src/lib.rs +++ b/systems/contact/src/lib.rs @@ -19,7 +19,7 @@ use std::sync::{Arc, Mutex, OnceLock}; use bevy::math::{Mat4, Vec3}; use serde_json::{json, Value}; -use raea_core_scene::{Item, excused, filled, gltf_tris, label, src_path, tracks_of, vec3}; +use raea_core_scene::{Item, Span, excused, gltf_tris, label, spans_of, src_path, vec3}; // what each part rests on, and the parts that float, sink or clip (`raea scene lint|explain`) pub mod lint; @@ -262,15 +262,15 @@ fn volume(a: &Solid, wa: &Mat4, b: &Solid, wb: &Mat4) -> Option { /// The overlaps a render shows: each with a sentence (for the agent) and its numbers (for the manifest). `all` is each /// frame's parts, `frames` the frames' numbers. pub fn check(all: &[Vec], frames: &[i64], w: u32, h: u32, fps: f64) -> Vec { - let parts: Vec<(String, Vec>)> = tracks_of(&all, &["mesh", "model"]); - let solids: Vec>>> = parts.iter().map(|(_, tr)| solid_of(tr.iter().flatten().next()?).ok()).collect(); - let placed: Vec> = parts.iter().map(|(_, tr)| filled(tr)).collect(); + // each part over the frames it's in (spans, not a slot per frame: SG4a); placed(i, k): part i's place on frame k + let parts: Vec<(String, Span)> = spans_of(all, &["mesh", "model"]); + let solids: Vec>>> = parts.iter().map(|(_, tr)| solid_of(tr.items.iter().flatten().next()?).ok()).collect(); + let placed = |i: usize, k: usize| parts[i].1.at(k).map_or(Mat4::IDENTITY, |it| it.world); // the pieces that hide others on frame k: every checked part there, see-through ones left out - let placed = &placed; let occluders = |k: usize| -> Vec<(&Solid, Mat4)> { - parts.iter().enumerate().filter_map(|(i, (_, tr))| Some((i, tr[k].as_ref()?, solids[i].as_ref()?))) - .filter(|(_, it, _)| !excused(it) || it.props.get("allowOverlap").and_then(Value::as_bool) == Some(true)) - .flat_map(|(i, _, ss)| ss.iter().map(move |s| (s, placed[i][k].world))) + parts.iter().enumerate().filter_map(|(i, (_, tr))| Some((tr.at(k)?, solids[i].as_ref()?))) + .filter(|(it, _)| !excused(it) || it.props.get("allowOverlap").and_then(Value::as_bool) == Some(true)) + .flat_map(|(it, ss)| ss.iter().map(move |s| (s, it.world))) .collect() }; // frame by frame: the parts' world boxes swept along x for the pairs that meet, each pair's test reused while @@ -282,11 +282,11 @@ pub fn check(all: &[Vec], frames: &[i64], w: u32, h: u32, fps: f64) -> Vec for k in 0..frames.len() { let mut boxes: Vec<(usize, Vec3, Vec3)> = Vec::new(); for (i, (_, tr)) in parts.iter().enumerate() { - let (Some(it), Some(ss)) = (&tr[k], &solids[i]) else { continue }; + let (Some(it), Some(ss)) = (tr.at(k), &solids[i]) else { continue }; if excused(it) { continue; } - let w = placed[i][k].world; + let w = it.world; let (mut lo, mut hi) = (Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)); for s in ss.iter() { let (l, h) = world_box(s, &w); @@ -307,7 +307,7 @@ pub fn check(all: &[Vec], frames: &[i64], w: u32, h: u32, fps: f64) -> Vec } let (i, j) = (i0.min(j0), i0.max(j0)); let (a, b) = (solids[i].as_ref().unwrap(), solids[j].as_ref().unwrap()); - let (wa, wb) = (placed[i][k].world, placed[j][k].world); + let (wa, wb) = (placed(i, k), placed(j, k)); let r = match last.get(&(i, j)) { Some((la, lb, r)) if *la == wa && *lb == wb => r.clone(), _ => { @@ -350,11 +350,11 @@ pub fn check(all: &[Vec], frames: &[i64], w: u32, h: u32, fps: f64) -> Vec let mut out = Vec::new(); // one overlap said, over frames f0 to f1 (`seen`: its seam on screen, else said as standing in the scene unseen) let mut say = |i: usize, j: usize, k: usize, hit: &Hit, f0: i64, f1: i64, seen: bool| { - let (pa, pb, sa, sb) = (parts[i].1[k].as_ref().unwrap(), parts[j].1[k].as_ref().unwrap(), solids[i].as_ref().unwrap(), solids[j].as_ref().unwrap()); + let (pa, pb, sa, sb) = (parts[i].1.at(k).unwrap(), parts[j].1.at(k).unwrap(), solids[i].as_ref().unwrap(), solids[j].as_ref().unwrap()); let (la, lb) = (label(pa, &sa[hit.pieces.0].name), label(pb, &sb[hit.pieces.1].name)); let (small, big) = if hit.small_first { (la, lb) } else { (lb, la) }; // out the shortest way: the smallest move along a world axis (not down) that leaves no piece in the other's - let (axis, step) = clear_move(&sa[..], &placed[i][k].world, &sb[..], &placed[j][k].world, hit.small_first); + let (axis, step) = clear_move(&sa[..], &pa.world, &sb[..], &pb.world, hit.small_first); let axis_name = ["x", "y", "z"][axis]; let when = if f0 == f1 { clock(f0) } else { format!("{}–{}", clock(f0), clock(f1)) }; let shows = if seen { "and the seam shows" } else { "where no frame shows it (off frame or behind something), so it stands in the scene unseen" };