Skip to content

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

Web app: an opened design is drawn when its meshes cannot be made on a thread (#10362)

kaltenbrunnermarkuse0582668bb8e
diff --git a/crates/ui-egui/src/file_load_ui_tests.rs b/crates/ui-egui/src/file_load_ui_tests.rs
index ad301a0506..ef714ab1a2 100644
--- a/crates/ui-egui/src/file_load_ui_tests.rs
+++ b/crates/ui-egui/src/file_load_ui_tests.rs
@@ -460,6 +460,37 @@ fn a_control_import_passes_its_matrix_and_name_on() {
let _ = std::fs::remove_dir_all(&dir);
}
+/// #10198: the web build cannot start the meshing thread. An opened design is still drawn whole
+/// and its "Loading parts" note goes, instead of an empty view with "Loading parts n/n" for good.
+#[test]
+fn an_opened_design_is_drawn_whole_when_meshing_has_no_thread() {
+ let dir = scratch("progressive-no-thread");
+ let path = large_design(&dir, 6);
+ let mut w = Win::new();
+ let (tx, rx) = std::sync::mpsc::channel();
+ w.app.control_rx = Some(rx);
+ let (open, open_rx) = crate::control::ControlRequest::new("engine.execute", json!({"command": "doc.open", "params": {"path": path}}));
+ tx.send(open).unwrap();
+ crate::open_meshing::without_threads(|| {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
+ while open_rx.try_recv().is_err() {
+ w.frame(vec![]);
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ assert!(std::time::Instant::now() < deadline, "the open answers");
+ }
+ for _ in 0..100 {
+ if !crate::open_meshing::active(&w.app) {
+ break;
+ }
+ w.frame(vec![]);
+ }
+ });
+ assert!(!crate::open_meshing::active(&w.app), "the parts are in: {:?}", crate::open_meshing::progress(&w.app));
+ let world = w.app.session.world_state();
+ assert!(!world.bodies.is_empty());
+ assert_eq!(crate::open_meshing::shown_world(&w.app).bodies.len(), world.bodies.len(), "the whole design is drawn");
+}
+
/// An opened design is in the window before its bodies are meshed: the view draws the bodies
/// meshed so far (never meshing one itself), framed from their outlines, until all are.
#[test]
diff --git a/crates/ui-egui/src/open_meshing.rs b/crates/ui-egui/src/open_meshing.rs
index 9f96b91923..65056ddfa3 100644
--- a/crates/ui-egui/src/open_meshing.rs
+++ b/crates/ui-egui/src/open_meshing.rs
@@ -66,33 +66,41 @@ impl Meshing {
}
first.extend(rest);
let job = (world.clone(), ready.clone(), done.clone(), stop.clone());
- let spawned = std::thread::Builder::new().name("open-meshing".into()).spawn(move || {
- let (world, ready, done, stop) = job;
- solidcraft_engine::kernel::for_each_on_threads(&first, 16, |&i| {
- if stop.load(Ordering::Relaxed) {
- return;
- }
- if let Some(b) = world.bodies.get(i) {
- // 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 (mesh, _) = b.mesh_in_place();
- // A large mesh's shared vertices too, so its first frame does not weld them.
- if shared {
- crate::viewport::warm_shared_tris(&mesh);
- }
- if plans {
- let _ = b.display_plan();
- let _ = b.plan_mesh_in_place_at(solidcraft_engine::doc::display_quality::current());
- }
- }
- if let Ok(mut r) = ready.lock() {
- r.push(i);
- }
- done.fetch_add(1, Ordering::Release);
- });
- });
- if spawned.is_err() {
- // No thread: the viewport meshes as it draws, as before.
+ let spawned = threads()
+ && std::thread::Builder::new()
+ .name("open-meshing".into())
+ .spawn(move || {
+ let (world, ready, done, stop) = job;
+ solidcraft_engine::kernel::for_each_on_threads(&first, 16, |&i| {
+ if stop.load(Ordering::Relaxed) {
+ return;
+ }
+ if let Some(b) = world.bodies.get(i) {
+ // 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 (mesh, _) = b.mesh_in_place();
+ // A large mesh's shared vertices too, so its first frame does not weld them.
+ if shared {
+ crate::viewport::warm_shared_tris(&mesh);
+ }
+ if plans {
+ let _ = b.display_plan();
+ let _ = b.plan_mesh_in_place_at(solidcraft_engine::doc::display_quality::current());
+ }
+ }
+ if let Ok(mut r) = ready.lock() {
+ r.push(i);
+ }
+ done.fetch_add(1, Ordering::Release);
+ });
+ })
+ .is_ok();
+ if !spawned {
+ // No thread (the web build): the viewport meshes as it draws, as before. Every body
+ // is listed ready too, or the picture stays empty with "Loading parts n/n" (#10198).
+ if let Ok(mut r) = ready.lock() {
+ r.extend(0..n);
+ }
done.store(n, Ordering::Release);
}
Meshing {
@@ -264,6 +272,30 @@ pub(crate) fn refresh(app: &mut SolveApp, now: f64) {
m.shown_at = now;
}
+/// Whether the meshes may be made on a thread of their own. The web build cannot start one; a
+/// test says no to do the same ([`without_threads`]).
+fn threads() -> bool {
+ #[cfg(test)]
+ if NO_THREADS.with(std::cell::Cell::get) {
+ return false;
+ }
+ true
+}
+
+#[cfg(test)]
+thread_local! {
+ static NO_THREADS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
+}
+
+/// Run `f` as the web build runs: meshing cannot start a thread.
+#[cfg(test)]
+pub(crate) fn without_threads<R>(f: impl FnOnce() -> R) -> R {
+ NO_THREADS.with(|t| t.set(true));
+ let r = f();
+ NO_THREADS.with(|t| t.set(false));
+ r
+}
+
fn profiling() -> bool {
solidcraft_engine::geom::env::var_os("PROFILE_OPEN").is_some()
}