Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Live: a power or material edit writes only the light list's codes that changed (a game flashing 300 moving emitters: 600 -> 11 ms a frame) (#180)

Evan (Yifeng) Wangb63c6aba417f
diff --git a/renderer/src/live/edits.rs b/renderer/src/live/edits.rs
index 207d5ab..b9ba70b 100644
--- a/renderer/src/live/edits.rs
+++ b/renderer/src/live/edits.rs
@@ -1329,7 +1329,7 @@ impl LiveCore {
pub(super) fn write_emitters(&mut self, new: &[(u32, crate::GpuEmitterTri)]) {
self.land_powers_now();
let g = &self.g;
- let LoadedScene { tables: e, base: b, up, pool_tris, tris: flat, tri_order, edit, instances, .. } = &mut self.sc;
+ let LoadedScene { tables: e, base: b, up, pool_tris, tris: flat, tri_order, edit, instances, codes_sent: sent, .. } = &mut self.sc;
let size = std::mem::size_of::<crate::GpuEmitterTri>();
let (mut w_tris, mut w_attrs) = (Gather::default(), Gather::default());
for (i, t) in new {
@@ -1369,8 +1369,18 @@ impl LiveCore {
// written over the code the new ones were made with.
let codes = crate::emitter_codes(&e.emitter_tris, &order, e.light_tree.as_ref());
let fixed = e.light_tree.as_ref().map_or(0, |t| t.dynamic.map_or(t.order.len(), |(_, ns)| ns as usize));
+ // Only the codes that changed since the last write (a power edit leaves them all as they were; each is a
+ // write of its own, the list's records being 48 bytes apart): a new emitter's record was made with code 0.
+ sent.resize(e.emitter_tris.len(), u32::MAX);
+ for &(i, _) in new {
+ sent[i as usize] = u32::MAX;
+ }
for &i in &order[fixed.min(order.len())..] {
- w_tris.put((i as usize * size + std::mem::offset_of!(crate::GpuEmitterTri, code)) as u64, bytemuck::bytes_of(&codes[i as usize]));
+ let c = codes[i as usize];
+ if sent[i as usize] != c {
+ w_tris.put((i as usize * size + std::mem::offset_of!(crate::GpuEmitterTri, code)) as u64, bytemuck::bytes_of(&c));
+ sent[i as usize] = c;
+ }
}
let (em, total, power) = crate::emitter_cdf(&e.emitter_tris, &order, &e.materials, &e.objects, e.stride, b.light_lod, crate::EmitterCull::of(b));
if let Some(t) = e.light_tree.as_mut() {
diff --git a/renderer/src/live/inplace_tests.rs b/renderer/src/live/inplace_tests.rs
index 7de22c4..f3dbaaa 100644
--- a/renderer/src/live/inplace_tests.rs
+++ b/renderer/src/live/inplace_tests.rs
@@ -755,6 +755,59 @@ fn an_emitter_scaled_in_place_is_lit_as_a_build_lights_it() {
}
}
+/// Power edits of emitters (a game flashing its glowing bodies every frame) write only the light list's codes that
+/// changed (`LoadedScene::codes_sent`: each code is a write of its own, and rewriting a list of 133 k moving emitters
+/// cost a frame 470 ms). After several, an emitter moved between them, and two materials of still meshes that start
+/// emitting one after the other (emitters added in place, their records made with code 0), every emitter on the GPU
+/// has its code: with emitters that move (their codes stay), and in a scene whose emitters all stand still, where the
+/// light tree has a slot for the added ones and the second material's regrows it (the first one's codes change).
+#[test]
+fn power_edits_keep_the_light_lists_codes() {
+ let presets = crate::default_presets_dir();
+ let open = |w: &World| gpu::Gpu::new().and_then(|g| LiveCore::new(g, w, Path::new("."), &presets, 96, 54));
+ let late = |emissive: f32| serde_json::from_value(serde_json::json!({ "name": "late", "color": [0.2, 0.2, 0.2], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [1.0, 0.6, 0.3], "emissiveIntensity": emissive, "doubleSided": true })).unwrap();
+ for (tree, moving) in [(true, true), (false, true), (true, false)] {
+ let what = |s: &str| format!("{s}, {}, {} a light tree", if moving { "emitters that move" } else { "still emitters" }, if tree { "with" } else { "without" });
+ let mut w = world();
+ w.set_material(6, late(0.0));
+ w.set_material(7, late(0.0));
+ // Two that emit (moving or not), and two still meshes of the materials that emit later, of their own BLASes
+ // (the scene edits meshes: a tree without moving emitters has a slot for emitters added in place).
+ for (id, x, material, movable) in [(13u32, 0.35f32, 5u32, moving), (14, -0.35, 5, moving), (15, 0.0, 6, false), (16, -0.7, 7, false)] {
+ w.set_object(id, format!("mesh{id}"), None, Kind::Mesh { geometry: 3, material, own_blas: !movable, materials: vec![] }, true, movable).unwrap();
+ w.set_transform(id, Vec3::new(x, -0.45, 0.6), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
+ }
+ if tree {
+ strip(&mut w);
+ }
+ let mut live = match open(&w) {
+ Ok(c) => c,
+ Err(e) => return eprintln!("skipped: {e}"),
+ };
+ w.take_changes();
+ if !live.g.hwrt {
+ return;
+ }
+ assert_eq!(live.sc.tables.light_tree.as_ref().map(|t| t.dynamic.is_some()), tree.then_some(!moving), "{}: the tree's slot", what("as loaded"));
+ list_is_its_triangles(&what("as loaded"), &live);
+ for (k, power) in [3.0f32, 0.5, 2.0].into_iter().enumerate() {
+ if k > 0 {
+ w.set_material(5 + k as u32, late(30.0));
+ }
+ w.set_power(13, power).unwrap();
+ if moving {
+ w.set_transform(14, Vec3::new(-0.35 + 0.1 * k as f32, -0.45, 0.6), glam::Quat::from_rotation_z(0.3 * k as f32), Vec3::ONE).unwrap();
+ }
+ live.commit(&mut w).unwrap();
+ live.land_powers_now();
+ list_is_its_triangles(&what(&format!("power {power}{}", if k > 0 { " and a material that starts emitting" } else { "" })), &live);
+ }
+ let t = &live.sc.tables;
+ let emitting = t.emitter_tris.iter().filter(|e| crate::emitter_power(e, &t.materials, &t.objects, t.stride) > 0.0).count();
+ assert!(emitting >= 8, "{}: the late materials' emitters were added ({emitting} emit)", what("at the end"));
+ }
+}
+
/// The light list's copies of its emitters' triangles follow them: as loaded, after an emitter the CPU moves has
/// moved (in the light tree's moving subtree, and without a tree), after an emitting mesh a body drives has moved,
/// after a model's placement or its clip has moved its emitters, after a particle's pass has placed it, and after
diff --git a/renderer/src/live/load.rs b/renderer/src/live/load.rs
index a762808..7b7372c 100644
--- a/renderer/src/live/load.rs
+++ b/renderer/src/live/load.rs
@@ -56,6 +56,9 @@ pub(super) struct LoadedScene {
/// and the flattened triangles' build indices in traversal order (positions in `tris`).
pub(super) mat_runs: std::collections::HashMap<u32, Vec<[u32; 3]>>,
pub(super) tri_order: Vec<u32>,
+ /// The light list's path codes as the last `write_emitters` wrote them (`GpuEmitterTri::code`, by emitter): it
+ /// writes only those that changed since. Empty after a load or a rebuild (the next write sends them all).
+ pub(super) codes_sent: Vec<u32>,
}
/// Cores the sky's worker thread bakes with: the rest stay with the frame (scripts, uploads, the GPU's driver).
@@ -302,5 +305,5 @@ pub(super) fn load_scene(g: &gpu::Gpu, world: &World, assets: &Path, presets: &P
pool_first: prep.groups.iter().sum(),
blobs,
};
- Ok((LoadedScene { timings, triangles, tris, tri_nodes, tri_instances, pool_tris, pool_place, meshes, instances, sections, rows, gpu_instances, procs, proc_first, moving_instances: tables.retained.moves_instances(), instanced, instance_features, name: sc.name.clone(), up, base, tables, fixed_features, dynamic, edit, emit, mat_runs, tri_order }, view, env))
+ Ok((LoadedScene { timings, triangles, tris, tri_nodes, tri_instances, pool_tris, pool_place, meshes, instances, sections, rows, gpu_instances, procs, proc_first, moving_instances: tables.retained.moves_instances(), instanced, instance_features, name: sc.name.clone(), up, base, tables, fixed_features, dynamic, edit, emit, mat_runs, tri_order, codes_sent: vec![] }, view, env))
}