diff --git a/crates/render/src/blocks.rs b/crates/render/src/blocks.rs index ccdab352c..e3736d16a 100644 --- a/crates/render/src/blocks.rs +++ b/crates/render/src/blocks.rs @@ -125,6 +125,7 @@ pub(crate) fn insert(b: &mut Builder, ctx: &Ctx, e: &Entity, ins: &cadcraft_doc: let mut part = attr_clip.as_ref().map(|_| b.part()); attributes(part.as_mut().unwrap_or(&mut *b), ctx, e, ins, rgb, own); if let (Some(part), Some((region, inverted))) = (part, &attr_clip) { + b.list.truncated |= part.list.truncated; let limits = b.limits(); if !append_clipped_keep(&mut b.list, &part.list, &if *inverted { Clip::Outside(region) } else { Clip::Poly(region) }, limits) { b.spend(); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index b91488e76..3852d6424 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -464,6 +464,23 @@ impl<'a> Builder<'a> { fn limits(&self) -> Limits { Limits { points: self.max_points, prims: self.max_prims } } + /// Join the parts of a list built on all cores to this builder's list, in drawing order and + /// within its limits ([`Limits`]): the clipped copies a part holds are in no count, so the + /// parts can hold more together than one list may. At the first part that doesn't fit the build + /// is spent and the parts left are not joined. + #[cfg(not(target_arch = "wasm32"))] + fn join(&mut self, parts: Vec) { + let points = |l: &DisplayList| l.verts.len().saturating_add(l.tris.len()); + for part in parts { + if self.list.prims.len().saturating_add(part.prims.len()) > self.max_prims + || points(&self.list).saturating_add(points(&part)) > self.max_points + { + self.spend(); + return; + } + display::append(&mut self.list, part); + } + } } /// The clip boundary of image `i` as a closed outline, or `None` when the whole image shows. diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 8307c38f6..f18cb4862 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -1,5 +1,3 @@ -#[cfg(not(target_arch = "wasm32"))] -use super::display::append; use super::display::{DisplayList, Kind, NO_LAYER, PlotText, VIEWPORT_CONTENT}; use super::draw::entity; use super::viewports::{MAX_VIEWPORTS, viewport}; @@ -196,13 +194,11 @@ fn build_parallel(b: &mut Builder, d: &Drawing, store: &cadcraft_doc::EntityStor part.list }) .collect(); - // One allocation per array for the joined list. - b.list.prims.reserve(parts.iter().map(|p| p.prims.len()).sum()); - b.list.verts.reserve(parts.iter().map(|p| p.verts.len()).sum()); - b.list.tris.reserve(parts.iter().map(|p| p.tris.len()).sum()); - for part in parts { - append(&mut b.list, part); - } + // One allocation per array for the joined list, within what it may hold. + b.list.prims.reserve(parts.iter().map(|p| p.prims.len()).sum::().min(b.max_prims)); + b.list.verts.reserve(parts.iter().map(|p| p.verts.len()).sum::().min(b.max_points)); + b.list.tris.reserve(parts.iter().map(|p| p.tris.len()).sum::().min(b.max_points)); + b.join(parts); } /// The plot style table a build draws with: the one in `opts`, else (on screen) the layout's diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 59a23572d..04d41b3cf 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1667,3 +1667,61 @@ fn pieces_cut_by_a_clip_stop_at_the_list_limits_and_say_so() { } } } + +/// A block reference clipped (XCLIP, frame off) to a large rectangle, of block "Z" holding `kind`. +fn xclipped_ref(d: &mut Drawing, kind: EntityKind, at: Vec3) -> EntityKind { + if d.block("Z").is_none() { + let mut blk = cadcraft_doc::Block::new("Z"); + blk.entities.push(block_entity(1, Common::default(), kind)); + d.blocks.insert("Z".into(), std::sync::Arc::new(blk)); + } + let EntityKind::Insert(mut ins) = insert_of("Z") else { panic!("an insert") }; + ins.insert = at; + ins.clip = Some(cadcraft_doc::ClipBoundary::new(vec![Vec2::new(-100.0, 1.0), Vec2::new(100.0, 100.0)])); + EntityKind::Insert(ins) +} + +#[test] +fn attributes_cut_by_a_clip_say_what_the_budget_left_out() { + // The last object of the drawing: a clipped reference whose attribute text runs out of points. + let mut d = Drawing::new_imperial(); + d.header.set_i64("XCLIPFRAME", 0); + let kind = xclipped_ref(&mut d, line_common(Common::default()).1, Vec3::ZERO); + let EntityKind::Insert(mut ins) = kind else { panic!("an insert") }; + let mut a = attrib("TAG"); + a.text.insert = Vec3::new(1.0, 2.0, 0.0); + a.text.value = "ABCDEFGHIJKLMNOP".into(); + ins.attribs.push(a); + d.add(&Space::Model, Common::default(), EntityKind::Insert(ins)).unwrap(); + let build = |max_points| crate::pipeline::build_space_within(&d, &Space::Model, &Options::default(), false, max_points, MAX_DISPLAY_PRIMS); + let full = build(MAX_DISPLAY_POINTS); + assert!(!full.truncated && points(&full) > 20, "{}", points(&full)); + let l = build(points(&full) - 1); + assert!(l.truncated && points(&l) < points(&full), "{} of {}", points(&l), points(&full)); +} + +#[test] +fn parts_built_on_all_cores_are_joined_within_the_list_limits() { + // Two clipped zigzags in different parts of the parallel build: each part fits the list's + // limits and the build's counts, which hold the zigzags and not their pieces; together they + // don't fit. + let mut d = Drawing::new_imperial(); + d.header.set_i64("XCLIPFRAME", 0); + for i in 0..600 { + let kind = if i == 0 || i == 300 { xclipped_ref(&mut d, zigzag(), Vec3::ZERO) } else { point_at(f64::from(i)) }; + d.add(&Space::Model, Common::default(), kind).unwrap(); + } + let build = |max_points, max_prims| crate::pipeline::build_space_within(&d, &Space::Model, &Options::default(), false, max_points, max_prims); + let full = build(MAX_DISPLAY_POINTS, MAX_DISPLAY_PRIMS); + assert!(!full.truncated && full.prims.len() > 700 && points(&full) > 1000, "{} prims, {} points", full.prims.len(), points(&full)); + let max = full.prims.len() - 50; + let l = build(MAX_DISPLAY_POINTS, max); + assert!(l.truncated && l.prims.len() <= max, "{} of {max}", l.prims.len()); + let max = points(&full) - 50; + let l = build(max, MAX_DISPLAY_PRIMS); + assert!(l.truncated && points(&l) <= max, "{} of {max}", points(&l)); + // Exactly as much as the parts hold is joined whole. + for l in [build(MAX_DISPLAY_POINTS, full.prims.len()), build(points(&full), MAX_DISPLAY_PRIMS)] { + assert!(!l.truncated && l.prims.len() == full.prims.len(), "{} of {}", l.prims.len(), full.prims.len()); + } +} diff --git a/crates/render/src/viewports.rs b/crates/render/src/viewports.rs index 50286a09e..08c255f49 100644 --- a/crates/render/src/viewports.rs +++ b/crates/render/src/viewports.rs @@ -1,5 +1,3 @@ -#[cfg(not(target_arch = "wasm32"))] -use super::display::append; use super::display::{DImage, DPrim, DisplayList, Kind, NO_LAYER, PlotText, VIEWPORT_CONTENT}; use super::draw::entity; #[cfg(not(target_arch = "wasm32"))] @@ -174,9 +172,7 @@ fn contents(b: &mut Builder, d: &Drawing, vp: &cadcraft_doc::Viewport, area: Bou part.list }) .collect(); - for part in parts { - append(&mut sub.list, part); - } + sub.join(parts); } } else { let c = ctx();