Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Render preview: extract the product scene only when what it shows changes (#7465)

Jackson Oursland6d16fae4ed4e
diff --git a/crates/ui-egui/src/render_workspace.rs b/crates/ui-egui/src/render_workspace.rs
index 63d52e5df0..2fba6821b1 100644
--- a/crates/ui-egui/src/render_workspace.rs
+++ b/crates/ui-egui/src/render_workspace.rs
@@ -143,7 +143,13 @@ pub struct State {
design_camera: Option<(Camera, bool)>,
slot: SceneSlot,
key: Option<u64>,
+ /// What the product scene was extracted from: the document's and the world state's
+ /// addresses, a hash of what decides visibility and the geometry budget
+ /// ([`State::prepare_preview`]).
scene_signature: Option<(u64, usize, u64, u64)>,
+ /// The document and world state `scene_signature` names, held so their addresses cannot be
+ /// reused by others while the signature refers to them.
+ scene_sources: Option<(Arc<solidcraft_engine::doc::Document>, Arc<solidcraft_engine::doc::ModelState>)>,
scene: Option<ProductScene>,
scene_error: Option<String>,
preview_upload_bytes: Option<usize>,
@@ -474,6 +480,7 @@ impl Default for State {
slot: Arc::new(Mutex::new(None)),
key: None,
scene_signature: None,
+ scene_sources: None,
scene: None,
scene_error: None,
preview_upload_bytes: None,
@@ -672,6 +679,7 @@ pub fn invalidate_scene(app: &mut SolveApp) {
app.render_workspace.thumbnails = Default::default();
app.render_workspace.revision = u64::MAX;
app.render_workspace.scene_signature = None;
+ app.render_workspace.scene_sources = None;
app.render_workspace.scene = None;
app.render_workspace.scene_error = None;
app.render_workspace.preview_upload_bytes = None;
@@ -1756,6 +1764,20 @@ fn show_result(app: &mut SolveApp, ui: &mut egui::Ui) {
crate::navigation_bar_view::show(app, ui, rect);
}
+/// What the product scene is extracted from: the document (appearances, the history marker),
+/// the world state (geometry and placements) and what decides which bodies are visible. Not the
+/// session's revision, which every command moves: a selection or a view change extracts no scene
+/// and builds no vertices. The document and world state are held while the signature names them.
+fn preview_signature(app: &mut SolveApp) -> (u64, usize, u64) {
+ let mut visibility = std::collections::hash_map::DefaultHasher::new();
+ let v = &app.session.visibility;
+ (&v.hidden_bodies, &v.hidden_occurrences, &v.hidden_folders, app.session.active_sketch).hash(&mut visibility);
+ let (doc, world) = (app.session.doc.clone(), crate::open_meshing::session_world(&app.session));
+ let signature = (Arc::as_ptr(&doc) as usize as u64, Arc::as_ptr(&world) as usize, visibility.finish());
+ app.render_workspace.scene_sources = Some((doc, world));
+ signature
+}
+
impl State {
fn prepare_preview(&mut self, signature: (u64, usize, u64, u64), capture: impl FnOnce() -> Result<ProductScene, String>) -> Result<(), String> {
if self.scene_signature != Some(signature) {
@@ -2172,16 +2194,9 @@ fn paint_product(app: &mut SolveApp, ui: &mut egui::Ui, rect: egui::Rect, camera
if app.session.model.is_stale() {
return Err(crate::i18n::tr("render.error_finish_gesture").into());
}
- let mut visibility = std::collections::hash_map::DefaultHasher::new();
- app.session.visibility.hidden_bodies.hash(&mut visibility);
- app.session.visibility.hidden_occurrences.hash(&mut visibility);
+ let (doc, world, visibility) = preview_signature(app);
let budget = preferred_geometry_budget(app.render_workspace.geometry_budget, app.preferences.render_previous_geometry_budget);
- let signature = (
- app.session.revision,
- Arc::as_ptr(&crate::open_meshing::session_world(&app.session)) as usize,
- visibility.finish() ^ assign::preview_key(app),
- budget,
- );
+ let signature = (doc, world, visibility ^ assign::preview_key(app), budget);
let preview = assign::preview(app);
let previous_key = app.render_workspace.key;
app.render_workspace.prepare_preview(signature, || {
diff --git a/crates/ui-egui/src/render_workspace_tests.rs b/crates/ui-egui/src/render_workspace_tests.rs
index 980c6bf93c..1e293e6fce 100644
--- a/crates/ui-egui/src/render_workspace_tests.rs
+++ b/crates/ui-egui/src/render_workspace_tests.rs
@@ -3843,6 +3843,31 @@ fn full_detail_preview_source_can_exceed_legacy_128_mib_without_mesh_reduction()
assert!(maximum.add(true, false, MAX_GEOMETRY_BYTES / (3 * crate::gpu::PBR_SIZE) + 1, MAX_GEOMETRY_BYTES).is_err());
}
+/// The product scene is extracted again only when what it shows can change: a selection or a
+/// view change keeps its signature (they extracted every body and built every vertex again),
+/// an appearance edit, hiding a body or a geometry edit changes it.
+#[test]
+fn product_preview_signature_follows_what_the_preview_shows() {
+ let mut h = Harness::new();
+ h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10})).unwrap();
+ let body = crate::open_meshing::session_world(&h.app.session).bodies[0].name.clone();
+ let first = preview_signature(&mut h.app);
+ assert_eq!(preview_signature(&mut h.app), first, "nothing changed");
+ h.app.run("select.set", json!({"items": [{"type": "body", "name": &body}]})).unwrap();
+ assert_eq!(preview_signature(&mut h.app), first, "a selection");
+ h.app.run("select.clear", json!({})).unwrap();
+ let _ = h.call("ui.view", json!({"view": "top"}));
+ assert_eq!(preview_signature(&mut h.app), first, "a view change");
+ h.app.run("appearance.assign", json!({"bodies": [&body], "color": "#336699"})).unwrap();
+ let painted = preview_signature(&mut h.app);
+ assert_ne!(painted, first, "an appearance edit");
+ h.app.run("browser.visibility", json!({"items": [&body], "visible": false})).unwrap();
+ assert_ne!(preview_signature(&mut h.app), painted, "hiding a body");
+ h.app.run("browser.visibility", json!({"items": [&body], "visible": true})).unwrap();
+ h.app.run("solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [20, 0, 0], "operation": "new"})).unwrap();
+ assert_ne!(preview_signature(&mut h.app), painted, "a geometry edit");
+}
+
#[test]
fn refused_product_capture_is_cached_until_identity_changes_or_explicit_retry() {
let mut h = Harness::new();