diff --git a/assets/shaders/viewport.wgsl b/assets/shaders/viewport.wgsl index 7e8c27a58..2cc978d85 100644 --- a/assets/shaders/viewport.wgsl +++ b/assets/shaders/viewport.wgsl @@ -64,7 +64,96 @@ fn vs_tri(@location(0) p: vec3, @location(1) n: vec3, @location(2) c: return o; } -/// Opaque model surfaces: cut by the section plane, the inside shows as a flat cap colour. +/// Shared triangles (in their part's own frame) drawn at one placement: the rows of its rigid +/// move (`r0`..`r2`, a point's coordinates are their dot products with (p, 1)) and its colour. +/// The vertex colour (location 2) is not read. +@vertex +fn vs_tri_inst( + @location(0) p: vec3, + @location(1) n: vec3, + @location(3) r0: vec4, + @location(4) r1: vec4, + @location(5) r2: vec4, + @location(6) c: vec4, +) -> TOut { + let q = vec4(p, 1.0); + let wp = vec3(dot(r0, q), dot(r1, q), dot(r2, q)); + var o: TOut; + o.pos = u.vp * vec4(wp, 1.0); + o.n = vec3(dot(r0.xyz, n), dot(r1.xyz, n), dot(r2.xyz, n)); + o.c = c; + o.wp = wp; + return o; +} + +/// A flat-shaded placement (`vs_tri_inst_flat`): its vertices are shared between triangles, so +/// the face's normal is the triangle's own plane, worked out per fragment. The fragment gets its +/// position from the eye (`rp`): small near what is looked at, so the plane comes out exact at +/// any zoom however far from the origin the part stands, and few values per vertex (a tiled GPU +/// stores every vertex's outputs: more of them cost more than the triangles' shared vertices +/// save). +struct FOut { + @builtin(position) pos: vec4, + @location(0) rp: vec3, + @location(1) c: vec4, +}; + +@vertex +fn vs_tri_inst_flat( + @location(0) p: vec3, + @location(3) r0: vec4, + @location(4) r1: vec4, + @location(5) r2: vec4, + @location(6) c: vec4, +) -> FOut { + let q = vec4(p, 1.0); + let wp = vec3(dot(r0, q), dot(r1, q), dot(r2, q)); + var o: FOut; + o.pos = u.vp * vec4(wp, 1.0); + o.rp = wp - u.eye.xyz; + o.c = c; + return o; +} + +/// The fragment of a flat-shaded placement as the other triangles' fragments are: its normal +/// the outward normal of its triangle (toward the eye on a front face, away on a back face, as +/// the triangles are wound outward). +fn flat_fragment(i: FOut, front: bool) -> TOut { + var n = cross(dpdx(i.rp), dpdy(i.rp)); + let toward = select(u.back.xyz, -i.rp, u.eye.w > 0.5); + n = n * inverseSqrt(max(dot(n, n), 1e-30)); + if ((dot(n, toward) < 0.0) == front) { n = -n; } + var o: TOut; + o.pos = i.pos; + o.n = n; + o.c = i.c; + o.wp = i.rp + u.eye.xyz; + return o; +} + +@fragment +fn fs_solid_flat(i: FOut, @builtin(front_facing) front: bool) -> @location(0) vec4 { + let t = flat_fragment(i, front); + let c = solid(t, front); + return c; +} + +@fragment +fn fs_solid_cut_flat(i: FOut, @builtin(front_facing) front: bool) -> CutOut { + let t = flat_fragment(i, front); + var o: CutOut; + o.depth = pushed(cap_depth(t.wp, t.pos.z)); + o.c = solid(t, front); + return o; +} + +@fragment +fn fs_ao_depth_flat(i: FOut, @builtin(front_facing) front: bool) -> AoDepth { + let t = flat_fragment(i, front); + return opaque_depth(t, front); +} + +/// Opaque model surfaces: cut by the section plane/// Opaque model surfaces: cut by the section plane, the inside shows as a flat cap colour. @fragment fn fs_solid(i: TOut, @builtin(front_facing) front: bool) -> @location(0) vec4 { let c = solid(i, front); @@ -150,6 +239,11 @@ struct AoDepth { @fragment fn fs_ao_depth(i: TOut, @builtin(front_facing) front: bool) -> AoDepth { + return opaque_depth(i, front); +} + +/// The AO prepass's depth of an opaque surface's fragment. +fn opaque_depth(i: TOut, front: bool) -> AoDepth { if (clipped(i.wp) || length(i.n) < 0.5) { discard; } var depth = i.pos.z; if (!front && length(u.clip.xyz) > 0.5) { depth = cap_depth(i.wp, i.pos.z); } diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 4927c5520..fad308266 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -163,6 +163,50 @@ impl ModelBody { .clone() } + /// Identity of this body's display geometry, without meshing it: the cell its mesh is kept + /// in, shared by every clone of the body (timeline snapshots, world states that place it + /// unchanged) and, while it is held, by no other body. + pub fn display_identity(&self) -> Arc { + self.mesh.clone() + } + + /// [`ModelBody::mesh`] as a shared mesh and the move that places it: for a rigid placement + /// of a definition, the definition's own mesh (the same one for every placement of it) and + /// the placement; for any other body its own mesh, unmoved. The mesh moved by the matrix is + /// `mesh()`, so a viewport can draw every placement of a part from one copy of its + /// triangles, without making the moved copies. + pub fn mesh_in_place(&self) -> (Arc, crate::Mat) { + match self.mesh_placement.as_deref() { + Some(placed) => placed.source_mesh(), + None => (self.mesh(), crate::IDENTITY), + } + } + + /// The box of [`ModelBody::mesh`] without making a placement's moved copy: the shared mesh's + /// points, moved. + pub fn mesh_bounds(&self) -> solidcraft_geom::Aabb3 { + if let Some(mesh) = self.cached_mesh() { + return mesh.bounds(); + } + let (mesh, m) = self.mesh_in_place(); + if crate::is_identity(&m) { + return mesh.bounds(); + } + mesh.positions.iter().fold(solidcraft_geom::Aabb3::EMPTY, |mut b, p| { + b.add(crate::apply_point(&m, *p)); + b + }) + } + + /// [`ModelBody::plan_mesh_at`] as a shared mesh and the move that places it, as + /// [`ModelBody::mesh_in_place`] does for the reference mesh. + pub fn plan_mesh_in_place_at(&self, q: crate::display_quality::DisplayQuality) -> Option<(Arc, crate::Mat)> { + match self.mesh_placement.as_deref() { + Some(placed) => placed.source_plan_mesh(q, &self.name), + None => Some((self.plan_mesh_at(q)?, crate::IDENTITY)), + } + } + /// [`ModelBody::plan_mesh`] if it is made already; otherwise it is made on a worker thread /// (once, however often this is asked) and `None` comes back meanwhile, so a caller drawing a /// frame (a face highlighted under the pointer) is not held up by a large body's plan diff --git a/crates/doc/src/placed_mesh.rs b/crates/doc/src/placed_mesh.rs index 6da3c4e08..2131f89db 100644 --- a/crates/doc/src/placed_mesh.rs +++ b/crates/doc/src/placed_mesh.rs @@ -59,6 +59,18 @@ impl PlacedMesh { /// The definition's display plan mesh at quality `q`, moved here. pub(super) fn plan_mesh(&self, q: crate::display_quality::DisplayQuality, name: &str) -> Option> { + let (source, _) = self.source_plan_mesh(q, name)?; + Some(Arc::new(self.moved(&source))) + } + + /// The definition's reference mesh (shared by every placement of it) and this placement. + pub(super) fn source_mesh(&self) -> (Arc, crate::Mat) { + (reference_mesh(&self.body, &self.mesh, self.tolerance), self.transform) + } + + /// The definition's display plan mesh at quality `q` (shared by every placement of it) and + /// this placement. + pub(super) fn source_plan_mesh(&self, q: crate::display_quality::DisplayQuality, name: &str) -> Option<(Arc, crate::Mat)> { let cell = self.plan_meshes.get(q.index())?; let source = cell .get_or_init(|| { @@ -66,7 +78,7 @@ impl PlacedMesh { self.body.display_mesh_of(&plan).ok().map(Arc::new) }) .clone()?; - Some(Arc::new(self.moved(&source))) + Some((source, self.transform)) } /// `source` (in the definition's place) moved by this placement. @@ -150,6 +162,44 @@ mod tests { assert!(Arc::ptr_eq(&mesh, &nested.mesh())); } + /// A placement hands out its definition's own meshes and its move without making the moved + /// copy: every placement of a part gives the same mesh, and that mesh moved is the + /// placement's own. + #[test] + fn placements_share_their_definitions_meshes_in_place() { + use crate::display_quality::DisplayQuality; + let source = ModelBody::new("Part".into(), kernel::cylinder(Vec3::ZERO, Vec3::Z, 2., 8.).unwrap(), 1); + let placed: Vec = + [crate::rigid(Vec3::new(10., 20., 30.), Vec3::ZERO, Vec3::Z, 0.7), crate::rigid(Vec3::new(-4., 5., 1.), Vec3::ZERO, Vec3::X, 1.1)] + .iter() + .map(|m| { + let mut p = ModelBody::new("Copy".into(), kernel::transform_matrix(&source.body, *m).unwrap(), 1); + p.mesh_placement = Some(Arc::new(PlacedMesh::new(&source, m).unwrap())); + p + }) + .collect(); + let (own, identity) = source.mesh_in_place(); + assert_eq!(identity, crate::IDENTITY); + let q = DisplayQuality::parse("normal").unwrap(); + for p in &placed { + let (mesh, m) = p.mesh_in_place(); + assert!(Arc::ptr_eq(&mesh, &own), "the definition's mesh, not a copy"); + let bounds = p.mesh_bounds(); + assert!(p.cached_mesh().is_none(), "no moved copy is made"); + let (plan_mesh, pm) = p.plan_mesh_in_place_at(q).unwrap(); + assert!(Arc::ptr_eq(&plan_mesh, &source.plan_mesh_at(q).unwrap())); + assert_eq!(pm, m); + let moved = p.mesh(); + let exact = moved.bounds(); + assert!(bounds.min.dist(exact.min) < 1e-9 && bounds.max.dist(exact.max) < 1e-9, "{bounds:?} vs {exact:?}"); + for (a, b) in mesh.positions.iter().zip(&moved.positions) { + assert!(crate::apply_point(&m, *a).dist(*b) < 1e-9); + } + assert!(!Arc::ptr_eq(&p.display_identity(), &source.display_identity()), "each placement is its own body"); + assert!(Arc::ptr_eq(&p.display_identity(), &p.clone().display_identity()), "clones are the same body"); + } + } + /// Placed parts share their definition's display plan (#3044): the definition is planned /// once, every placement moves that plan, and a moved plan evaluates to the definition's /// points moved, on every kind of surface (planes, revolved and spline surfaces). diff --git a/crates/engine/src/view.rs b/crates/engine/src/view.rs index 64863cc01..c45f30fd8 100644 --- a/crates/engine/src/view.rs +++ b/crates/engine/src/view.rs @@ -110,7 +110,7 @@ pub fn bounds(s: &Session) -> Aabb3 { /// [`bounds`], with the browser's Sketches folder shown (`sketches`) or hidden. pub fn visible_bounds(s: &Session, sketches: bool) -> Aabb3 { - bounds_by(s, sketches, |body| body.mesh().bounds()) + bounds_by(s, sketches, |body| body.mesh_bounds()) } /// [`visible_bounds`] without meshing anything: a body not meshed yet counts with its outline diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rs index c1b162c74..72a757301 100644 --- a/crates/gpu/src/view.rs +++ b/crates/gpu/src/view.rs @@ -21,6 +21,9 @@ pub const TRI_SIZE: usize = 28; pub const PBR_SIZE: usize = pbr::VERTEX_SIZE; /// Line instance: a, b (3 × f32 each), colour (4 × u8), width in pixels (f32). pub const LINE_SIZE: usize = 32; +/// Placement of an [`InstancedMesh`]: the rows of its rigid move (3 × 4 f32; a point's x, y and z +/// are each row's dot product with (p, 1)), then its colour (4 × u8). +pub const INSTANCE_SIZE: usize = 52; /// The shader's `U`: the view-projection matrix, 10 `vec4`s (the tenth carries the ambient /// occlusion radius), the 8 analysis colours, their flags and the depth direction /// (`uniform_bytes`). @@ -34,6 +37,154 @@ pub struct PlannedBody { pub face_colors: Vec<[u8; 4]>, } +/// A vertex of [`SharedTris`]: position and normal (3 × f32 each). +pub const SHARED_SIZE: usize = 24; + +/// A mesh's triangles as an [`InstancedMesh`] draws them: each vertex once, shared by the +/// triangles that meet there, and the triangles as indices into them, so the GPU transforms each +/// vertex once rather than once per triangle corner. +#[derive(Default)] +pub struct SharedTris { + /// [`SHARED_SIZE`] bytes per vertex, in the part's own frame. + pub vertices: Vec, + /// Three per triangle. + pub indices: Vec, + /// Every triangle is flat (its corners share one normal, as an STL's facets do): vertices + /// are shared by position alone, and each fragment takes its triangle's own normal. + pub flat: bool, + /// The box around the vertices (min, max), in the part's own frame: a placement whose box + /// is out of the view is not drawn. + pub bounds: [[f32; 3]; 2], +} + +impl SharedTris { + /// `mesh`'s triangles with their vertices shared: by position and normal, or by position + /// alone when every triangle is flat. + pub fn new(mesh: &solidcraft_geom::Mesh) -> SharedTris { + let corner = |i: u32| Some((mesh.positions.get(i as usize)?.to_f32(), mesh.normals.get(i as usize)?.to_f32())); + let bits = |v: [f32; 3]| v.map(|x| (x + 0.0).to_bits()); + let flat = mesh.triangles.iter().all(|t| { + let [a, b, c] = t.map(|i| corner(i).map(|(_, n)| bits(n))); + a == b && b == c + }); + let mut out = SharedTris { flat, ..Default::default() }; + out.indices.reserve_exact(mesh.triangles.len() * 3); + let mut seen: std::collections::HashMap<[u32; 6], u32> = std::collections::HashMap::with_capacity(mesh.positions.len()); + for t in &mesh.triangles { + let Some(corners) = t.iter().map(|i| corner(*i)).collect::>>() else { continue }; + for (p, n) in corners { + let [x, y, z] = bits(p); + let [a, b, c] = if flat { [0; 3] } else { bits(n) }; + let next = u32::try_from(out.vertices.len() / SHARED_SIZE).unwrap_or(u32::MAX); + let index = *seen.entry([x, y, z, a, b, c]).or_insert_with(|| { + for v in p.iter().chain(&n) { + out.vertices.extend_from_slice(&v.to_le_bytes()); + } + next + }); + out.indices.push(index); + } + } + out.vertices.shrink_to_fit(); + let mut lo = [f32::INFINITY; 3]; + let mut hi = [f32::NEG_INFINITY; 3]; + for v in 0..out.vertices.len() / SHARED_SIZE { + let (p, _) = out.vertex(v as u32); + for k in 0..3 { + lo[k] = lo[k].min(p[k]); + hi[k] = hi[k].max(p[k]); + } + } + 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; + let f = |k: usize| self.vertices.get(at + 4 * k..at + 4 * k + 4).and_then(|x| x.try_into().ok()).map_or(0.0, f32::from_le_bytes); + ([f(0), f(1), f(2)], [f(3), f(4), f(5)]) + } + + /// Each triangle's corners (position, and the normal they are lit with: a flat triangle's + /// own), in the part's frame. + pub fn corners(&self) -> impl Iterator + '_ { + self.indices.as_chunks::<3>().0.iter().map(move |t| { + let c = t.map(|i| self.vertex(i)); + if !self.flat { + return c; + } + let [a, b, d] = c.map(|(p, _)| p); + let (u, v) = ([b[0] - a[0], b[1] - a[1], b[2] - a[2]], [d[0] - a[0], d[1] - a[1], d[2] - a[2]]); + let n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]; + let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt(); + let n = if len > 0.0 { n.map(|x| x / len) } else { [0.0; 3] }; + c.map(|(p, _)| (p, n)) + }) + } + + /// The triangles unshared ([`SHARED_SIZE`] bytes per corner, three per triangle): for a mesh + /// too large for one buffer. + fn unshared(&self) -> Vec { + let mut out = Vec::with_capacity(self.indices.len() * SHARED_SIZE); + for t in self.corners() { + for (p, n) in t { + for v in p.iter().chain(&n) { + out.extend_from_slice(&v.to_le_bytes()); + } + } + } + out + } +} + +/// 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)] +pub struct InstancedMesh { + /// Names exactly these triangles: while it comes back with the same triangles, their buffers + /// are kept and only the placements are uploaded again. + pub key: u64, + /// The opaque triangles, in the part's own frame. + pub tris: Arc, + /// The placements, [`INSTANCE_SIZE`] bytes each ([`InstancedMesh::place`]). + pub instances: Vec, +} + +impl InstancedMesh { + /// Add a placement: `rows` of its rigid move (see [`INSTANCE_SIZE`]) and its colour. + pub fn place(&mut self, rows: [[f32; 4]; 3], color: [u8; 4]) { + for v in rows.iter().flatten() { + self.instances.extend_from_slice(&v.to_le_bytes()); + } + self.instances.extend_from_slice(&color); + } + + /// Its triangles at every placement, as the GPU draws them, appended to `out` (laid out as + /// [`GpuScene::tris`]). + pub fn expand_into(&self, out: &mut Vec) { + 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 inst in self.instances.as_chunks::().0 { + let row = |r: usize| [f(inst, 16 * r), f(inst, 16 * r + 4), f(inst, 16 * r + 8), f(inst, 16 * r + 12)]; + let rows = [row(0), row(1), row(2)]; + let color = inst.get(48..52).unwrap_or(&[0; 4]); + for (p, n) in self.tris.corners().flatten() { + let moved = rows.map(|r| r[0] * p[0] + r[1] * p[1] + r[2] * p[2] + r[3]); + let turned = rows.map(|r| r[0] * n[0] + r[1] * n[1] + r[2] * n[2]); + for x in moved.iter().chain(&turned) { + out.extend_from_slice(&x.to_le_bytes()); + } + out.extend_from_slice(color); + } + } + } + + /// How many placements it is drawn at. + pub fn count(&self) -> usize { + self.instances.len() / INSTANCE_SIZE + } +} + /// CPU-side geometry waiting to be uploaded. The model scene changes with the design; the /// highlight scene (hover, selection, the origin widget) changes often and is small. #[derive(Default, Clone)] @@ -68,6 +219,8 @@ pub struct GpuScene { pub guide_lines: Vec, /// Opaque bodies drawn from their plans on the GPU, like `tris`. pub planned: Vec, + /// Opaque bodies drawn as placements of shared triangles, like `tris`. + pub instanced: Vec, /// The model's bodies, a few at a time, drawn before the loose geometry above (`tris`, /// `trans`, `lines` and `hidden` of each block, in order, then the scene's own). A block /// that comes back unchanged keeps its buffers: a design changing one body at a time (an @@ -86,6 +239,9 @@ impl GpuScene { out.lines.extend_from_slice(&block.bytes.lines); out.hidden.extend_from_slice(&block.bytes.hidden); } + for m in std::mem::take(&mut self.instanced) { + m.expand_into(&mut out.tris); + } out.tris.append(&mut self.tris); out.trans.append(&mut self.trans); out.lines.append(&mut self.lines); @@ -380,6 +536,98 @@ struct PlannedDraw { colors: wgpu::Buffer, } +/// An [`InstancedMesh`]'s triangles on the GPU (kept while its key comes back). +enum SharedGpu { + /// Shared vertices and their indices. + Indexed { vertices: wgpu::Buffer, indices: wgpu::Buffer, count: u32 }, + /// Too large for one buffer: unshared, in as many buffers as it takes. + Unshared(Batch), +} + +impl SharedGpu { + fn new(device: &wgpu::Device, tris: &SharedTris) -> Option { + let limit = device.limits().max_buffer_size; + let index_bytes = (tris.indices.len() * 4) as u64; + if tris.vertices.is_empty() || tris.indices.is_empty() { + return None; + } + if tris.vertices.len() as u64 > limit || index_bytes > limit { + return upload(device, "sc_instanced_unshared", &tris.unshared(), SHARED_SIZE).map(SharedGpu::Unshared); + } + let buffer = |label: &str, contents: &[u8], usage: wgpu::BufferUsages| { + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some(label), contents, usage }) + }; + let indices: Vec = tris.indices.iter().flat_map(|i| i.to_le_bytes()).collect(); + Some(SharedGpu::Indexed { + vertices: buffer("sc_instanced_vertices", &tris.vertices, wgpu::BufferUsages::VERTEX), + indices: buffer("sc_instanced_indices", &indices, wgpu::BufferUsages::INDEX), + count: u32::try_from(tris.indices.len()).unwrap_or(u32::MAX), + }) + } +} + +/// 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, +/// before the near plane or past the far one). Tested on the clip-space corners, so it holds for +/// corners behind the eye too. +pub fn placed_box_in_view(rows: &[[f32; 4]; 3], bounds: &[[f32; 3]; 2], vp: &[[f32; 4]; 4]) -> bool { + let [lo, hi] = bounds; + if !lo.iter().chain(hi).all(|x| x.is_finite()) { + return true; + } + // Per plane, whether every corner so far is outside it: x > w, -x > w, y > w, -y > w, z < 0, z > w. + let mut out = [true; 6]; + for i in 0..8 { + let p = [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 w = rows.map(|r| r[0] * p[0] + r[1] * p[1] + r[2] * p[2] + r[3]); + let c: [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]); + let outside = [c[0] > c[3], -c[0] > c[3], c[1] > c[3], -c[1] > c[3], c[2] < 0.0, c[2] > c[3]]; + for (o, x) in out.iter_mut().zip(outside) { + *o &= x; + } + } + !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 { + tris: Arc, + flat: bool, + 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>, +} + +impl InstancedDraw { + /// Draw every placement with `pipeline` (the placements in vertex buffer 1). + fn draw(&self, pass: &mut wgpu::RenderPass<'_>) { + if self.count == 0 { + return; + } + pass.set_vertex_buffer(1, self.instances.slice(..)); + match &*self.tris { + SharedGpu::Indexed { vertices, indices, count } => { + pass.set_vertex_buffer(0, vertices.slice(..)); + pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32); + pass.draw_indexed(0..*count, 0, 0..self.count); + } + SharedGpu::Unshared(batch) => { + for (buffer, count) in &batch.parts { + pass.set_vertex_buffer(0, buffer.slice(..)); + pass.draw(0..*count, 0..self.count); + } + } + } + } +} + /// A scene block on the GPU, with the bytes it was uploaded from. struct BlockGpu { bytes: Arc, @@ -425,6 +673,10 @@ struct Batches { guide_trans: Option, 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)>, /// 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)>, @@ -442,6 +694,7 @@ impl Batches { Some((tess, encoder)) => self.planned(device, tess, encoder, &sc.planned), None => Vec::new(), }; + self.instanced = self.instanced(device, &sc.instanced); let mut kept = std::mem::take(&mut self.blocks); let mut blocks: Vec<&BlockGpu> = Vec::with_capacity(sc.blocks.len()); for block in &sc.blocks { @@ -480,6 +733,70 @@ impl Batches { } } + /// The instanced meshes on the GPU: triangles uploaded once per key (kept while the key comes + /// back with the same bytes), placements every time. + fn instanced(&mut self, device: &wgpu::Device, meshes: &[InstancedMesh]) -> Vec { + let mut kept = std::mem::take(&mut self.instanced_tris); + let mut out = Vec::with_capacity(meshes.len()); + for m in meshes { + let count = u32::try_from(m.count()).unwrap_or(u32::MAX); + 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); + } + out + } + + /// 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]) { + 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 + .placements + .as_chunks::() + .0 + .iter() + .enumerate() + .filter(|(_, 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) + }) + .map(|(i, _)| i as u32) + .collect(); + 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); + } + d.count = u32::try_from(shown.len()).unwrap_or(u32::MAX); + d.shown = Some(shown); + } + } + /// The planned bodies on the GPU: in chunks of at most `MAX_BATCH` vertices (one for any /// ordinary scene), each uploaded and evaluated in one dispatch and drawn in one call. A /// chunk whose plans are unchanged is not evaluated again; colours are rebuilt each time. @@ -555,6 +872,13 @@ pub struct Renderer { /// colours per vertex, indexed). tri_planned: wgpu::RenderPipeline, tri_cut_planned: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>, + /// `tri` and `tri_cut` for instanced meshes (shared triangles, a move and colour per + /// placement). + tri_instanced: wgpu::RenderPipeline, + tri_cut_instanced: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>, + /// `tri_instanced` and `tri_cut_instanced` for flat triangles. + tri_instanced_flat: wgpu::RenderPipeline, + tri_cut_instanced_flat: Option<(wgpu::RenderPipeline, wgpu::RenderPipeline)>, /// The tessellation compute pipeline, where the device runs compute shaders. tess: Option, /// While a section is shown (when the depth buffer has a stencil): `tri` marking where the @@ -741,6 +1065,50 @@ fn planned_layout() -> wgpu::VertexBufferLayout<'static> { wgpu::VertexBufferLayout { array_stride: crate::VERTEX_STRIDE, step_mode: wgpu::VertexStepMode::Vertex, attributes: &A } } +/// An instanced mesh's vertices ([`SHARED_SIZE`]). +fn shared_layout() -> wgpu::VertexBufferLayout<'static> { + const A: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3]; + wgpu::VertexBufferLayout { array_stride: SHARED_SIZE as u64, step_mode: wgpu::VertexStepMode::Vertex, attributes: &A } +} + +/// An instanced mesh's placements ([`INSTANCE_SIZE`]), one per instance. +fn instance_layout() -> wgpu::VertexBufferLayout<'static> { + const A: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![3 => Float32x4, 4 => Float32x4, 5 => Float32x4, 6 => Unorm8x4]; + wgpu::VertexBufferLayout { array_stride: INSTANCE_SIZE as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &A } +} + +/// Where an opaque triangle pipeline reads its vertices from. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum TriSource { + /// Built on the CPU ([`tri_layout`]). + Built, + /// Evaluated from display plans on the GPU ([`planned_layout`], [`color_layout`]), indexed. + Planned, + /// Shared vertices ([`shared_layout`]) drawn at each placement ([`instance_layout`]). + Instanced, + /// [`TriSource::Instanced`] for flat triangles: each fragment takes its triangle's normal. + InstancedFlat, +} + +impl TriSource { + /// The vertex shader entry point. + fn entry(self) -> &'static str { + match self { + TriSource::Instanced => "vs_tri_inst", + TriSource::InstancedFlat => "vs_tri_inst_flat", + TriSource::Built | TriSource::Planned => "vs_tri", + } + } + + /// The fragment shader entry point standing for `fs` (written for [`TriSource::Built`]). + fn fragment(self, fs: &str) -> String { + match self { + TriSource::InstancedFlat => format!("{fs}_flat"), + _ => fs.to_string(), + } + } +} + /// A planned body's colours, one per vertex. fn color_layout() -> wgpu::VertexBufferLayout<'static> { const A: [wgpu::VertexAttribute; 1] = wgpu::vertex_attr_array![2 => Unorm8x4]; @@ -976,14 +1344,20 @@ impl Renderer { }); let built = [Some(tri_layout())]; let evaluated = [Some(planned_layout()), Some(color_layout())]; - let solid = |label: &str, fs: &str, cull: Option, ds: Option, planned: bool| { + let placed = [Some(shared_layout()), Some(instance_layout())]; + let solid = |label: &str, fs: &str, cull: Option, ds: Option, source: TriSource| { + let fs = source.fragment(fs); device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(label), layout: Some(&solid_layout), vertex: wgpu::VertexState { module: &module, - entry_point: Some("vs_tri"), - buffers: if planned { &evaluated[..] } else { &built[..] }, + entry_point: Some(source.entry()), + buffers: match source { + TriSource::Built => &built[..], + TriSource::Planned => &evaluated[..], + TriSource::Instanced | TriSource::InstancedFlat => &placed[..], + }, compilation_options: Default::default(), }, primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, cull_mode: cull, ..Default::default() }, @@ -991,7 +1365,7 @@ impl Renderer { multisample: wgpu::MultisampleState { count: t.samples.max(1), mask: !0, alpha_to_coverage_enabled: false }, fragment: Some(wgpu::FragmentState { module: &module, - entry_point: Some(fs), + entry_point: Some(&fs), targets: &[Some(wgpu::ColorTargetState { format: t.format, blend: None, write_mask: wgpu::ColorWrites::ALL })], compilation_options: Default::default(), }), @@ -1000,7 +1374,7 @@ impl Renderer { }) }; // While a section is shown: the stencil marking and the cap (see `Renderer::tri_cut`). - let cut = |planned: bool| { + let cut = |source: TriSource| { t.depth.filter(|f| f.has_stencil_aspect()).map(|format| { // The visible surface marks the stencil: 1 where it is a back face (the cap). use wgpu::StencilOperation::{Keep, Replace, Zero}; @@ -1022,8 +1396,8 @@ impl Renderer { bias: Default::default(), }; ( - solid("sc_tris_mark", "fs_solid", None, Some(mark), planned), - solid("sc_tris_cap", "fs_solid_cut", Some(wgpu::Face::Front), Some(cap), planned), + solid("sc_tris_mark", "fs_solid", None, Some(mark), source), + solid("sc_tris_cap", "fs_solid_cut", Some(wgpu::Face::Front), Some(cap), source), ) }) }; @@ -1131,10 +1505,20 @@ impl Renderer { sampler, textures: Default::default(), quads: Vec::new(), - tri: solid("sc_tris", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), false), - tri_cut: cut(false), - tri_planned: solid("sc_tris_planned", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), true), - tri_cut_planned: cut(true), + tri: solid("sc_tris", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), TriSource::Built), + tri_cut: cut(TriSource::Built), + tri_planned: solid("sc_tris_planned", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), TriSource::Planned), + tri_cut_planned: cut(TriSource::Planned), + tri_instanced: solid("sc_tris_instanced", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), TriSource::Instanced), + tri_cut_instanced: cut(TriSource::Instanced), + tri_instanced_flat: solid( + "sc_tris_instanced_flat", + "fs_solid", + None, + pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), + TriSource::InstancedFlat, + ), + tri_cut_instanced_flat: cut(TriSource::InstancedFlat), tess, access_bgl, access_bind, @@ -1309,6 +1693,7 @@ impl Renderer { 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); 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 @@ -1441,21 +1826,36 @@ impl Renderer { pass.draw_indexed(0..d.mesh.index_count, 0, 0..1); } }; + // 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); + } + }; let sectioned = cb.clip[..3].iter().map(|c| c * c).sum::() > 0.25; - match (res.tri_cut.as_ref().filter(|_| sectioned), res.tri_cut_planned.as_ref()) { - (Some((mark, cap)), Some((mark_planned, cap_planned))) => { + match ( + res.tri_cut.as_ref().filter(|_| sectioned), + res.tri_cut_planned.as_ref(), + res.tri_cut_instanced.as_ref(), + res.tri_cut_instanced_flat.as_ref(), + ) { + (Some((mark, cap)), Some((mark_planned, cap_planned)), Some((mark_instanced, cap_instanced)), Some((mark_flat, cap_flat))) => { pass.set_stencil_reference(1); tris(pass, mark, &m.tris); tris(pass, mark, &pv.tris); planned(pass, mark_planned); + instanced(pass, mark_instanced, mark_flat); tris(pass, cap, &m.tris); tris(pass, cap, &pv.tris); planned(pass, cap_planned); + instanced(pass, cap_instanced, cap_flat); } _ => { tris(pass, &res.tri, &m.tris); tris(pass, &res.tri, &pv.tris); planned(pass, &res.tri_planned); + instanced(pass, &res.tri_instanced, &res.tri_instanced_flat); } } tris(pass, &res.pbr, &m.pbr); @@ -1609,6 +2009,72 @@ pub fn analysis_colors(c: &solidcraft_render::AnalysisColors) -> [f32; 36] { #[cfg(test)] mod tests { + /// A placed box is kept while any of it may be in view, and left out only when it lies + /// wholly past one side of the view: in an orthographic view and a perspective one, behind + /// the eye too. + #[test] + fn placed_boxes_out_of_view_are_left_out() { + use super::placed_box_in_view; + 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: x and y from -10 to 10 on screen, depth z from 0 (near) to 1 (far) over + // -100..100 (columns). + 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]]; + assert!(placed_box_in_view(&at(0.0, 0.0, 0.0), &unit, &ortho)); + assert!(placed_box_in_view(&at(9.5, 9.5, 0.0), &unit, &ortho), "partly in view"); + for (x, y, z) in [(11.0, 0.0, 0.0), (-12.0, 0.0, 0.0), (0.0, 11.0, 0.0), (0.0, -12.0, 0.0), (0.0, 0.0, -120.0), (0.0, 0.0, 120.0)] { + assert!(!placed_box_in_view(&at(x, y, z), &unit, &ortho), "{x} {y} {z}"); + } + // Turned a quarter about z, the box at x 10.5 reaches back into view (x from 9.5). + let turned = [[0.0, -1.0, 0.0, 10.5], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; + assert!(placed_box_in_view(&turned, &unit, &ortho)); + // Perspective, looking down -z from the origin (w = -z, 90° field of view, near 1). + 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]]; + assert!(placed_box_in_view(&at(-0.5, -0.5, -10.0), &unit, &persp)); + assert!(!placed_box_in_view(&at(-0.5, -0.5, 10.0), &unit, &persp), "behind the eye"); + assert!(!placed_box_in_view(&at(30.0, -0.5, -10.0), &unit, &persp), "beside the view"); + assert!(placed_box_in_view(&at(-0.5, -0.5, -20.0), &[[0.0, 0.0, 0.0], [1.0, 1.0, 21.0]], &persp), "from in front of the eye to behind it"); + // A box that is not finite is drawn (never culled by mistake). + assert!(placed_box_in_view(&at(30.0, 0.0, -10.0), &[[f32::NAN; 3], [1.0; 3]], &persp)); + } + + /// 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. + #[test] + fn shared_tris_share_each_vertex_once() { + use super::{SHARED_SIZE, SharedTris}; + use solidcraft_geom::{Mesh, Vec3}; + let p = [Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 1.0, 0.0), Vec3::new(0.0, 1.0, 0.0)]; + // Two triangles of a square, each corner its own vertex (as an STL is read). + let mut flat = Mesh::default(); + for t in [[0, 1, 2], [0, 2, 3]] { + let base = flat.positions.len() as u32; + flat.positions.extend(t.map(|i| p[i])); + flat.normals.extend([Vec3::Z; 3]); + flat.triangles.push([base, base + 1, base + 2]); + } + let shared = SharedTris::new(&flat); + assert!(shared.flat); + assert_eq!(shared.vertices.len() / SHARED_SIZE, 4, "the diagonal's ends are shared"); + assert_eq!(shared.indices.len(), 6); + let corners: Vec<_> = shared.corners().flatten().collect(); + for (k, (q, n)) in corners.iter().enumerate() { + assert_eq!(*q, flat.positions[k].to_f32()); + assert!((n[2] - 1.0).abs() < 1e-6, "{n:?}"); + } + // Smooth: the same corners with different normals stay apart. + let mut smooth = flat.clone(); + smooth.normals = vec![Vec3::Z, Vec3::Z, Vec3::new(0.0, 0.6, 0.8), Vec3::Z, Vec3::X, Vec3::new(0.0, 0.6, 0.8)]; + let shared = SharedTris::new(&smooth); + assert!(!shared.flat); + assert_eq!(shared.vertices.len() / SHARED_SIZE, 5, "corner 0 differs in normal only"); + let back: Vec<_> = shared.corners().flatten().map(|(_, n)| n).collect(); + assert_eq!(back, smooth.normals.iter().map(|n| n.to_f32()).collect::>()); + // Indices past the vertices: the triangle is left out, nothing panics. + smooth.triangles.push([0, 1, 99]); + assert_eq!(SharedTris::new(&smooth).indices.len(), 6); + } /// Only the FirePro under Metal skips GPU evaluation; the same card under another backend /// and other Metal GPUs keep it. diff --git a/crates/gpu/src/view/ao.rs b/crates/gpu/src/view/ao.rs index 9381a20a2..ff876ad05 100644 --- a/crates/gpu/src/view/ao.rs +++ b/crates/gpu/src/view/ao.rs @@ -6,7 +6,7 @@ //! bits for portable integer texel reads. GLSL cannot load from a depth texture, and float //! colour attachments are not available on every GL adapter. -use super::{Batches, color_layout, depth_compare, far_depth, planned_layout, tri_layout}; +use super::{Batches, TriSource, color_layout, depth_compare, far_depth, instance_layout, planned_layout, shared_layout, tri_layout}; pub(super) struct Resources { pub layout: wgpu::BindGroupLayout, @@ -16,6 +16,8 @@ pub(super) struct Resources { size: [u32; 2], pipeline: wgpu::RenderPipeline, planned_pipeline: wgpu::RenderPipeline, + instanced_pipeline: wgpu::RenderPipeline, + instanced_flat_pipeline: wgpu::RenderPipeline, } fn texture(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, size: [u32; 2]) -> (wgpu::TextureView, wgpu::TextureView, wgpu::BindGroup) { @@ -64,14 +66,19 @@ impl Resources { }); let built = [Some(tri_layout())]; let evaluated = [Some(planned_layout()), Some(color_layout())]; - let pipeline = |planned: bool| { + let placed = [Some(shared_layout()), Some(instance_layout())]; + let pipeline = |source: TriSource| { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("sc_ao_prepass"), layout: Some(&pipeline_layout), vertex: wgpu::VertexState { module, - entry_point: Some("vs_tri"), - buffers: if planned { &evaluated } else { &built }, + entry_point: Some(source.entry()), + buffers: match source { + TriSource::Built => &built[..], + TriSource::Planned => &evaluated[..], + TriSource::Instanced | TriSource::InstancedFlat => &placed[..], + }, compilation_options: Default::default(), }, primitive: wgpu::PrimitiveState::default(), @@ -85,7 +92,7 @@ impl Resources { multisample: wgpu::MultisampleState::default(), fragment: Some(wgpu::FragmentState { module, - entry_point: Some("fs_ao_depth"), + entry_point: Some(&source.fragment("fs_ao_depth")), targets: &[Some(wgpu::ColorTargetState { format: wgpu::TextureFormat::R32Uint, blend: None, @@ -97,10 +104,12 @@ impl Resources { cache: None, }) }; - let planned_pipeline = pipeline(true); - let pipeline = pipeline(false); + let planned_pipeline = pipeline(TriSource::Planned); + let instanced_pipeline = pipeline(TriSource::Instanced); + let instanced_flat_pipeline = pipeline(TriSource::InstancedFlat); + let pipeline = pipeline(TriSource::Built); let (depth, values, bind) = texture(device, &layout, [1, 1]); - Self { layout, bind, depth, values, size: [1, 1], pipeline, planned_pipeline } + Self { layout, bind, depth, values, size: [1, 1], pipeline, planned_pipeline, instanced_pipeline, instanced_flat_pipeline } } pub fn prepare( @@ -159,6 +168,10 @@ 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); + } } } } diff --git a/crates/ui-egui/src/display_lod.rs b/crates/ui-egui/src/display_lod.rs index e62bf5c04..2d39bdae2 100644 --- a/crates/ui-egui/src/display_lod.rs +++ b/crates/ui-egui/src/display_lod.rs @@ -41,11 +41,16 @@ pub(crate) fn enabled() -> bool { *ON.get_or_init(|| std::env::var("SOLIDCRAFT_DISPLAY_REFINE").map_or(true, |v| v != "0")) } -/// A body's identity here: the address of its reference mesh, which is shared by every copy of -/// the same body across timeline snapshots and held by the cache so the address stays its own. -pub(crate) fn body_id(b: &ModelBody) -> (usize, Arc) { - let m = b.mesh(); - (Arc::as_ptr(&m) as usize, m) +/// What keeps a body's identity ([`body_id`]) its own while the cache holds it. +pub(crate) type Holder = Arc; + +/// A body's identity here: the address of its [`ModelBody::display_identity`], which is shared by +/// every copy of the same body across timeline snapshots and held by the cache so the address +/// stays its own. Taking it meshes nothing: a part placed many times is drawn without making its +/// moved copies. +pub(crate) fn body_id(b: &ModelBody) -> (usize, Holder) { + let holder = b.display_identity(); + (Arc::as_ptr(&holder) as *const () as usize, holder) } /// How the camera maps millimetres to pixels. @@ -118,7 +123,7 @@ pub(crate) struct Candidate<'a> { struct Entry { /// Keeps the body's id address its own while the entry lives. - _holder: Arc, + _holder: Holder, refined: Refined, bytes: usize, used: u64, @@ -127,7 +132,7 @@ struct Entry { #[cfg(not(target_arch = "wasm32"))] struct Job { key: (usize, DisplayQuality, u8), - holder: Arc, + holder: Holder, rx: std::sync::mpsc::Receiver, } @@ -264,7 +269,7 @@ impl Lod { } } - fn insert(&mut self, key: (usize, DisplayQuality, u8), holder: Arc, refined: Refined) { + fn insert(&mut self, key: (usize, DisplayQuality, u8), holder: Holder, refined: Refined) { let bytes = refined.as_ref().map_or(0, |(plan, mesh)| mesh_bytes(mesh) + plan.vertex_count() * 48 + mesh.triangles.len() * 12); self.cache.insert(key, Entry { _holder: holder, refined, bytes, used: self.clock }); } @@ -464,8 +469,8 @@ mod tests { .map(|[a, b, c]| 10.0 - r((a + b + c) * (1.0 / 3.0))) .fold(0.0, f64::max); assert!(sag <= q.refined_chord(b.body.size(), 3) * 1.01, "sag {sag}"); - // The reference mesh never changes. - assert!(Arc::ptr_eq(&body_id(&b).1, &b.mesh())); + // Refining never makes (or replaces) the reference mesh. + assert!(b.cached_mesh().is_none()); // Zoomed out a little: kept; far out: the chosen level. assert!(!lod.update(q, cand(3.0), &live)); assert_eq!(lod.step(&b), 3); diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 4179ebedf..f638f70d6 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, LINE_SIZE, PBR_SIZE, PlannedBody, Renderer, SceneBlock, SceneSlot, TRI_SIZE, - supersampled_size, + AccessMap, BlockBytes, Frame, GpuImage, GpuScene, GpuTarget, InstancedMesh, LINE_SIZE, PBR_SIZE, PlannedBody, Renderer, SHARED_SIZE, SceneBlock, + SceneSlot, SharedTris, TRI_SIZE, supersampled_size, }; /// A frame of the viewport as an egui paint callback. diff --git a/crates/ui-egui/src/open_meshing.rs b/crates/ui-egui/src/open_meshing.rs index 088929e9f..7428e81df 100644 --- a/crates/ui-egui/src/open_meshing.rs +++ b/crates/ui-egui/src/open_meshing.rs @@ -4,7 +4,7 @@ //! file) and placed; the display meshes are made afterwards, off the UI thread. Until they all //! are, the viewport draws the bodies already meshed ([`shown_world`]), so the first parts show //! within moments and the window stays responsive while the rest come in. The first placement of -//! every part is meshed first: its other placements only move that mesh. The view is framed from +//! every part is meshed first: its other placements are drawn from that mesh. The view is framed from //! the bodies' outlines meanwhile, and framed again from the meshes when they are all made, unless //! the camera was moved in between. Editing the design meanwhile meshes the new world instead. //! @@ -71,10 +71,12 @@ impl Meshing { return; } if let Some(b) = world.bodies.get(i) { - let _ = b.mesh(); + // What the viewport draws and picks: a placed part's shared mesh, not a + // moved copy of it (its placements are drawn as instances of it). + let _ = b.mesh_in_place(); if plans { let _ = b.display_plan(); - let _ = b.plan_mesh(); + let _ = b.plan_mesh_in_place_at(solidcraft_engine::doc::display_quality::current()); } } if let Ok(mut r) = ready.lock() { diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index b754118bd..cc1024949 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -63,13 +63,16 @@ pub struct ViewportState { pick_bounds: std::sync::Mutex>, /// The model scene's blocks of bodies by key, from the last scene built. pub(crate) blocks: std::sync::Mutex>, + /// 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, /// 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, /// The view and model the refinement was last chosen for. lod_view: u64, /// Each body mesh's box (the mesh held, so its address stays its own), from the last state. - mesh_bounds: std::sync::Mutex, solidcraft_engine::geom::Aabb3)>>, + mesh_bounds: std::sync::Mutex, /// Each shown body mesh's section index for the cut's normal (the mesh held, so its address /// stays its own): a Section Analysis dragged along its normal cuts through it. section_index: std::sync::Mutex, std::sync::Arc)>>, @@ -535,7 +538,7 @@ fn update_refinement(app: &mut SolveApp, ctx: &egui::Context, proj: &Proj) { None => (shown && proj.cam.fov > 0.0 && lens.in_front(bx)).then_some(f64::INFINITY), }; let err_px = crate::display_lod::error_px(q, b.body.size(), bx, on_screen, &lens); - candidates.push(crate::display_lod::Candidate { body: b, err_px, triangles: b.mesh().triangles.len() }); + candidates.push(crate::display_lod::Candidate { body: b, err_px, triangles: b.mesh_in_place().0.triangles.len() }); } let busy = match app.viewport.lod.lock() { Ok(mut lod) => { @@ -557,14 +560,31 @@ pub(crate) struct CachedBlock { meshes: Vec>, } +/// [`ViewportState::mesh_bounds`]: each box by the shared mesh it was measured on (held, so its +/// address stays its own) and the placement that moved it ([`ModelBody::mesh_in_place`]). +/// +/// [`ModelBody::mesh_in_place`]: solidcraft_engine::doc::ModelBody::mesh_in_place +type MeshBounds = std::collections::HashMap<(usize, [u64; 16]), (std::sync::Arc, solidcraft_engine::geom::Aabb3)>; + +/// [`ViewportState::instanced_tris`]. +type InstancedTris = std::collections::HashMap, std::sync::Arc)>; + +/// A body alone is drawn as an instance of its triangles (shared vertices, indexed) from this many +/// triangles: a large imported mesh. Smaller ones are drawn so only when placed more than once. +const INSTANCE_ALONE: usize = 50_000; + /// One body as the model scene draws it. struct BodyDraw { + /// Its mesh where it stands, or (with `place`) the shared mesh `place` moves there. mesh: std::sync::Arc, + /// The move from `mesh` to where the body stands, for a body drawn as an instance. + place: Option, col: [u8; 4], faded: bool, opacity: f64, face_looks: std::collections::HashMap, - /// Its faces are drawn from its plan on the GPU (only its edges are built here). + /// Its faces are drawn elsewhere, from its plan on the GPU or as an instance of shared + /// triangles (only its edges are built here). on_gpu: bool, } @@ -573,6 +593,7 @@ impl BodyDraw { fn key(&self, shaded: bool, edges: bool, hidden_edges: bool, edge_col: [u8; 4], hidden_col: [u8; 4]) -> u64 { let mut h = std::collections::hash_map::DefaultHasher::new(); (std::sync::Arc::as_ptr(&self.mesh) as usize).hash(&mut h); + self.place.iter().flatten().flatten().for_each(|v| v.to_bits().hash(&mut h)); (self.col, self.faded, self.opacity.to_bits(), self.on_gpu, shaded, edges, hidden_edges, edge_col, hidden_col).hash(&mut h); let mut looks: Vec<_> = self.face_looks.iter().map(|(f, l)| (*f, l.color, l.opacity.to_bits())).collect(); looks.sort_unstable(); @@ -608,14 +629,16 @@ impl BodyDraw { } } } + let at = |p: Vec3| self.place.as_ref().map_or(p, |m| solidcraft_engine::doc::apply_point(m, p)).to_f32(); for (ei, e) in m.edges.iter().enumerate().filter(|_| edges) { if !m.edge_pickable(ei) || (shaded && !m.shaded_edge_visible(ei)) { continue; } for w in e.windows(2) { - out.line(w[0].to_f32(), w[1].to_f32(), edge_col, 1.3, false); + let (a, b) = (at(w[0]), at(w[1])); + out.line(a, b, edge_col, 1.3, false); if hidden_edges { - out.hidden_line(w[0].to_f32(), w[1].to_f32(), hidden_col, 1.0); + out.hidden_line(a, b, hidden_col, 1.0); } } } @@ -654,6 +677,86 @@ fn push_block( made.insert(key, block); } +/// The model scene's instanced meshes: the shared triangles drawn more than once among the shown +/// bodies (or large: [`INSTANCE_ALONE`]), and each body placed from them. +struct Instances { + /// Each candidate body's shared mesh and placement, by body name. + candidates: std::collections::HashMap, solidcraft_engine::doc::Mat)>, + /// The meshes drawn as instances, in the order their first placement came, by address. + meshes: Vec<(std::sync::Arc, crate::gpu::InstancedMesh)>, + kept: InstancedTris, +} + +impl Instances { + /// The shown bodies' shared meshes, keeping those more than one body is drawn from and large + /// ones. + fn new(app: &SolveApp, shown: &[&solidcraft_engine::doc::ModelBody], shaded: bool) -> Instances { + let kept = app.viewport.instanced_tris.lock().map(|mut k| std::mem::take(&mut *k)).unwrap_or_default(); + let mut candidates = std::collections::HashMap::new(); + if shaded { + let mut uses: std::collections::HashMap = std::collections::HashMap::new(); + let placed: Vec<_> = shown.iter().filter_map(|b| Some((b.name.clone(), shared_mesh(app, b)?))).collect(); + for (_, (mesh, _)) in &placed { + *uses.entry(std::sync::Arc::as_ptr(mesh) as usize).or_default() += 1; + } + candidates = placed + .into_iter() + .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 } + } + + /// Draw `b` (in colour `col`) as an instance when it is one: its shared mesh and placement. + fn place( + &mut self, + b: &solidcraft_engine::doc::ModelBody, + col: [u8; 4], + ) -> 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)) { + 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 => std::sync::Arc::new(crate::gpu::SharedTris::new(&mesh)), + }; + self.meshes.push((mesh.clone(), crate::gpu::InstancedMesh { key: key as u64, tris, instances: Vec::new() })); + self.meshes.len() - 1 + } + }; + let (_, inst) = self.meshes.get_mut(at)?; + inst.place(rows, [col[0], col[1], col[2], 255]); + Some((mesh, m)) + } + + /// The instanced meshes, keeping their triangles for the next scene. + 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(); + if let Ok(mut k) = app.viewport.instanced_tris.lock() { + *k = kept; + } + self.meshes.into_iter().map(|(_, inst)| inst).collect() + } +} + +/// The shared mesh `b`'s faces are drawn from and the move that places it ([`drawn_mesh`] +/// without making the moved copy); `None` for a body drawn finer than its display quality. +fn shared_mesh( + app: &SolveApp, + b: &solidcraft_engine::doc::ModelBody, +) -> Option<(std::sync::Arc, solidcraft_engine::doc::Mat)> { + use solidcraft_engine::doc::display_quality::{DisplayQuality, current}; + if refined(app, b).is_some() { + return None; + } + let q = current(); + Some(if q == DisplayQuality::Normal { b.mesh_in_place() } else { b.plan_mesh_in_place_at(q).unwrap_or_else(|| b.mesh_in_place()) }) +} + pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { let tk = Tokens::get(); let mut sc = GpuScene::default(); @@ -675,10 +778,13 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { let mut made: std::collections::HashMap = std::collections::HashMap::new(); let mut run: Vec = Vec::new(); let mut run_key = std::collections::hash_map::DefaultHasher::new(); - for b in &st.bodies { - if !app.session.body_visible(&b.name) || app.preview.effective_replaced().contains(&b.name) { - continue; - } + let shown: Vec<&solidcraft_engine::doc::ModelBody> = + st.bodies.iter().filter(|b| app.session.body_visible(&b.name) && !app.preview.effective_replaced().contains(&b.name)).collect(); + // Bodies drawn from the same triangles more than once (a part placed by many occurrences) + // are drawn as instances of them: one copy of the triangles on the GPU, and none moved on + // the CPU. + let mut instances = Instances::new(app, &shown, shaded); + for b in shown { let faded = body_faded(app, b, &active_bodies); let base = body_color(&s.doc, b).unwrap_or(tk.body); let col = if faded { c4(base.gamma_multiply(0.35)) } else { c4(base) }; @@ -698,7 +804,11 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { .collect(); sc.planned.push(crate::gpu::PlannedBody { plan, face_colors }); } - let body = BodyDraw { mesh: drawn_mesh(app, b), col, faded, opacity, face_looks, on_gpu }; + let instanced = (!on_gpu && !faded && face_looks.is_empty() && opacity >= 0.999).then(|| instances.place(b, col)).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 }, + }; let key = body.key(shaded, edges, hidden_edges, edge_col, hidden_col); key.hash(&mut run_key); run.push(body); @@ -716,6 +826,7 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { if let Ok(mut b) = app.viewport.blocks.lock() { *b = made; } + sc.instanced = instances.finish(app); if app.ui.ground_shadow && app.ui.visual_style != 2 && app.cam.pitch > 0.0 { ground_shadow(&mut sc, app, &st); } @@ -1061,25 +1172,29 @@ fn body_bounds( { return b.clone(); } - // Boxes of meshes the last state had too are kept (each body's box walks its mesh). + // Boxes of meshes the last state had too are kept (each body's box walks its mesh). A + // placed part's box is its shared mesh's points moved, so no moved copy is made. let mut known = app.viewport.mesh_bounds.lock().map(|mut k| std::mem::take(&mut *k)).unwrap_or_default(); - let mut now = std::collections::HashMap::with_capacity(st.bodies.len()); + let mut now: MeshBounds = std::collections::HashMap::with_capacity(st.bodies.len()); let b = std::sync::Arc::new( st.bodies .iter() .map(|b| { - let mesh = b.mesh(); - let ptr = std::sync::Arc::as_ptr(&mesh) as usize; - let bounds = match known.remove(&ptr).or_else(|| now.remove(&ptr)) { + let (mesh, m) = b.mesh_in_place(); + let key = (std::sync::Arc::as_ptr(&mesh) as usize, std::array::from_fn(|i| m[i / 4][i % 4].to_bits())); + let bounds = match known.remove(&key).or_else(|| now.get(&key).cloned()) { Some((_, bounds)) => bounds, - None => mesh.bounds(), + None => b.mesh_bounds(), }; - now.insert(ptr, (mesh, bounds)); + now.insert(key, (mesh, bounds)); (b.name.clone(), bounds) }) .collect::>(), ); - app.viewport.pick_index.retain(|k| now.contains_key(&k)); + // Pick indexes are kept for the meshes this state is picked from (a placed part's + // shared mesh). + let live: std::collections::HashSet = now.keys().map(|(mesh, _)| *mesh).collect(); + app.viewport.pick_index.retain(|k| live.contains(&k)); if let Ok(mut k) = app.viewport.mesh_bounds.lock() { *k = now; } @@ -1346,8 +1461,11 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, _ => {} } } - let m = b.mesh(); - if let Some((t, ti)) = crate::pick_index::raycast(app.viewport.pick_index.of(&m).as_deref(), &m, o, d) { + // A placed part is picked in its own frame, from its shared mesh and index: the ray is + // moved there (a rigid move keeps distances along it), so no moved copy is made. + let (m, place) = b.mesh_in_place(); + let Some((lo, ld)) = local_ray(&place, o, d) else { continue }; + if let Some((t, ti)) = crate::pick_index::raycast(app.viewport.pick_index.of(&m).as_deref(), &m, lo, ld) { let fi = m.tri_face.get(ti).copied().unwrap_or(0) as usize; let hit = Hit::Face { body: b.name.clone(), index: fi, point: o + d * t }; if bestf.as_ref().is_none_or(|(bt, _)| t < *bt) { @@ -1379,13 +1497,17 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, { continue; } - let m = b.mesh(); + // In the part's own frame, as for faces; its points are moved back to where it stands. + let (m, place) = b.mesh_in_place(); + let Some((lo, ld)) = local_ray(&place, o, d) else { continue }; + let moved = !solidcraft_engine::doc::is_identity(&place); + let at = |p: Vec3| if moved { solidcraft_engine::doc::apply_point(&place, p) } else { p }; // Only the segments that can come near the cursor: of the edges the pick buffer sees // there, else from the index. Either way in the order a scan of every edge would meet // them, so ties go the same way. let near = match &seen { Some(seen) => seen.segments(i, &m), - None => crate::pick_index::segments_near(app.viewport.pick_index.of(&m).as_deref(), &m, o, d, reach), + None => crate::pick_index::segments_near(app.viewport.pick_index.of(&m).as_deref(), &m, lo, ld, reach), }; let mut last_edge = None; for (ei, si) in near { @@ -1395,22 +1517,22 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, } if last_edge != Some(ei) { last_edge = Some(ei); - for v in [e.first(), e.last()].into_iter().flatten() { - if let Some(sp) = proj.to_screen(*v) { + for v in [e.first(), e.last()].into_iter().flatten().map(|v| at(*v)) { + if let Some(sp) = proj.to_screen(v) { let dd = sp.distance(pos); - if dd < 7.0 && visible(*v) && bestv.wants(dd) { - bestv.offer(dd, Hit::Vertex { body: b.name.clone(), point: *v }); + if dd < 7.0 && visible(v) && bestv.wants(dd) { + bestv.offer(dd, Hit::Vertex { body: b.name.clone(), point: v }); } } } } - let (Some(p0), Some(p1)) = (e.get(si), e.get(si + 1)) else { continue }; - let (Some(a), Some(c)) = (proj.to_screen(*p0), proj.to_screen(*p1)) else { continue }; + let (Some(p0), Some(p1)) = (e.get(si).map(|p| at(*p)), e.get(si + 1).map(|p| at(*p))) else { continue }; + let (Some(a), Some(c)) = (proj.to_screen(p0), proj.to_screen(p1)) else { continue }; let dd = seg_dist(pos, a, c); // Visible where the cursor is: the edge's point under the cursor, not an end or // the middle, which can lie deeper than the face the cursor is on. - if dd < 6.0 && beste.wants(dd) && visible(ray_segment_closest(o, d, *p0, *p1)) { - beste.offer(dd, Hit::Edge { body: b.name.clone(), index: ei, mid: polyline_mid(e) }); + if dd < 6.0 && beste.wants(dd) && visible(ray_segment_closest(o, d, p0, p1)) { + beste.offer(dd, Hit::Edge { body: b.name.clone(), index: ei, mid: at(polyline_mid(e)) }); } } } @@ -2294,6 +2416,16 @@ fn body_rgb(app: &SolveApp, name: &str) -> (u8, u8, u8) { (c.r(), c.g(), c.b()) } +/// The ray `o` + t `d` in the frame of a mesh that `place` (a rigid move) puts in the world, with +/// the same t along it; `None` for a move with no inverse. +fn local_ray(place: &solidcraft_engine::doc::Mat, o: Vec3, d: Vec3) -> Option<(Vec3, Vec3)> { + if solidcraft_engine::doc::is_identity(place) { + return Some((o, d)); + } + let back = solidcraft_engine::doc::mat_inverse(place)?; + Some((solidcraft_engine::doc::apply_point(&back, o), solidcraft_engine::doc::apply_vector(&back, d))) +} + pub(crate) fn polyline_mid(pts: &[Vec3]) -> Vec3 { let len: f64 = pts.windows(2).map(|w| w[0].dist(w[1])).sum(); let mut acc = 0.0; @@ -4620,6 +4752,339 @@ mod tests { assert!(bounds.min.x >= 29.99 && bounds.max.x <= 32.01 && bounds.max.y >= 41.99, "{bounds:?}"); } + /// The parts the instancing tests place: a box (flat triangles, its vertices shared by + /// position and each fragment lit with its triangle's normal) and a cylinder (smooth, its + /// vertices shared by position and normal). + const PARTS: [(&str, &str); 2] = [("solid.box", r#"{"length":20,"width":10,"height":5}"#), ("solid.cylinder", r#"{"radius":6,"height":12}"#)]; + + /// A design with a part (`command` with `params`, see [`PARTS`]) placed three times (one + /// placement turned), drawn on a device that builds bodies on the CPU. + fn three_placed_parts((command, params): (&str, &str)) -> SolveApp { + let mut app = SolveApp::new(solidcraft_engine::Session::default(), 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.run("component.create", json!({"name":"Part"})).unwrap(); + app.run(command, serde_json::from_str(params).unwrap()).unwrap(); + app.run("occurrence.copy", json!({"component":"Part","translate":[40,0,0]})).unwrap(); + let turned = app.run("occurrence.copy", json!({"component":"Part","translate":[0,40,0]})).unwrap()["occurrence"].clone(); + app.run("occurrence.move", json!({"occurrence":turned,"axis":[1,1,0],"angle":"30 deg"})).unwrap(); + // With the part active, its other placements are drawn faded. + app.run("component.activate", json!({"component":"root"})).unwrap(); + app + } + + /// A part placed by several occurrences is drawn from one copy of its triangles, once per + /// placement: one instanced mesh with a placement for each occurrence, whose triangles land + /// where each occurrence's own moved mesh has them, and no moved copy is made to draw them. + #[test] + fn a_part_placed_many_times_is_drawn_from_one_copy_of_its_triangles() { + for (part, flat) in PARTS.into_iter().zip([true, false]) { + placed_three_times_is_drawn_from_one_copy(part, flat); + } + } + + fn placed_three_times_is_drawn_from_one_copy(part: (&str, &str), flat: bool) { + let app = three_placed_parts(part); + let st = app.session.world_state(); + assert_eq!(st.bodies.len(), 3); + eprintln!("DBG before {}", st.bodies.iter().filter(|b| b.cached_mesh().is_some()).count()); + let scene = build_scene(&app); + assert_eq!(scene.instanced.len(), 1, "one shared mesh"); + assert_eq!(scene.instanced[0].count(), 3, "a placement per occurrence"); + let shared = &scene.instanced[0].tris; + assert_eq!(shared.flat, flat, "{part:?}"); + let corners = shared.indices.len(); + let vertices = shared.vertices.len() / crate::gpu::SHARED_SIZE; + assert!(vertices * 2 < corners, "{part:?}: {vertices} vertices shared by {corners} corners"); + assert!(scene.blocks.iter().all(|b| b.bytes.tris.is_empty()), "no body's triangles built where it stands"); + let moved = st.bodies.iter().filter(|b| b.cached_mesh().is_some()).count(); + assert!(moved <= 1, "{moved} bodies made a moved mesh to be drawn"); + let merged = scene.merged(); + // Each placement's triangles where its own mesh has them, with its normals. + let vertex = |b: &[u8]| -> [f32; 6] { std::array::from_fn(|i| f32::from_le_bytes([b[4 * i], b[4 * i + 1], b[4 * i + 2], b[4 * i + 3]])) }; + let mut drawn: Vec<[f32; 6]> = merged.tris.as_chunks::<{ crate::gpu::TRI_SIZE }>().0.iter().map(|v| vertex(v)).collect(); + let mut expected: Vec<[f32; 6]> = Vec::new(); + for b in &st.bodies { + let m = b.mesh(); + for i in m.triangles.iter().flatten().map(|i| *i as usize) { + let (p, n) = (m.positions[i].to_f32(), m.normals[i].to_f32()); + expected.push([p[0], p[1], p[2], n[0], n[1], n[2]]); + } + } + let order = |a: &[f32; 6], b: &[f32; 6]| a.iter().map(|x| (x * 1e3).round() as i64).cmp(b.iter().map(|x| (x * 1e3).round() as i64)); + drawn.sort_by(order); + expected.sort_by(order); + assert_eq!(drawn.len(), expected.len()); + for (d, e) in drawn.iter().zip(&expected) { + assert!(d.iter().zip(e).all(|(x, y)| (x - y).abs() < 1e-3), "{d:?} vs {e:?}"); + } + // Each placement's edges are drawn where it stands. + let one = st.bodies[0].mesh(); + let segments: usize = one + .edges + .iter() + .enumerate() + .filter(|(i, _)| one.edge_pickable(*i) && one.shaded_edge_visible(*i)) + .map(|(_, e)| e.len().saturating_sub(1)) + .sum(); + assert_eq!(merged.lines.len(), 3 * segments * crate::gpu::LINE_SIZE); + } + + /// Manual GPU benchmark: 25 placements of the mesh `SOLIDCRAFT_BENCH_MESH` (an STL, OBJ or + /// 3MF) drawn offscreen at 3600 × 2190 px (divided by `SOLIDCRAFT_BENCH_DIV`) with 4× MSAA + /// while the camera turns, each frame waited for: the placements' own triangles (`merged`, as + /// before instancing) and as instances. Prints the median frame time of each. + #[cfg(not(target_arch = "wasm32"))] + #[test] + #[ignore = "manual: SOLIDCRAFT_BENCH_MESH=part.stl cargo test --release -p solidcraft-ui-egui --lib -- --ignored instanced_scene_gpu_benchmark --nocapture"] + fn instanced_scene_gpu_benchmark() { + use egui_wgpu::wgpu; + let Some(path) = std::env::var_os("SOLIDCRAFT_BENCH_MESH") else { return }; + let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default()); + app.ui.ground_shadow = false; + app.ui.show_origin = false; + let component = app.run("file.insert_mesh", json!({"path": path.to_string_lossy()})).unwrap()["component"].clone(); + app.run("component.activate", json!({"component":"root"})).unwrap(); + let size = app.session.world_state().bodies[0].mesh_bounds().size(); + let step = size.x.max(size.y) * 1.3; + 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(); + } + let Some((_, device, queue)) = crate::gpu::offscreen_device() else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let scale: u32 = std::env::var("SOLIDCRAFT_BENCH_DIV").ok().and_then(|v| v.parse().ok()).unwrap_or(1).max(1); + let (w, h, samples) = (3600u32 / scale, 2190u32 / scale, 4u32); + let depth = solidcraft_gpu::depth_format(&device, true); + let format = wgpu::TextureFormat::Rgba8Unorm; + app.viewport.gpu = Some(GpuTarget { format, depth: Some(depth), samples, tess: false }); + app.fit_view(); + let texture = |format, samples| { + device + .create_texture(&wgpu::TextureDescriptor { + label: Some("bench"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: samples, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }) + .create_view(&Default::default()) + }; + let (color, resolve, depth_view) = (texture(format, samples), texture(format, 1), texture(depth, samples)); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(w as f32, h as f32)); + let t0 = std::time::Instant::now(); + let scene = build_scene(&app); + let t1 = std::time::Instant::now(); + drop(build_scene(&app)); + eprintln!("scene built in {:.0} ms, again in {:.0} ms", (t1 - t0).as_secs_f64() * 1e3, t1.elapsed().as_secs_f64() * 1e3); + let proj = projection(&app, rect); + let st = app.session.world_state(); + let seen = st.bodies.iter().filter(|b| proj.to_screen(b.mesh_bounds().center()).is_some_and(|p| rect.contains(p))).count(); + eprintln!("{w} x {h} px: {seen} of {} placements in view", st.bodies.len()); + // `SOLIDCRAFT_BENCH_CLOSE=1`: close up on the middle placement, most of the others out of view. + if std::env::var("SOLIDCRAFT_BENCH_CLOSE").is_ok_and(|v| v == "1") { + let middle = st.bodies.iter().map(|b| b.mesh_bounds()).nth(12).unwrap_or_default(); + app.cam.target = middle.center(); + app.cam.distance = middle.diagonal() * 1.5; + let proj = projection(&app, rect); + let seen = st.bodies.iter().filter(|b| proj.to_screen(b.mesh_bounds().center()).is_some_and(|p| rect.contains(p))).count(); + 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)] { + 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(); + for k in 0..40 { + app.cam.yaw = yaw + f64::from(k) * 0.05; + let proj = projection(&app, rect); + let e = proj.cam.eye().to_f32(); + let frame = Frame { + product: false, + key: 1, + slot: slot.clone(), + sketch_key: 0, + sketch_slot: none(), + hl_key: 0, + hl_slot: none(), + pv_key: 0, + pv_slot: none(), + pv_hide: false, + view_proj: proj.vp.to_f32(), + back: proj.cam.back().to_f32(), + size_px: [w as f32, h as f32], + clip: [0.0; 4], + cap: [0.0; 4], + images: Vec::new(), + analysis: [0.0; 8], + analysis_colors: crate::gpu::analysis_colors(&Tokens::get().analysis), + access: None, + outline: true, + eye: [e[0], e[1], e[2], if proj.cam.fov > 0.0 { 1.0 } else { 0.0 }], + ao_radius: 0.0, + ssaa: 1.0, + }; + let t0 = std::time::Instant::now(); + let mut encoder = device.create_command_encoder(&Default::default()); + renderer.prepare(&device, &queue, &mut encoder, &frame); + { + let mut pass = encoder + .begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("bench"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color, + depth_slice: None, + resolve_target: Some(&resolve), + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), store: wgpu::StoreOp::Discard }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(solidcraft_gpu::view::far_depth(solidcraft_gpu::view::reversed_depth(Some(depth)))), + store: wgpu::StoreOp::Discard, + }), + stencil_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0), store: wgpu::StoreOp::Discard }), + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }) + .forget_lifetime(); + renderer.paint(&frame, &mut pass, [0.0, 0.0, w as f32, h as f32]); + } + queue.submit([encoder.finish()]); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + if k >= 5 { + times.push(t0.elapsed().as_secs_f64() * 1e3); + } + } + 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]); + } + } + + /// 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. + #[test] + fn each_placement_is_picked_where_it_stands_from_the_shared_mesh() { + let mut app = three_placed_parts(PARTS[0]); + app.fit_view(); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let proj = projection(&app, rect); + let st = app.session.world_state(); + // The top face's centre of each placement, from its moved box corners. + let tops: Vec<(String, Vec3)> = st + .bodies + .iter() + .map(|b| { + let (mesh, m) = b.mesh_in_place(); + let local = mesh.bounds(); + ( + b.name.clone(), + solidcraft_engine::doc::apply_point( + &m, + Vec3::new((local.min.x + local.max.x) / 2.0, (local.min.y + local.max.y) / 2.0, local.max.z), + ), + ) + }) + .collect(); + for (name, top) in &tops { + let at = proj.to_screen(*top).unwrap(); + let hits = pick(&app, &proj, at); + let face = hits.iter().find_map(|h| match h { + Hit::Face { body, point, .. } => Some((body.clone(), *point)), + _ => None, + }); + let (body, point) = face.unwrap_or_else(|| panic!("no face of {name} under {at:?}: {hits:?}")); + assert_eq!(&body, name); + assert!(point.dist(*top) < 0.05, "{point:?} vs {top:?}"); + } + assert!( + st.bodies.iter().all(|b| b.cached_mesh().is_none() || b.mesh_in_place().1 == solidcraft_engine::doc::IDENTITY), + "a moved copy was made to pick" + ); + } + + /// A part drawn as instances looks the same as its placements' own triangles drawn one by + /// one. Skipped without a GPU adapter. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn instances_draw_like_their_placements_own_triangles() { + for part in PARTS { + if !placed_three_times_draws_like_its_own_triangles(part) { + return; + } + } + } + + /// `false` without a GPU adapter. + #[cfg(not(target_arch = "wasm32"))] + fn placed_three_times_draws_like_its_own_triangles(part: (&str, &str)) -> bool { + let mut app = three_placed_parts(part); + 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 scene = build_scene(&app); + assert!(!scene.instanced.is_empty()); + let (Some(instanced), Some(one_by_one)) = (render(scene.clone()), render(scene.clone().merged())) else { + eprintln!("skipping: no GPU adapter"); + return false; + }; + let drawn = instanced.chunks(4).filter(|p| p[..3] != [255, 255, 255]).count(); + assert!(drawn > (SIZE * SIZE) as usize / 50, "the parts are drawn: {drawn} pixels"); + let differ = instanced.chunks(4).zip(one_by_one.chunks(4)).filter(|(a, b)| a.iter().zip(b.iter()).any(|(x, y)| x.abs_diff(*y) > 24)).count(); + assert!(differ * 200 < drawn, "{part:?}: {differ} of {drawn} pixels differ"); + // Close up on the turned placement, the others out of view: what is in view is all drawn. + let turned = app.session.world_state().bodies[2].mesh_bounds(); + app.cam.target = turned.center(); + app.cam.distance = turned.diagonal() * 0.75; + 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(one_by_one)) = (render(scene.clone()), render(scene.merged())) else { return false }; + let drawn = instanced.chunks(4).filter(|p| p[..3] != [255, 255, 255]).count(); + assert!(drawn > (SIZE * SIZE) as usize / 4, "{part:?}: close up, the part fills the view: {drawn} pixels"); + let differ = instanced.chunks(4).zip(one_by_one.chunks(4)).filter(|(a, b)| a.iter().zip(b.iter()).any(|(x, y)| x.abs_diff(*y) > 24)).count(); + assert!(differ * 200 < drawn, "{part:?} close up: {differ} of {drawn} pixels differ"); + true + } + #[test] fn ray_segment_closest_takes_the_point_under_the_ray() { let (o, d) = (Vec3::new(0.0, 0.0, 10.0), Vec3::new(0.0, 0.0, -1.0));