diff --git a/assets/shaders/viewport.wgsl b/assets/shaders/viewport.wgsl index 2cc978d85..6884ad856 100644 --- a/assets/shaders/viewport.wgsl +++ b/assets/shaders/viewport.wgsl @@ -147,6 +147,48 @@ fn fs_solid_cut_flat(i: FOut, @builtin(front_facing) front: bool) -> CutOut { return o; } +/// Weighted blended order-independent transparency (`view/oit.rs`): a see-through fragment's +/// colour, premultiplied and weighted by its alpha and nearness, into the accumulation target, +/// and its alpha into the revealage target (which keeps what is behind it by 1 - alpha). +struct Oit { + @location(0) accum: vec4, + @location(1) reveal: vec4, +}; + +fn oit(c: vec4, z: f32) -> Oit { + let a = clamp(c.a, 0.0, 1.0); + // Depth 0 near and 1 far, whichever way the buffer runs. + let d = select(z, 1.0 - z, reversed()); + let w = clamp(a * max(1e-2, 3e3 * pow(1.0 - d, 3.0)), 1e-2, 3e3); + var o: Oit; + o.accum = vec4(c.rgb * a, a) * w; + o.reveal = vec4(a); + return o; +} + +@fragment +fn fs_oit(i: TOut) -> Oit { + if (clipped(i.wp)) { discard; } + return oit(shade(i), i.pos.z); +} + +@fragment +fn fs_oit_flat(i: FOut, @builtin(front_facing) front: bool) -> Oit { + let t = flat_fragment(i, front); + let o = oit(shade(t), t.pos.z); + if (clipped(t.wp)) { discard; } + return o; +} + +/// A see-through flat-shaded placement (faded or glass), shaded like `fs_tri`. +@fragment +fn fs_tri_flat(i: FOut, @builtin(front_facing) front: bool) -> @location(0) vec4 { + let t = flat_fragment(i, front); + let c = shade(t); + if (clipped(t.wp)) { discard; } + return c; +} + @fragment fn fs_ao_depth_flat(i: FOut, @builtin(front_facing) front: bool) -> AoDepth { let t = flat_fragment(i, front); diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs index 7864c1ba3..19a8f77a0 100644 --- a/crates/geom/src/lib.rs +++ b/crates/geom/src/lib.rs @@ -27,6 +27,7 @@ pub mod network; mod plane; mod profile; pub mod section; +pub mod simplify; pub mod spline_space; pub mod tess; pub mod tspline; diff --git a/crates/geom/src/simplify.rs b/crates/geom/src/simplify.rs new file mode 100644 index 000000000..7a4c0f3fb --- /dev/null +++ b/crates/geom/src/simplify.rs @@ -0,0 +1,551 @@ +//! Triangle mesh simplification, for drawing a large mesh coarser where it is small on screen +//! (display levels of detail). Display only: nothing measured, exported or picked reads a +//! simplified mesh. +//! +//! Edge collapse ordered by quadric error (Garland and Heckbert, "Surface Simplification Using +//! Quadric Error Metrics", SIGGRAPH 1997): each vertex carries the sum of the squared-distance +//! quadrics of the planes of its faces; the edge whose two ends merge at the point of least +//! summed squared distance goes first. A collapse that would fold a face over (its normal turning +//! by more than about 80°) or join two sheets of the surface (the ends sharing more neighbours +//! than the faces between them) is skipped. Open boundaries carry extra planes across them, so +//! they keep their place. +//! +//! [`levels`] simplifies once, keeping a copy of the mesh each time it reaches the next target +//! triangle count, with its error measured: how far its vertices and the centres of its triangles +//! lie from the original surface, at most. + +use std::cmp::Ordering; +use std::collections::BinaryHeap; + +use crate::Vec3; + +/// Most triangles [`levels`] simplifies (more: no levels). +pub const MAX_TRIANGLES: usize = 50_000_000; + +/// How much more an open boundary's planes count than a face's. +const BOUNDARY_WEIGHT: f64 = 10.0; +/// A collapse may turn a face's normal by at most this (cosine): about 80°. +const MIN_TURN_COS: f64 = 0.2; + +/// A simplified copy of a mesh. +#[derive(Clone, Debug, Default)] +pub struct Level { + pub positions: Vec, + pub triangles: Vec<[u32; 3]>, + /// How far (mm) its vertices and triangle centres lie from the original surface, at most. + pub error: f64, +} + +/// The mesh (`positions`, and `triangles` indexing them; corners at the same position are one +/// vertex) simplified to each of `targets` triangles in turn (largest first), one [`Level`] per +/// target reached. Simplification stops early when no edge can collapse any more; the levels made +/// by then are returned, but none that is not at least a fifth smaller than the one before it. +/// Triangles with an index out of range or a point not finite are left out; an empty or oversized +/// mesh ([`MAX_TRIANGLES`]) has no levels. +pub fn levels(positions: &[Vec3], triangles: &[[u32; 3]], targets: &[usize]) -> Vec { + if triangles.is_empty() || triangles.len() > MAX_TRIANGLES { + return Vec::new(); + } + let Some(mut m) = Collapser::new(positions, triangles) else { return Vec::new() }; + let original = Surface::new(&m.pos, &m.tris); + let mut out: Vec = Vec::new(); + let mut previous = m.live_triangles; + for &target in targets { + if target >= previous { + continue; + } + m.collapse_to(target); + if m.live_triangles * 5 > previous * 4 { + break; + } + previous = m.live_triangles; + let (mut level, seeds) = m.level(); + // Never less than a coarser level's own error. + level.error = original.deviation(&level, &seeds).max(out.last().map_or(0.0, |l| l.error)); + out.push(level); + } + out +} + +/// The original surface, for measuring how far a level strays from it. +struct Surface { + tris: Vec<[Vec3; 3]>, + bvh: crate::Bvh, +} + +impl Surface { + fn new(pos: &[[f64; 3]], tris: &[[u32; 3]]) -> Surface { + let tris: Vec<[Vec3; 3]> = tris.iter().map(|t| t.map(|i| pos.get(i as usize).map_or(Vec3::ZERO, |p| Vec3::new(p[0], p[1], p[2])))).collect(); + let boxes: Vec = tris + .iter() + .map(|t| { + let mut b = crate::Aabb3::EMPTY; + t.iter().for_each(|p| b.add(*p)); + b + }) + .collect(); + Surface { bvh: crate::Bvh::new(&boxes), tris } + } + + fn to_triangle(&self, p: Vec3, t: u32) -> f64 { + self.tris.get(t as usize).map_or(f64::INFINITY, |[a, b, c]| crate::compare::closest_on_triangle(p, *a, *b, *c).dist(p)) + } + + /// How far `p` lies from the surface, when that is more than `beyond`; otherwise + /// `beyond` or less (the search stops at the first triangle within `beyond`). `near`: an + /// original triangle `p` is likely near (where it came from), so the search starts close. + fn distance_beyond(&self, p: Vec3, beyond: f64, near: u32) -> f64 { + let best = std::cell::Cell::new(self.to_triangle(p, near)); + let gap = |b: &crate::Aabb3| { + let d = |v: f64, lo: f64, hi: f64| (lo - v).max(v - hi).max(0.0); + Vec3::new(d(p.x, b.min.x, b.max.x), d(p.y, b.min.y, b.max.y), d(p.z, b.min.z, b.max.z)).len() + }; + self.bvh.visit(|b| best.get() > beyond && gap(b) < best.get(), |t| best.set(best.get().min(self.to_triangle(p, t)))); + best.get() + } + + /// How far `level`'s vertices and triangle centres lie from the surface, at most (`seeds`: + /// an original triangle each vertex came from). + fn deviation(&self, level: &Level, seeds: &[u32]) -> f64 { + let at = |i: u32| level.positions.get(i as usize).copied().unwrap_or(Vec3::ZERO); + let seed = |i: u32| seeds.get(i as usize).copied().unwrap_or(0); + let vertices = level.positions.iter().copied().zip(seeds.iter().copied()); + let centres = level.triangles.iter().map(|t| { + let [a, b, c] = t.map(at); + ((a + b + c) * (1.0 / 3.0), seed(t[0])) + }); + let worst = vertices.chain(centres).fold(0.0, |worst: f64, (p, near)| worst.max(self.distance_beyond(p, worst, near))); + if worst.is_finite() { worst } else { 0.0 } + } +} + +/// A symmetric 4 × 4 quadric: a a, a b, a c, a d, b b, b c, b d, c c, c d, d d. +#[derive(Clone, Copy, Default)] +struct Quadric([f64; 10]); + +impl Quadric { + /// The squared distance to the plane n · p + d = 0 (`n` unit), times `w`. + fn plane(n: [f64; 3], d: f64, w: f64) -> Quadric { + let [a, b, c] = n; + Quadric([a * a * w, a * b * w, a * c * w, a * d * w, b * b * w, b * c * w, b * d * w, c * c * w, c * d * w, d * d * w]) + } + + fn add(&mut self, o: &Quadric) { + for (x, y) in self.0.iter_mut().zip(o.0) { + *x += y; + } + } + + fn sum(&self, o: &Quadric) -> Quadric { + let mut q = *self; + q.add(o); + q + } + + /// The summed squared distance at `p`. + fn at(&self, p: [f64; 3]) -> f64 { + let [aa, ab, ac, ad, bb, bc, bd, cc, cd, dd] = self.0; + let [x, y, z] = p; + (aa * x * x + + 2.0 * ab * x * y + + 2.0 * ac * x * z + + 2.0 * ad * x + + bb * y * y + + 2.0 * bc * y * z + + 2.0 * bd * y + + cc * z * z + + 2.0 * cd * z + + dd) + .max(0.0) + } + + /// The point of least summed squared distance, when it is well defined. + fn best(&self) -> Option<[f64; 3]> { + let [aa, ab, ac, ad, bb, bc, bd, cc, cd, _] = self.0; + let det = aa * (bb * cc - bc * bc) - ab * (ab * cc - bc * ac) + ac * (ab * bc - bb * ac); + let scale = (aa.abs() + bb.abs() + cc.abs()).powi(3); + if !(det.abs() > 1e-12 * scale) { + return None; + } + let (r0, r1, r2) = (-ad, -bd, -cd); + let x = (r0 * (bb * cc - bc * bc) - ab * (r1 * cc - bc * r2) + ac * (r1 * bc - bb * r2)) / det; + let y = (aa * (r1 * cc - bc * r2) - r0 * (ab * cc - bc * ac) + ac * (ab * r2 - r1 * ac)) / det; + let z = (aa * (bb * r2 - r1 * bc) - ab * (ab * r2 - r1 * ac) + r0 * (ab * bc - bb * ac)) / det; + [x, y, z].iter().all(|v| v.is_finite()).then_some([x, y, z]) + } +} + +fn sub(a: [f64; 3], b: [f64; 3]) -> [f64; 3] { + [a[0] - b[0], a[1] - b[1], a[2] - b[2]] +} + +fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] { + [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] +} + +fn dot(a: [f64; 3], b: [f64; 3]) -> f64 { + a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +} + +fn unit(a: [f64; 3]) -> Option<[f64; 3]> { + let l = dot(a, a).sqrt(); + (l > 0.0 && l.is_finite()).then(|| [a[0] / l, a[1] / l, a[2] / l]) +} + +/// A candidate collapse: its cost, its ends, and their versions when it was costed (a later +/// change to either end makes it stale). +#[derive(Clone, Copy)] +struct Candidate { + cost: f64, + u: u32, + v: u32, + stamps: (u32, u32), +} + +impl PartialEq for Candidate { + fn eq(&self, o: &Self) -> bool { + self.cmp(o) == Ordering::Equal + } +} +impl Eq for Candidate {} +impl PartialOrd for Candidate { + fn partial_cmp(&self, o: &Self) -> Option { + Some(self.cmp(o)) + } +} +impl Ord for Candidate { + /// The cheapest first (the heap pops the greatest), ties by the ends, so the result never + /// depends on the heap's order. + fn cmp(&self, o: &Self) -> Ordering { + o.cost.total_cmp(&self.cost).then_with(|| (o.u, o.v).cmp(&(self.u, self.v))) + } +} + +/// A quick hasher for welding by position (the keys are coordinates' bits). +#[derive(Default)] +struct BitsHasher(u64); + +impl std::hash::Hasher for BitsHasher { + fn finish(&self) -> u64 { + self.0 + } + fn write(&mut self, bytes: &[u8]) { + for b in bytes { + self.write_u64(u64::from(*b)); + } + } + fn write_u64(&mut self, x: u64) { + self.0 = (self.0.rotate_left(5) ^ x).wrapping_mul(0x51_7c_c1_b7_27_22_0a_95); + } +} + +/// The lists a collapse works with, kept between collapses. +#[derive(Default)] +struct Scratch { + at_u: Vec, + at_v: Vec, + shared: Vec, + nu: Vec, + nv: Vec, +} + +impl Scratch { + fn clear(&mut self) { + for l in [&mut self.at_u, &mut self.at_v, &mut self.shared, &mut self.nu, &mut self.nv] { + l.clear(); + } + } +} + +struct Collapser { + pos: Vec<[f64; 3]>, + quadric: Vec, + alive: Vec, + stamp: Vec, + tris: Vec<[u32; 3]>, + tri_alive: Vec, + /// The triangles at each vertex (dead ones are dropped lazily). + around: Vec>, + /// An original triangle at each vertex (where it came from), to start a search near it. + seed: Vec, + /// Lists a collapse reuses. + scratch: Scratch, + heap: BinaryHeap, + live_triangles: usize, +} + +impl Collapser { + fn new(positions: &[Vec3], triangles: &[[u32; 3]]) -> Option { + // Weld corners at the same position. + let mut index: std::collections::HashMap<[u64; 3], u32, std::hash::BuildHasherDefault> = + std::collections::HashMap::with_capacity_and_hasher(positions.len() / 2, Default::default()); + let mut remap: Vec> = Vec::with_capacity(positions.len()); + let mut pos: Vec<[f64; 3]> = Vec::new(); + for p in positions { + if !p.is_finite() { + remap.push(None); + continue; + } + let next = u32::try_from(pos.len()).ok()?; + let key = [(p.x + 0.0).to_bits(), (p.y + 0.0).to_bits(), (p.z + 0.0).to_bits()]; + remap.push(Some(*index.entry(key).or_insert_with(|| { + pos.push([p.x, p.y, p.z]); + next + }))); + } + drop(index); + let mut tris: Vec<[u32; 3]> = Vec::with_capacity(triangles.len()); + for t in triangles { + let r = |i: u32| remap.get(i as usize).copied().flatten(); + let (Some(a), Some(b), Some(c)) = (r(t[0]), r(t[1]), r(t[2])) else { continue }; + if a != b && b != c && c != a { + tris.push([a, b, c]); + } + } + if tris.is_empty() { + return None; + } + let n = pos.len(); + let mut quadric = vec![Quadric::default(); n]; + let mut around: Vec> = vec![Vec::new(); n]; + let mut sides: Vec<(u32, u32, u32)> = Vec::with_capacity(tris.len() * 3); + for (ti, t) in tris.iter().enumerate() { + let ti = u32::try_from(ti).ok()?; + let [a, b, c] = t.map(|i| pos.get(i as usize).copied().unwrap_or_default()); + if let Some(nrm) = unit(cross(sub(b, a), sub(c, a))) { + let q = Quadric::plane(nrm, -dot(nrm, a), 1.0); + for &i in t { + if let Some(x) = quadric.get_mut(i as usize) { + x.add(&q); + } + } + } + for (k, &i) in t.iter().enumerate() { + if let Some(list) = around.get_mut(i as usize) { + list.push(ti); + } + let j = t[(k + 1) % 3]; + sides.push((i.min(j), i.max(j), ti)); + } + } + // Open boundaries (sides of one triangle only): a plane across each, weighted, so the + // boundary keeps its place. + sides.sort_unstable(); + let mut k = 0; + while k < sides.len() { + let (a, b, t) = sides[k]; + let mut e = k + 1; + while e < sides.len() && sides[e].0 == a && sides[e].1 == b { + e += 1; + } + if e - k == 1 + && let (Some(pa), Some(pb), Some(tri)) = (pos.get(a as usize), pos.get(b as usize), tris.get(t as usize)) + { + let [p0, p1, p2] = tri.map(|i| pos.get(i as usize).copied().unwrap_or_default()); + let face = cross(sub(p1, p0), sub(p2, p0)); + if let Some(across) = unit(cross(sub(*pb, *pa), face)) { + let q = Quadric::plane(across, -dot(across, *pa), BOUNDARY_WEIGHT); + for i in [a, b] { + if let Some(x) = quadric.get_mut(i as usize) { + x.add(&q); + } + } + } + } + k = e; + } + let live_triangles = tris.len(); + let seed: Vec = around.iter().map(|list| list.first().copied().unwrap_or(0)).collect(); + let mut m = Collapser { + alive: vec![true; n], + stamp: vec![0; n], + tri_alive: vec![true; tris.len()], + heap: BinaryHeap::with_capacity(sides.len() / 2 + 1), + pos, + quadric, + tris, + around, + seed, + scratch: Scratch::default(), + live_triangles, + }; + let mut last = None; + for &(a, b, _) in &sides { + if last != Some((a, b)) { + m.offer(a, b); + last = Some((a, b)); + } + } + Some(m) + } + + /// Where `u` and `v` would merge, and its cost. + fn target(&self, u: u32, v: u32) -> Option<([f64; 3], f64)> { + let (qu, qv) = (self.quadric.get(u as usize)?, self.quadric.get(v as usize)?); + let q = qu.sum(qv); + let (pu, pv) = (*self.pos.get(u as usize)?, *self.pos.get(v as usize)?); + let mid = [(pu[0] + pv[0]) * 0.5, (pu[1] + pv[1]) * 0.5, (pu[2] + pv[2]) * 0.5]; + // The best point when it lies near the edge; else the best of its ends and middle. + let reach = dot(sub(pu, pv), sub(pu, pv)) * 4.0; + let best = q.best().filter(|p| dot(sub(*p, mid), sub(*p, mid)) <= reach); + best.into_iter().chain([pu, pv, mid]).map(|p| (p, q.at(p))).min_by(|a, b| a.1.total_cmp(&b.1)) + } + + fn offer(&mut self, u: u32, v: u32) { + let Some((_, cost)) = self.target(u, v) else { return }; + let stamps = (self.stamp.get(u as usize).copied().unwrap_or(0), self.stamp.get(v as usize).copied().unwrap_or(0)); + self.heap.push(Candidate { cost, u, v, stamps }); + } + + /// The live vertices sharing a live triangle with `v`. + fn neighbours(&self, v: u32, out: &mut Vec) { + out.clear(); + out.extend( + self.around + .get(v as usize) + .into_iter() + .flatten() + .filter(|t| self.tri_alive.get(**t as usize).copied().unwrap_or(false)) + .filter_map(|t| self.tris.get(*t as usize)) + .flat_map(|t| t.iter().copied()) + .filter(|i| *i != v), + ); + out.sort_unstable(); + out.dedup(); + } + + fn collapse_to(&mut self, target: usize) { + while self.live_triangles > target { + let Some(c) = self.heap.pop() else { return }; + let current = |i: u32| self.alive.get(i as usize).copied().unwrap_or(false); + let stamp = |i: u32| self.stamp.get(i as usize).copied().unwrap_or(u32::MAX); + if !current(c.u) || !current(c.v) || (stamp(c.u), stamp(c.v)) != c.stamps { + continue; + } + self.collapse(c.u, c.v); + } + } + + /// Merge `v` into `u` at their best point, unless that would fold a face over or join two + /// sheets of the surface. + fn collapse(&mut self, u: u32, v: u32) { + let Some((p, _)) = self.target(u, v) else { return }; + let live = |t: &u32| self.tri_alive.get(*t as usize).copied().unwrap_or(false); + let mut s = std::mem::take(&mut self.scratch); + s.clear(); + s.at_v.extend(self.around.get(v as usize).into_iter().flatten().copied().filter(live)); + s.at_u.extend(self.around.get(u as usize).into_iter().flatten().copied().filter(live)); + s.shared.extend(s.at_v.iter().copied().filter(|t| self.tris.get(*t as usize).is_some_and(|tri| tri.contains(&u)))); + let done = self.collapse_with(u, v, p, &mut s); + self.scratch = s; + if done { + self.reoffer(u); + } + } + + /// [`Collapser::collapse`] with its lists in `s`: whether it collapsed. + fn collapse_with(&mut self, u: u32, v: u32, p: [f64; 3], s: &mut Scratch) -> bool { + if s.shared.is_empty() { + return false; + } + // Two sheets meeting only at these ends would be joined. + self.neighbours(u, &mut s.nu); + self.neighbours(v, &mut s.nv); + let common = s.nu.iter().filter(|x| s.nv.binary_search(x).is_ok()).count(); + if common > s.shared.len() { + return false; + } + // No face folds over. + for &t in s.at_u.iter().chain(&s.at_v).filter(|t| !s.shared.contains(t)) { + let Some(tri) = self.tris.get(t as usize) else { continue }; + let corners = tri.map(|i| self.pos.get(i as usize).copied().unwrap_or_default()); + let moved = tri.map(|i| if i == u || i == v { p } else { self.pos.get(i as usize).copied().unwrap_or_default() }); + let before = cross(sub(corners[1], corners[0]), sub(corners[2], corners[0])); + let after = cross(sub(moved[1], moved[0]), sub(moved[2], moved[0])); + let (lb, la) = (dot(before, before).sqrt(), dot(after, after).sqrt()); + if !(la > 0.0) || (lb > 0.0 && dot(before, after) < MIN_TURN_COS * lb * la) { + return false; + } + } + for t in &s.shared { + if let Some(a) = self.tri_alive.get_mut(*t as usize) { + *a = false; + } + self.live_triangles = self.live_triangles.saturating_sub(1); + } + let mut list = self.around.get_mut(u as usize).map(std::mem::take).unwrap_or_default(); + let tri_alive = &self.tri_alive; + list.retain(|t| tri_alive.get(*t as usize).copied().unwrap_or(false)); + for &t in s.at_v.iter().filter(|t| !s.shared.contains(t)) { + if let Some(tri) = self.tris.get_mut(t as usize) { + for i in tri.iter_mut() { + if *i == v { + *i = u; + } + } + list.push(t); + } + } + if let Some(slot) = self.around.get_mut(u as usize) { + *slot = list; + } + if let Some(slot) = self.around.get_mut(v as usize) { + *slot = Vec::new(); + } + let qv = self.quadric.get(v as usize).copied().unwrap_or_default(); + if let Some(q) = self.quadric.get_mut(u as usize) { + q.add(&qv); + } + if let Some(x) = self.pos.get_mut(u as usize) { + *x = p; + } + if let Some(a) = self.alive.get_mut(v as usize) { + *a = false; + } + for i in [u, v] { + if let Some(stamp) = self.stamp.get_mut(i as usize) { + *stamp = stamp.wrapping_add(1); + } + } + true + } + + /// Cost again every edge at `u` (it moved). + fn reoffer(&mut self, u: u32) { + let mut around = std::mem::take(&mut self.scratch.nu); + self.neighbours(u, &mut around); + for &w in &around { + self.offer(u.min(w), u.max(w)); + } + self.scratch.nu = around; + } + + /// The mesh as it is now, and an original triangle each of its vertices came from. + fn level(&self) -> (Level, Vec) { + let mut index: Vec> = vec![None; self.pos.len()]; + let mut out = Level::default(); + let mut seeds = Vec::new(); + out.triangles.reserve(self.live_triangles); + for (t, alive) in self.tris.iter().zip(&self.tri_alive) { + if !alive { + continue; + } + let mut tri = [0u32; 3]; + for (k, &i) in t.iter().enumerate() { + let Some(slot) = index.get_mut(i as usize) else { continue }; + tri[k] = *slot.get_or_insert_with(|| { + let p = self.pos.get(i as usize).copied().unwrap_or_default(); + out.positions.push(Vec3::new(p[0], p[1], p[2])); + seeds.push(self.seed.get(i as usize).copied().unwrap_or(0)); + u32::try_from(out.positions.len() - 1).unwrap_or(u32::MAX) + }); + } + out.triangles.push(tri); + } + (out, seeds) + } +} + +#[cfg(test)] +#[path = "simplify_tests.rs"] +mod tests; diff --git a/crates/geom/src/simplify_tests.rs b/crates/geom/src/simplify_tests.rs new file mode 100644 index 000000000..dcb589883 --- /dev/null +++ b/crates/geom/src/simplify_tests.rs @@ -0,0 +1,158 @@ +use super::*; + +/// A sphere of radius `r` as a scan reads it: `n` × 2`n` quads, each triangle's corners its own +/// points (the same position repeated where triangles meet). +fn sphere(r: f64, n: u32) -> (Vec, Vec<[u32; 3]>) { + let at = |i: u32, j: u32| { + let (t, p) = (std::f64::consts::PI * f64::from(i) / f64::from(n), std::f64::consts::TAU * f64::from(j) / f64::from(2 * n)); + let (st, ct, sp, cp) = (t.sin(), t.cos(), p.sin(), p.cos()); + // The poles exactly, and the seam the same as the start, so corners weld. + let (st, sp, cp) = (if i == 0 || i == n { 0.0 } else { st }, if j == 2 * n { 0.0 } else { sp }, if j == 2 * n { 1.0 } else { cp }); + Vec3::new(r * st * cp, r * st * sp, r * ct) + }; + let (mut pos, mut tris) = (Vec::new(), Vec::new()); + let mut tri = |a: Vec3, b: Vec3, c: Vec3| { + let k = pos.len() as u32; + pos.extend([a, b, c]); + tris.push([k, k + 1, k + 2]); + }; + for i in 0..n { + for j in 0..2 * n { + let (a, b, c, d) = (at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)); + if i > 0 { + tri(a, c, d); + } + if i + 1 < n { + tri(a, b, c); + } + } + } + (pos, tris) +} + +fn volume(l: &Level) -> f64 { + l.triangles + .iter() + .map(|t| { + let [a, b, c] = t.map(|i| l.positions[i as usize]); + a.dot(b.cross(c)) / 6.0 + }) + .sum() +} + +/// Each side of a closed surface is shared by exactly two triangles, once each way. +fn closed(l: &Level) -> bool { + let mut sides: Vec<(u32, u32)> = l.triangles.iter().flat_map(|t| [(t[0], t[1]), (t[1], t[2]), (t[2], t[0])]).collect(); + sides.sort_unstable(); + let n = sides.len(); + sides.dedup(); + sides.len() == n && sides.iter().all(|(a, b)| sides.binary_search(&(*b, *a)).is_ok()) +} + +/// A finely meshed sphere comes down to each target, closed, with its volume and shape kept +/// within the error each level reports, and the errors grow as the levels coarsen. +#[test] +fn a_sphere_comes_down_to_each_target_within_its_error() { + let (pos, tris) = sphere(50.0, 100); + let full = tris.len(); + let made = levels(&pos, &tris, &[full / 4, full / 16, full / 64]); + assert_eq!(made.len(), 3); + let exact = 4.0 / 3.0 * std::f64::consts::PI * 50.0f64.powi(3); + let mut last = 0.0; + for (l, target) in made.iter().zip([full / 4, full / 16, full / 64]) { + assert!(l.triangles.len() <= target, "{} > {target}", l.triangles.len()); + assert!(closed(l), "closed and consistently wound"); + assert!(l.error >= last, "the error grows: {} after {last}", l.error); + last = l.error; + // Each point within the error of the sphere (its faces' planes lie within their sag of it). + let sag = 50.0 * (1.0 - (std::f64::consts::PI / 100.0).cos()); + for p in &l.positions { + assert!((p.len() - 50.0).abs() <= l.error + sag + 1e-9, "{} from the centre, error {}", p.len(), l.error); + } + assert!((volume(l) - exact).abs() < exact * 0.02, "volume {} vs {exact}", volume(l)); + // A 600-triangle sphere of radius 50 bows in by about 0.4 at its triangles' centres at + // best; finer levels a fraction of that. + assert!(l.error < 1.0, "error {}", l.error); + } + // The same mesh simplifies the same way. + let again = levels(&pos, &tris, &[full / 4, full / 16, full / 64]); + assert!(made.iter().zip(&again).all(|(a, b)| a.triangles == b.triangles && a.positions == b.positions)); +} + +/// A flat sheet comes down to a few triangles with no error, and its open outline stays put. +#[test] +fn a_flat_sheet_keeps_its_outline() { + let n = 40u32; + let at = |i: u32, j: u32| Vec3::new(f64::from(i), f64::from(j) * 0.5, 0.0); + let (mut pos, mut tris) = (Vec::new(), Vec::new()); + for i in 0..n { + for j in 0..n { + for t in [[at(i, j), at(i + 1, j), at(i + 1, j + 1)], [at(i, j), at(i + 1, j + 1), at(i, j + 1)]] { + let k = pos.len() as u32; + pos.extend(t); + tris.push([k, k + 1, k + 2]); + } + } + } + let made = levels(&pos, &tris, &[tris.len() / 16]); + let l = made.first().unwrap(); + assert!(l.triangles.len() <= tris.len() / 16); + assert!(l.error < 1e-6, "flat: {}", l.error); + assert!(l.positions.iter().all(|p| p.z.abs() < 1e-9)); + let b = l.positions.iter().fold(crate::Aabb3::EMPTY, |mut b, p| { + b.add(*p); + b + }); + assert!(b.min.dist(Vec3::ZERO) < 1e-9 && b.max.dist(Vec3::new(40.0, 20.0, 0.0)) < 1e-9, "{b:?}"); + let area: f64 = l + .triangles + .iter() + .map(|t| { + let [a, b, c] = t.map(|i| l.positions[i as usize]); + (b - a).cross(c - a).len() / 2.0 + }) + .sum(); + assert!((area - 800.0).abs() < 1e-6, "the sheet is still whole: {area}"); +} + +/// Input from a file is hostile: points that are not finite, indices out of range, triangles +/// with a repeated corner and empty meshes are left out, never a panic. +#[test] +fn hostile_meshes_never_panic() { + let p = |x: f64, y: f64| Vec3::new(x, y, 0.0); + assert!(levels(&[], &[], &[1]).is_empty()); + assert!(levels(&[p(0.0, 0.0)], &[[0, 0, 0]], &[1]).is_empty()); + assert!(levels(&[p(f64::NAN, 0.0), p(1.0, 0.0), p(0.0, 1.0)], &[[0, 1, 2]], &[0]).is_empty()); + assert!(levels(&[p(0.0, 0.0), p(1.0, 0.0), p(0.0, 1.0)], &[[0, 1, 99]], &[0]).is_empty()); + let (pos, mut tris) = sphere(5.0, 12); + tris.push([0, 1, u32::MAX]); + tris.push([2, 2, 3]); + let made = levels(&pos, &tris, &[usize::MAX, 100, 0]); + assert!(made.iter().all(|l| l.triangles.iter().flatten().all(|i| (*i as usize) < l.positions.len()))); +} + +/// Timing, not a check (`SOLIDCRAFT_BENCH_STL=part.stl cargo test --release -p solidcraft-geom +/// --lib simplify_timing -- --ignored --nocapture`): the levels of a binary STL. +#[test] +#[ignore] +fn simplify_timing() { + let Some(path) = std::env::var_os("SOLIDCRAFT_BENCH_STL") else { return }; + let bytes = std::fs::read(path).unwrap(); + let n = u32::from_le_bytes(bytes[80..84].try_into().unwrap()) as usize; + let f = |o: usize| f64::from(f32::from_le_bytes(bytes[o..o + 4].try_into().unwrap())); + let (mut pos, mut tris) = (Vec::with_capacity(n * 3), Vec::with_capacity(n)); + for t in 0..n { + let o = 84 + t * 50 + 12; + let k = pos.len() as u32; + for v in 0..3 { + pos.push(Vec3::new(f(o + v * 12), f(o + v * 12 + 4), f(o + v * 12 + 8))); + } + tris.push([k, k + 1, k + 2]); + } + let started = std::time::Instant::now(); + let made = levels(&pos, &tris, &[n / 4, n / 16, n / 64]); + eprintln!("{n} triangles: levels in {:.2} s", started.elapsed().as_secs_f64()); + for l in &made { + eprintln!(" {} triangles, error {:.3} mm", l.triangles.len(), l.error); + } +} diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rs index e7798a73f..6b96e5fd8 100644 --- a/crates/gpu/src/view.rs +++ b/crates/gpu/src/view.rs @@ -15,6 +15,7 @@ use wgpu::util::DeviceExt; use crate::pbr; mod ao; +mod oit; /// Triangle vertex: position (3 × f32), normal (3 × f32), sRGB colour (4 × u8). pub const TRI_SIZE: usize = 28; @@ -121,6 +122,29 @@ impl SharedTris { out } + /// A simplified copy of a mesh ([`solidcraft_geom::simplify::Level`]): its vertices already + /// shared, its triangles lit flat (each fragment with its triangle's own normal). + pub fn from_level(level: &solidcraft_geom::simplify::Level) -> SharedTris { + let mut out = SharedTris { flat: true, ..Default::default() }; + out.vertices.reserve_exact(level.positions.len() * SHARED_SIZE); + let mut lo = [f32::INFINITY; 3]; + let mut hi = [f32::NEG_INFINITY; 3]; + for p in &level.positions { + let p = p.to_f32(); + for k in 0..3 { + lo[k] = lo[k].min(p[k]); + hi[k] = hi[k].max(p[k]); + } + for v in p.iter().chain(&[0.0f32; 3]) { + out.vertices.extend_from_slice(&v.to_le_bytes()); + } + } + let n = u32::try_from(level.positions.len()).unwrap_or(u32::MAX); + out.indices = level.triangles.iter().filter(|t| t.iter().all(|i| *i < n)).flatten().copied().collect(); + out.bounds = [lo, hi]; + out + } + /// Vertex `i`: position and normal. fn vertex(&self, i: u32) -> ([f32; 3], [f32; 3]) { let at = i as usize * SHARED_SIZE; @@ -160,6 +184,18 @@ impl SharedTris { } } +/// How an [`InstancedMesh`]'s placements are drawn: opaque, or see-through (their colour's alpha) +/// as faded context (another component active) or as glass (an appearance's opacity). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum InstanceLook { + #[default] + Opaque, + /// With the translucent triangles ([`GpuScene::trans`]): no depth written. + Faded, + /// With the see-through bodies ([`GpuScene::glass`]): far sides, then near sides. + Glass, +} + /// Triangles drawn at several places: a part placed by many occurrences keeps one copy of its /// triangles on the GPU, drawn once per placement, however often it is placed. #[derive(Clone)] @@ -171,6 +207,12 @@ pub struct InstancedMesh { pub tris: Arc, /// The placements, [`INSTANCE_SIZE`] bytes each ([`InstancedMesh::place`]). pub instances: Vec, + /// How its placements are drawn (see-through ones are drawn back to front). + pub look: InstanceLook, + /// Coarser copies of the triangles, finest first, each with how far it strays from them (mm, + /// in the part's frame): a placement small on screen is drawn from the coarsest whose error + /// stays under [`LOD_PX`] there ([`Batches::cull`]). + pub levels: Vec<(Arc, f32)>, } impl InstancedMesh { @@ -262,7 +304,11 @@ impl GpuScene { out.hidden.extend_from_slice(&block.bytes.hidden); } for m in std::mem::take(&mut self.instanced) { - m.expand_into(&mut out.tris); + match m.look { + InstanceLook::Opaque => m.expand_into(&mut out.tris), + InstanceLook::Faded => m.expand_into(&mut out.trans), + InstanceLook::Glass => m.expand_into(&mut out.glass), + } } out.tris.append(&mut self.tris); out.trans.append(&mut self.trans); @@ -588,6 +634,61 @@ impl SharedGpu { } } +/// How far (pixels) a level of detail may stray from the full triangles where a placement is +/// drawn from it. +pub const LOD_PX: f32 = 0.75; + +/// Pixels per millimetre where a placement (`rows`, see [`INSTANCE_SIZE`]) of a box (`bounds`) +/// is nearest the eye, along whichever of its axes shows largest, in the view `vp` (columns) of +/// `size` pixels. Infinite when a corner of it is at or behind the eye, or for a box that is not +/// finite: such a placement is drawn in full. +pub fn placement_scale(rows: &[[f32; 4]; 3], bounds: &[[f32; 3]; 2], vp: &[[f32; 4]; 4], size: [f32; 2]) -> f32 { + let [lo, hi] = bounds; + if !lo.iter().chain(hi).all(|x| x.is_finite()) { + return f32::INFINITY; + } + let clip = |p: [f32; 3]| -> [f32; 4] { + let w = rows.map(|r| r[0] * p[0] + r[1] * p[1] + r[2] * p[2] + r[3]); + std::array::from_fn(|k| vp[0][k] * w[0] + vp[1][k] * w[1] + vp[2][k] * w[2] + vp[3][k]) + }; + let corners: [[f32; 3]; 8] = std::array::from_fn(|i| { + [if i & 1 == 0 { lo[0] } else { hi[0] }, if i & 2 == 0 { lo[1] } else { hi[1] }, if i & 4 == 0 { lo[2] } else { hi[2] }] + }); + let mut nearest = (f32::INFINITY, corners[0]); + for c in corners { + let w = clip(c)[3]; + if !(w > 1e-6) { + return f32::INFINITY; + } + if w < nearest.0 { + nearest = (w, c); + } + } + let screen = |c: [f32; 4]| [c[0] / c[3] * size[0] * 0.5, c[1] / c[3] * size[1] * 0.5]; + let (_, c) = nearest; + let at = screen(clip(c)); + let mut scale = 0.0f32; + for k in 0..3 { + // A step of a hundredth of the box (at least a micrometre) along each axis. + let step = ((hi[k] - lo[k]) * 0.01).max(1e-3); + let mut q = c; + q[k] += step; + let qc = clip(q); + if !(qc[3] > 1e-6) { + return f32::INFINITY; + } + let s = screen(qc); + scale = scale.max(((s[0] - at[0]).powi(2) + (s[1] - at[1]).powi(2)).sqrt() / step); + } + if scale.is_finite() { scale } else { f32::INFINITY } +} + +/// The coarsest level whose error (mm; level 0, the full triangles, has none) stays under +/// [`LOD_PX`] at `scale` pixels per millimetre. +pub fn level_for(errors: &[f32], scale: f32) -> usize { + errors.iter().rposition(|e| *e * scale <= LOD_PX).unwrap_or(0) +} + /// Whether a box (`bounds`, min and max) moved by a placement (`rows`, see [`INSTANCE_SIZE`]) /// may be seen in the view `vp` (the view-projection, columns, depth 0 near and 1 far): `false` /// only when all its corners lie beyond the same side of the view (left, right, top, bottom, @@ -612,23 +713,43 @@ pub fn placed_box_in_view(rows: &[[f32; 4]; 3], bounds: &[[f32; 3]; 2], vp: &[[f !out.iter().any(|o| *o) } -/// An [`InstancedMesh`] on the GPU: its triangles (shared with the next scene while its key -/// comes back) and the placements in view. -struct InstancedDraw { +/// One level of an [`InstancedDraw`]: its triangles and the placements drawn from it. +struct LevelDraw { tris: Arc, flat: bool, + /// How far it strays from the full triangles (mm; 0 for them). + error: f32, + /// The placements drawn from it, first `count` of them ([`Batches::cull`]). + instances: wgpu::Buffer, + count: u32, +} + +/// An [`InstancedMesh`] on the GPU: its triangles and coarser levels (shared with the next scene +/// while they come back), and the placements in view, each drawn from its level. +struct InstancedDraw { + look: InstanceLook, + /// The full triangles first, then coarser ones. + levels: Vec, bounds: [[f32; 3]; 2], /// Every placement ([`INSTANCE_SIZE`] bytes each), as the scene has them. placements: Vec, - /// The placements in view, first `count` of them ([`Batches::cull`]). - instances: wgpu::Buffer, - count: u32, - /// Which placements `instances` holds now (none drawn yet: `None`). - shown: Option>, + /// Which placements the levels hold now, with their level (none drawn yet: `None`). + shown: Option>, } impl InstancedDraw { - /// Draw every placement with `pipeline` (the placements in vertex buffer 1). + /// Draw every placement in view, from its level, with `smooth` or `flat` as the level's + /// triangles are lit. + pub(crate) fn draw(&self, pass: &mut wgpu::RenderPass<'_>, smooth: &wgpu::RenderPipeline, flat: &wgpu::RenderPipeline) { + for level in &self.levels { + pass.set_pipeline(if level.flat { flat } else { smooth }); + level.draw(pass); + } + } +} + +impl LevelDraw { + /// Draw its placements (in vertex buffer 1). fn draw(&self, pass: &mut wgpu::RenderPass<'_>) { if self.count == 0 { return; @@ -696,9 +817,9 @@ struct Batches { guide_lines: Option, planned: Vec, instanced: Vec, - /// The instanced meshes' triangles on the GPU by key, with the triangles they were uploaded - /// from. - instanced_tris: std::collections::HashMap, Arc)>, + /// The instanced meshes' triangles (and their levels) on the GPU, by the address of the + /// triangles they were uploaded from (held, so it stays theirs). + instanced_tris: std::collections::HashMap, Arc)>, /// Evaluated chunks by their plans' addresses, holding the plans so the addresses stay /// theirs: a chunk whose plans are unchanged is not evaluated again when the scene is rebuilt. resident: std::collections::HashMap, (Vec>, Arc)>, @@ -765,21 +886,30 @@ impl Batches { if count == 0 { continue; } - let tris = match kept.remove(&m.key).or_else(|| self.instanced_tris.remove(&m.key)).filter(|(b, _)| Arc::ptr_eq(b, &m.tris)) { - Some(t) => t, - None => match SharedGpu::new(device, &m.tris) { - Some(g) => (m.tris.clone(), Arc::new(g)), - None => continue, - }, - }; let placements = m.instances.get(..count as usize * INSTANCE_SIZE).unwrap_or_default().to_vec(); - let instances = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("sc_instances"), - contents: &placements, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - }); - out.push(InstancedDraw { tris: tris.1.clone(), flat: m.tris.flat, bounds: m.tris.bounds, placements, instances, count, shown: None }); - self.instanced_tris.insert(m.key, tris); + let mut levels = Vec::with_capacity(1 + m.levels.len()); + for (i, (tris, error)) in std::iter::once((&m.tris, 0.0f32)).chain(m.levels.iter().map(|(t, e)| (t, *e))).enumerate() { + let key = Arc::as_ptr(tris) as usize; + let gpu = match kept.remove(&key).or_else(|| self.instanced_tris.remove(&key)).filter(|(b, _)| Arc::ptr_eq(b, tris)) { + Some(t) => t, + None => match SharedGpu::new(device, tris) { + Some(g) => (tris.clone(), Arc::new(g)), + None => continue, + }, + }; + // Until the first cull, every placement is drawn in full. + let instances = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("sc_instances"), + contents: &placements, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + }); + let count = if i == 0 { count } else { 0 }; + levels.push(LevelDraw { tris: gpu.1.clone(), flat: tris.flat, error, instances, count }); + self.instanced_tris.insert(key, gpu); + } + if !levels.is_empty() { + out.push(InstancedDraw { look: m.look, levels, bounds: m.tris.bounds, placements, shown: None }); + } } out } @@ -787,34 +917,57 @@ impl Batches { /// Keep only the placements of the instanced meshes that may be seen in the view `vp` (see /// [`placed_box_in_view`]): a part placed many times, most of it out of the view, draws /// only what is in it. The placements are written again only when that set changes. - fn cull(&mut self, queue: &wgpu::Queue, vp: &[[f32; 4]; 4]) { + /// Each placement's level of detail is chosen here too: the coarsest whose error stays under + /// [`LOD_PX`] where it is drawn ([`placement_scale`], [`level_for`]), in a view of `size` + /// pixels. + fn cull(&mut self, queue: &wgpu::Queue, vp: &[[f32; 4]; 4], size: [f32; 2]) { let f = |b: &[u8], i: usize| b.get(i..i + 4).and_then(|x| x.try_into().ok()).map_or(0.0, f32::from_le_bytes); for d in &mut self.instanced { - let shown: Vec = d + let errors: Vec = d.levels.iter().map(|l| l.error).collect(); + let mut shown: Vec<(u32, u8)> = d .placements .as_chunks::() .0 .iter() .enumerate() - .filter(|(_, p)| { + .filter_map(|(i, p)| { let rows = [0, 1, 2].map(|r| [f(&p[..], 16 * r), f(&p[..], 16 * r + 4), f(&p[..], 16 * r + 8), f(&p[..], 16 * r + 12)]); - placed_box_in_view(&rows, &d.bounds, vp) + placed_box_in_view(&rows, &d.bounds, vp).then(|| { + let level = if errors.len() > 1 { level_for(&errors, placement_scale(&rows, &d.bounds, vp, size)) } else { 0 }; + (i as u32, u8::try_from(level).unwrap_or(0)) + }) }) - .map(|(i, _)| i as u32) .collect(); + // See-through placements back to front (by their box's centre), so each blends over + // what lies behind it. + if d.look != InstanceLook::Opaque { + let depth = |i: u32| { + let p = d.placements.get(i as usize * INSTANCE_SIZE..(i as usize + 1) * INSTANCE_SIZE).unwrap_or_default(); + let rows = [0, 1, 2].map(|r| [f(p, 16 * r), f(p, 16 * r + 4), f(p, 16 * r + 8), f(p, 16 * r + 12)]); + let [lo, hi] = d.bounds; + let c = [0, 1, 2].map(|k| (lo[k] + hi[k]) * 0.5); + let w = rows.map(|r| r[0] * c[0] + r[1] * c[1] + r[2] * c[2] + r[3]); + let clip: [f32; 4] = std::array::from_fn(|k| vp[0][k] * w[0] + vp[1][k] * w[1] + vp[2][k] * w[2] + vp[3][k]); + if clip[3].abs() > 1e-12 { clip[2] / clip[3] } else { f32::INFINITY } + }; + shown.sort_by(|a, b| depth(b.0).total_cmp(&depth(a.0)).then(a.0.cmp(&b.0))); + } if d.shown.as_ref() == Some(&shown) { continue; } - let bytes: Vec = shown - .iter() - .filter_map(|i| d.placements.get(*i as usize * INSTANCE_SIZE..(*i as usize + 1) * INSTANCE_SIZE)) - .flatten() - .copied() - .collect(); - if !bytes.is_empty() { - queue.write_buffer(&d.instances, 0, &bytes); + for (k, level) in d.levels.iter_mut().enumerate() { + let bytes: Vec = shown + .iter() + .filter(|(_, l)| usize::from(*l) == k) + .filter_map(|(i, _)| d.placements.get(*i as usize * INSTANCE_SIZE..(*i as usize + 1) * INSTANCE_SIZE)) + .flatten() + .copied() + .collect(); + if !bytes.is_empty() { + queue.write_buffer(&level.instances, 0, &bytes); + } + level.count = u32::try_from(bytes.len() / INSTANCE_SIZE).unwrap_or(u32::MAX); } - d.count = u32::try_from(shown.len()).unwrap_or(u32::MAX); d.shown = Some(shown); } } @@ -900,6 +1053,10 @@ pub struct Renderer { tri_cut_instanced: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>, /// `tri_instanced` and `tri_cut_instanced` for flat triangles. tri_instanced_flat: wgpu::RenderPipeline, + /// `glass_far`, `glass_near` and `trans` for instanced meshes, smooth and flat. + glass_far_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline), + glass_near_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline), + trans_instanced: (wgpu::RenderPipeline, wgpu::RenderPipeline), tri_cut_instanced_flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>, /// The tessellation compute pipeline, where the device runs compute shaders. tess: Option, @@ -949,6 +1106,15 @@ pub struct Renderer { max_texture: u32, /// Supersampling (#1288): the filter that draws the supersampled frame into the viewport. ss_blit: wgpu::RenderPipeline, + /// The frame drawn offscreen at the viewport's own size (for the see-through surfaces' order- + /// independent transparency), copied as it is. + copy_blit: wgpu::RenderPipeline, + /// The offscreen frame is not at the viewport's size: filtered ([`Renderer::ss_blit`]). + ss_scaled: bool, + /// Order-independent transparency for the model's see-through surfaces (`None` where the + /// device cannot blend into float targets: they are sorted instead), and its targets. + oit: Option, + oit_targets: Option, ss_bgl: wgpu::BindGroupLayout, /// The supersampled frame's textures, kept while its size stays. ss: Option, @@ -1023,6 +1189,11 @@ fn vs(@builtin(vertex_index) i: u32) -> O { o.uv = vec2(x, y); return o; } +/// The frame as it is (drawn at the viewport's own size). +@fragment +fn fs_copy(i: O) -> @location(0) vec4 { + return textureSampleLevel(src, smp, i.uv, 0.0); +} @fragment fn fs(i: O) -> @location(0) vec4 { let d = vec2(dpdx(i.uv.x), dpdy(i.uv.y)) * 0.25; @@ -1293,6 +1464,37 @@ impl Renderer { cache: None, }) }; + // See-through instanced meshes: blended, depth tested, not written; `cull` for glass. + let placed_see_through = [Some(shared_layout()), Some(instance_layout())]; + let see_through = |label: &str, source: TriSource, cull: Option| { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(label), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some(source.entry()), + buffers: &placed_see_through, + compilation_options: Default::default(), + }, + primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, cull_mode: cull, ..Default::default() }, + depth_stencil: depth(wgpu::CompareFunction::LessEqual, false), + multisample: wgpu::MultisampleState { count: t.samples.max(1), mask: !0, alpha_to_coverage_enabled: false }, + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some(&source.fragment("fs_tri")), + targets: &[Some(wgpu::ColorTargetState { format: t.format, blend: alpha, write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) + }; + let both = |label: &str, cull: Option| { + (see_through(label, TriSource::Instanced, cull), see_through(&format!("{label}_flat"), TriSource::InstancedFlat, cull)) + }; + let glass_far_instanced = both("sc_glass_far_instanced", Some(wgpu::Face::Front)); + let glass_near_instanced = both("sc_glass_near_instanced", Some(wgpu::Face::Back)); + let trans_instanced = both("sc_trans_instanced", None); let image_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("sc_image"), entries: &[ @@ -1460,26 +1662,30 @@ impl Renderer { }); // The frame is premultiplied (drawn over transparent black), so it goes over the // viewport's background premultiplied; the viewport's depth is left as it is. - let ss_blit = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("sc_ssaa"), - layout: Some(&ss_layout), - vertex: wgpu::VertexState { module: &ss_module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, - primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() }, - depth_stencil: depth(wgpu::CompareFunction::Always, false), - multisample: wgpu::MultisampleState { count: t.samples.max(1), mask: !0, alpha_to_coverage_enabled: false }, - fragment: Some(wgpu::FragmentState { - module: &ss_module, - entry_point: Some("fs"), - targets: &[Some(wgpu::ColorTargetState { - format: t.format, - blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), - write_mask: wgpu::ColorWrites::ALL, - })], - compilation_options: Default::default(), - }), - multiview_mask: None, - cache: None, - }); + let blit = |entry: &str| { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("sc_ssaa"), + layout: Some(&ss_layout), + vertex: wgpu::VertexState { module: &ss_module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() }, + depth_stencil: depth(wgpu::CompareFunction::Always, false), + multisample: wgpu::MultisampleState { count: t.samples.max(1), mask: !0, alpha_to_coverage_enabled: false }, + fragment: Some(wgpu::FragmentState { + module: &ss_module, + entry_point: Some(entry), + targets: &[Some(wgpu::ColorTargetState { + format: t.format, + blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), + write_mask: wgpu::ColorWrites::ALL, + })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) + }; + let (ss_blit, copy_blit) = (blit("fs"), blit("fs_copy")); + let oit = oit::Pipelines::new(device, &module, &layout, t.format, t.depth, t.samples); let depth_reset = t.depth.filter(|_| reversed).map(|format| { let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("sc_depth_reset"), @@ -1519,6 +1725,10 @@ impl Renderer { target: t, max_texture: device.limits().max_texture_dimension_2d, ss_blit, + copy_blit, + ss_scaled: false, + oit, + oit_targets: None, ss_bgl, ss: None, ss_ready: false, @@ -1541,6 +1751,9 @@ impl Renderer { TriSource::InstancedFlat, ), tri_cut_instanced_flat: cut(TriSource::InstancedFlat), + glass_far_instanced, + glass_near_instanced, + trans_instanced, tess, access_bgl, access_bind, @@ -1652,6 +1865,11 @@ pub fn sort_back_to_front(bytes: &[u8], back: [f32; 3]) -> Vec { } impl Batches { + /// Whether it has see-through surfaces (glass or faded, instanced or not). + fn see_through(&self) -> bool { + self.glass.is_some() || self.trans.is_some() || self.instanced.iter().any(|d| d.look != InstanceLook::Opaque) + } + /// Re-sort the glass for a new view direction (when it turned more than about 2 degrees). fn sort_glass(&mut self, queue: &wgpu::Queue, back: [f32; 3]) { let (Some(g), false) = (&self.glass, self.glass_cpu.is_empty()) else { return }; @@ -1714,8 +1932,13 @@ impl Renderer { res.sketch.take(device, cb.sketch_key, &cb.sketch_slot, None); res.highlight.take(device, cb.hl_key, &cb.hl_slot, None); res.preview.take(device, cb.pv_key, &cb.pv_slot, None); - res.model.sort_glass(queue, cb.back); - res.model.cull(queue, &cb.view_proj); + // See-through surfaces are drawn without sorting where the device can (order-independent + // transparency, in an offscreen frame); else their triangles are sorted for the view. + let see_through = res.oit.is_some() && res.model.see_through(); + if !see_through { + res.model.sort_glass(queue, cb.back); + } + res.model.cull(queue, &cb.view_proj, cb.size_px); queue.write_buffer(&res.uniform, 0, &uniform_bytes(cb, cb.size_px[0], cb.size_px[1], res.linear_out, res.target.reversed_depth())); if let Some(a) = &cb.access && a.version != res.access_version @@ -1773,7 +1996,7 @@ impl Renderer { res.quads.push((img.id, b)); } } - res.ss_ready = cb.ssaa != 1.0 && res.supersample(device, encoder, cb); + res.ss_ready = (cb.ssaa != 1.0 || see_through) && res.supersample(device, encoder, cb, see_through); } /// Draw the frame into `pass` within `viewport` (x, y, width, height in physical pixels): @@ -1787,7 +2010,7 @@ impl Renderer { if self.ss_ready && let Some(ss) = &self.ss { - pass.set_pipeline(&self.ss_blit); + pass.set_pipeline(if self.ss_scaled { &self.ss_blit } else { &self.copy_blit }); pass.set_bind_group(0, &ss.bind, &[]); pass.draw(0..3, 0..1); return; @@ -1796,7 +2019,7 @@ impl Renderer { pass.set_pipeline(reset); pass.draw(0..3, 0..1); } - self.draw(frame, pass); + self.draw(frame, pass, Phase::All); } /// The target the pipelines draw into. @@ -1807,7 +2030,7 @@ impl Renderer { impl Renderer { /// Draw the frame's scenes into `pass` (its viewport already set). - fn draw(&self, cb: &Frame, pass: &mut wgpu::RenderPass<'static>) { + fn draw(&self, cb: &Frame, pass: &mut wgpu::RenderPass<'static>, phase: Phase) { let res = self; pass.set_bind_group(0, &res.bind, &[]); let tris = |pass: &mut wgpu::RenderPass<'static>, p: &wgpu::RenderPipeline, b: &Option| { @@ -1835,6 +2058,12 @@ impl Renderer { } else { (&res.model, &res.sketch, &res.highlight, &res.preview) }; + if phase == Phase::AfterSeeThrough { + pass.set_bind_group(1, &res.access_bind, &[]); + pass.set_bind_group(2, &res.ao.bind, &[]); + Self::draw_over(res, pass, sk, h, pv, m, &tris, &lines); + return; + } lines(pass, &res.overlay, &m.under); lines(pass, &res.overlay, &sk.under); pass.set_bind_group(1, &res.access_bind, &[]); @@ -1850,9 +2079,8 @@ impl Renderer { }; // Instanced meshes, with the pipeline for smooth and for flat triangles. let instanced = |pass: &mut wgpu::RenderPass<'static>, smooth: &wgpu::RenderPipeline, flat: &wgpu::RenderPipeline| { - for d in &m.instanced { - pass.set_pipeline(if d.flat { flat } else { smooth }); - d.draw(pass); + for d in m.instanced.iter().filter(|d| d.look == InstanceLook::Opaque) { + d.draw(pass, smooth, flat); } }; let sectioned = cb.clip[..3].iter().map(|c| c * c).sum::() > 0.25; @@ -1900,9 +2128,37 @@ impl Renderer { } } tris(pass, &res.ghost, &pv.ghost); + if phase == Phase::BeforeSeeThrough { + return; + } + // See-through instanced meshes (placements back to front) with the rest of their kind. + let see_through = + |pass: &mut wgpu::RenderPass<'static>, look: InstanceLook, (smooth, flat): &(wgpu::RenderPipeline, wgpu::RenderPipeline)| { + for d in m.instanced.iter().filter(|d| d.look == look) { + d.draw(pass, smooth, flat); + } + }; tris(pass, &res.glass_far, &m.glass); + see_through(pass, InstanceLook::Glass, &res.glass_far_instanced); tris(pass, &res.glass_near, &m.glass); + see_through(pass, InstanceLook::Glass, &res.glass_near_instanced); tris(pass, &res.trans, &m.trans); + see_through(pass, InstanceLook::Faded, &res.trans_instanced); + Self::draw_over(res, pass, sk, h, pv, m, &tris, &lines); + } + + /// What is drawn over the model's see-through surfaces. + #[allow(clippy::too_many_arguments)] + fn draw_over( + res: &Renderer, + pass: &mut wgpu::RenderPass<'static>, + sk: &Batches, + h: &Batches, + pv: &Batches, + m: &Batches, + tris: &dyn Fn(&mut wgpu::RenderPass<'static>, &wgpu::RenderPipeline, &Option), + lines: &dyn Fn(&mut wgpu::RenderPass<'static>, &wgpu::RenderPipeline, &Option), + ) { tris(pass, &res.trans, &sk.trans); tris(pass, &res.trans, &h.trans); tris(pass, &res.guide_trans, &h.guide_trans); @@ -1912,48 +2168,94 @@ impl Renderer { lines(pass, &res.overlay, &sk.overlays); lines(pass, &res.overlay, &h.overlays); } + + /// The model's see-through surfaces into the order-independent transparency's targets. + fn draw_see_through(&self, pass: &mut wgpu::RenderPass<'static>, p: &oit::Pipelines) { + let m = &self.model; + pass.set_bind_group(0, &self.bind, &[]); + pass.set_pipeline(&p.built); + for batch in [&m.glass, &m.trans].into_iter().flatten() { + for (buffer, count) in &batch.parts { + pass.set_vertex_buffer(0, buffer.slice(..)); + pass.draw(0..*count, 0..1); + } + } + for d in m.instanced.iter().filter(|d| d.look != InstanceLook::Opaque) { + d.draw(pass, &p.instanced, &p.instanced_flat); + } + } } impl Renderer { /// Draw the frame supersampled (`cb.ssaa` times the viewport's pixels each way, within the /// device's texture limit and the memory budget) into textures of the viewport's kind. /// `false` when it cannot be (too small a gain, a zero-sized viewport): drawn as usual. - fn supersample(&mut self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, cb: &Frame) -> bool { - let Some(size) = supersampled_size(cb.size_px, cb.ssaa, self.max_texture, self.target.samples) else { + fn supersample(&mut self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, cb: &Frame, see_through: bool) -> bool { + // Supersampled when asked (and worth it); else, for order-independent transparency, at + // the viewport's own size. + let own = || { + let [w, h] = cb.size_px; + (w >= 1.0 && h >= 1.0) + .then(|| [(w.round() as u32).clamp(1, self.max_texture.max(1)), (h.round() as u32).clamp(1, self.max_texture.max(1))]) + }; + let scaled = if cb.ssaa != 1.0 { supersampled_size(cb.size_px, cb.ssaa, self.max_texture, self.target.samples) } else { None }; + let Some(size) = scaled.or_else(|| if see_through { own() } else { None }) else { self.ss = None; return false; }; + self.ss_scaled = scaled.is_some(); if self.ss.as_ref().is_none_or(|ss| ss.size != size) { self.ss = Some(self.supersampled(device, size)); } + if see_through + && let Some(p) = &self.oit + && self.oit_targets.as_ref().is_none_or(|t| t.size != size) + { + self.oit_targets = Some(p.targets(device, size, self.target.samples)); + } let Some(ss) = &self.ss else { return false }; let depth_format = self.target.depth; - let mut pass = encoder - .begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("sc_ssaa"), - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &ss.color, - depth_slice: None, - resolve_target: ss.resolve.as_ref(), - ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), store: wgpu::StoreOp::Store }, - })], - depth_stencil_attachment: ss.depth.as_ref().map(|view| wgpu::RenderPassDepthStencilAttachment { - view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Clear(far_depth(self.target.reversed_depth())), - store: wgpu::StoreOp::Discard, + let stencil = depth_format.is_some_and(|f| f.has_stencil_aspect()); + // The depth is kept for the passes after the first when there are see-through surfaces. + let keep = if see_through { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard }; + let pass = |encoder: &mut wgpu::CommandEncoder, first: bool| { + let load = |clear| if first { wgpu::LoadOp::Clear(clear) } else { wgpu::LoadOp::Load }; + encoder + .begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("sc_ssaa"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &ss.color, + depth_slice: None, + resolve_target: ss.resolve.as_ref(), + ops: wgpu::Operations { load: load(wgpu::Color::TRANSPARENT), store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: ss.depth.as_ref().map(|view| wgpu::RenderPassDepthStencilAttachment { + view, + depth_ops: Some(wgpu::Operations { load: load_depth(first, far_depth(self.target.reversed_depth())), store: keep }), + stencil_ops: stencil.then_some(wgpu::Operations { load: load_depth(first, 0), store: keep }), }), - stencil_ops: depth_format - .filter(|f| f.has_stencil_aspect()) - .map(|_| wgpu::Operations { load: wgpu::LoadOp::Clear(0), store: wgpu::StoreOp::Discard }), - }), - timestamp_writes: None, - occlusion_query_set: None, - multiview_mask: None, - }) - .forget_lifetime(); - pass.set_viewport(0.0, 0.0, size[0] as f32, size[1] as f32, 0.0, 1.0); - self.draw(cb, &mut pass); + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }) + .forget_lifetime() + }; + let (Some(p), Some(t), true) = (&self.oit, &self.oit_targets, see_through) else { + let mut first = pass(encoder, true); + first.set_viewport(0.0, 0.0, size[0] as f32, size[1] as f32, 0.0, 1.0); + self.draw(cb, &mut first, Phase::All); + return true; + }; + { + let mut first = pass(encoder, true); + first.set_viewport(0.0, 0.0, size[0] as f32, size[1] as f32, 0.0, 1.0); + self.draw(cb, &mut first, Phase::BeforeSeeThrough); + } + p.accumulate(encoder, t, ss.depth.as_ref(), stencil, |pass| self.draw_see_through(pass, p)); + p.composite(encoder, t, &ss.color, ss.resolve.as_ref()); + let mut over = pass(encoder, false); + over.set_viewport(0.0, 0.0, size[0] as f32, size[1] as f32, 0.0, 1.0); + self.draw(cb, &mut over, Phase::AfterSeeThrough); true } @@ -1994,6 +2296,22 @@ impl Renderer { } } +/// Which part of a frame [`Renderer::draw`] draws. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Phase { + /// All of it, the model's see-through surfaces sorted. + All, + /// Up to the model's see-through surfaces, which order-independent transparency draws. + BeforeSeeThrough, + /// What is drawn over them (sketch fills, highlights, X-ray, overlays). + AfterSeeThrough, +} + +/// A depth (or stencil) attachment's load: cleared to `clear` for the first pass, else kept. +fn load_depth(first: bool, clear: T) -> wgpu::LoadOp { + if first { wgpu::LoadOp::Clear(clear) } else { wgpu::LoadOp::Load } +} + /// Uniform bytes for a frame. pub fn uniform_bytes(cb: &Frame, w: f32, h: f32, linear: bool, reversed: bool) -> Vec { let mut v: Vec = gpu_view_proj(cb.view_proj, reversed).iter().flatten().copied().collect(); @@ -2060,6 +2378,41 @@ mod tests { assert!(placed_box_in_view(&at(30.0, 0.0, -10.0), &[[f32::NAN; 3], [1.0; 3]], &persp)); } + /// A placement's pixels per millimetre: in an orthographic view the same anywhere, along the + /// axis that shows largest (a turned box too); in perspective, larger nearer the eye, and + /// infinite (drawn in full) with a corner behind it. The level chosen is the coarsest whose + /// error stays under `LOD_PX` there. + #[test] + fn levels_of_detail_follow_the_size_on_screen() { + use super::{LOD_PX, level_for, placement_scale}; + let unit = [[0.0f32, 0.0, 0.0], [1.0, 1.0, 1.0]]; + let at = |x: f32, y: f32, z: f32| [[1.0, 0.0, 0.0, x], [0.0, 1.0, 0.0, y], [0.0, 0.0, 1.0, z]]; + // Orthographic over x, y in -10..10 on a 200 × 200 view: 10 px a millimetre. + let ortho = [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0], [0.0, 0.0, 0.005, 0.0], [0.0, 0.0, 0.5, 1.0]]; + let size = [200.0, 200.0]; + assert!((placement_scale(&at(0.0, 0.0, 0.0), &unit, &ortho, size) - 10.0).abs() < 1e-3); + assert!((placement_scale(&at(5.0, -3.0, 0.0), &unit, &ortho, size) - 10.0).abs() < 1e-3); + let (c, s) = (0.6f32.cos(), 0.6f32.sin()); + let turned = [[c, -s, 0.0, 0.0], [s, c, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; + assert!((placement_scale(&turned, &unit, &ortho, size) - 10.0).abs() < 1e-2); + // Perspective looking down -z from the origin (w = -z), 90° over 200 px. + let persp = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, -1.0], [0.0, 0.0, -1.0, 0.0]]; + let near = placement_scale(&at(0.0, 0.0, -10.0), &unit, &persp, size); + let far = placement_scale(&at(0.0, 0.0, -100.0), &unit, &persp, size); + assert!((near - 100.0 / 9.0).abs() < 0.2, "nearest corner at depth 9: {near}"); + assert!(far < near / 9.0 && far > 0.0, "{far} vs {near}"); + assert!(placement_scale(&at(0.0, 0.0, 0.5), &unit, &persp, size).is_infinite(), "a corner behind the eye"); + assert!(placement_scale(&at(0.0, 0.0, -10.0), &[[f32::NAN; 3], [1.0; 3]], &persp, size).is_infinite()); + // The coarsest level within LOD_PX: errors 0 (full), 0.05, 0.2, 1.0 mm. + let errors = [0.0, 0.05, 0.2, 1.0]; + assert_eq!(level_for(&errors, f32::INFINITY), 0); + assert_eq!(level_for(&errors, 100.0), 0, "5 px for the first level"); + assert_eq!(level_for(&errors, 10.0), 1); + assert_eq!(level_for(&errors, LOD_PX / 0.2), 2); + assert_eq!(level_for(&errors, 0.5), 3); + assert_eq!(level_for(&[0.0], 0.001), 0); + } + /// Shared vertices: a flat mesh's by position alone (an STL's facets: one normal per /// triangle), a smooth mesh's by position and normal; corners that differ in either stay /// apart. The corners come back as they went in, a flat triangle's lit with its own normal. @@ -2214,6 +2567,149 @@ mod glass_tests { assert!(buffer_chunk(TRI_SIZE, 1) >= TRI_SIZE * 6, "a tiny limit still holds one unit"); } + /// The middle pixel of `scene` drawn looking down -Z (x, y in -10..10) into a 64 × 64 frame, + /// with order-independent transparency (`oit`) or with the see-through triangles sorted. + /// `None` without a GPU adapter. + fn middle_pixel(scene: GpuScene, oit: bool) -> Option<[u8; 4]> { + const SIZE: u32 = 64; + let (_, device, queue) = crate::open_headless().ok()?; + let depth = crate::depth_format(&device, true); + let format = wgpu::TextureFormat::Rgba8Unorm; + let mut r = Renderer::new(&device, GpuTarget { format, depth: Some(depth), samples: 1, tess: false }, None); + assert!(r.oit.is_some(), "this device does order-independent transparency"); + if !oit { + r.oit = None; + } + let none = || Arc::new(Mutex::new(None)); + let frame = Frame { + product: false, + key: 1, + slot: Arc::new(Mutex::new(Some(scene))), + sketch_key: 0, + sketch_slot: none(), + hl_key: 0, + hl_slot: none(), + pv_key: 0, + pv_slot: none(), + pv_hide: false, + view_proj: [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0], [0.0, 0.0, -0.01, 0.0], [0.0, 0.0, 0.5, 1.0]], + back: [0.0, 0.0, 1.0], + size_px: [SIZE as f32; 2], + clip: [0.0; 4], + cap: [0.0; 4], + images: Vec::new(), + analysis: [0.0; 8], + analysis_colors: [0.0; 36], + access: None, + outline: false, + eye: [0.0, 0.0, 100.0, 0.0], + ao_radius: 0.0, + ssaa: 1.0, + }; + let texture = |format, usage| { + device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { width: SIZE, height: SIZE, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + }; + let color = texture(format, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC); + let depth_tex = texture(depth, wgpu::TextureUsages::RENDER_ATTACHMENT); + let (color_view, depth_view) = (color.create_view(&Default::default()), depth_tex.create_view(&Default::default())); + let mut encoder = device.create_command_encoder(&Default::default()); + r.prepare(&device, &queue, &mut encoder, &frame); + { + let mut pass = encoder + .begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color_view, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(far_depth(reversed_depth(Some(depth)))), + store: wgpu::StoreOp::Store, + }), + stencil_ops: depth + .has_stencil_aspect() + .then_some(wgpu::Operations { load: wgpu::LoadOp::Clear(0), store: wgpu::StoreOp::Store }), + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }) + .forget_lifetime(); + r.paint(&frame, &mut pass, [0.0, 0.0, SIZE as f32, SIZE as f32]); + } + let read = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: u64::from(SIZE * SIZE * 4), + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + encoder.copy_texture_to_buffer( + color.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &read, + layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(SIZE * 4), rows_per_image: Some(SIZE) }, + }, + wgpu::Extent3d { width: SIZE, height: SIZE, depth_or_array_layers: 1 }, + ); + queue.submit([encoder.finish()]); + read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::wait_indefinitely()).ok()?; + let bytes = read.slice(..).get_mapped_range().ok()?.to_vec(); + let at = ((SIZE / 2 * SIZE + SIZE / 2) * 4) as usize; + bytes.get(at..at + 4).and_then(|p| p.try_into().ok()) + } + + /// Two see-through squares, one at z = 5 (near) and one at z = -5, in `near` and `far` + /// colours, given near first or far first. + fn two_layers(near: [u8; 4], far: [u8; 4], near_first: bool) -> GpuScene { + let mut sc = GpuScene::default(); + let mut layers = [(5.0f32, near), (-5.0, far)]; + if !near_first { + layers.reverse(); + } + for (z, c) in layers { + for p in [[-5.0, -5.0], [5.0, -5.0], [5.0, 5.0], [-5.0, -5.0], [5.0, 5.0], [-5.0, 5.0]] { + sc.glass_tri([p[0], p[1], z], [0.0, 0.0, 1.0], c); + } + } + sc + } + + /// Order-independent transparency: the same see-through layers give the same picture in + /// whichever order they come, the same as sorting them where they share a colour, and close to + /// it where they do not (the weighted average of their colours, by how much they cover). + /// Skipped without a GPU adapter. + #[test] + fn see_through_layers_blend_in_any_order() { + let (blue, red) = ([40, 80, 220, 128], [220, 40, 40, 128]); + let Some(either) = middle_pixel(two_layers(blue, red, true), true) else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let other = middle_pixel(two_layers(blue, red, false), true).unwrap(); + assert!(either.iter().zip(other).all(|(a, b)| a.abs_diff(b) <= 1), "{either:?} vs {other:?}"); + let near = |a: [u8; 4], b: [u8; 4], by: u8| a.iter().zip(b).all(|(x, y)| x.abs_diff(y) <= by); + let same = middle_pixel(two_layers(blue, blue, false), true).unwrap(); + let sorted = middle_pixel(two_layers(blue, blue, false), false).unwrap(); + assert!(near(same, sorted, 3), "one colour: {same:?} vs sorted {sorted:?}"); + let sorted = middle_pixel(two_layers(blue, red, false), false).unwrap(); + assert!(near(either, sorted, 48), "two colours: {either:?} vs sorted {sorted:?}"); + assert!(either[0] > 100 && either[2] > 100, "both colours show: {either:?}"); + } + #[test] fn glass_is_drawn_far_to_near() { // Two triangles at z = 5 and z = -5, given near first; looking down from +Z. diff --git a/crates/gpu/src/view/ao.rs b/crates/gpu/src/view/ao.rs index ff876ad05..884467105 100644 --- a/crates/gpu/src/view/ao.rs +++ b/crates/gpu/src/view/ao.rs @@ -168,9 +168,8 @@ impl Resources { pass.set_index_buffer(draw.mesh.indices.slice(..), wgpu::IndexFormat::Uint32); pass.draw_indexed(0..draw.mesh.index_count, 0, 0..1); } - for draw in &batch.instanced { - pass.set_pipeline(if draw.flat { &self.instanced_flat_pipeline } else { &self.instanced_pipeline }); - draw.draw(&mut pass); + for draw in batch.instanced.iter().filter(|d| d.look == super::InstanceLook::Opaque) { + draw.draw(&mut pass, &self.instanced_pipeline, &self.instanced_flat_pipeline); } } } diff --git a/crates/gpu/src/view/oit.rs b/crates/gpu/src/view/oit.rs new file mode 100644 index 000000000..54245ceec --- /dev/null +++ b/crates/gpu/src/view/oit.rs @@ -0,0 +1,284 @@ +//! Weighted blended order-independent transparency (McGuire and Bavoil, "Weighted Blended +//! Order-Independent Transparency", JCGT 2013) for the model's see-through surfaces: glass (an +//! appearance's opacity) and faded context, instanced or not. +//! +//! Each see-through fragment adds its colour, weighted by its alpha and its depth, into an +//! accumulation target, and multiplies its transparency into a revealage target; a full-screen +//! pass then lays the weighted average over what is behind it, by how much is covered. Nothing is +//! sorted, on the CPU or the GPU, however many see-through triangles there are. The targets share +//! the frame's depth (tested, not written), so the opaque model hides what lies behind it. + +use super::{TriSource, depth_compare, instance_layout, shared_layout, tri_layout}; + +/// The accumulated colour (premultiplied, weighted) and alpha. +const ACCUM: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; +/// How much of what is behind still shows (1: all of it). +const REVEAL: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm; + +const COMPOSITE: &str = r#" +@group(0) @binding(0) var accum_t: texture_2d; +@group(0) @binding(1) var reveal_t: texture_2d; +@vertex +fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { + let x = f32((i << 1u) & 2u); + let y = f32(i & 2u); + return vec4(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0); +} +@fragment +fn fs(@builtin(position) pos: vec4) -> @location(0) vec4 { + let p = vec2(pos.xy); + let reveal = textureLoad(reveal_t, p, 0).r; + let accum = textureLoad(accum_t, p, 0); + if (reveal >= 1.0) { discard; } + return vec4(accum.rgb / max(accum.a, 1e-5), 1.0 - reveal); +} +"#; + +/// The pipelines, made once per target kind. +pub(super) struct Pipelines { + /// Built triangles ([`tri_layout`]), and instanced meshes smooth and flat. + pub built: wgpu::RenderPipeline, + pub instanced: wgpu::RenderPipeline, + pub instanced_flat: wgpu::RenderPipeline, + composite: wgpu::RenderPipeline, + bgl: wgpu::BindGroupLayout, +} + +/// The targets, kept while the frame's size stays. +pub(super) struct Targets { + pub size: [u32; 2], + accum: wgpu::TextureView, + accum_resolve: Option, + reveal: wgpu::TextureView, + reveal_resolve: Option, + bind: wgpu::BindGroup, +} + +impl Pipelines { + /// The pipelines for frames of `format` with `depth` and `samples`, using the viewport's + /// shader `module` and uniform `layout`; `None` where the device cannot blend into a float + /// target (the model's see-through surfaces are then sorted as before). + pub fn new( + device: &wgpu::Device, + module: &wgpu::ShaderModule, + layout: &wgpu::PipelineLayout, + format: wgpu::TextureFormat, + depth: Option, + samples: u32, + ) -> Option { + let scopes = [wgpu::ErrorFilter::OutOfMemory, wgpu::ErrorFilter::Internal, wgpu::ErrorFilter::Validation].map(|f| device.push_error_scope(f)); + let reversed = super::reversed_depth(depth); + let samples = samples.max(1); + let multisample = wgpu::MultisampleState { count: samples, mask: !0, alpha_to_coverage_enabled: false }; + let add = + wgpu::BlendComponent { src_factor: wgpu::BlendFactor::One, dst_factor: wgpu::BlendFactor::One, operation: wgpu::BlendOperation::Add }; + let keep = wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::Zero, + dst_factor: wgpu::BlendFactor::OneMinusSrc, + operation: wgpu::BlendOperation::Add, + }; + let targets = [ + Some(wgpu::ColorTargetState { + format: ACCUM, + blend: Some(wgpu::BlendState { color: add, alpha: add }), + write_mask: wgpu::ColorWrites::ALL, + }), + Some(wgpu::ColorTargetState { + format: REVEAL, + blend: Some(wgpu::BlendState { color: keep, alpha: keep }), + write_mask: wgpu::ColorWrites::RED, + }), + ]; + let built_buffers = [Some(tri_layout())]; + let placed = [Some(shared_layout()), Some(instance_layout())]; + let pipeline = |label: &str, source: TriSource| { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(label), + layout: Some(layout), + vertex: wgpu::VertexState { + module, + entry_point: Some(source.entry()), + buffers: if source == TriSource::Built { &built_buffers[..] } else { &placed[..] }, + compilation_options: Default::default(), + }, + primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() }, + depth_stencil: depth.map(|format| wgpu::DepthStencilState { + format, + depth_write_enabled: Some(false), + depth_compare: Some(depth_compare(reversed, wgpu::CompareFunction::LessEqual)), + stencil: Default::default(), + bias: Default::default(), + }), + multisample, + fragment: Some(wgpu::FragmentState { + module, + entry_point: Some(&source.fragment("fs_oit")), + targets: &targets, + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) + }; + let built = pipeline("sc_oit", TriSource::Built); + let instanced = pipeline("sc_oit_instanced", TriSource::Instanced); + let instanced_flat = pipeline("sc_oit_instanced_flat", TriSource::InstancedFlat); + let texture = |binding| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: false }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }; + let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("sc_oit"), entries: &[texture(0), texture(1)] }); + let composite_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("sc_oit_composite"), + bind_group_layouts: &[Some(&bgl)], + immediate_size: 0, + }); + let composite_module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("sc_oit_composite"), + source: wgpu::ShaderSource::Wgsl(COMPOSITE.into()), + }); + // Over the frame (premultiplied over transparent black): the average colour by how much + // is covered. + let over = wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + }; + let composite = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("sc_oit_composite"), + layout: Some(&composite_layout), + vertex: wgpu::VertexState { module: &composite_module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() }, + depth_stencil: None, + multisample, + fragment: Some(wgpu::FragmentState { + module: &composite_module, + entry_point: Some("fs"), + targets: &[Some(wgpu::ColorTargetState { format, blend: Some(over), write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }); + let mut failed = None; + for scope in scopes.into_iter().rev() { + if let Some(e) = crate::popped(scope.pop()) { + failed.get_or_insert(e.to_string()); + } + } + if let Some(e) = failed { + log::info!("see-through surfaces are sorted (no order-independent transparency here: {e})"); + return None; + } + Some(Pipelines { built, instanced, instanced_flat, composite, bgl }) + } + + /// The targets for a frame of `size` pixels at `samples` per pixel. + pub fn targets(&self, device: &wgpu::Device, size: [u32; 2], samples: u32) -> Targets { + let samples = samples.max(1); + let texture = |label: &str, format, samples, usage| { + device + .create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { width: size[0].max(1), height: size[1].max(1), depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: samples, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + .create_view(&Default::default()) + }; + let read = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING; + let pair = |label: &str, format| { + if samples > 1 { + (texture(label, format, samples, wgpu::TextureUsages::RENDER_ATTACHMENT), Some(texture(label, format, 1, read))) + } else { + (texture(label, format, 1, read), None) + } + }; + let (accum, accum_resolve) = pair("sc_oit_accum", ACCUM); + let (reveal, reveal_resolve) = pair("sc_oit_reveal", REVEAL); + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("sc_oit"), + layout: &self.bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(accum_resolve.as_ref().unwrap_or(&accum)) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(reveal_resolve.as_ref().unwrap_or(&reveal)) }, + ], + }); + Targets { size, accum, accum_resolve, reveal, reveal_resolve, bind } + } + + /// The pass that gathers the see-through surfaces (`draw` records them), against `depth`. + pub fn accumulate( + &self, + encoder: &mut wgpu::CommandEncoder, + t: &Targets, + depth: Option<&wgpu::TextureView>, + stencil: bool, + draw: impl FnOnce(&mut wgpu::RenderPass<'static>), + ) { + let attach = |view, resolve_target, clear| { + Some(wgpu::RenderPassColorAttachment { + view, + depth_slice: None, + resolve_target, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(clear), store: wgpu::StoreOp::Store }, + }) + }; + let mut pass = encoder + .begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("sc_oit"), + color_attachments: &[ + attach(&t.accum, t.accum_resolve.as_ref(), wgpu::Color::TRANSPARENT), + attach(&t.reveal, t.reveal_resolve.as_ref(), wgpu::Color::WHITE), + ], + depth_stencil_attachment: depth.map(|view| wgpu::RenderPassDepthStencilAttachment { + view, + depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }), + stencil_ops: stencil.then_some(wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }), + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }) + .forget_lifetime(); + pass.set_viewport(0.0, 0.0, t.size[0] as f32, t.size[1] as f32, 0.0, 1.0); + draw(&mut pass); + } + + /// Lay what was gathered over the frame's `color` (resolved into `resolve`). + pub fn composite(&self, encoder: &mut wgpu::CommandEncoder, t: &Targets, color: &wgpu::TextureView, resolve: Option<&wgpu::TextureView>) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("sc_oit_composite"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: color, + depth_slice: None, + resolve_target: resolve, + ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&self.composite); + pass.set_bind_group(0, &t.bind, &[]); + pass.draw(0..3, 0..1); + } +} diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index f638f70d6..d4ca383f0 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -8,8 +8,8 @@ use std::sync::{Arc, Mutex}; use egui_wgpu::wgpu; pub use solidcraft_gpu::view::{ - AccessMap, BlockBytes, Frame, GpuImage, GpuScene, GpuTarget, InstancedMesh, LINE_SIZE, PBR_SIZE, PlannedBody, Renderer, SHARED_SIZE, SceneBlock, - SceneSlot, SharedTris, TRI_SIZE, supersampled_size, + AccessMap, BlockBytes, Frame, GpuImage, GpuScene, GpuTarget, INSTANCE_SIZE, InstanceLook, InstancedMesh, LINE_SIZE, LOD_PX, PBR_SIZE, + PlannedBody, Renderer, SHARED_SIZE, SceneBlock, SceneSlot, SharedTris, TRI_SIZE, level_for, placement_scale, supersampled_size, }; /// A frame of the viewport as an egui paint callback. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 40dbc43c9..ca2ea5720 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -92,6 +92,7 @@ mod material_card_review; mod measurement_ui_tests; #[cfg(test)] mod menu_tests; +pub(crate) mod mesh_lod; pub mod messages; #[cfg(test)] mod pattern_arrow_tests; diff --git a/crates/ui-egui/src/mesh_lod.rs b/crates/ui-egui/src/mesh_lod.rs new file mode 100644 index 000000000..0e31ec34d --- /dev/null +++ b/crates/ui-egui/src/mesh_lod.rs @@ -0,0 +1,108 @@ +//! Levels of detail for large meshes drawn as instances: coarser copies of a shared mesh +//! ([`solidcraft_engine::geom::simplify`]), made once per mesh off the window thread, so a +//! placement small on screen is drawn from far fewer triangles ([`crate::gpu::LOD_PX`] decides +//! which). Until a mesh's levels are made it is drawn in full; when they are, the scene is built +//! again with them ([`MeshLods::generation`]). +//! +//! Display only: the reference mesh (picking, measuring, exports) is never simplified. + +use std::collections::{HashMap, HashSet}; +use std::sync::{Arc, Mutex}; + +use solidcraft_engine::geom::Mesh; + +use crate::gpu::SharedTris; + +/// A mesh gets levels of detail from this many triangles. +pub(crate) const MIN_TRIANGLES: usize = 20_000; +/// Fewest triangles a level keeps. +const MIN_LEVEL: usize = 1_000; +/// Meshes simplified at once. +const MAX_JOBS: usize = 2; + +/// A mesh's levels: coarser copies, finest first, each with its error (mm). +pub(crate) type Levels = Arc, f32)>>; + +/// The levels made (and being made), by mesh address with the mesh held, so its address stays +/// its own. +#[derive(Clone, Default)] +pub(crate) struct MeshLods(Arc>); + +#[derive(Default)] +struct State { + by_mesh: HashMap, Option)>, + /// Counts the levels made: the scene is built again when it changes. + generation: u64, + making: usize, +} + +impl MeshLods { + /// `mesh`'s levels when made; otherwise `None`, and they are started (once) on a worker + /// when one is free. + pub(crate) fn levels(&self, mesh: &Arc) -> Option { + if mesh.triangles.len() < MIN_TRIANGLES { + return None; + } + let key = Arc::as_ptr(mesh) as usize; + let mut state = self.0.lock().ok()?; + if let Some((_, levels)) = state.by_mesh.get(&key).filter(|(held, _)| Arc::ptr_eq(held, mesh)) { + return levels.clone(); + } + if state.making >= MAX_JOBS { + return None; + } + state.by_mesh.insert(key, (mesh.clone(), None)); + state.making += 1; + let (shared, source) = (self.0.clone(), mesh.clone()); + let started = std::thread::Builder::new().name("mesh-lod".into()).spawn(move || { + let levels = Arc::new(make(&source)); + if let Ok(mut state) = shared.lock() { + if let Some(slot) = state.by_mesh.get_mut(&key).filter(|(held, _)| Arc::ptr_eq(held, &source)) { + slot.1 = Some(levels); + } + state.making = state.making.saturating_sub(1); + state.generation = state.generation.wrapping_add(1); + } + }); + if started.is_err() { + // No threads (the browser): drawn in full. + state.making = state.making.saturating_sub(1); + if let Some(slot) = state.by_mesh.get_mut(&key) { + slot.1 = Some(Arc::new(Vec::new())); + } + } + None + } + + /// Changes whenever a mesh's levels are made. + pub(crate) fn generation(&self) -> u64 { + self.0.lock().map_or(0, |s| s.generation) + } + + /// Levels are being made (keep frames coming to pick them up). + pub(crate) fn busy(&self) -> bool { + self.0.lock().is_ok_and(|s| s.making > 0) + } + + /// Let go of the levels of meshes not in `used` (by address); those being made stay. + pub(crate) fn retain(&self, used: &HashSet) { + if let Ok(mut state) = self.0.lock() { + state.by_mesh.retain(|k, (_, levels)| levels.is_none() || used.contains(k)); + } + } +} + +/// `mesh`'s levels: a quarter, a sixteenth and a sixty-fourth of its triangles (none under +/// [`MIN_LEVEL`]). +fn make(mesh: &Mesh) -> Vec<(Arc, f32)> { + let n = mesh.triangles.len(); + let targets: Vec = [4, 16, 64].into_iter().map(|d| n / d).filter(|t| *t >= MIN_LEVEL).collect(); + solidcraft_engine::geom::simplify::levels(&mesh.positions, &mesh.triangles, &targets) + .iter() + .map(|l| (Arc::new(SharedTris::from_level(l)), l.error as f32)) + .collect() +} + +#[cfg(test)] +#[path = "mesh_lod_tests.rs"] +mod tests; diff --git a/crates/ui-egui/src/mesh_lod_tests.rs b/crates/ui-egui/src/mesh_lod_tests.rs new file mode 100644 index 000000000..319ac5851 --- /dev/null +++ b/crates/ui-egui/src/mesh_lod_tests.rs @@ -0,0 +1,64 @@ +use super::*; + +/// A sphere mesh of about `2 n²` triangles, each corner its own point. +fn sphere(n: u32) -> Arc { + use solidcraft_engine::geom::Vec3; + let at = |i: u32, j: u32| { + let (t, p) = (std::f64::consts::PI * f64::from(i) / f64::from(n), std::f64::consts::TAU * f64::from(j) / f64::from(2 * n)); + let (st, sp, cp) = + (if i == 0 || i == n { 0.0 } else { t.sin() }, if j == 2 * n { 0.0 } else { p.sin() }, if j == 2 * n { 1.0 } else { p.cos() }); + Vec3::new(50.0 * st * cp, 50.0 * st * sp, 50.0 * t.cos()) + }; + let mut m = Mesh::default(); + for i in 0..n { + for j in 0..2 * n { + let (a, b, c, d) = (at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)); + for [x, y, z] in [[a, c, d], [a, b, c]] { + let k = m.positions.len() as u32; + let nrm = (y - x).cross(z - x).normalized().unwrap_or(Vec3::Z); + m.positions.extend([x, y, z]); + m.normals.extend([nrm; 3]); + m.triangles.push([k, k + 1, k + 2]); + } + } + } + Arc::new(m) +} + +/// A large mesh's levels are made once, on a worker: asked for, `None` comes back at once and +/// the work starts; when done, the generation moves on and the levels come back, coarser and +/// coarser, each with its error. A small mesh gets none. +#[test] +fn a_large_meshs_levels_are_made_once_on_a_worker() { + let lods = MeshLods::default(); + assert!(lods.levels(&sphere(20)).is_none(), "small: none"); + assert!(!lods.busy()); + let mesh = sphere(110); + assert!(mesh.triangles.len() >= MIN_TRIANGLES); + let before = lods.generation(); + assert!(lods.levels(&mesh).is_none(), "not made in the asking frame"); + let began = std::time::Instant::now(); + let levels = loop { + if let Some(l) = lods.levels(&mesh) { + break l; + } + assert!(began.elapsed() < std::time::Duration::from_secs(60), "made on a worker"); + std::thread::sleep(std::time::Duration::from_millis(5)); + }; + assert_ne!(lods.generation(), before); + assert!(!lods.busy()); + // A quarter and a sixteenth (a sixty-fourth would be under `MIN_LEVEL`). + assert_eq!(levels.len(), 2, "{} triangles", mesh.triangles.len()); + let mut last = (mesh.triangles.len(), 0.0f32); + for (tris, error) in levels.iter() { + let n = tris.indices.len() / 3; + assert!(n * 3 <= last.0, "{n} after {}", last.0); + assert!(*error >= last.1 && *error < 2.0, "error {error}"); + assert!(tris.flat); + last = (n, *error); + } + assert!(Arc::ptr_eq(&levels, &lods.levels(&mesh).unwrap()), "made once"); + // Let go when no longer drawn. + lods.retain(&HashSet::new()); + assert!(lods.levels(&mesh).is_none(), "made again when asked again"); +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 62aa0e1e6..d890ae7b6 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -66,6 +66,8 @@ pub struct ViewportState { /// The triangles of each mesh the last scene drew as instances, by the mesh's address (the /// mesh held, so its address stays its own). instanced_tris: std::sync::Mutex, + /// The levels of detail of large instanced meshes ([`crate::mesh_lod`]). + pub(crate) mesh_lods: crate::mesh_lod::MeshLods, /// View-dependent display refinement (#1320): which bodies are drawn finer than the chosen /// display quality, and their refined plans. pub(crate) lod: std::sync::Mutex, @@ -319,6 +321,7 @@ fn scene_key(app: &SolveApp) -> u64 { app.ui.ground_shadow.hash(&mut h); solidcraft_engine::doc::display_quality::current().hash(&mut h); lod_generation(app).hash(&mut h); + app.viewport.mesh_lods.generation().hash(&mut h); // The section plane (its cut outlines are part of the model scene). section_plane(app).map(|(o, n)| [o.x, o.y, o.z, n.x, n.y, n.z].map(f64::to_bits)).hash(&mut h); app.cam.up.hash(&mut h); @@ -714,6 +717,7 @@ struct Instances { /// The meshes drawn as instances, in the order their first placement came, by address. meshes: Vec<(std::sync::Arc, crate::gpu::InstancedMesh)>, kept: InstancedTris, + lods: crate::mesh_lod::MeshLods, } impl Instances { @@ -733,31 +737,42 @@ impl Instances { .filter(|(_, (m, _))| m.triangles.len() >= INSTANCE_ALONE || uses.get(&(std::sync::Arc::as_ptr(m) as usize)).is_some_and(|n| *n > 1)) .collect(); } - Instances { candidates, meshes: Vec::new(), kept } + Instances { candidates, meshes: Vec::new(), kept, lods: app.viewport.mesh_lods.clone() } } - /// Draw `b` (in colour `col`) as an instance when it is one: its shared mesh and placement. + /// Draw `b` as an instance when it is one, in colour `col` (its alpha for a see-through + /// `look`): its shared mesh and placement. fn place( &mut self, b: &solidcraft_engine::doc::ModelBody, col: [u8; 4], + look: crate::gpu::InstanceLook, ) -> Option<(std::sync::Arc, solidcraft_engine::doc::Mat)> { let (mesh, m) = self.candidates.get(&b.name)?.clone(); let rows = [0, 1, 2].map(|r| [m[0][r] as f32, m[1][r] as f32, m[2][r] as f32, m[3][r] as f32]); let key = std::sync::Arc::as_ptr(&mesh) as usize; - let at = match self.meshes.iter().position(|(k, _)| std::sync::Arc::ptr_eq(k, &mesh)) { + let at = match self.meshes.iter().position(|(k, i)| std::sync::Arc::ptr_eq(k, &mesh) && i.look == look) { Some(at) => at, None => { - let tris = match self.kept.get(&key).filter(|(k, _)| std::sync::Arc::ptr_eq(k, &mesh)) { - Some((_, t)) => t.clone(), - None => taken_shared_tris(&mesh).unwrap_or_else(|| std::sync::Arc::new(crate::gpu::SharedTris::new(&mesh))), + // The same triangles (and levels) for each look of a mesh. + let made = self.meshes.iter().find(|(k, _)| std::sync::Arc::ptr_eq(k, &mesh)).map(|(_, i)| (i.tris.clone(), i.levels.clone())); + let (tris, levels) = match made { + Some(made) => made, + None => ( + match self.kept.get(&key).filter(|(k, _)| std::sync::Arc::ptr_eq(k, &mesh)) { + Some((_, t)) => t.clone(), + None => taken_shared_tris(&mesh).unwrap_or_else(|| std::sync::Arc::new(crate::gpu::SharedTris::new(&mesh))), + }, + // A large mesh's levels of detail, once made (until then, drawn in full). + self.lods.levels(&mesh).map(|l| l.as_ref().clone()).unwrap_or_default(), + ), }; - self.meshes.push((mesh.clone(), crate::gpu::InstancedMesh { key: key as u64, tris, instances: Vec::new() })); + self.meshes.push((mesh.clone(), crate::gpu::InstancedMesh { key: key as u64, tris, instances: Vec::new(), look, levels })); self.meshes.len() - 1 } }; let (_, inst) = self.meshes.get_mut(at)?; - inst.place(rows, [col[0], col[1], col[2], 255]); + inst.place(rows, col); Some((mesh, m)) } @@ -765,6 +780,7 @@ impl Instances { fn finish(self, app: &SolveApp) -> Vec { let kept: InstancedTris = self.meshes.iter().map(|(mesh, inst)| (std::sync::Arc::as_ptr(mesh) as usize, (mesh.clone(), inst.tris.clone()))).collect(); + self.lods.retain(&kept.keys().copied().collect()); if let Ok(mut k) = app.viewport.instanced_tris.lock() { *k = kept; } @@ -833,7 +849,21 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { .collect(); sc.planned.push(crate::gpu::PlannedBody { plan, face_colors }); } - let instanced = (!on_gpu && !faded && face_looks.is_empty() && opacity >= 0.999).then(|| instances.place(b, col)).flatten(); + // Opaque, faded and see-through bodies alike, in one colour (faces of their own colour + // are built as before). + let look = if faded { + crate::gpu::InstanceLook::Faded + } else if opacity < 0.999 { + crate::gpu::InstanceLook::Glass + } else { + crate::gpu::InstanceLook::Opaque + }; + let alpha = match look { + crate::gpu::InstanceLook::Faded => col[3], + crate::gpu::InstanceLook::Glass => (opacity.clamp(0.05, 1.0) * 255.0).round() as u8, + crate::gpu::InstanceLook::Opaque => 255, + }; + let instanced = (!on_gpu && face_looks.is_empty()).then(|| instances.place(b, [col[0], col[1], col[2], alpha], look)).flatten(); let body = match instanced { Some((mesh, place)) => BodyDraw { mesh, place: Some(place), col, faded, opacity, face_looks, on_gpu: true }, None => BodyDraw { mesh: drawn_mesh(app, b), place: None, col, faded, opacity, face_looks, on_gpu }, @@ -2625,6 +2655,10 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { // ---- model ---- crate::frame_watch::step(app, "viewport before the model"); update_refinement(app, ui.ctx(), &proj); + // Levels of detail being made: keep frames coming to draw them when they are. + if app.viewport.mesh_lods.busy() { + ui.ctx().request_repaint_after(std::time::Duration::from_millis(100)); + } crate::frame_watch::step(app, "display refinement"); let key = scene_key(app); let t0 = crate::now_ms(); @@ -4883,6 +4917,10 @@ mod tests { for k in 1..25 { app.run("occurrence.copy", json!({"component":component,"translate":[f64::from(k % 5) * step, f64::from(k / 5) * step, 0.0]})).unwrap(); } + // `SOLIDCRAFT_BENCH_OPACITY=0.5`: every placement see-through. + if let Some(opacity) = std::env::var("SOLIDCRAFT_BENCH_OPACITY").ok().and_then(|v| v.parse::().ok()) { + app.run("appearance.assign", json!({"components": [component.to_string()], "color": "#b4b8bc", "opacity": opacity})).unwrap(); + } let Some((_, device, queue)) = crate::gpu::offscreen_device() else { eprintln!("skipping: no GPU adapter"); return; @@ -4928,11 +4966,50 @@ mod tests { eprintln!("close up: {seen} of {} placements' centres in view", st.bodies.len()); } let yaw = app.cam.yaw; - for (label, scene) in [("own triangles", scene.clone().merged()), ("instanced", scene)] { + // The levels of detail, made off the window thread meanwhile. + let t = std::time::Instant::now(); + while app.viewport.mesh_lods.busy() { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + let lod = build_scene(&app); + eprintln!( + "levels of detail made in {:.1} s: {:?}", + t.elapsed().as_secs_f64(), + lod.instanced.iter().map(|m| m.levels.iter().map(|(t, e)| (t.indices.len() / 3, *e)).collect::>()).collect::>() + ); + // `SOLIDCRAFT_BENCH_PNG=dir`: the view drawn from each placement's own triangles, in full + // as instances, and from the levels (`SOLIDCRAFT_BENCH_PNG_ONLY=1`: then stop). + if let Some(dir) = std::env::var_os("SOLIDCRAFT_BENCH_PNG") { + const SIDE: u32 = 1024; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(SIDE as f32, SIDE as f32)); + let proj = projection(&app, rect); + let e = proj.cam.eye().to_f32(); + let eye = [e[0], e[1], e[2], if proj.cam.fov > 0.0 { 1.0 } else { 0.0 }]; + for (name, scene) in [("own", scene.clone().merged()), ("full", scene.clone()), ("lod", lod.clone())] { + if let Some(rgba) = crate::gpu::render_offscreen( + scene, + None, + proj.vp.to_f32(), + proj.cam.back().to_f32(), + eye, + SIDE, + None, + [0.0; 8], + crate::gpu::analysis_colors(&Tokens::get().analysis), + ) && let Some(png) = solidcraft_engine::render::png(SIDE as usize, SIDE as usize, rgba) + { + let _ = std::fs::write(std::path::Path::new(&dir).join(format!("{name}.png")), png); + } + } + } + if std::env::var_os("SOLIDCRAFT_BENCH_PNG_ONLY").is_some() { + return; + } + for (label, scene) in [("own triangles", scene.clone().merged()), ("instanced", scene), ("instanced, levels of detail", lod)] { let mut renderer = crate::gpu::Renderer::new(&device, GpuTarget { format, depth: Some(depth), samples, tess: false }, None); let slot: SceneSlot = std::sync::Arc::new(std::sync::Mutex::new(Some(scene))); let none = || std::sync::Arc::new(std::sync::Mutex::new(None)); - let mut times = Vec::new(); + let (mut times, mut prepared) = (Vec::new(), Vec::new()); for k in 0..40 { app.cam.yaw = yaw + f64::from(k) * 0.05; let proj = projection(&app, rect); @@ -4965,6 +5042,9 @@ mod tests { let t0 = std::time::Instant::now(); let mut encoder = device.create_command_encoder(&Default::default()); renderer.prepare(&device, &queue, &mut encoder, &frame); + if k >= 5 { + prepared.push(t0.elapsed().as_secs_f64() * 1e3); + } { let mut pass = encoder .begin_render_pass(&wgpu::RenderPassDescriptor { @@ -4997,7 +5077,14 @@ mod tests { } } times.sort_by(f64::total_cmp); - eprintln!("{label}: median {:.1} ms a frame (min {:.1}, max {:.1})", times[times.len() / 2], times[0], times[times.len() - 1]); + prepared.sort_by(f64::total_cmp); + eprintln!( + "{label}: median {:.1} ms a frame (min {:.1}, max {:.1}), of which {:.1} ms preparing on the CPU", + times[times.len() / 2], + times[0], + times[times.len() - 1], + prepared[prepared.len() / 2] + ); } } @@ -5031,6 +5118,154 @@ mod tests { assert!(!WARMED.lock().unwrap().iter().any(|(m, _)| std::sync::Arc::ptr_eq(m, &mesh)), "taken"); } + /// A design with one imported mesh of about 100 000 triangles (a sphere of radius 50, each + /// corner its own point, as an STL is read), drawn on a device that builds bodies on the CPU. + fn large_scan() -> SolveApp { + use solidcraft_engine::doc::{Document, FeatureKind, MeshData}; + let n = 160u32; + let at = |i: u32, j: u32| { + let (t, p) = (std::f64::consts::PI * f64::from(i) / f64::from(n), std::f64::consts::TAU * f64::from(j) / f64::from(2 * n)); + let (st, sp, cp) = + (if i == 0 || i == n { 0.0 } else { t.sin() }, if j == 2 * n { 0.0 } else { p.sin() }, if j == 2 * n { 1.0 } else { p.cos() }); + [50.0 * st * cp, 50.0 * st * sp, 50.0 * t.cos()] + }; + let corners: Vec<[f64; 3]> = (0..n) + .flat_map(|i| (0..2 * n).flat_map(move |j| [at(i, j), at(i + 1, j + 1), at(i, j + 1), at(i, j), at(i + 1, j), at(i + 1, j + 1)])) + .collect(); + let mut doc = Document::default(); + let meshes = vec![MeshData { + name: "Scan".into(), + positions: corners.iter().flatten().copied().collect(), + triangles: (0..u32::try_from(corners.len()).unwrap()).collect(), + color: None, + }]; + doc.add_feature(FeatureKind::MeshImport { file: "scan.stl".into(), meshes }, Some("Scan")).unwrap(); + let mut app = SolveApp::new(solidcraft_engine::Session::new(doc), crate::Services::default()); + app.ui.ground_shadow = false; + app.ui.show_origin = false; + app.viewport.gpu = Some(GpuTarget { format: egui_wgpu::wgpu::TextureFormat::Rgba8Unorm, depth: None, samples: 1, tess: false }); + app + } + + /// A large mesh is drawn in full until its levels of detail are made off the window thread; + /// then the scene is built again with them, and seen small, a coarse level draws it as the + /// full triangles do (within a fraction of a pixel). Skipped without a GPU adapter (the + /// levels are checked anyway). + #[test] + fn a_large_mesh_seen_small_is_drawn_from_its_levels_of_detail() { + let mut app = large_scan(); + let (mesh, _) = app.session.world_state().bodies[0].mesh_in_place(); + assert!(mesh.triangles.len() >= INSTANCE_ALONE); + let key = scene_key(&app); + let first = build_scene(&app); + assert_eq!(first.instanced.len(), 1); + assert!(first.instanced[0].levels.is_empty(), "drawn in full meanwhile"); + let began = std::time::Instant::now(); + while app.viewport.mesh_lods.busy() { + assert!(began.elapsed() < std::time::Duration::from_secs(120), "the levels are made"); + std::thread::sleep(std::time::Duration::from_millis(10)); + } + assert_ne!(scene_key(&app), key, "the scene is built again when they are"); + let scene = build_scene(&app); + let levels = &scene.instanced[0].levels; + assert!(levels.len() >= 2, "{} levels", levels.len()); + // Small on screen: the sphere about 30 px across. + app.fit_view(); + app.cam.distance *= 4.0; + const SIZE: u32 = 256; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(SIZE as f32, SIZE as f32)); + let proj = projection(&app, rect); + let e = proj.cam.eye().to_f32(); + let eye = [e[0], e[1], e[2], if proj.cam.fov > 0.0 { 1.0 } else { 0.0 }]; + let render = |scene: GpuScene| { + crate::gpu::render_offscreen( + scene, + None, + proj.vp.to_f32(), + proj.cam.back().to_f32(), + eye, + SIZE, + None, + [0.0; 8], + crate::gpu::analysis_colors(&Tokens::get().analysis), + ) + }; + // The level the placement is drawn from there. + let rows = [[1.0f32, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; + let scale = crate::gpu::placement_scale(&rows, &scene.instanced[0].tris.bounds, &proj.vp.to_f32(), [SIZE as f32; 2]); + let errors: Vec = std::iter::once(0.0).chain(levels.iter().map(|(_, e)| *e)).collect(); + assert!(crate::gpu::level_for(&errors, scale) >= 1, "a coarse level: scale {scale} px/mm, errors {errors:?}"); + let (Some(lod), Some(full)) = (render(scene.clone()), render(scene.merged())) else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let drawn = full.chunks(4).filter(|p| p[..3] != [255, 255, 255]).count(); + assert!(drawn > 300, "the sphere is drawn: {drawn} pixels"); + let differ = lod.chunks(4).zip(full.chunks(4)).filter(|(a, b)| a.iter().zip(b.iter()).any(|(x, y)| x.abs_diff(*y) > 24)).count(); + assert!(differ * 20 < drawn, "{differ} of {drawn} pixels differ"); + } + + /// See-through and faded placements of a part are drawn as instances too, with their alpha: + /// glass with an appearance's opacity, faded context with another component active. None + /// of their triangles go to the glass sorted triangle by triangle, and they look as their + /// own triangles did. GPU pictures skipped without an adapter. + #[test] + fn see_through_and_faded_placements_are_drawn_as_instances() { + use crate::gpu::InstanceLook; + let mut app = three_placed_parts(PARTS[0]); + app.run("appearance.assign", json!({"components": ["Part"], "color": "#3366cc", "opacity": 0.4})).unwrap(); + let glass = build_scene(&app); + assert_eq!(glass.instanced.len(), 1); + assert_eq!((glass.instanced[0].look, glass.instanced[0].count()), (InstanceLook::Glass, 3)); + assert!(glass.instanced[0].instances.as_chunks::<{ crate::gpu::INSTANCE_SIZE }>().0.iter().all(|p| p[51] == 102), "alpha 0.4"); + assert!(glass.glass.is_empty(), "no triangle sorted one by one"); + assert!(!glass.clone().merged().glass.is_empty()); + // One placement active (as right after the part is made): the others faded. + let mut context = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default()); + context.viewport.gpu = app.viewport.gpu; + context.ui.ground_shadow = false; + context.ui.show_origin = false; + context.run("component.create", json!({"name":"Part"})).unwrap(); + context.run(PARTS[0].0, serde_json::from_str(PARTS[0].1).unwrap()).unwrap(); + context.run("occurrence.copy", json!({"component":"Part","translate":[40,0,0]})).unwrap(); + context.run("occurrence.copy", json!({"component":"Part","translate":[0,40,0]})).unwrap(); + let faded = build_scene(&context); + let looks: Vec<_> = faded.instanced.iter().map(|m| (m.look, m.count())).collect(); + assert!(looks.contains(&(InstanceLook::Faded, 2)), "the other placements faded: {looks:?}"); + assert!(faded.trans.is_empty()); + for (name, scene) in [("glass", glass), ("faded", faded)] { + app.fit_view(); + const SIZE: u32 = 256; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(SIZE as f32, SIZE as f32)); + let proj = projection(&app, rect); + let e = proj.cam.eye().to_f32(); + let eye = [e[0], e[1], e[2], if proj.cam.fov > 0.0 { 1.0 } else { 0.0 }]; + let render = |scene: GpuScene| { + crate::gpu::render_offscreen( + scene, + None, + proj.vp.to_f32(), + proj.cam.back().to_f32(), + eye, + SIZE, + None, + [0.0; 8], + crate::gpu::analysis_colors(&Tokens::get().analysis), + ) + }; + let (Some(instanced), Some(own)) = (render(scene.clone()), render(scene.merged())) else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let drawn = own.chunks(4).filter(|p| p[..3] != [255, 255, 255]).count(); + assert!(drawn > 1000, "{name}: drawn {drawn}"); + let differ = instanced.chunks(4).zip(own.chunks(4)).filter(|(a, b)| a.iter().zip(b.iter()).any(|(x, y)| x.abs_diff(*y) > 24)).count(); + // A pixel here and there along the boxes' edges, where moving the corners on the GPU + // rounds them differently. + assert!(differ * 50 < drawn, "{name}: {differ} of {drawn} pixels differ"); + } + } + /// Each placement of a part is picked where it stands, the turned one too, from the part's /// shared mesh: the face under the cursor names that placement's body, at the point the /// cursor is over, and no placement makes a moved copy of the mesh to be picked.