From acf4b0bfeef2254397d111ea9c14872a82b683ee Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:10:34 +1100 Subject: [PATCH 1/2] Builder: simplify 3-4 times faster, to the same meshes Nearly all of a mesh's build is its simplification, and nearly all of that was working out again, for each of the ~80 collapse costs a collapse re-queues, each vertex's neighbours, UV seams, wedges and normal, each time allocating and sorting. They are now kept per vertex and worked out again only when its triangles change. Each edge is queued once rather than from both ends, and a group simplified towards several targets, as a tree's branches are, is simplified once, taking each result on the way down: the collapses come in the same order whatever the target. The meshes built are the same, byte for byte: oak, birch, bush and willow, a meadow patch and a terrain grid, and whole scenes with woods and a meadow. On one thread, a 256-cell terrain builds in 1.6 s rather than 6.0, an oak's wood in 0.47 s rather than 1.78. Co-Authored-By: Claude Opus 5.5 --- crates/solstice-builder/src/dag.rs | 42 +++-- crates/solstice-builder/src/simplify.rs | 205 ++++++++++++++++++++---- 2 files changed, 200 insertions(+), 47 deletions(-) diff --git a/crates/solstice-builder/src/dag.rs b/crates/solstice-builder/src/dag.rs index 59e0c77..53dea85 100644 --- a/crates/solstice-builder/src/dag.rs +++ b/crates/solstice-builder/src/dag.rs @@ -5,7 +5,9 @@ use crate::group::{group_clusters, GroupInput, MAX_GROUP_SIZE, MAX_SKINNED_GROUP use crate::hierarchy::{build_hierarchy, Leaf}; use crate::mesh::Mesh; use crate::partition::{partition_triangles, ClusterLimits}; -use crate::simplify::{simplify_skinned, Attribute, Corner, SkinCost, Triangle}; +use crate::simplify::{ + simplify_skinned, simplify_skinned_each, Attribute, Corner, SkinCost, Triangle, +}; use crate::thin::{find_pieces, set_aside_wide_pieces, thin, NEW_POSITION}; use glam::{Vec2, Vec3}; use rayon::prelude::*; @@ -274,25 +276,35 @@ fn build_level( |v| shared[v as usize], ) }; - let mut simplified = simplify_to(target(rest.len())); // A group's error is the largest of its parts'. Where thinning carries the // reduction, the rest, such as a branch among twigs, goes only as far as it can - // without being the coarser part, so it never holds the twigs' detail back. - if !found.pieces.is_empty() { + // without being the coarser part, so it never holds the twigs' detail back: the + // first of these targets, deepest first, that stays within the twigs' error. + // One run gives all three, as simplifying to each alone would. + let simplified = if found.pieces.is_empty() { + simplify_to(target(rest.len())) + } else { let budget = thinning_error.max(welded_view.step); - for ratio in [0.75, 0.9] { - if simplified.error <= budget { - break; - } - simplified = simplify_to(((rest.len() as f32 * ratio) as usize).max(1)); - } - if simplified.error > budget { - simplified = crate::simplify::Simplified { + let at = |ratio: f32| ((rest.len() as f32 * ratio) as usize).max(1); + let mut tries = simplify_skinned_each( + &rest, + &welded_view.positions, + &welded_view.attributes, + skin, + &[at(0.9), at(0.75), target(rest.len())], + |v| shared[v as usize], + ); + let deepest = tries.pop().expect("three targets"); + let middle = tries.pop().expect("three targets"); + let shallow = tries.pop().expect("three targets"); + [deepest, middle, shallow] + .into_iter() + .find(|s| s.error <= budget) + .unwrap_or_else(|| crate::simplify::Simplified { triangles: rest.clone(), error: 0.0, - }; - } - } + }) + }; reduced.extend(simplified.triangles); simplified_error = simplified.error; } diff --git a/crates/solstice-builder/src/simplify.rs b/crates/solstice-builder/src/simplify.rs index 225e26c..4529bbd 100644 --- a/crates/solstice-builder/src/simplify.rs +++ b/crates/solstice-builder/src/simplify.rs @@ -17,6 +17,7 @@ use glam::{DVec2, DVec3, Vec2, Vec3}; use rustc_hash::FxHashMap; use solstice_asset::PackedSkin; +use std::cell::RefCell; use std::cmp::Ordering; use std::collections::BinaryHeap; @@ -102,6 +103,19 @@ pub fn simplify_skinned( Simplifier::new(triangles, positions, attributes, skin, &is_locked).run(target_triangles) } +/// [`simplify_skinned`] towards each of several targets, largest first, in one run: the result +/// at each is what simplifying to it alone gives. +pub fn simplify_skinned_each( + triangles: &[Triangle], + positions: &[Vec3], + attributes: &[Attribute], + skin: Option>, + targets: &[usize], + is_locked: impl Fn(u32) -> bool, +) -> Vec { + Simplifier::new(triangles, positions, attributes, skin, &is_locked).run_to_each(targets) +} + /// A symmetric 4x4 error quadric plus the accumulated area weight. #[derive(Clone, Copy, Default)] struct Quadric { @@ -201,6 +215,25 @@ impl Ord for Candidate { } } +/// What is known about the triangles around a vertex, worked out once and kept until they change. +/// A collapse's cost asks for these of the same few vertices tens of times, and working them out +/// afresh each time was nearly all of a build's time. +#[derive(Clone, Default)] +struct Around { + /// Whether this holds anything yet. + ready: bool, + /// The vertex's version this was worked out at. + version: u32, + /// Neighbouring vertices, sorted. + neighbors: Vec, + /// The attributes the vertex's corners use, sorted. + wedges: Vec, + /// The neighbours across a UV seam, sorted. + seams: Vec, + /// The shading normal of the vertex's first corner. + normal: Vec3, +} + struct Simplifier<'a> { attributes: &'a [Attribute], skin: Option>, @@ -222,6 +255,10 @@ struct Simplifier<'a> { alive_count: usize, heap: BinaryHeap, max_error: f64, + /// Raised whenever the triangles around a vertex change, or the corners of one of them. + versions: Vec, + /// What is known around each vertex, kept while its version holds. + around: RefCell>, } impl<'a> Simplifier<'a> { @@ -310,13 +347,52 @@ impl<'a> Simplifier<'a> { alive_count, heap: BinaryHeap::new(), max_error: 0.0, + versions: vec![0; vertex_count], + around: RefCell::new(vec![Around::default(); vertex_count]), } } - fn run(mut self, target: usize) -> Simplified { + /// Makes sure what is known around `v` is up to date. + fn fresh(&self, v: u32) { + let version = self.versions[v as usize]; + { + let around = self.around.borrow(); + let entry = &around[v as usize]; + if entry.ready && entry.version == version { + return; + } + } + let neighbors = self.find_neighbors(v); + let seams = self.find_seam_neighbors(v, &neighbors); + let entry = Around { + ready: true, + version, + wedges: self.find_wedges(v), + normal: self.find_normal(v), + neighbors, + seams, + }; + self.around.borrow_mut()[v as usize] = entry; + } + + /// Simplifies towards each of `targets`, largest first, and gives the result at each. The + /// collapses come in the same order whatever the target, so each result is what a run to it + /// alone would give. + fn run_to_each(mut self, targets: &[usize]) -> Vec { + debug_assert!(targets.windows(2).all(|w| w[0] >= w[1])); for v in 0..self.positions.len() as u32 { - self.push_edges_around(v); + self.push_edges_around(v, &|n| n < v); + } + let mut results = Vec::with_capacity(targets.len()); + for &target in targets { + self.collapse_to(target); + results.push(self.snapshot()); } + results + } + + /// Collapses until no more than `target` triangles are left, or nothing more can be. + fn collapse_to(&mut self, target: usize) { while self.alive_count > target { let Some(candidate) = self.heap.pop() else { break; @@ -330,10 +406,18 @@ impl<'a> Simplifier<'a> { self.collapse(candidate.from, candidate.to); } } - self.finish() + } + + fn run(self, target: usize) -> Simplified { + self.run_to_each(&[target]).pop().expect("one target") } fn neighbors(&self, v: u32) -> Vec { + self.fresh(v); + self.around.borrow()[v as usize].neighbors.clone() + } + + fn find_neighbors(&self, v: u32) -> Vec { let mut result: Vec = self.vertex_triangles[v as usize] .iter() .flat_map(|&t| self.triangles[t as usize]) @@ -345,8 +429,14 @@ impl<'a> Simplifier<'a> { result } - fn push_edges_around(&mut self, v: u32) { + /// Queues both collapses of each edge from `v`, but those to neighbours `skip` passes over, + /// which are queued from the other end: an edge queued twice would be popped twice, to the + /// same effect, so once is the same simplification for half the work. + fn push_edges_around(&mut self, v: u32, skip: &dyn Fn(u32) -> bool) { for neighbor in self.neighbors(v) { + if skip(neighbor) { + continue; + } for (from, to) in [(v, neighbor), (neighbor, v)] { if !self.locked[from as usize] { let candidate = Candidate { @@ -390,23 +480,26 @@ impl<'a> Simplifier<'a> { /// How far the texture would slide across the surface if `from` collapsed into `to`, in world /// units, including what earlier collapses already moved at `from`. fn texture_error(&self, from: u32, to: u32) -> f64 { - let wedges = self.wedges_at(to); + self.fresh(from); + self.fresh(to); + let around = self.around.borrow(); + let wedges = &around[to as usize].wedges; let worst = self.vertex_triangles[from as usize] .iter() .map(|&t| self.triangles[t as usize]) .filter(|t| !t.iter().any(|corner| corner.position == to)) - .map(|t| self.moved_corner(&t, from, to, &wedges).1) + .map(|t| self.moved_corner(&t, from, to, wedges).1) .fold(0.0, f64::max); - worst.max(self.seam_movement(from, to)) + self.texture_errors[from as usize] + let seams = &around[from as usize].seams; + worst.max(self.seam_movement(seams, from, to)) + self.texture_errors[from as usize] } /// How far the collapse moves a UV seam. Each side's mapping can stay perfectly continuous /// while one side grows over the other, replacing what the surface showed there. - fn seam_movement(&self, from: u32, to: u32) -> f64 { - let seams = self.seam_neighbors(from); + fn seam_movement(&self, seams: &[u32], from: u32, to: u32) -> f64 { let start = self.positions[from as usize]; let end = self.positions[to as usize]; - match seams[..] { + match *seams { [] => 0.0, // Sliding along a seam only moves it as far as the seam bends here. [a, b] if a == to || b == to => { @@ -419,14 +512,14 @@ impl<'a> Simplifier<'a> { } /// The neighbours of `v` connected by an edge whose two sides use different textures. - fn seam_neighbors(&self, v: u32) -> Vec { + fn find_seam_neighbors(&self, v: u32, neighbors: &[u32]) -> Vec { let uv_of = |triangle: &Triangle, vertex: u32| { triangle .iter() .find(|corner| corner.position == vertex) .map(|corner| self.attributes[corner.attribute as usize].uv) }; - let mut seams = self.neighbors(v); + let mut seams = neighbors.to_vec(); seams.retain(|&n| { let mut sides = self.vertex_triangles[v as usize] .iter() @@ -486,27 +579,38 @@ impl<'a> Simplifier<'a> { /// How much the shading normals disagree between the two ends, in `0..=1`. fn normal_difference(&self, from: u32, to: u32) -> f64 { - let normal_at = |v: u32| { - self.vertex_triangles[v as usize] - .iter() - .flat_map(|&t| self.triangles[t as usize]) - .find(|corner| corner.position == v) - .map_or(Vec3::Y, |corner| { - self.attributes[corner.attribute as usize].normal - }) - }; - f64::from((1.0 - normal_at(from).dot(normal_at(to))) * 0.5) + self.fresh(from); + self.fresh(to); + let around = self.around.borrow(); + let (a, b) = (around[from as usize].normal, around[to as usize].normal); + f64::from((1.0 - a.dot(b)) * 0.5) + } + + /// The shading normal of the first of `v`'s corners. + fn find_normal(&self, v: u32) -> Vec3 { + self.vertex_triangles[v as usize] + .iter() + .flat_map(|&t| self.triangles[t as usize]) + .find(|corner| corner.position == v) + .map_or(Vec3::Y, |corner| { + self.attributes[corner.attribute as usize].normal + }) } fn can_collapse(&self, from: u32, to: u32) -> bool { // Link condition: the only vertices adjacent to both ends must be the two // opposite the shared edge, otherwise the collapse pinches the surface. - let from_neighbors = self.neighbors(from); - let to_neighbors = self.neighbors(to); - let shared = from_neighbors - .iter() - .filter(|n| to_neighbors.binary_search(n).is_ok()) - .count(); + self.fresh(from); + self.fresh(to); + let shared = { + let around = self.around.borrow(); + let to_neighbors = &around[to as usize].neighbors; + around[from as usize] + .neighbors + .iter() + .filter(|n| to_neighbors.binary_search(n).is_ok()) + .count() + }; if shared != 2 { return false; } @@ -558,12 +662,14 @@ impl<'a> Simplifier<'a> { let target_wedges = self.wedges_at(to); let from_triangles = std::mem::take(&mut self.vertex_triangles[from as usize]); + let mut changed = vec![from, to]; for triangle in from_triangles { let index = triangle as usize; if !self.alive[index] { continue; } let corners = self.triangles[index]; + changed.extend(corners.iter().map(|c| c.position)); if corners.iter().any(|c| c.position == to) { self.alive[index] = false; self.alive_count -= 1; @@ -584,6 +690,10 @@ impl<'a> Simplifier<'a> { } } + for v in changed { + self.versions[v as usize] += 1; + } + // Invalidate every queued collapse touching the changed neighbourhood. self.stamps[from as usize] += 1; self.stamps[to as usize] += 1; @@ -591,13 +701,21 @@ impl<'a> Simplifier<'a> { for &n in &neighbors { self.stamps[n as usize] += 1; } - self.push_edges_around(to); - for n in neighbors { - self.push_edges_around(n); + // Every edge from `to`, then each neighbour's other edges: those round the ring of + // neighbours from its lower end, and all the rest. + self.push_edges_around(to, &|_| false); + for &n in &neighbors { + let ring = &neighbors; + self.push_edges_around(n, &|m| m == to || (m < n && ring.binary_search(&m).is_ok())); } } fn wedges_at(&self, v: u32) -> Vec { + self.fresh(v); + self.around.borrow()[v as usize].wedges.clone() + } + + fn find_wedges(&self, v: u32) -> Vec { let mut wedges: Vec = self.vertex_triangles[v as usize] .iter() .flat_map(|&t| self.triangles[t as usize]) @@ -609,7 +727,7 @@ impl<'a> Simplifier<'a> { wedges } - fn finish(self) -> Simplified { + fn snapshot(&self) -> Simplified { let global = &self.global; let triangles = self .triangles @@ -867,6 +985,29 @@ mod tests { assert!(result.error > 0.1, "error {}", result.error); } + #[test] + fn one_run_to_several_targets_gives_what_a_run_to_each_does() { + for (triangles, positions, attributes) in + [closed_sphere(24, 16), flat_with_seam(12, 5, 0.3)] + { + let count = triangles.len(); + let targets = [count * 9 / 10, count * 3 / 4, count / 2, count / 5]; + let together = + simplify_skinned_each(&triangles, &positions, &attributes, None, &targets, |_| { + false + }); + for (target, result) in targets.iter().zip(&together) { + let alone = simplify(&triangles, &positions, &attributes, *target, |_| false); + assert_eq!(result.triangles, alone.triangles, "target {target}"); + assert_eq!( + result.error.to_bits(), + alone.error.to_bits(), + "target {target}" + ); + } + } + } + #[test] fn reaches_target_on_a_closed_surface() { let (triangles, positions, attributes) = closed_sphere(32, 16); From f15d1d6ba0caf0524ed34f41a2feca7cf46578c3 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:42:29 +1100 Subject: [PATCH 2/2] Builder: targets in any order, and the tests check every kept neighbourhood From the review: several targets simplified in one run may now come in any order, each result handed back in its place, so a target ratio above 0.75 can't quietly reorder the branch tries; and in tests, after every collapse, each vertex's kept neighbourhood is checked against working it out afresh. Co-Authored-By: Claude Opus 5.5 --- crates/solstice-builder/src/simplify.rs | 48 ++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/solstice-builder/src/simplify.rs b/crates/solstice-builder/src/simplify.rs index 4529bbd..a99df10 100644 --- a/crates/solstice-builder/src/simplify.rs +++ b/crates/solstice-builder/src/simplify.rs @@ -103,8 +103,8 @@ pub fn simplify_skinned( Simplifier::new(triangles, positions, attributes, skin, &is_locked).run(target_triangles) } -/// [`simplify_skinned`] towards each of several targets, largest first, in one run: the result -/// at each is what simplifying to it alone gives. +/// [`simplify_skinned`] towards each of several targets, in one run: the result at each, in the +/// order given, is what simplifying to it alone gives. pub fn simplify_skinned_each( triangles: &[Triangle], positions: &[Vec3], @@ -113,7 +113,20 @@ pub fn simplify_skinned_each( targets: &[usize], is_locked: impl Fn(u32) -> bool, ) -> Vec { - Simplifier::new(triangles, positions, attributes, skin, &is_locked).run_to_each(targets) + // One run reaches the largest target first, so they are taken largest first and handed back + // in the order asked for. + let mut order: Vec = (0..targets.len()).collect(); + order.sort_by_key(|&i| std::cmp::Reverse(targets[i])); + let descending: Vec = order.iter().map(|&i| targets[i]).collect(); + let results = Simplifier::new(triangles, positions, attributes, skin, &is_locked) + .run_to_each(&descending); + let mut out: Vec> = (0..targets.len()).map(|_| None).collect(); + for (result, i) in results.into_iter().zip(order) { + out[i] = Some(result); + } + out.into_iter() + .map(|r| r.expect("one result for each target")) + .collect() } /// A symmetric 4x4 error quadric plus the accumulated area weight. @@ -352,6 +365,28 @@ impl<'a> Simplifier<'a> { } } + /// In tests, after each collapse: everything kept as up to date is what working it out + /// afresh gives, so a change that should have raised a version and didn't is caught. + #[cfg(test)] + fn check_kept(&self) { + let around = self.around.borrow(); + for (v, kept) in around.iter().enumerate() { + if !kept.ready || kept.version != self.versions[v] { + continue; + } + let v = v as u32; + let neighbors = self.find_neighbors(v); + assert_eq!(kept.neighbors, neighbors, "neighbours of {v}"); + assert_eq!(kept.wedges, self.find_wedges(v), "wedges of {v}"); + assert_eq!( + kept.seams, + self.find_seam_neighbors(v, &neighbors), + "seams of {v}" + ); + assert_eq!(kept.normal, self.find_normal(v), "normal of {v}"); + } + } + /// Makes sure what is known around `v` is up to date. fn fresh(&self, v: u32) { let version = self.versions[v as usize]; @@ -379,7 +414,7 @@ impl<'a> Simplifier<'a> { /// collapses come in the same order whatever the target, so each result is what a run to it /// alone would give. fn run_to_each(mut self, targets: &[usize]) -> Vec { - debug_assert!(targets.windows(2).all(|w| w[0] >= w[1])); + assert!(targets.windows(2).all(|w| w[0] >= w[1]), "targets descend"); for v in 0..self.positions.len() as u32 { self.push_edges_around(v, &|n| n < v); } @@ -693,6 +728,8 @@ impl<'a> Simplifier<'a> { for v in changed { self.versions[v as usize] += 1; } + #[cfg(test)] + self.check_kept(); // Invalidate every queued collapse touching the changed neighbourhood. self.stamps[from as usize] += 1; @@ -991,7 +1028,8 @@ mod tests { [closed_sphere(24, 16), flat_with_seam(12, 5, 0.3)] { let count = triangles.len(); - let targets = [count * 9 / 10, count * 3 / 4, count / 2, count / 5]; + // In any order: each result is the one for the target in its place. + let targets = [count / 2, count * 9 / 10, count / 5, count * 3 / 4]; let together = simplify_skinned_each(&triangles, &positions, &attributes, None, &targets, |_| { false