From d5661a344877faa122808a706c6255e3154a2839 Mon Sep 17 00:00:00 2001 From: Nikita Pekin Date: Thu, 19 Nov 2020 02:45:03 -0500 Subject: [PATCH 1/3] fix: Silence arithmetic overflow warning --- src/manifold_dual_contouring.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifold_dual_contouring.rs b/src/manifold_dual_contouring.rs index 2e3825f..2086548 100644 --- a/src/manifold_dual_contouring.rs +++ b/src/manifold_dual_contouring.rs @@ -231,7 +231,7 @@ fn pow2roundup(x: usize) -> usize { x |= x >> 4; x |= x >> 8; x |= x >> 16; - x |= x >> 32; + x |= x.wrapping_shr(32); x + 1 } From de8edc5ce6129d0038dd01ecf8638be4ab45989b Mon Sep 17 00:00:00 2001 From: Nikita Pekin Date: Wed, 25 Nov 2020 21:23:33 -0500 Subject: [PATCH 2/3] impr: Disable rayon on wasm32 target --- src/manifold_dual_contouring.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/manifold_dual_contouring.rs b/src/manifold_dual_contouring.rs index 2086548..56e38c8 100644 --- a/src/manifold_dual_contouring.rs +++ b/src/manifold_dual_contouring.rs @@ -10,6 +10,7 @@ use crate::{ use bbox::BoundingBox; use nalgebra as na; use num_traits::Float; +#[cfg(not(target_arch="wasm32"))] use rayon::prelude::*; use std::{ cell::{Cell, RefCell}, @@ -607,8 +608,11 @@ impl<'a, S: From + RealField + Float + AsUSize> ManifoldDualContouring<'a, fn compact_value_grid(&mut self) { // Collect all indexes to remove. let value_grid = &mut self.value_grid; - let keys_to_remove: Vec<_> = value_grid - .par_iter() + #[cfg(not(target_arch="wasm32"))] + let value_grid_iter = value_grid.par_iter(); + #[cfg(target_arch="wasm32")] + let value_grid_iter = value_grid.iter(); + let keys_to_remove: Vec<_> = value_grid_iter .filter(|&(idx, &v)| { if idx[0] == 0 || idx[1] == 0 || idx[2] == 0 { // This grid cell does not have neighbors in some directions. Ignore. From d4882273faeeaba53576aed726c039b76056ff17 Mon Sep 17 00:00:00 2001 From: Nikita Pekin Date: Wed, 25 Nov 2020 21:40:53 -0500 Subject: [PATCH 3/3] impr: Replace `std::time::Instant` with `instant::Instant` Replace `std::time::Instant` with `instant::Instant`. `instant::Instant`, when compiled natively, is just a type alias for `std::time::Instant`, which means that this change has no effect on native platforms. On platforms like `wasm32-unknown-unknown`, the `instant` crate can be compiled with either the `instant/stdweb` or `instant/wasm-bindgen` features. When compiled with these features, `instant::Instant` emulates `std::time::Instant` using features like `performance.now()`. This allows `instant` (and transitively `tessellation`) to run on WASM platforms. To compile `tessellation` with support for WASM platforms, enable either the `stdweb` or `wasm-bindgen` feature of `tessellation`. These features enable the respective `stdweb` or `wasm-bindgen` features of `instant`. Whether you should use `stdweb` or `wasm-bindgen` depends on which of these the rest of your application/library uses. Even if the `stdweb` or `wasm-bindgen` feature is enabled, the `instant` crate will continue to rely on `std::time::Instant` as long as your are not targetting wasm32. This allows for portable code that works on both native and WASM platforms. To support this behaviour, the `wasm-bindgen` feature is enabled by default. The choice of `wasm-bindgen` over `stdweb` was arbitrary. --- Cargo.toml | 6 ++++++ src/manifold_dual_contouring.rs | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e0c21cf..db005bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,14 @@ license = "Apache-2.0 OR MIT" name = "tessellation" path = "src/lib.rs" +[features] +default = ["wasm-bindgen"] +stdweb = ["instant/stdweb"] +wasm-bindgen = ["instant/wasm-bindgen"] + [dependencies] alga = "0.9" +instant = "0.1" nalgebra = "0.22" rand = "0.7" rayon = "1.2" diff --git a/src/manifold_dual_contouring.rs b/src/manifold_dual_contouring.rs index 56e38c8..57946ab 100644 --- a/src/manifold_dual_contouring.rs +++ b/src/manifold_dual_contouring.rs @@ -398,17 +398,17 @@ fn subsample_octtree>(base: &[Vertex]) -> Ve } struct Timer { - t: std::time::Instant, + t: instant::Instant, } impl Timer { fn new() -> Timer { Timer { - t: std::time::Instant::now(), + t: instant::Instant::now(), } } fn elapsed(&mut self) -> std::time::Duration { - let now = std::time::Instant::now(); + let now = instant::Instant::now(); let result = now - self.t; self.t = now; result