From a7615fbb471783a98dbb45883cf7291520073e0f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 14:46:58 -0800 Subject: [PATCH] Canvas: Defer patches whose bounds need a cold 3D render --- crates/render/src/fx/renderer.rs | 2 +- crates/render/src/fx/tests.rs | 41 +++++++++++++++++++++++ crates/render/src/group.rs | 5 ++- crates/render/src/live.rs | 2 +- crates/render/src/renderer/nodes.rs | 50 +++++++++++++++++++++++++---- 5 files changed, 91 insertions(+), 9 deletions(-) diff --git a/crates/render/src/fx/renderer.rs b/crates/render/src/fx/renderer.rs index b66a1db09e..1bc71d4626 100644 --- a/crates/render/src/fx/renderer.rs +++ b/crates/render/src/fx/renderer.rs @@ -474,7 +474,7 @@ impl Renderer { let opacity = self.opacity_of(a); if !f.opts.outline && (opacity < 1.0 || a.blend != vectorcraft_doc::color::BlendMode::Normal || a.isolate) { let blends = self.blends_through(a); - let bounds = if blends { self.bounds_of(f.symbols, a) } else { None }; + let bounds = if blends { self.frame_bounds_of(f, a) } else { None }; let comp = crate::group::Composite { blend: a.blend, opacity, isolated: a.isolate, blends, bounds, ..Default::default() }; self.group(ctx, f, comp, &mut |r, c, fr| r.draw_object_fx_art(c, fr, a, cache)); } else { diff --git a/crates/render/src/fx/tests.rs b/crates/render/src/fx/tests.rs index 4838d27354..552a5c2e89 100644 --- a/crates/render/src/fx/tests.rs +++ b/crates/render/src/fx/tests.rs @@ -1554,6 +1554,47 @@ fn a_plain_patch_stops_at_a_raster_effect() { assert!(scratch.stats.drawn < total_drawn, "the rejected patch does not draw the artwork after the effect"); } +#[test] +fn a_plain_patch_does_not_build_unrelated_3d_art_to_cull_it() { + let mut doc = doc_with(Rect::new(20.0, 20.0, 80.0, 76.0), Color::BLACK, vec![("threeD.extrude", json!({"depth": 10}))]); + let plain = Node::path( + doc.alloc_id(), + shapes::rectangle(Rect::new(290.0, 10.0, 300.0, 20.0)), + Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0), + ); + doc.insert(Some(doc.layers[0].id), usize::MAX, plain).unwrap(); + let opts = RenderOptions::default(); + let mut whole = Renderer::new(); + whole.threads = 0; + whole.render(&doc, 350, 150, Affine::IDENTITY, &opts); + assert!(whole.stats.three_d_built > 0, "the fixture generates 3D art"); + let mut scratch = Renderer::new(); + scratch.threads = 0; + let patch = Affine::translate((-280.0, 0.0)); + let result = scratch.render_plain_patch(&doc, 40, 30, patch, &opts); + assert_eq!(scratch.stats.three_d_built, 0, "fall back before rebuilding 3D art"); + assert!(result.is_none()); + + let layer = doc.layers[0].id; + let art = doc.layers[0].children().unwrap()[0].clone(); + doc.remove(art.id).unwrap(); + let mut template = Node::layer(doc.alloc_id(), "Template", vectorcraft_doc::LayerColor::Preset(0)); + if let vectorcraft_doc::NodeKind::Layer { template: flag, children, .. } = &mut template.kind { + *flag = true; + children.push(art.clone()); + } + let template = doc.insert(Some(layer), 0, template).unwrap(); + let hidden = RenderOptions { hide_templates: true, ..opts.clone() }; + assert!(scratch.render_plain_patch(&doc, 40, 30, patch, &hidden).is_none(), "layer bounds conservatively include hidden templates"); + assert_eq!(scratch.stats.three_d_built, 0); + + doc.remove(template).unwrap(); + doc.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Solid".into(), art }); + let instance = Node::new(doc.alloc_id(), vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Solid".into(), xf: Affine::IDENTITY }); + doc.insert(Some(layer), 0, instance).unwrap(); + assert!(scratch.render_plain_patch(&doc, 40, 30, patch, &opts).is_none(), "a symbol's definition also needs 3D bounds"); +} + #[test] fn a_drag_renders_raster_effects_within_the_drag_budget() { let effects = vec![("stylize.dropShadow", json!({"x": 4, "y": 3, "blur": 7, "opacity": 80})), ("texture.texturizer", json!({}))]; diff --git a/crates/render/src/group.rs b/crates/render/src/group.rs index b80b903ae9..12490eb308 100644 --- a/crates/render/src/group.rs +++ b/crates/render/src/group.rs @@ -312,7 +312,10 @@ impl Renderer { /// `c` over the backdrop and its shape are drawn offscreen over `c`'s bounds, and `c` is drawn /// as in the other knockout passes (groups in it can't copy their backdrop). pub(crate) fn knockout_over(&mut self, ctx: &mut RenderContext, f: &Frame, c: &Arc, backdrop: &Arc) { - let bounds = self.bounds_of(f.symbols, c); + let bounds = self.frame_bounds_of(f, c); + if f.patch_rejected() { + return; + } let Some(r) = region(ctx, f, bounds) else { return }; let view = Affine::translate((-(r.x as f64), -(r.y as f64))) * f.view; let visible = view.inverse().transform_rect_bbox(Rect::new(0.0, 0.0, r.w as f64, r.h as f64)); diff --git a/crates/render/src/live.rs b/crates/render/src/live.rs index dc0ac84e2b..341aa22ff9 100644 --- a/crates/render/src/live.rs +++ b/crates/render/src/live.rs @@ -583,7 +583,7 @@ impl Renderer { let enclosing = std::mem::replace(&mut self.knockout, knockout); if !f.opts.outline && (opacity < 1.0 || a.blend != vectorcraft_color::BlendMode::Normal || a.isolate || knockout) { let blends = self.blends_through(a); - let bounds = if blends { self.bounds_of(f.symbols, a) } else { None }; + let bounds = if blends { self.frame_bounds_of(f, a) } else { None }; let comp = crate::group::Composite { blend: a.blend, opacity, isolated: a.isolate, blends, bounds, ..Default::default() }; self.group(ctx, f, comp, &mut |r, c, fr| r.draw_live_body(c, fr, a, true)); } else { diff --git a/crates/render/src/renderer/nodes.rs b/crates/render/src/renderer/nodes.rs index 654dd938ee..8edb216ee2 100644 --- a/crates/render/src/renderer/nodes.rs +++ b/crates/render/src/renderer/nodes.rs @@ -11,6 +11,17 @@ impl Renderer { /// Cached cull bounds of a node (containers union their cached children), symbol instances /// measured by their art box from `symbols`. pub(crate) fn bounds_of(&mut self, symbols: &SymbolBoxes, a: &Arc) -> Option { + self.bounds_with_patch(symbols, a, None) + } + + pub(crate) fn frame_bounds_of(&mut self, f: &Frame, a: &Arc) -> Option { + self.bounds_with_patch(f.symbols, a, f.patch.map(|_| f)) + } + + fn bounds_with_patch(&mut self, symbols: &SymbolBoxes, a: &Arc, patch: Option<&Frame<'_>>) -> Option { + if patch.is_some_and(Frame::patch_rejected) { + return None; + } let key = Arc::as_ptr(a) as usize; if let Some(e) = self.geom.get_mut(&key) && Arc::ptr_eq(&e.node, a) @@ -18,6 +29,19 @@ impl Renderer { e.stamp = self.stamp; return e.bounds; } + if let Some(f) = patch { + // The full renderer can reuse its 3D art. A cold scratch renderer may otherwise + // build it while calculating cull bounds, including bounds of symbol definitions. + let symbol_needs_full = match &a.kind { + NodeKind::SymbolInstance { symbol, .. } => { + symbols.symbol(symbol).is_some_and(|s| holds_measured_three_d(&s.art) || holds_instance(&s.art) && symbols.has_effects(symbol)) + } + _ => false, + }; + if (measures_three_d(a) || symbol_needs_full) && f.reject_patch() { + return None; + } + } let b = match &a.kind { NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } if !fx::has_object_fx(a) => { let mut acc: Option = None; @@ -25,7 +49,10 @@ impl Renderer { let source = a.extras().trace.is_some().then(|| children.first()).flatten(); for c in children { if c.visible || source.is_some_and(|s| Arc::ptr_eq(s, c)) { - acc = vectorcraft_geom::union_opt(acc, self.bounds_of(symbols, c)); + acc = vectorcraft_geom::union_opt(acc, self.bounds_with_patch(symbols, c, patch)); + if patch.is_some_and(Frame::patch_rejected) { + return None; + } } } acc @@ -227,7 +254,11 @@ impl Renderer { if crate::node_checks::left_out(f.opts, f.doc, a) { return true; } - match self.bounds_of(f.symbols, a) { + let bounds = self.frame_bounds_of(f, a); + if f.patch_rejected() { + return true; + } + match bounds { Some(b) => { let pad = f.px * 2.0; // Level of detail: leaves smaller than a quarter pixel are invisible. @@ -272,7 +303,10 @@ impl Renderer { let Some(m) = a.extras().mask.as_deref().filter(|m| !m.disabled && !f.opts.outline && !self.is_shape(a)) else { return false }; // A masked object is a non-isolated group of its own, drawn over its own pixels: the // mask is rendered over those only. - let bounds = self.bounds_of(f.symbols, a); + let bounds = self.frame_bounds_of(f, a); + if f.patch_rejected() { + return true; + } let Some((r, sub)) = group::area(ctx, f, bounds) else { return true }; let Some(mask) = self.mask_values(&sub, m, r.w, r.h) else { return true }; let blends = a.blend != vectorcraft_color::BlendMode::Normal || (!a.isolate && self.blends_through(a)); @@ -396,7 +430,7 @@ impl Renderer { return; } // A cancelled export stops drawing; whoever asked for the picture drops it (see `cancel`). - if f.patch_rejected() || vectorcraft_doc::cancel::is_cancelled() { + if f.patch_rejected() || f.patch.is_some() && measures_three_d(n) && f.reject_patch() || vectorcraft_doc::cancel::is_cancelled() { return; } if force { @@ -682,7 +716,7 @@ impl Renderer { let inverse = im.xf.inverse(); let (mut x0, mut x1, mut y0, mut y1) = (0.0_f64, w, 0.0_f64, h); for c in shapes { - if let Some(b) = self.bounds_of(f.symbols, c).map(|b| b.inflate(f.px * 2.0, f.px * 2.0)) { + if let Some(b) = self.frame_bounds_of(f, c).map(|b| b.inflate(f.px * 2.0, f.px * 2.0)) { for q in [(b.x0, b.y0), (b.x1, b.y0), (b.x1, b.y1), (b.x0, b.y1)] { let p = inverse * kurbo::Point::new(q.0, q.1); if p.x.is_finite() && p.y.is_finite() { @@ -745,7 +779,11 @@ impl Renderer { if crate::node_checks::left_out(f.opts, f.doc, c) { return; } - match self.bounds_of(f.symbols, c) { + let bounds = self.frame_bounds_of(f, c); + if f.patch_rejected() { + return; + } + match bounds { Some(b) => { let pad = f.px * 2.0; if !rects_overlap(b.inflate(pad, pad), f.visible) {