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.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
A posed skinned model is posed in place under software traversal (#184)
Yujiad2d6f27eba12
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 65c4462..084ad09 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -2776,6 +2776,35 @@ in-place tests' world, rows given back; two models in a shared pose posed again(tests/render/data/arm.glb, a skinned strip of two materials); `geometry/emitter-model-live` (a model's emittingsection that is not its first, the model a unit under software traversal).+**A posed skinned model is posed in place under software traversal (2026-11-22).** Under software traversal a mesh+that deforms was written out into the still scene's tree, so every pose rebuilt the scene. Now it is a unit as any+mesh. Its tree has the shape of a tree built once over its model's bind pose, one shape for every placement of the+model (the pass's corners and each such mesh's triangles are in that tree's order), and the boxes of its pose. A new+pose places the model's corners on the CPU, takes the boxes again (`units::refit`), writes the nodes where they lie+and gives the top tree the unit's new bounds; the skinning pass writes the triangles and attributes as under+hardware traversal, without the BLAS input. Hidden, shown and removed, it goes as any unit.+Why the boxes alone: a tree made again for a pose costs sixty times as much (a character of 13,744 triangles, one+core: 3.0 ms against 0.05) and walks 4 to 11 % shorter by its nodes' areas; the clip's first pose is no better a+shape than the bind pose.+Still a rebuild under software traversal: a skinned model added (its triangles and its tree need room, which a+scene has there only with geometry of its own to edit, and which does not grow there).+Measured, software traversal. A game with a character posed every frame: every frame rebuilt the scene, 30.6 ms of+CPU; now none does, 3.2 ms. A crowd of 150 walkers: 2.0 s a frame; now no rebuild, 7.6 ms of commit. What a frame's+trace pays, nothing moving, at 1280 x 720: four characters filling the view, 4.67 ms before and 5.20 now (+11 %),+of which 6.5 % is the scene having units at all (its kernel had no unit walk) and 4.9 % the kept shape (the same+units with trees built for the pose: 4.97); the crowd standing in a street, 85 ms either way.+What a pose costs the CPU there: the corners' positions (0.2 ms a character of 13,744 triangles, one core) and the+boxes (0.05). The crowd's commit is 7.6 ms, against 3.3 under hardware traversal, whose BLASes the GPU builds.+Tests, under both traversals: four models of two files, each mesh's triangles, attributes and previous vertices on+the GPU against the CPU skin; under software traversal each mesh's tree read back (its model's links, a leaf's box+its triangles', an interior node's its children's), its bounds its root's, its unit's leaf of the top tree; the+motion vectors of a pose no longer than the furthest a corner went; one hidden, posed while hidden, shown; one+removed. A unit test of the boxes taken again (corners that are not finite too).+22 mutations of the new logic: 20 fail a test under software traversal, one natively (the pass bound again when the+scene's buffers grow, which they do under hardware traversal alone), and one is equivalent (the pass writing the+BLAS input under software traversal: the writes fall in a spare word no one reads; the constant saves the work).+Three lines that told the traversals apart where no test could were taken out: the pose path is one for both.+**A posed skinned model is added and removed in place (2026-11-21).** A character that comes or goes while the gameruns rebuilt the scene. Now one of a model the scene has is added as the build makes one: a mesh of its own in theroom for edits, its triangles skinned on the CPU once for the pose it comes in, a job of the skinning pass for itsdiff --git a/docs/RUNTIME.md b/docs/RUNTIME.mdindex 5c8b31e..171f3d6 100644--- a/docs/RUNTIME.md+++ b/docs/RUNTIME.md@@ -1175,10 +1175,16 @@ procedural objects (`trace_procs`: the ray into the object's space, a tree of itas vertex colours are; gpu.rs `KernelVariant::flags`): the file's walk, a call at the end of the still walk, and the stillwalk's stack declaration taken out ("The stack"). A scene without units has none of it; hardware traversal'skernel never does.-- Still flattened, as before: meshes that deform (a posed skinned model's), every instance of a scene with more- of them than a top tree holds (2^18), and every instance of an offline render (`prepare`; a live session's scene- goes through `prepare_live`), unless `THREERT_UNITS=1`. `THREERT_UNITS=0` flattens everything, as before this- step (the A/B).+- **A mesh that deforms** (a posed skinned model's) is a unit too. Its tree has the shape of a tree built once over+ its model's bind pose, one shape for every placement of the model (the skinning pass's corners and each such+ mesh's triangles are in that tree's order), and the boxes of its pose. A new pose places the model's corners on+ the CPU, takes the boxes again (`units::refit`: a leaf's from its triangles, an interior node's from its+ children), and writes the nodes where they lie; the pass writes the triangles. Made again for a pose, a tree+ costs sixty times as much (a character of 13,744 triangles, one core: 3.0 ms against 0.05) and walks 4 to 11 %+ shorter by its nodes' areas.+- Still flattened, as before: every instance of a scene with more of them than a top tree holds (2^18), and every+ instance of an offline render (`prepare`; a live session's scene goes through `prepare_live`), unless+ `THREERT_UNITS=1`. `THREERT_UNITS=0` flattens everything, as before this step (the A/B).**Edits in place.** The edits that hardware traversal makes in place go through one type (`gpu::Top`: the TLAS andits BLASes, or the units), so the engine's path is the same under both traversals (`live/edits.rs` `move_instances`,@@ -1191,11 +1197,12 @@ its BLASes, or the units), so the engine's path is the same under both traversal| a mesh of its own is added (a new chunk, a new box) | its triangles and attributes into the edit room in its tree's order, its tree into the mesh range, a record, the top tree || a mesh of its own changes (a voxel edit: the chunks it touched) | those meshes' trees made again (the old tree's nodes given back and filled), their records; no other unit is read || a mesh is removed | its record cleared, its tree given back and filled, the top tree |+| a skinned model takes a new pose | its mesh's tree's boxes written where they lie (the shape kept), its triangles by the skinning pass, the top tree |+| a skinned model is hidden, shown or removed | its record, the top tree (shown: skinned as it is posed then) |The scene is rebuilt, as before this step, by: the first instanced mesh of a scene that has none (its kernel has nounit walk: one rebuild, as hardware traversal's first instance is); an edit of a procedural object; a skinned-model's pose (its triangles are in the still tree; a rigid group's parts are units, posed in place); an instance of-a mesh that deforms; **room that is+model added (hardware traversal adds it in place); **room that isfull** (the edit room and the tree ranges are sized at the build, a quarter of the scene and at least 65,536triangles and 1,024 records, and do not grow under software traversal: under hardware traversal they do; what anedit replaces stays behind until the next rebuild, under both; a scene with units and no geometry of its own todiff --git a/renderer/shaders/skin.wgsl b/renderer/shaders/skin.wgslindex d18b17f..f824d9c 100644--- a/renderer/shaders/skin.wgsl+++ b/renderer/shaders/skin.wgsl@@ -44,6 +44,9 @@ const CORNER_NORMAL: u32 = 1u;const CORNER_TANGENT: u32 = 2u;// Workgroups a row of the dispatch (a dimension holds 65,535).const ROW: u32 = 32768u;+// Whether the BLASes' input is written too: hardware traversal. Software traversal has none (its meshes' trees take+// their boxes on the CPU), and binds a word there.+override BLAS: bool = true;@group(0) @binding(0) var<uniform> PP: Params;// (mesh, its first triangle of the block)@@ -166,6 +169,9 @@ fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_index) lverts[k * 3u] = vec4<f32>(c0.p, bitcast<f32>(TRI_DYNAMIC));verts[k * 3u + 1u] = vec4<f32>(e1, 0.0);verts[k * 3u + 2u] = vec4<f32>(e2, 0.0);+ if !BLAS {+ return;+ }// The BLAS's vertices: a triangle that is not finite has no size there.var q0 = c0.p;var q1 = c1.p;diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rsindex c95ae05..bc8ec56 100644--- a/renderer/src/gpu.rs+++ b/renderer/src/gpu.rs@@ -228,7 +228,8 @@ pub struct TopBuild {pub instances: bool,/// The dynamic groups' BLASes: their triangles were re-transformed.pub dynamic: bool,- /// Instanced meshes' BLASes: new meshes, a skinned model's new pose.+ /// Instanced meshes' BLASes: new meshes, a skinned model's new pose. (Software traversal: their trees are written+ /// by then; the top tree is made over their bounds.)pub meshes: Vec<u32>,/// Procedural BLASes an edit made.pub procs: Vec<PendingProc>,@@ -765,6 +766,8 @@ pub struct SkinGpu {/// Each source's first triangle among the corners (`Scene::skin_sources`), and the palette's matrices in use.source_first: Vec<u32>,palette_end: u32,+ /// Software traversal: what stands at the BLAS input's binding (the pass writes none there).+ no_blas: Option<wgpu::Buffer>,}impl SkinGpu {@@ -2363,7 +2366,8 @@ impl Gpu {timings.push(("vcolor_mb".into(), if scene.gpu_vcolors.is_empty() { 0.0 } else { (scene.gpu_vcolors.len() * 4 + room.tris * 12) as f64 / (1 << 20) as f64 }));let b_attrs = buf_room("attrs", bytemuck::cast_slice(&scene.attrs), room.tris * std::mem::size_of::<crate::scene::GpuTriAttr>(), edit);// (With units, edits write trees into it: units.rs.)- let b_nodes = buf("nodes", nodes, if scene.units.is_some() { st | wgpu::BufferUsages::COPY_DST } else { st });+ // (COPY_SRC: read back by the test that holds a refit tree to its triangles.)+ let b_nodes = buf("nodes", nodes, if scene.units.is_some() { st | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC } else { st });// What the triangles hold on the GPU besides their BLASes: vertices (a, e1, e2), attributes, the software BVH's nodes// (ROADMAP § 6 step 2's table: a meshed representation against voxel data).timings.push(("tri_data_mb".into(), (std::mem::size_of_val(tri_verts) + std::mem::size_of_val(&scene.attrs[..]) + nodes.len()) as f64 / (1 << 20) as f64));@@ -2760,9 +2764,12 @@ impl Gpu {}_ => None,};- // The skinning pass (hardware RT: it writes the BLASes' input), a job each mesh that deforms.- let skin = match &accel {- Some(a) if !scene.retained.skinned.is_empty() => Some(self.skin_pass(scene, a.pool_first, &b_verts, &b_attrs, &a.verts)),+ // The skinning pass, a job each mesh that deforms: under hardware traversal (it writes the BLASes' input too),+ // and under software traversal where such a mesh is a unit.+ let skin = match (&accel, &scene.units) {+ _ if scene.retained.skinned.is_empty() => None,+ (Some(a), _) => Some(self.skin_pass(scene, a.pool_first, &b_verts, &b_attrs, Some(&a.verts))),+ (None, Some(_)) => Some(self.skin_pass(scene, groups.iter().sum(), &b_verts, &b_attrs, None)),_ => None,};self.queue.submit([]);@@ -3439,11 +3446,17 @@ impl Gpu {}/// The bind groups that hold the scene's buffers again, from `up`'s as they are now (one that grew: the tables,- /// live/edits.rs): the kernel's, the light list's fills' and the particle passes'.+ /// live/edits.rs): the kernel's, the light list's fills', the skinning pass's and the particle passes'.pub fn rebind_scene(&self, up: &mut Uploaded) {for f in [&mut up.fills.moved, &mut up.fills.particles].into_iter().flatten() {f.bg = self.emitter_fill_bind_group(&f.runs, &up.verts, &up.heap);}+ if let Some(k) = &mut up.skin {+ // (The BLASes' input, or what stands there under software traversal.)+ if let Some(blas) = up.accel.as_ref().map(|a| &a.verts).or(k.no_blas.as_ref()) {+ k.bg = skin_bind_group(&self.device, &k.pipeline, [&k.params, &k.blocks, &k.table, &k.corners, &k.palette], &up.verts, &up.attrs, blas);+ }+ }up.bg = self.scene_bind_group(&SceneBuffers { params: &up.params, verts: &up.verts, attrs: &up.attrs, heap: &up.heap, words: &up.words, prev_verts: &up.prev_verts, accum: &up.accum, parts: &up.bg_parts, accel: up.accel.as_ref(), uniform: up.uniform.as_ref() });let Some(a) = &up.accel else { return };let written = ParticleTargets { verts: &up.verts, prev_verts: &up.prev_verts, blas_verts: &a.verts, heap: &up.heap, attrs: &up.attrs };@@ -3453,17 +3466,17 @@ impl Gpu {if let Some(p) = &mut up.gpu_particles {p.bg = particle_bind_group(&self.device, &p.pipeline, "gpu particles", &[(0, &p.params), (1, &p.table), (7, &p.last), (8, &p.state), (9, &p.spawns), (10, &p.grid)], &written);}- if let Some(k) = &mut up.skin {- k.bg = skin_bind_group(&self.device, &k.pipeline, [&k.params, &k.blocks, &k.table, &k.corners, &k.palette], &up.verts, &up.attrs, &a.verts);- }}/// The skinning pass of `scene`: its pipeline, the models' corners, a job each skinned model, and room for a frame's/// work and every job's palette.- fn skin_pass(&self, scene: &Scene, pool_first: usize, verts: &wgpu::Buffer, attrs: &wgpu::Buffer, blas_verts: &wgpu::Buffer) -> SkinGpu {+ fn skin_pass(&self, scene: &Scene, pool_first: usize, verts: &wgpu::Buffer, attrs: &wgpu::Buffer, blas_verts: Option<&wgpu::Buffer>) -> SkinGpu {let d = &self.device;let module = d.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("skin"), source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/skin.wgsl").into()) });- let pipeline = d.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { label: Some("skin"), layout: None, module: &module, entry_point: Some("main"), compilation_options: Default::default(), cache: self.cache() });+ let blas = [("BLAS", if blas_verts.is_some() { 1.0 } else { 0.0 })];+ let pipeline = d.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { label: Some("skin"), layout: None, module: &module, entry_point: Some("main"), compilation_options: wgpu::PipelineCompilationOptions { constants: &blas, ..Default::default() }, cache: self.cache() });+ let no_blas = blas_verts.is_none().then(|| d.create_buffer(&wgpu::BufferDescriptor { label: Some("skin no blas"), size: 64, usage: wgpu::BufferUsages::STORAGE, mapped_at_creation: false }));+ let blas_verts = blas_verts.or(no_blas.as_ref()).unwrap();let mut source_first = vec![];let mut at = 0u32;for source in &scene.skin_sources {@@ -3486,7 +3499,7 @@ impl Gpu {let params = room("skin params", 16, wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST);let (b_blocks, palette) = (room("skin blocks", blocks as u64 * 8, storage), room("skin palette", matrices as u64 * 48, storage));let bg = skin_bind_group(d, &pipeline, [¶ms, &b_blocks, &b_table, &b_corners, &palette], verts, attrs, blas_verts);- SkinGpu { pipeline, bg, params, blocks: b_blocks, table: b_table, corners: b_corners, palette, jobs, source_first, palette_end: matrices }+ SkinGpu { pipeline, bg, params, blocks: b_blocks, table: b_table, corners: b_corners, palette, jobs, source_first, palette_end: matrices, no_blas }}/// Skins the meshes of `work` (a job and its pose's palette each) on the GPU: their triangles, shading attributes@@ -3576,7 +3589,12 @@ impl Gpu {/// A mesh tree's nodes into the node buffer at its root (`Units::add_tree` placed it).pub fn write_tree(&self, nodes: &wgpu::Buffer, root: u32, tree: &crate::units::MeshTree) {- self.queue.write_buffer(nodes, root as u64 * 32, bytemuck::cast_slice(&tree.nodes));+ self.write_nodes(nodes, root, &tree.nodes);+ }++ /// A tree's nodes written again where they lie (its boxes taken again, `units::refit`: as many, the same shape).+ pub fn write_nodes(&self, nodes: &wgpu::Buffer, root: u32, tree: &[crate::bvh::GpuNode]) {+ self.queue.write_buffer(nodes, root as u64 * 32, bytemuck::cast_slice(tree));}/// A freed mesh tree's nodes (`Units::drop_tree`: its root and length) are room again: leaves that end a walkdiff --git a/renderer/src/lib.rs b/renderer/src/lib.rsindex 1b48569..20f09ef 100644--- a/renderer/src/lib.rs+++ b/renderer/src/lib.rs@@ -555,7 +555,7 @@ pub fn prepare_as(hwrt: bool, units: bool, sc: &mut scene::Scene, timings: &mutif !hwrt && !sc.instances.is_empty() {trees = if units && sc.instances.len() <= units::MAX_UNITS { unit_trees(sc) } else { None };match &trees {- Some(_) => flatten_deforming(sc),+ Some(t) => flatten_deforming(sc, t),None => flatten_instances(sc),}}@@ -897,12 +897,48 @@ pub fn prepare_as(hwrt: bool, units: bool, sc: &mut scene::Scene, timings: &mut/// what goes with them: attributes, vertex colours) put in the tree's order. None: no units (a mesh no tree can be/// made of: a vertex that is not a number); the caller flattens every instance then.fn unit_trees(sc: &mut scene::Scene) -> Option<Vec<Option<units::MeshTree>>> {+ // A mesh that deforms (a posed skinned model's) is a unit as any: its tree has the shape of the tree over its+ // model's bind pose, one shape for every mesh skinned from the source, and its boxes are the pose's (taken again+ // at each new one: `units::refit`; a tree made again a pose costs sixty times that, and traces 5 % faster).+ // The source's corners are put in that tree's order, and so are each such mesh's triangles: the skinning pass+ // writes a triangle where its corners are.+ let source_of: std::collections::HashMap<u32, u32> = sc.retained.skinned.iter().map(|k| (k.mesh, k.source)).collect();+ let mut shapes: Vec<Option<(Vec<u32>, usize)>> = vec![None; sc.skin_sources.len()];+ sc.skin_trees = vec![vec![]; sc.skin_sources.len()];+ for s in 0..sc.skin_sources.len() {+ if !source_of.values().any(|&x| x as usize == s) {+ continue;+ }+ let rest = &sc.skin_rest[s];+ let bind: Vec<[Vec3A; 3]> = sc.skin_sources[s].iter().map(|c| c.map(|c| Vec3A::from(skin::skin_corner(&c, rest).0))).collect();+ match units::mesh_tree(&bind) {+ Ok(t) => {+ sc.skin_sources[s] = t.order.iter().map(|&i| sc.skin_sources[s][i as usize]).collect();+ sc.skin_trees[s] = t.nodes;+ shapes[s] = Some((t.order, t.depth));+ }+ Err(e) => {+ eprintln!("units: {e}: the scene's instances are flattened");+ return None;+ }+ }+ }let mut trees = Vec::with_capacity(sc.meshes.len());- for m in &sc.meshes {- if m.deforms || m.count == 0 {+ for (k, m) in sc.meshes.iter().enumerate() {+ if m.count == 0 {trees.push(None);continue;}+ if m.deforms {+ // (One no source skins: flattened, `flatten_deforming`.)+ let shape = source_of.get(&(k as u32)).and_then(|&s| shapes[s as usize].as_ref().map(|x| (s as usize, x)));+ trees.push(shape.filter(|(_, (order, _))| order.len() == m.count as usize).map(|(s, (order, depth))| {+ let mut nodes = sc.skin_trees[s].clone();+ let (lo, hi) = units::refit(&mut nodes, |i| sc.pool_tris[m.first as usize + order[i] as usize]);+ units::MeshTree { nodes, order: order.clone(), depth: *depth, lo, hi }+ }));+ continue;+ }match units::mesh_tree(&sc.pool_tris[m.first as usize..(m.first + m.count) as usize]) {Ok(t) => trees.push(Some(t)),Err(e) => {@@ -927,17 +963,28 @@ fn unit_trees(sc: &mut scene::Scene) -> Option<Vec<Option<units::MeshTree>>> {sc.pool_vcolors[r].copy_from_slice(&colors);}}+ // A skinned model's list of its mesh's triangles (the model's triangle, its section, its index in it) as they lie+ // now: one list a source, as before.+ let mut lists: Vec<Option<std::sync::Arc<Vec<(u32, u32, u32)>>>> = vec![None; shapes.len()];+ for k in &mut sc.retained.skinned {+ let (Some((order, _)), Some(Some(_))) = (&shapes[k.source as usize], trees.get(k.mesh as usize)) else { continue };+ let first = sc.meshes[k.mesh as usize].first as usize;+ let list = lists[k.source as usize].get_or_insert_with(|| {+ std::sync::Arc::new(order.iter().enumerate().map(|(at, &i)| (k.order[i as usize].0, sc.pool_attrs[first + at].uv[3].to_bits(), sc.pool_attrs[first + at].x[3].to_bits())).collect())+ });+ k.order = list.clone();+ }Some(trees)}-/// With units: the instances of meshes that deform (a posed skinned model's) are still written out as world-space-/// triangles, as every instance was. They keep their place among the instances (edits name instances by index) and-/// are no unit.-fn flatten_deforming(sc: &mut scene::Scene) {+/// With units: the instances of meshes that deform and have no tree (none skins them from a source) are still+/// written out as world-space triangles, as every instance was. They keep their place among the instances (edits+/// name instances by index) and are no unit.+fn flatten_deforming(sc: &mut scene::Scene, trees: &[Option<units::MeshTree>]) {for k in 0..sc.instances.len() {let inst = sc.instances[k].clone();let m = sc.meshes[inst.mesh as usize];- if inst.hidden || !m.deforms {+ if inst.hidden || !m.deforms || trees.get(inst.mesh as usize).is_some_and(|t| t.is_some()) {continue;}let first = sc.tris.len() as u32;diff --git a/renderer/src/live/edits.rs b/renderer/src/live/edits.rsindex 5ac1d26..7a1a950 100644--- a/renderer/src/live/edits.rs+++ b/renderer/src/live/edits.rs@@ -1075,7 +1075,7 @@ impl LiveCore {continue;}let Some(mut top) = gpu::Top::of(&mut self.sc.up.accel, &mut self.sc.up.units) else { return false };- // (Software traversal has a skinned model's triangles in its still tree: no instance to mask.)+ // (Software traversal: a mesh that deforms and has no tree is in the still tree, no instance to mask.)if !top.has_mesh(inst.mesh) {return false;}@@ -1874,13 +1874,16 @@ impl LiveCore {}/// Posed skinned models (`scene::Skinned`): each whose pose changed is skinned in its own space by the skinning- /// pass (gpu.rs `skin`: the CPU sends its pose's matrices); the commit builds their BLASes and the TLAS over their- /// new bounds. One that is hidden keeps the pose its mesh has: it is skinned when it is shown (`show_meshes`).- /// False when one is another model now, or its new pose mirrors other primitives than the one its mesh was made- /// for: the caller rebuilds. (The CPU keeps no triangles of such a pose: `LiveCore::mesh_tris` skins it when asked.)+ /// pass (gpu.rs `skin`: the CPU sends its pose's matrices). Hardware traversal: the commit builds their BLASes+ /// and the TLAS over their new bounds. Software traversal: each mesh's tree keeps its shape and takes its boxes+ /// again on the CPU, from the model's corners placed by the pose (`units::refit`), and the commit makes the top+ /// tree over the units' new bounds. One that is hidden keeps the pose its mesh has: it is skinned when it is+ /// shown (`show_meshes`). False when one is another model now, its new pose mirrors other primitives than the one+ /// its mesh was made for, or (software traversal) its mesh is no unit: the caller rebuilds. (The CPU keeps no+ /// triangles of such a pose: `LiveCore::mesh_tris` skins it when asked.)pub(super) fn repose_skinned(&mut self, world: &World, ids: &[u32], build: &mut gpu::TopBuild) -> Result<bool> {use rayon::prelude::*;- if self.sc.up.accel.is_none() || self.sc.up.skin.is_none() {+ if self.sc.up.skin.is_none() || (self.sc.up.accel.is_none() && self.sc.up.units.is_none()) {return Ok(false);}let r = &self.sc.tables.retained;@@ -1919,6 +1922,27 @@ impl LiveCore {let Some(rows) = rows? else { return Ok(false) };work.push((*g, rows));}+ // Software traversal: the meshes' trees, their boxes taken again from the corners placed by the poses (the+ // models in parallel). Nothing is written unless every one is a unit with such a tree.+ let mut trees: Vec<(u32, u32, Vec<crate::bvh::GpuNode>, (glam::Vec3A, glam::Vec3A))> = vec![];+ if let Some(units) = &self.sc.up.units {+ let (sources, shapes) = (&self.sc.skin_sources, &self.sc.skin_trees);+ let made: Vec<Option<(u32, u32, Vec<crate::bvh::GpuNode>, (glam::Vec3A, glam::Vec3A))>> = work+ .par_iter()+ .map(|(g, rows)| {+ let sk = &r.skinned[*g as usize];+ let (corners, shape) = (sources.get(sk.source as usize)?, shapes.get(sk.source as usize)?);+ let tree = units.trees.get(sk.mesh as usize).copied().flatten().filter(|t| t.len as usize == shape.len() && !shape.is_empty() && corners.len() == sk.order.len())?;+ let mut nodes = shape.clone();+ let bounds = crate::units::refit(&mut nodes, |i| corners[i].map(|c| glam::Vec3A::from(crate::skin::skin_corner(&c, rows).0)));+ Some((sk.mesh, tree.root, nodes, bounds))+ })+ .collect();+ for m in made {+ let Some(m) = m else { return Ok(false) };+ trees.push(m);+ }+ }self.cpu_skin_ms += t_skin.elapsed().as_secs_f32() * 1000.0;let t_up = Instant::now();// Motion vectors: the GPU still holds the previous pose of these meshes (and of the last re-skin's, if others):@@ -1941,6 +1965,13 @@ impl LiveCore {sk.animation = a;build.meshes.push(sk.mesh);}+ // (Software traversal: the trees where they lie, and the units' bounds for the top tree.)+ for (mesh, root, nodes, (lo, hi)) in trees {+ self.g.write_nodes(&self.sc.up.bg_parts.nodes, root, &nodes);+ if let Some(units) = self.sc.up.units.as_mut() {+ units.set_bounds(mesh, lo, hi);+ }+ }self.cpu_upload_ms += t_up.elapsed().as_secs_f32() * 1000.0;Ok(true)}@@ -2273,8 +2304,8 @@ impl LiveCore {}emitters_changed |= release(*id, tables, instances);// (A still model the build placed, whose instance is all of it, is taken out by it too; and a posed skinned- // model, an instance of its own mesh, where there is such an instance: software traversal has its triangles- // in the still tree. Its mesh's triangles and its skinning job stay until the next rebuild, as the build's do.)+ // model, an instance of its own mesh, where its mesh is one the rays reach by its instance. Its mesh's+ // triangles and its skinning job stay until the next rebuild, as the build's do.)let its_own = |i: &scene::InstanceRef| whole(i, instances, meshes) || (matches!(i.mesh, scene::MeshKind::Skinned(_)) && instances.get(i.instance as usize).is_some_and(|x| top.has_mesh(x.mesh)));let at = slot_of(*id, edit, tables).or_else(|| tables.retained.instance(*id).filter(|i| its_own(i)).map(|i| (inst_base + i.instance as usize, i.instance as usize)));// Drawn, and not at a record an edit can clear (a model with triangles flattened beside its instance,@@ -2440,7 +2471,9 @@ impl LiveCore {// triangles are skinned here once, for the pose it comes in; the pass skins it from its next// pose on, from the corners the scene's others of the model are skinned from.if top.sw() {- return Err("a skinned model (software traversal has its triangles in the still tree)".into());+ // (Its triangles and its tree need room, which software traversal's scenes have only+ // with geometry of their own to edit, and which does not grow there.)+ return Err("a skinned model added (software traversal)".into());}if at.is_some() {return Err("a skinned model placed again".into());diff --git a/renderer/src/live/inplace_tests.rs b/renderer/src/live/inplace_tests.rsindex a8bc4f9..b03f1e1 100644--- a/renderer/src/live/inplace_tests.rs+++ b/renderer/src/live/inplace_tests.rs@@ -2548,9 +2548,6 @@ fn posed_skinned_models_are_meshes_of_their_own() {Err(e) => return eprintln!("skipped: {e}"),};w.take_changes();- if !live.g.hwrt {- return eprintln!("skipped: no hardware ray tracing");- }let of = |c: &LiveCore, id: u32| c.sc.tables.retained.skinned.iter().find(|s| s.object == id).expect("a posed skinned model").clone();let (a, b) = (of(&live, 30), of(&live, 31));assert_eq!((live.sc.tables.retained.skinned.len(), a.mesh != b.mesh, a.source == b.source, live.sc.meshes[a.mesh as usize].sect_count), (2, true, true, 2), "two meshes of two sections, skinned from the model's one copy of its corners");@@ -2578,8 +2575,8 @@ fn posed_skinned_models_are_meshes_of_their_own() {}/// A posed skinned model is hidden and shown in place: hidden, no ray sees its instance and a new pose skins-/// nothing; shown, it is skinned as it is posed then, where it stands. The picture is a build's each time. (Under-/// software traversal, by a rebuild.)+/// nothing; shown, it is skinned as it is posed then, where it stands. The picture is a build's each time, under+/// either traversal.#[test]fn a_posed_skinned_model_is_hidden_and_shown_in_place() {let presets = crate::default_presets_dir();@@ -2604,18 +2601,6 @@ fn a_posed_skinned_model_is_hidden_and_shown_in_place() {a};let loaded = against_a_build("as loaded", &mut live, &w);- // Software traversal has a skinned model's triangles in its still tree, and no instance to mask: a rebuild's.- if !live.g.hwrt {- w.set_visible(31, false).unwrap();- assert!(live.commit(&mut w).unwrap(), "hidden under software traversal: a rebuild");- let hidden = against_a_build("hidden under software traversal", &mut live, &w);- assert!(apart(&loaded, &hidden) > 0.5, "it is out of the picture ({:.2} steps)", apart(&loaded, &hidden));- w.set_visible(31, true).unwrap();- assert!(live.commit(&mut w).unwrap(), "shown under software traversal: a rebuild");- let shown = against_a_build("shown under software traversal", &mut live, &w);- assert!(apart(&hidden, &shown) > 0.5, "it is in the picture again ({:.2} steps)", apart(&hidden, &shown));- return;- }let record = of(&live, 31).instance;w.set_visible(31, false).unwrap();assert!(!live.commit(&mut w).unwrap(), "hidden: in place ({:?})", live.fallback);@@ -3002,8 +2987,7 @@ fn a_posed_skinned_model_added_beside_particles_is_a_rebuilds() {}/// A posed skinned model is removed in place: its instance is no object's and no ray sees it, the others are posed-/// on as ever, and the picture is a build's. One that is hidden is removed in place too. (Under software traversal,-/// where it has no instance, by a rebuild.)+/// on as ever, and the picture is a build's. One that is hidden is removed in place too. Under either traversal.#[test]fn a_posed_skinned_model_is_removed_in_place() {let presets = crate::default_presets_dir();@@ -3027,13 +3011,6 @@ fn a_posed_skinned_model_is_removed_in_place() {a};let loaded = against_a_build("as loaded", &mut live, &w);- if !live.g.hwrt {- w.remove(31);- assert!(live.commit(&mut w).unwrap(), "removed under software traversal: a rebuild");- let removed = against_a_build("removed under software traversal", &mut live, &w);- assert!(apart(&loaded, &removed) > 0.5, "it is out of the picture ({:.2} steps)", apart(&loaded, &removed));- return;- }let record = |c: &LiveCore, id: u32| c.sc.tables.retained.instance(id).expect("a posed skinned model").instance;let (r30, r31, r32) = (record(&live, 30), record(&live, 31), record(&live, 32));w.remove(31);@@ -3071,7 +3048,9 @@ fn a_posed_skinned_model_is_removed_in_place() {/// triangles, shading attributes and BLAS input of each mesh are its model's triangles for the pose, the previous/// frame's vertices are the pose before, and the CPU has the mesh as it is now when asked. One posed alone leaves/// the others' as they were. Also after the scene's buffers were made again larger, and a pose that mirrors other-/// primitives than the mesh was made for is a rebuild's.+/// primitives than the mesh was made for is a rebuild's. Under software traversal there is no BLAS input: each mesh's+/// tree keeps its model's shape and has the pose's boxes, and the top tree its unit by them. The motion vectors of a+/// pose are no longer than its corners went, under either.#[test]fn the_skinning_pass_writes_what_the_cpu_skin_writes() {let presets = crate::default_presets_dir();@@ -3092,9 +3071,7 @@ fn the_skinning_pass_writes_what_the_cpu_skin_writes() {Err(e) => return eprintln!("skipped: {e}"),};w.take_changes();- if !live.g.hwrt {- return eprintln!("skipped: no hardware ray tracing");- }+ let hw = live.g.hwrt;assert!(live.sc.edit.pool_first > 0, "a mesh's first triangle on the GPU is not its first in the pool");{let g = live.sc.tables.retained.skinned.iter().find(|g| g.uri.ends_with("skin-blend.glb")).expect("the model's mesh");@@ -3142,7 +3119,39 @@ fn the_skinning_pass_writes_what_the_cpu_skin_writes() {// (Its previous vertices: where its instance's record says, on the CPU and on the GPU.)let prev = c.sc.instances[*instance as usize].prev;assert_eq!(c.sc.gpu_instances[*instance as usize].n[2][3].to_bits(), prev, "the record has where the mesh's previous vertices are");- (read(c, &up.verts, k * 48, n * 48), read(c, &up.attrs, k * 96, n * 96), read(c, &up.accel.as_ref().unwrap().verts, k * 36, n * 36), read(c, &up.prev_verts, prev as u64 * 48, n * 48))+ (read(c, &up.verts, k * 48, n * 48), read(c, &up.attrs, k * 96, n * 96), up.accel.as_ref().map(|a| read(c, &a.verts, k * 36, n * 36)), read(c, &up.prev_verts, prev as u64 * 48, n * 48))+ };+ // Software traversal: a mesh's tree on the GPU has its model's tree's shape (the links, word for word) and the+ // pose's boxes: a leaf's its triangles' (`corners`: the CPU skin's, nine numbers a triangle), an interior node's+ // its two children's. The mesh's bounds are the root's, and the top tree's leaf of its unit is those, placed.+ let tree = |what: &str, c: &LiveCore, g: usize, corners: &[f32]| {+ let Some(units) = &c.sc.up.units else { return };+ let sk = &c.sc.tables.retained.skinned[g];+ let (t, shape) = (units.trees[sk.mesh as usize].expect("a unit's tree"), &c.sc.skin_trees[sk.source as usize]);+ assert!(t.len as usize == shape.len() && t.len > 0, "{what}: its tree is as long as its model's ({} nodes, {})", t.len, shape.len());+ let words = read(c, &c.sc.up.bg_parts.nodes, t.root as u64 * 32, t.len as u64 * 32);+ let nodes: &[crate::bvh::GpuNode] = bytemuck::cast_slice(&words);+ let (mut leaves, mut seen) = (0, vec![false; corners.len() / 9]);+ for (k, (n, m)) in nodes.iter().zip(shape).enumerate() {+ assert_eq!((n.left_or_first, n.count), (m.left_or_first, m.count), "{what}: node {k} is linked as its model's");+ let (lo, hi) = (Vec3::from(n.bmin), Vec3::from(n.bmax));+ if n.count > 0 {+ let own = &corners[n.left_or_first as usize * 9..(n.left_or_first + n.count) as usize * 9];+ let (a, b) = own.chunks_exact(3).fold((Vec3::splat(f32::INFINITY), Vec3::splat(f32::NEG_INFINITY)), |(a, b), p| (a.min(Vec3::from_slice(p)), b.max(Vec3::from_slice(p))));+ assert!((lo - a).abs().max_element() <= 2e-5 * a.abs().max_element().max(1.0) && (hi - b).abs().max_element() <= 2e-5 * b.abs().max_element().max(1.0), "{what}: leaf {k}'s box is {lo} .. {hi}; its triangles', {a} .. {b}");+ (n.left_or_first..n.left_or_first + n.count).for_each(|i| seen[i as usize] = true);+ leaves += 1;+ } else {+ let (x, y) = (nodes[n.left_or_first as usize], nodes[n.left_or_first as usize + 1]);+ assert_eq!((lo, hi), (Vec3::from(x.bmin).min(y.bmin.into()), Vec3::from(x.bmax).max(y.bmax.into())), "{what}: node {k}'s box is its children's");+ }+ }+ // (A leaf holds 16 triangles at most: the larger model's tree has interior nodes.)+ assert!((leaves > 1 || seen.len() <= 16) && seen.iter().all(|&s| s), "{what}: every triangle is in a leaf ({leaves} leaves)");+ assert_eq!((Vec3::from(t.lo), Vec3::from(t.hi)), (nodes[0].bmin.into(), nodes[0].bmax.into()), "{what}: the mesh's bounds are its root's");+ let unit = crate::units::UnitBox::new(sk.instance, c.sc.instances[sk.instance as usize].world, t.lo, t.hi);+ let leaf = units.kept.nodes.iter().find(|n| n.count > 0 && n.left_or_first == sk.instance).expect("the unit's leaf of the top tree");+ assert_eq!((leaf.bmin, leaf.bmax), (unit.lo.into(), unit.hi.into()), "{what}: the top tree has the unit by its mesh's bounds, placed");};let check_one = |what: &str, c: &LiveCore, g: usize, now: &crate::desc::Animation, before: &crate::desc::Animation| {let what = format!("{what}, {} (object {})", deforming(c)[g].0, deforming(c)[g].3);@@ -3150,7 +3159,10 @@ fn the_skinning_pass_writes_what_the_cpu_skin_writes() {same(&format!("{what}: the vertices"), &v, &cv, &[3], 12);// (An attribute's words: the section in `uv.w` (15), the rank in `x.w` (23), the object and a spare beside.)same(&format!("{what}: the attributes"), &at, &cat, &[15, 21, 22, 23], 24);- same(&format!("{what}: the BLAS input"), &b, &cb, &[], 9);+ if let Some(b) = b {+ same(&format!("{what}: the BLAS input"), &b, &cb, &[], 9);+ }+ tree(&what, c, g, &cb);same(&format!("{what}: the previous frame's vertices"), &prev, &cpu(c, g, before).0, &[3], 12);// The CPU's own: the build's pose in the pool, the pose as it is now when asked.let (instance, id) = (deforming(c)[g].2, deforming(c)[g].3);@@ -3221,11 +3233,35 @@ fn the_skinning_pass_writes_what_the_cpu_skin_writes() {w.set_geometry(7, Geometry { normals: [0.0, 0.0, 1.0].repeat(corners), uvs: vec![0.0; corners * 2], positions, indices, groups: vec![], colors: vec![] }).unwrap();w.set_object(40, "sheet".into(), None, Kind::Mesh { geometry: 7, material: 1, own_blas: false, materials: vec![] }, true, false).unwrap();w.set_transform(40, Vec3::new(0.0, -80.0, 0.0), glam::Quat::IDENTITY, Vec3::splat(0.001)).unwrap();- assert!(!live.commit(&mut w).unwrap(), "a large mesh added: in place ({:?})", live.fallback);+ // (Software traversal's room does not grow: a rebuild's there.)+ assert_eq!(live.commit(&mut w).unwrap(), !hw, "a large mesh added: in place under hardware traversal ({:?})", live.fallback);assert!(live.sc.up.verts.size() > size, "the triangles' buffer was made again, larger ({} -> {})", size, live.sc.up.verts.size());+ if !hw {+ picture(&mut live, 2);+ }+ // (Under software traversal this is a build's: the meshes, and their trees, as the build makes them for a pose.)+ check("after a large mesh was added", &live, &bend(0.9), &bend(0.9));all(&mut w, &bend(0.25));assert!(!live.commit(&mut w).unwrap(), "posed after the buffers grew: in place ({:?})", live.fallback);check("posed after the buffers grew", &live, &bend(0.25), &bend(0.9));+ // The motion of that pose: no vector is longer than the furthest a corner of a model went (a kernel that takes+ // another triangle's previous vertices gives vectors as long as the mesh), and some are nearly as long.+ {+ (live.fixed_dt, live.frame, live.reset) = (Some(1.0 / 60.0), 16, true);+ live.render(1, |_, _| {});+ let m = &live.targets.kernel.motion;+ let moved = read(&live, &m.buffer, m.offset, m.size);+ let corners = |v: &[f32]| -> Vec<Vec3> {+ v.chunks_exact(12).flat_map(|t| { let (p, e1, e2) = (Vec3::new(t[0], t[1], t[2]), Vec3::new(t[4], t[5], t[6]), Vec3::new(t[8], t[9], t[10])); [p, p + e1, p + e2] }).collect()+ };+ let far = (0..4).map(|g| {+ let world = live.sc.instances[deforming(&live)[g].2 as usize].world;+ corners(&cpu(&live, g, &bend(0.25)).0).iter().zip(corners(&cpu(&live, g, &bend(0.9)).0)).map(|(x, y)| world.transform_vector3(*x - y).length()).fold(0.0f32, f32::max)+ }).fold(0.0f32, f32::max);+ let vectors: Vec<f32> = moved.chunks_exact(4).filter(|p| p[3] > 0.5).map(|p| Vec3::new(p[0], p[1], p[2]).length()).collect();+ let longest = vectors.iter().copied().fold(0.0f32, f32::max);+ assert!(vectors.iter().filter(|&&l| l > 1e-3).count() > 20 && longest <= far * 1.001 + 1e-4 && longest > far * 0.2, "{} pixels moved; the longest motion vector is {longest}; the furthest a corner went, {far}", vectors.iter().filter(|&&l| l > 1e-3).count());+ }pictured("posed after the buffers grew", &mut live, &w);// A pose that mirrors other primitives than the mesh was made for (the clip `flip` turns the badge's node the// other way round): its triangles are in another order, a build's to make.@@ -3256,9 +3292,6 @@ fn models_posed_node_by_node_are_posed_in_place() {Err(e) => return eprintln!("skipped: {e}"),};w.take_changes();- if !live.g.hwrt {- return eprintln!("skipped: no hardware ray tracing");- }assert_eq!(live.sc.tables.retained.skinned.len(), 2, "each a mesh of its own, though in the same pose");let before = picture(&mut live, 64);for (id, a) in [(30, 0.9), (31, -1.2)] {diff --git a/renderer/src/live/load.rs b/renderer/src/live/load.rsindex f1085eb..dbd343c 100644--- a/renderer/src/live/load.rs+++ b/renderer/src/live/load.rs@@ -14,6 +14,11 @@ pub(super) struct LoadedScene {pub(super) tri_instances: Vec<(u32, u32, u32)>,/// (A mesh the GPU skins has its build's pose here: `LiveCore::mesh_tris` has it as it is now.)pub(super) pool_tris: Vec<[glam::Vec3A; 3]>,+ /// Software traversal with units: what the skinning pass skins each model from (`scene::Scene::skin_sources`) and+ /// the shape of its meshes' trees (`skin_trees`), kept on the CPU: a new pose takes a tree's boxes again from+ /// them (live/edits.rs `repose_skinned`). Empty under hardware traversal, whose BLASes the GPU builds.+ pub(super) skin_sources: Vec<Vec<[crate::skin::SkinCorner; 3]>>,+ pub(super) skin_trees: Vec<Vec<crate::bvh::GpuNode>>,/// Software traversal: each pool triangle's section and its index in it (a unit's tree orders its mesh's/// triangles; live/edits.rs `mesh_places`). Empty under hardware traversal, where they lie by section.pub(super) pool_place: Vec<[u32; 2]>,@@ -262,6 +267,7 @@ pub(super) fn load_scene(g: &gpu::Gpu, world: &World, assets: &Path, presets: &Plet instance_features = gpu::instance_features(&sc);let pool_place: Vec<[u32; 2]> = if up.units.is_some() { sc.pool_attrs.iter().map(|a| [a.uv[3].to_bits(), a.x[3].to_bits()]).collect() } else { vec![] };let (pool_tris, meshes, instances) = (std::mem::take(&mut sc.pool_tris), std::mem::take(&mut sc.meshes), std::mem::take(&mut sc.instances));+ let (skin_sources, skin_trees) = if up.units.is_some() { (std::mem::take(&mut sc.skin_sources), std::mem::take(&mut sc.skin_trees)) } else { (vec![], vec![]) };let (sections, rows) = (std::mem::take(&mut sc.sections), std::mem::take(&mut sc.rows));let mut gpu_instances = std::mem::take(&mut sc.gpu_instances);let proc_first = gpu_instances.len();@@ -313,5 +319,5 @@ pub(super) fn load_scene(g: &gpu::Gpu, world: &World, assets: &Path, presets: &P},free_skinned: vec![],};- 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))+ Ok((LoadedScene { timings, triangles, tris, tri_nodes, tri_instances, pool_tris, skin_sources, skin_trees, 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))}diff --git a/renderer/src/scene.rs b/renderer/src/scene.rsindex f8cb78e..d087753 100644--- a/renderer/src/scene.rs+++ b/renderer/src/scene.rs@@ -678,6 +678,11 @@ pub struct Scene {/// What the skinning pass reads of the models whose meshes deform (`Skinned::source`): a model's triangles as/// its file has them, in its mesh's order. One a model, whatever its placements and poses.pub skin_sources: Vec<Vec<[skin::SkinCorner; 3]>>,+ /// Beside each: the model's palette as its file has it (its bind pose), and, under software traversal with units+ /// (lib.rs `unit_trees`), the tree over that pose's triangles: the shape of the tree of each mesh skinned from the+ /// source, whose boxes are taken again at each pose (`units::refit`). The source's triangles are in its order.+ pub skin_rest: Vec<Vec<skin::PaletteRows>>,+ pub skin_trees: Vec<Vec<crate::bvh::GpuNode>>,/// The meshes' sections (`InstMesh::sect_first`) and the instances' rows (`Instance::rows`): as the kernel reads/// the rows once `prepare` has set where each section's triangles and emitters are.pub sections: Vec<Section>,@@ -1544,6 +1549,7 @@ pub fn build(w: &World, assets: &Path, presets_dir: &Path, overrides: Option<&selet mut rigid_parts: HashMap<(String, String), std::rc::Rc<Vec<RigidPart>>> = HashMap::new();// What the skinning pass reads, by (model, nodes, the primitives mirrored): shared by the model's placements.let mut skin_sources: Vec<Vec<[skin::SkinCorner; 3]>> = vec![];+ let mut skin_rest: Vec<Vec<skin::PaletteRows>> = vec![];let mut skin_source_of: HashMap<(String, Option<Vec<String>>, Vec<bool>), (u32, Arc<Vec<(u32, u32, u32)>>)> = HashMap::new();// Flattened instances' triangle ranges.let mut tri_instances: Vec<(u32, u32, u32)> = vec![];@@ -1880,6 +1886,7 @@ pub fn build(w: &World, assets: &Path, presets_dir: &Path, overrides: Option<&seNone => {let corners = glb.skin_source(nodes.as_deref(), Some(a))?;skin_sources.push(order.iter().map(|&(k, _, _)| corners[k]).collect());+ skin_rest.push(glb.palette(None)?);let s = (skin_sources.len() as u32 - 1, Arc::new(order.iter().map(|&(k, section, rank)| (k as u32, section, rank)).collect::<Vec<_>>()));skin_source_of.insert(skey, s.clone());s@@ -2280,6 +2287,8 @@ pub fn build(w: &World, assets: &Path, presets_dir: &Path, overrides: Option<&semeshes,instances,skin_sources,+ skin_rest,+ skin_trees: vec![],sections,rows,gpu_instances: vec![],diff --git a/renderer/src/units.rs b/renderer/src/units.rsindex f53363d..7d8ab24 100644--- a/renderer/src/units.rs+++ b/renderer/src/units.rs@@ -249,6 +249,34 @@ pub fn mesh_tree(tris: &[[Vec3A; 3]]) -> Result<MeshTree, String> {Ok(MeshTree { nodes, order, depth, lo, hi })}+/// A tree's boxes taken again from its triangles as they are now (`tri`: a triangle of the mesh, in the tree's+/// order), its shape kept: a mesh that deforms, at a new pose. A leaf's box is its triangles' (a corner that is not+/// finite is in none), an interior node's its children's, which lie after it. The mesh's bounds.+pub fn refit(nodes: &mut [GpuNode], tri: impl Fn(usize) -> [Vec3A; 3]) -> (Vec3A, Vec3A) {+ for k in (0..nodes.len()).rev() {+ let n = nodes[k];+ let (mut lo, mut hi) = (Vec3A::splat(f32::INFINITY), Vec3A::splat(f32::NEG_INFINITY));+ if n.count > 0 {+ for i in n.left_or_first..n.left_or_first + n.count {+ for v in tri(i as usize) {+ if v.is_finite() {+ (lo, hi) = (lo.min(v), hi.max(v));+ }+ }+ }+ // (A leaf of nothing finite: a point, which no ray meets through a triangle that is not there.)+ if lo.x > hi.x {+ (lo, hi) = (Vec3A::ZERO, Vec3A::ZERO);+ }+ } else {+ let (a, b) = (nodes[n.left_or_first as usize], nodes[n.left_or_first as usize + 1]);+ (lo, hi) = (Vec3A::from(a.bmin).min(b.bmin.into()), Vec3A::from(a.bmax).max(b.bmax.into()));+ }+ (nodes[k].bmin, nodes[k].bmax) = (lo.into(), hi.into());+ }+ (nodes[0].bmin.into(), nodes[0].bmax.into())+}+/// A unit for the top tree: its record, and its mesh's model-space bounds under its world transform.#[derive(Clone, Copy, Debug)]pub struct UnitBox {@@ -598,8 +626,8 @@ pub struct Tree {#[derive(Clone, Debug)]pub struct Units {pub layout: Layout,- /// By instanced mesh (the scene's `meshes` index): its tree. None: no tree (a mesh that deforms or has no- /// triangle, or one an edit freed).+ /// By instanced mesh (the scene's `meshes` index): its tree. None: no tree (a mesh without a triangle, or one an+ /// edit freed).pub trees: Vec<Option<Tree>>,/// By kernel record: the unit there, if the record is one's.pub slots: Vec<Option<Unit>>,@@ -693,6 +721,13 @@ impl Units {Some(root)}+ /// Mesh `k`'s bounds, its tree's boxes taken again where they lie (`refit`): what the top tree places its units by.+ pub fn set_bounds(&mut self, k: u32, lo: Vec3A, hi: Vec3A) {+ if let Some(t) = self.trees.get_mut(k as usize).and_then(|t| t.as_mut()) {+ (t.lo, t.hi) = (lo, hi);+ }+ }+/// Mesh `k`'s tree given back: its root and length (the caller fills them with `FILL`), if it had one.pub fn drop_tree(&mut self, k: u32) -> Option<(u32, u32)> {let t = self.trees.get_mut(k as usize)?.take()?;@@ -1813,6 +1848,62 @@ mod tests {assert!(top[0].bmin[1] <= 12.0 && top[0].bmin[1] > 11.0 && top[0].bmax[1] >= 13.0, "{:?}", top[0]);}+ /// A mesh that deforms keeps its tree's shape and takes its boxes again (`refit`): the links are as built, a leaf's+ /// box is its triangles' as they are now, an interior node's its two children's, and the tree passes the mesh+ /// trees' check as before. The bounds returned are the triangles', and the unit's box in the top tree is theirs+ /// once they are set. A corner that is not finite is in no box; a leaf with none that is finite is a point.+ #[test]+ fn a_deforming_meshs_tree_takes_its_boxes_again() {+ let mut rng = Rng(11);+ let rest = rng.tris(300, 4.0, 0.3);+ let t = mesh_tree(&rest).unwrap();+ assert!(t.nodes.iter().filter(|n| n.count > 1).count() > 8, "leaves of several triangles");+ // The mesh's triangles in the tree's order, bent: each corner moved by where it is.+ let bend = |v: Vec3A| Vec3A::new(v.x + 0.5 * (v.y * 1.3).sin(), v.y * 1.2 + 0.3 * v.z, v.z - 0.4 * v.x * v.x + 7.0);+ let posed: Vec<[Vec3A; 3]> = t.order.iter().map(|&i| rest[i as usize].map(bend)).collect();+ let exact = |tris: &[[Vec3A; 3]]| tris.iter().flatten().filter(|v| v.is_finite()).fold((Vec3A::splat(f32::INFINITY), Vec3A::splat(f32::NEG_INFINITY)), |(a, b), v| (a.min(*v), b.max(*v)));+ let holds = |what: &str, nodes: &[GpuNode], tris: &[[Vec3A; 3]]| {+ for (k, (n, was)) in nodes.iter().zip(&t.nodes).enumerate() {+ assert_eq!((n.left_or_first, n.count), (was.left_or_first, was.count), "{what}: node {k}'s links are as built");+ let want = if n.count > 0 {+ let (a, b) = exact(&tris[n.left_or_first as usize..(n.left_or_first + n.count) as usize]);+ if a.x > b.x { (Vec3A::ZERO, Vec3A::ZERO) } else { (a, b) }+ } else {+ let (a, b) = (nodes[n.left_or_first as usize], nodes[n.left_or_first as usize + 1]);+ (Vec3A::from(a.bmin).min(b.bmin.into()), Vec3A::from(a.bmax).max(b.bmax.into()))+ };+ assert_eq!((Vec3A::from(n.bmin), Vec3A::from(n.bmax)), want, "{what}: node {k}'s box ({} triangles)", n.count);+ }+ assert_eq!(check_mesh(nodes, tris.len()), Ok(t.depth), "{what}: the tree is a mesh's still");+ };+ let mut nodes = t.nodes.clone();+ let (lo, hi) = refit(&mut nodes, |i| posed[i]);+ holds("bent", &nodes, &posed);+ assert_eq!((lo, hi), exact(&posed), "the bounds are the triangles'");+ assert!(lo.z > t.hi.z, "the bent mesh is out of its first bounds");+ // Its unit in the top tree: by the bounds it has, once they are set.+ let mut u = Units { layout: Layout::new(1, t.nodes.len() + 8, 2), trees: vec![], slots: vec![], count: 0, records: 2, kept: Default::default() };+ u.add_tree(0, &t).unwrap();+ u.set(0, u.unit(0, Mat4::IDENTITY, false));+ let top = u.top().unwrap();+ assert!(top[0].bmax[2] < lo.z, "{:?}", top[0]);+ u.set_bounds(0, lo, hi);+ let top = u.top().unwrap();+ assert!((0..3).all(|k| top[0].bmin[k] <= lo[k] && top[0].bmax[k] >= hi[k] && top[0].bmax[k] - top[0].bmin[k] < (hi[k] - lo[k]) * 1.01 + 1e-3), "{:?} holds {lo} .. {hi}", top[0]);+ // Corners that are not finite: one of a triangle, and every corner of a leaf's triangles.+ let leaf = *t.nodes.iter().filter(|n| n.count > 1).nth(3).unwrap();+ let other = (0..posed.len()).find(|i| !(leaf.left_or_first as usize..(leaf.left_or_first + leaf.count) as usize).contains(i)).unwrap();+ let mut broken = posed.clone();+ broken[other][1] = Vec3A::new(f32::NAN, 0.0, 1.0);+ for i in leaf.left_or_first..leaf.left_or_first + leaf.count {+ broken[i as usize] = [Vec3A::splat(f32::INFINITY), Vec3A::splat(f32::NAN), Vec3A::new(0.0, f32::NEG_INFINITY, 0.0)];+ }+ let mut nodes = t.nodes.clone();+ let (lo, hi) = refit(&mut nodes, |i| broken[i]);+ holds("with corners that are not finite", &nodes, &broken);+ assert!(lo.is_finite() && hi.is_finite() && nodes.iter().all(|n| n.bmin.iter().chain(&n.bmax).all(|v| v.is_finite())), "every box is finite");+ }+/// Units that only move keep their top tree, its boxes theirs again (each leaf's its unit's box, each node's what/// holds its children's), while it costs a ray no more than `REFIT_SLACK` of what it did when built; a unit that/// goes far, comes, goes, is hidden or shown has it built again. A kept tree is one a build could have made: the@@ -1980,3 +2071,59 @@ mod tests {assert_eq!(rec(Mat4::IDENTITY).n[2][3].to_bits(), 0, "the third word is not the unit's");}}++#[cfg(test)]+mod bench {+ use super::*;++ /// What a posed skinned model's tree costs a new pose (THREERT_SKIN_GLB: the model): made again, or its boxes+ /// taken again in the tree of an earlier pose (the bind pose's, the clip's first), and how much worse the walk of+ /// such a tree is (its surface-area cost against the new tree's).+ #[test]+ #[ignore]+ fn deforming_tree_cost() {+ let path = std::env::var("THREERT_SKIN_GLB").unwrap();+ let m = crate::skin::Model::new(gltf::import(&path).unwrap()).unwrap();+ let area = |n: &GpuNode| {+ let d = Vec3A::from(n.bmax) - Vec3A::from(n.bmin);+ 2.0 * (d.x * d.y + d.y * d.z + d.z * d.x) as f64+ };+ // (A walk's expected cost: each node's chance of being entered, an interior node a step, a leaf a test a triangle.)+ let cost = |nodes: &[GpuNode]| nodes.iter().map(|n| area(n) * if n.count == 0 { 1.0 } else { n.count as f64 }).sum::<f64>() / area(&nodes[0]);+ // The boxes again, the tree's shape and its triangles' order kept.+ let refit = |nodes: &mut [GpuNode], tris: &[[Vec3A; 3]], order: &[u32]| {+ super::refit(nodes, |i| tris[order[i] as usize]);+ };+ let pose = |clip: &str, time: f32| crate::desc::Animation { clip: clip.into(), time, r#loop: true, nodes: Default::default() };+ let tris_of = |a: Option<&crate::desc::Animation>| -> Vec<[Vec3A; 3]> { m.triangles(glam::Mat4::IDENTITY, None, a).unwrap().iter().map(|t| t.p.map(Vec3A::from)).collect() };+ let bind = mesh_tree(&tris_of(None)).unwrap();+ for clip in std::env::var("THREERT_SKIN_CLIPS").unwrap_or_else(|_| m.clip_names()[0].clone()).split(',') {+ let first = mesh_tree(&tris_of(Some(&pose(clip, 0.0)))).unwrap();+ let (mut skin, mut build, mut fit) = (0.0, 0.0, 0.0);+ let (mut worst, mut mean) = ([0.0f64; 2], [0.0f64; 2]);+ let poses = 40;+ for k in 0..poses {+ let a = pose(clip, 0.05 + k as f32 * 0.07);+ let t0 = web_time::Instant::now();+ let tris = tris_of(Some(&a));+ skin += t0.elapsed().as_secs_f64() * 1e3;+ let t0 = web_time::Instant::now();+ let fresh = mesh_tree(&tris).unwrap();+ build += t0.elapsed().as_secs_f64() * 1e3;+ for (j, kept) in [&bind, &first].into_iter().enumerate() {+ let mut nodes = kept.nodes.clone();+ let t0 = web_time::Instant::now();+ refit(&mut nodes, &tris, &kept.order);+ fit += t0.elapsed().as_secs_f64() * 1e3 / 2.0;+ let r = cost(&nodes) / cost(&fresh.nodes);+ (worst[j], mean[j]) = (worst[j].max(r), mean[j] + r / poses as f64);+ }+ }+ let n = poses as f64;+ eprintln!(+ "{clip}: {} triangles, {} nodes; a pose: skin {:.2} ms, tree made again {:.2} ms, boxes taken again {:.3} ms; a kept tree's walk against a new one's: the bind pose's x{:.2} (worst x{:.2}), the clip's first x{:.2} (worst x{:.2})",+ first.order.len(), first.nodes.len(), skin / n, build / n, fit / n, mean[0], worst[0], mean[1], worst[1]+ );+ }+ }+}