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.

An empty slot of the top-level structure is an instance at a point, not a zero transform (#291)

Yujia5727e6b529b0
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 248449d..d03aad6 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -2921,6 +2921,16 @@ Mutations: 26. 25 failed a test there was; one, a texture put on a shelf too low
Found by review: a translucent particle's material given another function kept the kernel's old dispatch for the
translucency pass, which has its particles' functions alone; the kernel is made again where those are others.
+**An empty slot of the top-level structure is an instance at a point (2026-11-27, after it).** The patched build
+of the top-level structure wrote an empty slot as an instance of a zero transform (`renderer/third_party/wgpu-core/
+PATCH.md`), and a scene with room for edits has a thousand of them. On Metal such a structure lost hits: a scene of
+one wall was black from everywhere, and a camera at the world's origin saw nothing in others (the cause of eleven
+render tests that failed between 2026-11-23 and 2026-11-25, while every scene had eight such slots). An empty slot
+is now the same instance scaled to a point (1e-6 of its size, as an instance placed without size is) where the
+structure's first instance stood, with mask 0; slots left empty before the first instance came are written again
+when it comes. Test: `the_camera_at_the_worlds_origin_sees_what_it_sees_elsewhere` (black under hardware
+traversal before). Not checked on Vulkan.
+
**The emitters' weights are made on the GPU (2026-11-27).** An emitter's weight (its area × its material's emissive
luminance × its object's power), the cumulative table, its guide and the light tree's powers were made on the CPU,
over every emitter, at each power or material edit: 59 to 74 ms in the frame of a material edit in Tokyo's
diff --git a/renderer/src/live/inplace_tests.rs b/renderer/src/live/inplace_tests.rs
index 42bf925..8a7c28a 100644
--- a/renderer/src/live/inplace_tests.rs
+++ b/renderer/src/live/inplace_tests.rs
@@ -5376,3 +5376,52 @@ fn the_gpu_weighs_the_emitters_as_the_cpu_does() {
fn the_gpu_weighs_the_emitters_as_the_cpu_does_without_a_tree() {
weights_on_the_gpu(100);
}
+
+/// One emissive wall 50 m before a camera at `at`, in a scene with room for edits (its top-level structure has empty
+/// slots): the picture after 16 frames. Wherever the camera stands it sees the wall: at the world's origin too, where
+/// an empty slot must not stand in a ray's way.
+fn wall_seen_from(at: Vec3, marker: bool) -> Option<(LiveCore, Vec<u8>)> {
+ let mut w = World::default();
+ w.set_material(5, serde_json::from_value(serde_json::json!({ "name": "wall", "color": [0.0, 0.0, 0.0], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [1, 1, 1], "emissiveIntensity": 100.0, "doubleSided": true })).unwrap());
+ let s = 50.0;
+ w.set_geometry(2, Geometry { positions: vec![-s, -s, 0.0, s, -s, 0.0, s, s, 0.0, -s, s, 0.0], normals: [0.0, 0.0, 1.0].repeat(4), uvs: vec![0.0; 8], indices: vec![0, 1, 2, 0, 2, 3], groups: vec![], colors: vec![] }).unwrap();
+ // (A mesh the scene may edit: the scene gets its room.)
+ w.set_object(10, "wall".into(), None, Kind::Mesh { geometry: 2, material: 5, own_blas: true, materials: vec![] }, true, false).unwrap();
+ w.set_transform(10, at + Vec3::new(0.0, 0.0, -50.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
+ if marker {
+ // (The scene's first instance stands where the camera does: the empty slots stand at that point.)
+ w.set_material(1, serde_json::from_value(serde_json::json!({ "name": "m", "color": [0.5, 0.5, 0.5], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [0, 0, 0], "emissiveIntensity": 0.0, "doubleSided": true })).unwrap());
+ w.set_geometry(3, Geometry { positions: vec![-0.1, -0.1, 0.0, 0.1, -0.1, 0.0, 0.1, 0.1, 0.0, -0.1, 0.1, 0.0], normals: [0.0, 0.0, 1.0].repeat(4), uvs: vec![0.0; 8], indices: vec![0, 1, 2, 0, 2, 3], groups: vec![], colors: vec![] }).unwrap();
+ w.set_object(3, "marker".into(), None, Kind::Mesh { geometry: 3, material: 1, own_blas: true, materials: vec![] }, true, false).unwrap();
+ // (Turned edge-on to the view: the camera in its plane sees nothing of it.)
+ w.set_transform(3, at, glam::Quat::from_rotation_y(std::f32::consts::FRAC_PI_2), Vec3::ONE).unwrap();
+ }
+ w.set_object(4, "camera".into(), None, Kind::Camera(camera(50.0, None, manual(8.0))), true, false).unwrap();
+ w.set_transform(4, at, glam::Quat::IDENTITY, Vec3::ONE).unwrap();
+ w.set_settings(Settings { camera: Some(4), ..Default::default() });
+ let presets = crate::default_presets_dir();
+ let mut live = match gpu::Gpu::new().and_then(|g| LiveCore::new(g, &w, Path::new("."), &presets, 96, 54)) {
+ Ok(c) => c,
+ Err(e) => {
+ eprintln!("skipped: {e}");
+ return None;
+ }
+ };
+ let p = picture(&mut live, 16);
+ Some((live, p))
+}
+
+#[test]
+fn the_camera_at_the_worlds_origin_sees_what_it_sees_elsewhere() {
+ for marker in [false, true] {
+ let what = if marker { "the empty slots at the camera" } else { "the empty slots at the wall" };
+ let Some((_, elsewhere)) = wall_seen_from(Vec3::new(0.31, 0.17, 0.23), marker) else { return };
+ let Some((live, origin)) = wall_seen_from(Vec3::ZERO, marker) else { return };
+ let (a, b) = (region_mean(&origin, [0, 0, 96, 54]), region_mean(&elsewhere, [0, 0, 96, 54]));
+ eprintln!("ROOM ({what}): {} records of room, {} instances, hardware traversal {}; picture means {a:.1} at the origin, {b:.1} elsewhere; first instance at {:?}", live.sc.up.room.records, live.sc.instances.len(), live.g.hwrt, live.sc.instances.first().map(|i| i.world.w_axis.truncate()));
+ assert!(!live.g.hwrt || live.sc.up.room.records > 0, "{what}: the scene has room for edits: empty slots in its top-level structure");
+ assert!(b > 30.0, "{what}: the wall is seen from 0.4 m off the origin: {b}");
+ let d = apart(&origin, &elsewhere);
+ assert!(d < 1.0, "{what}: from the world's origin the picture is {d:.2} steps from the one seen 0.4 m away (means {a:.1} for {b:.1})");
+ }
+}
diff --git a/renderer/third_party/wgpu-core/PATCH.md b/renderer/third_party/wgpu-core/PATCH.md
index 00d554f..56ed8cc 100644
--- a/renderer/third_party/wgpu-core/PATCH.md
+++ b/renderer/third_party/wgpu-core/PATCH.md
@@ -34,8 +34,12 @@ far up a slot was touched, nothing uses it, and its comment says as much.
it). A build:
- serialises the changed slots alone and copies each run of them to its place in the TLAS's instance buffer (the
slots keep their positions: upstream packs the present instances together, so one removed moved all after it);
- - writes an empty slot as a copy of the TLAS's first BLAS (`TlasSlots::null`, held for the TLAS's life) with mask 0
- and a zero transform: no extent, never hit;
+ - writes an empty slot as a copy of the TLAS's first BLAS (`TlasSlots::null`, held for the TLAS's life) with mask 0,
+ scaled to a point (1e-6 of its size) where the instance that gave it stood (`TlasSlots::null_at`): never hit, and
+ an instance as any other to the build. (It was a zero transform until 2026-11-27. Among such instances Metal's
+ structure lost a scene's only wall to every ray, and a ray from the world origin its hits: a scene with room
+ for edits has a thousand empty slots.) The slots left empty before the first BLAS came are written so when it
+ comes (the builds until then take no instance);
- builds over all `total` slots, from the instance buffer as it now is;
- gives the TLAS its BLASes once each as its dependencies (validation and residency per use: the distinct BLASes,
not the instances).
diff --git a/renderer/third_party/wgpu-core/src/command/ray_tracing.rs b/renderer/third_party/wgpu-core/src/command/ray_tracing.rs
index 717ae25..d42bcc9 100644
--- a/renderer/third_party/wgpu-core/src/command/ray_tracing.rs
+++ b/renderer/third_party/wgpu-core/src/command/ray_tracing.rs
@@ -246,6 +246,44 @@ pub(crate) fn build_acceleration_structures(
slots.blas.resize(total, None);
}
let key = |b: &Arc<Blas>| Arc::as_ptr(b) as usize;
+ // What the empty slots name, and where they stand: the first instance the TLAS is given, before any slot of
+ // this build is written (an empty slot ahead of it in the list is written with it too).
+ let had_null = slots.null.is_some();
+ if !had_null {
+ if let Some(first) = package.instances.iter().flatten().next() {
+ slots.null = Some(first.blas.clone());
+ slots.null_at = [first.transform[3], first.transform[7], first.transform[11]];
+ }
+ }
+ // An empty slot: the null BLAS at a point (its size times EMPTY_SCALE), mask 0: never hit, and an instance as
+ // any other to the structure's build. (Not a zero transform: among instances without extent Metal's
+ // structure lost a scene's only wall to every ray, and a ray from the world origin its hits.)
+ const EMPTY_SCALE: f32 = 1.0e-6;
+ let empty_device = state.device.clone();
+ let empty = |null: &Arc<Blas>, at: [f32; 3]| {
+ empty_device.raw().tlas_instance_to_bytes(hal::TlasInstance {
+ transform: [EMPTY_SCALE, 0.0, 0.0, at[0], 0.0, EMPTY_SCALE, 0.0, at[1], 0.0, 0.0, EMPTY_SCALE, at[2]],
+ custom_data: 0,
+ mask: 0,
+ blas_address: null.handle,
+ pipeline_intersection_data_offset: 0,
+ })
+ };
+ // (one copy a run of adjacent slots)
+ fn put(copies: &mut Vec<hal::BufferCopy>, staging: &mut Vec<u8>, k: usize, bytes: Vec<u8>) {
+ let (at, len) = (staging.len() as u64, bytes.len() as u64);
+ match copies.last_mut() {
+ Some(c) if c.src_offset + c.size.get() == at && c.dst_offset + c.size.get() == k as u64 * len => {
+ c.size = wgt::BufferSize::new(c.size.get() + len).unwrap();
+ }
+ _ => {
+ if let Some(size) = wgt::BufferSize::new(len) {
+ copies.push(hal::BufferCopy { src_offset: at, dst_offset: k as u64 * len, size });
+ }
+ }
+ }
+ staging.extend(bytes);
+ }
// (the slots of the changed ranges, in order, as `instances` lists them)
let slots_of = package.ranges.iter().flat_map(|r| r.start as usize..(r.end as usize).min(total));
let mut copies: Vec<hal::BufferCopy> = Vec::new();
@@ -290,9 +328,6 @@ pub(crate) fn build_acceleration_structures(
}
if let Some(b) = &new {
slots.refs.entry(key(b)).or_insert_with(|| (b.clone(), 0)).1 += 1;
- if slots.null.is_none() {
- slots.null = Some(b.clone());
- }
}
slots.blas[k] = new;
}
@@ -304,15 +339,9 @@ pub(crate) fn build_acceleration_structures(
blas_address: instance.blas.handle,
pipeline_intersection_data_offset: 0,
}),
- // (an empty slot: the null BLAS, no extent, no mask; with no BLAS yet, nothing names it)
+ // (an empty slot; with no BLAS yet nothing names it, and the build takes no instance)
None => match &slots.null {
- Some(null) => state.device.raw().tlas_instance_to_bytes(hal::TlasInstance {
- transform: [0.0; 12],
- custom_data: 0,
- mask: 0,
- blas_address: null.handle,
- pipeline_intersection_data_offset: 0,
- }),
+ Some(null) => empty(null, slots.null_at),
None => {
let size = state.device.raw().tlas_instance_to_bytes(hal::TlasInstance {
transform: [0.0; 12],
@@ -325,19 +354,26 @@ pub(crate) fn build_acceleration_structures(
}
},
};
- // (one copy a run of adjacent slots)
- let (at, len) = (instance_buffer_staging_source.len() as u64, bytes.len() as u64);
- match copies.last_mut() {
- Some(c) if c.src_offset + c.size.get() == at && c.dst_offset + c.size.get() == k as u64 * len => {
- c.size = wgt::BufferSize::new(c.size.get() + len).unwrap();
+ put(&mut copies, &mut instance_buffer_staging_source, k, bytes);
+ }
+ // The TLAS's first BLAS has come: the slots that were empty before it were written as nothing (the builds
+ // until now took no instance); each is written as an empty slot now.
+ if !had_null {
+ if let Some(null) = slots.null.clone() {
+ let mut written = alloc::vec![false; total];
+ for r in package.ranges.iter() {
+ for k in r.start as usize..(r.end as usize).min(total) {
+ written[k] = true;
+ }
}
- _ => {
- if let Some(size) = wgt::BufferSize::new(len) {
- copies.push(hal::BufferCopy { src_offset: at, dst_offset: k as u64 * len, size });
+ for k in 0..total {
+ if !written[k] && slots.blas[k].is_none() {
+ put(&mut copies, &mut instance_buffer_staging_source, k, empty(&null, slots.null_at));
}
}
+ // (in slot order, as the copies of a build are)
+ copies.sort_by_key(|c| c.dst_offset);
}
- instance_buffer_staging_source.extend(bytes);
}
// The TLAS's dependencies: each BLAS once (the null one among them).
let mut dependencies: Vec<Arc<Blas>> = slots.refs.values().map(|(b, _)| b.clone()).collect();
diff --git a/renderer/third_party/wgpu-core/src/resource.rs b/renderer/third_party/wgpu-core/src/resource.rs
index e6af155..7dfdba4 100644
--- a/renderer/third_party/wgpu-core/src/resource.rs
+++ b/renderer/third_party/wgpu-core/src/resource.rs
@@ -2694,13 +2694,15 @@ pub struct Tlas {
/// writes only the slots that changed (PATCH.md).
#[derive(Debug, Default)]
pub(crate) struct TlasSlots {
- /// Each slot's BLAS (None: an empty slot, written as `null`'s copy with mask 0 and no extent).
+ /// Each slot's BLAS (None: an empty slot, written as `null`'s copy with mask 0, at a point: `null_at`).
pub(crate) blas: Vec<Option<Arc<Blas>>>,
/// The BLASes the slots name, each once, with how many slots name it: the TLAS's dependencies.
pub(crate) refs: crate::FastHashMap<usize, (Arc<Blas>, u32)>,
/// What the empty slots name: the first BLAS the TLAS was given, held for as long as the TLAS (an empty slot written
/// with another must not outlive it).
pub(crate) null: Option<Arc<Blas>>,
+ /// Where the empty slots stand: where the instance that gave `null` stood.
+ pub(crate) null_at: [f32; 3],
}
impl Drop for Tlas {