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.

LT3: the TLAS is kept from frame to frame, built again only when an instance comes, goes, moves or changes its mask

shinyobjectz425d7439b94f
diff --git a/systems/rt/src/gi.rs b/systems/rt/src/gi.rs
index b2339ad1..c1e75886 100644
--- a/systems/rt/src/gi.rs
+++ b/systems/rt/src/gi.rs
@@ -370,7 +370,7 @@ fn report(state: &mut State, budget: u32, total: u32) {
format!("{:.3}", us as f64 / n.max(1) as f64 / 1000.0)
};
let text = format!(
- "{{\"rays_a_frame\":{},\"probes\":{total},\"probes_a_frame\":{},\"frames\":{},\"gpu_ms_p50\":{:.3},\"gpu_ms_p90\":{:.3},\"gpu_ms_max\":{:.3},\"cache_ms_p50\":{:.3},\"cache_ms_p90\":{:.3},\"probes_ms_p50\":{:.3},\"probes_ms_p90\":{:.3},\"cache_cells_p50\":{},\"cache_cells_max\":{},\"cpu_ms_gather\":{},\"cpu_ms_tlas\":{},\"cpu_ms_blas_batch\":{},\"blas_batches\":{}}}\n",
+ "{{\"rays_a_frame\":{},\"probes\":{total},\"probes_a_frame\":{},\"frames\":{},\"gpu_ms_p50\":{:.3},\"gpu_ms_p90\":{:.3},\"gpu_ms_max\":{:.3},\"cache_ms_p50\":{:.3},\"cache_ms_p90\":{:.3},\"probes_ms_p50\":{:.3},\"probes_ms_p90\":{:.3},\"cache_cells_p50\":{},\"cache_cells_max\":{},\"cpu_ms_gather\":{},\"cpu_ms_tlas\":{},\"cpu_ms_blas_batch\":{},\"blas_batches\":{},\"tlas_builds\":{},\"tlas_instances\":{}}}\n",
(budget / RAYS).min(total) * RAYS,
(budget / RAYS).min(total),
all.len(),
@@ -386,7 +386,9 @@ fn report(state: &mut State, budget: u32, total: u32) {
cpu(0),
cpu(1),
cpu(2),
- crate::CPU_US[5].load(Ordering::Relaxed)
+ crate::CPU_US[5].load(Ordering::Relaxed),
+ crate::TLAS_BUILT[0].load(Ordering::Relaxed),
+ crate::TLAS_BUILT[1].load(Ordering::Relaxed)
);
let _ = std::fs::write(path, text);
}
diff --git a/systems/rt/src/lib.rs b/systems/rt/src/lib.rs
index 502206f7..025aad8b 100644
--- a/systems/rt/src/lib.rs
+++ b/systems/rt/src/lib.rs
@@ -52,8 +52,9 @@ pub const ALL: u8 = 2;
#[extract_app(bevy::render::RenderApp)]
pub struct Sun(pub Vec3);
+/// a BLAS a mesh, and how many batches have been built (a mesh changed keeps its id, so the TLAS knows by this)
#[derive(Resource, Default)]
-struct Blases(HashMap<AssetId<Mesh>, Blas>);
+struct Blases(HashMap<AssetId<Mesh>, Blas>, u64);
/// The ray scene's CPU cost so far, for raea GI's stats (gi.rs `report`; LT2): microseconds and calls of `gather`
/// (the main world's instance list), `build_tlas` and `build_blas` (counted only when it builds), in that order.
@@ -63,10 +64,23 @@ fn spent(k: usize, since: std::time::Instant) {
CPU_US[2 * k + 1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
-/// This frame's TLAS, for the traced passes to bind.
+/// The TLAS, for the traced passes to bind: one kept from frame to frame (LT3), rebuilt only when the instances change.
#[derive(Resource)]
pub struct Scene(pub Tlas);
+/// How many times the TLAS was built and how many instances it last held, for raea GI's stats (gi.rs `report`)
+pub(crate) static TLAS_BUILT: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
+
+/// What the kept TLAS holds: each instance's BLAS (its mesh), its transform as the TLAS takes it, and its mask; and how
+/// many instances it has room for
+#[derive(Default)]
+struct Held {
+ instances: Vec<(AssetId<Mesh>, [f32; 12], u8)>,
+ room: u32,
+ /// Blases' batch count it was built against
+ batches: u64,
+}
+
pub fn install(app: &mut App) {
// (once, whether a traced pass or traced AO asked first)
if app.world().contains_resource::<Instances>() {
@@ -206,38 +220,58 @@ fn build_blas(
let mut enc = device.create_command_encoder(&CommandEncoderDescriptor { label: Some("raea rt blas") });
enc.build_acceleration_structures(&entries, &[]);
queue.submit([enc.finish()]);
+ blases.1 += 1;
spent(2, t0);
}
-/// the TLAS: every instance whose mesh has a BLAS, rebuilt each frame (things move)
-fn build_tlas(mut commands: Commands, instances: Option<Res<Instances>>, blases: Res<Blases>, device: Res<RenderDevice>, queue: Res<RenderQueue>) {
+/// The TLAS: every instance whose mesh has a BLAS. One is kept (LT3): built again only when an instance came, went,
+/// moved or changed its mask, so a still scene costs a comparison a frame; made anew only when it runs out of room,
+/// with half as much again to spare. Each instance's custom index is its place in the list, where LT4's record table
+/// will keep what a hit on it sees.
+fn build_tlas(mut commands: Commands, instances: Option<Res<Instances>>, blases: Res<Blases>, scene: Option<ResMut<Scene>>, mut held: Local<Held>, device: Res<RenderDevice>, queue: Res<RenderQueue>) {
let Some(instances) = instances else { return };
if !device.features().contains(WgpuFeatures::EXPERIMENTAL_RAY_QUERY) {
return;
}
let t0 = std::time::Instant::now();
- let present: Vec<_> = instances.0.iter().filter_map(|(id, m, mask)| blases.0.get(id).map(|b| (b, m, *mask))).collect();
+ let now: Vec<(AssetId<Mesh>, [f32; 12], u8)> = instances.0.iter().filter(|(id, ..)| blases.0.contains_key(id)).map(|(id, m, mask)| (*id, m.transpose().to_cols_array()[..12].try_into().unwrap(), *mask)).collect();
if std::env::var_os("RAEA_RT_DEBUG").is_some() {
for (id, _, _) in instances.0.iter().filter(|(id, _, _)| !blases.0.contains_key(id)) {
eprintln!("raea rt: no BLAS for {id:?}");
}
}
- let mut tlas = device.wgpu_device().create_tlas(&CreateTlasDescriptor {
- label: Some("raea rt tlas"),
- flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
- update_mode: AccelerationStructureUpdateMode::Build,
- max_instances: present.len().max(1) as u32,
- });
- for (i, (blas, m, mask)) in present.iter().enumerate() {
- let t: [f32; 12] = m.transpose().to_cols_array()[..12].try_into().unwrap();
- *tlas.get_mut_single(i).unwrap() = Some(TlasInstance::new(blas, t, 0, *mask));
+ if scene.is_some() && now == held.instances && held.batches == blases.1 {
+ spent(1, t0);
+ return;
+ }
+ // out of room (or the first): a new TLAS, half as much again as now
+ let mut made = None;
+ if scene.is_none() || now.len() as u32 > held.room {
+ held.room = (now.len() as u32 * 3 / 2).max(64);
+ made = Some(device.wgpu_device().create_tlas(&CreateTlasDescriptor { label: Some("raea rt tlas"), flags: AccelerationStructureFlags::PREFER_FAST_TRACE, update_mode: AccelerationStructureUpdateMode::Build, max_instances: held.room }));
+ }
+ let mut scene = scene;
+ let (tlas, was) = match (made.as_mut(), scene.as_deref_mut()) {
+ (Some(t), _) => (t, 0),
+ (None, Some(s)) => (&mut s.0, held.instances.len()),
+ (None, None) => return,
+ };
+ // each slot this frame's instance, or empty where last frame's list was longer
+ for i in 0..now.len().max(was) {
+ *tlas.get_mut_single(i).unwrap() = now.get(i).map(|(id, t, mask)| TlasInstance::new(&blases.0[id], *t, i as u32, *mask));
}
let mut enc = device.create_command_encoder(&CommandEncoderDescriptor { label: Some("raea rt tlas") });
- enc.build_acceleration_structures(&[], [&tlas]);
+ enc.build_acceleration_structures(&[], [&*tlas]);
queue.submit([enc.finish()]);
spent(1, t0);
+ TLAS_BUILT[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ TLAS_BUILT[1].store(now.len() as u64, std::sync::atomic::Ordering::Relaxed);
if std::env::var_os("RAEA_RT_DEBUG").is_some() {
- eprintln!("raea rt: {} instances, {} with a BLAS ({} BLAS)", instances.0.len(), present.len(), blases.0.len());
+ eprintln!("raea rt: {} instances, {} with a BLAS ({} BLAS)", instances.0.len(), now.len(), blases.0.len());
+ }
+ held.instances = now;
+ held.batches = blases.1;
+ if let Some(t) = made {
+ commands.insert_resource(Scene(t));
}
- commands.insert_resource(Scene(tlas));
}