diff --git a/render-wasm/_build_env b/render-wasm/_build_env index 7f9c2e2486..8fb4ede1be 100644 --- a/render-wasm/_build_env +++ b/render-wasm/_build_env @@ -80,6 +80,9 @@ function copy_artifacts { cp target/wasm32-unknown-emscripten/$BUILD_MODE/render_wasm.js $DEST/$BUILD_NAME.js; cp target/wasm32-unknown-emscripten/$BUILD_MODE/render_wasm.wasm $DEST/$BUILD_NAME.wasm; + if [ -f target/wasm32-unknown-emscripten/$BUILD_MODE/render_wasm.wasm.map ]; then + cp target/wasm32-unknown-emscripten/$BUILD_MODE/render_wasm.wasm.map $DEST/$BUILD_NAME.wasm.map; + fi sed -i "s/render_wasm.wasm/$BUILD_NAME.wasm?version=$VERSION_TAG/g" $DEST/$BUILD_NAME.js; diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 36c00314fc..380e0e1f47 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -233,7 +233,7 @@ pub extern "C" fn set_canvas_background(raw_color: u32) -> Result<()> { #[no_mangle] #[wasm_error] -pub extern "C" fn render(_: i32) -> Result<()> { +pub extern "C" fn render(timestamp: i32) -> Result<()> { with_state_mut!(state, { state.rebuild_touched_tiles(); // Drain the throttled modifier-tile invalidation accumulated @@ -248,7 +248,7 @@ pub extern "C" fn render(_: i32) -> Result<()> { } } state - .start_render_loop(performance::get_time()) + .start_render_loop(timestamp) .map_err(|_| Error::RecoverableError("Error rendering".to_string()))?; }); Ok(()) @@ -260,7 +260,7 @@ pub extern "C" fn render_sync() -> Result<()> { with_state_mut!(state, { state.rebuild_tiles(); state - .render_sync(performance::get_time()) + .render_sync(0) .map_err(|_| Error::RecoverableError("Error rendering".to_string()))?; }); Ok(()) @@ -286,7 +286,7 @@ pub extern "C" fn render_sync_shape(a: u32, b: u32, c: u32, d: u32) -> Result<() state.rebuild_tiles_from(Some(&id)); state - .render_sync_shape(&id, performance::get_time()) + .render_sync_shape(&id, 0) .map_err(|e| Error::RecoverableError(e.to_string()))?; }); Ok(()) diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 3fa3a45e63..97564b7ac8 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -534,7 +534,7 @@ impl RenderState { options.dpr_viewport_interest_area_threshold, 1.0, ), - pending_tiles: PendingTiles::new_empty(), + pending_tiles: PendingTiles::new(), nested_fills: vec![], nested_blurs: vec![], nested_shadows: vec![], diff --git a/render-wasm/src/state.rs b/render-wasm/src/state.rs index 3cf48aae44..1ff3e2889b 100644 --- a/render-wasm/src/state.rs +++ b/render-wasm/src/state.rs @@ -8,12 +8,10 @@ pub use text_editor::*; use crate::error::{Error, Result}; use crate::render::RenderState; -use crate::shapes::Shape; +use crate::shapes::{modifiers::grid_layout::grid_cell_data, Shape}; use crate::tiles; use crate::uuid::Uuid; -use crate::shapes::modifiers::grid_layout::grid_cell_data; - /// This struct holds the state of the Rust application between JS calls. /// /// It is created by [init] and passed to the other exported functions. diff --git a/render-wasm/src/tiles.rs b/render-wasm/src/tiles.rs index 6f0786df35..7c078ad2e6 100644 --- a/render-wasm/src/tiles.rs +++ b/render-wasm/src/tiles.rs @@ -22,43 +22,95 @@ impl Tile { pub struct TileRect(pub i32, pub i32, pub i32, pub i32); impl TileRect { + pub fn empty() -> Self { + Self(0, 0, 0, 0) + } + + #[inline] pub fn x1(&self) -> i32 { self.0 } + #[inline] pub fn y1(&self) -> i32 { self.1 } + #[inline] pub fn x2(&self) -> i32 { self.2 } + #[inline] pub fn y2(&self) -> i32 { self.3 } + #[inline] + pub fn left(&self) -> i32 { + self.0 + } + + #[inline] + pub fn top(&self) -> i32 { + self.1 + } + + #[inline] + pub fn right(&self) -> i32 { + self.2 + } + + #[inline] + pub fn bottom(&self) -> i32 { + self.3 + } + + #[inline] + pub fn x(&self) -> i32 { + self.0 + } + + #[inline] + pub fn y(&self) -> i32 { + self.1 + } + + #[inline] pub fn width(&self) -> i32 { self.x2() - self.x1() } + #[inline] + pub fn half_width(&self) -> i32 { + self.width() / 2 + } + + #[inline] pub fn height(&self) -> i32 { self.y2() - self.y1() } - pub fn center_x(&self) -> i32 { - self.x1() + self.width() / 2 + #[inline] + pub fn half_height(&self) -> i32 { + self.height() / 2 } + #[inline] + pub fn center_x(&self) -> i32 { + self.x() + self.half_width() + } + + #[inline] pub fn center_y(&self) -> i32 { - self.y1() + self.height() / 2 + self.y() + self.half_height() } pub fn contains(&self, tile: &Tile) -> bool { - tile.x() >= self.x1() - && tile.y() >= self.y1() - && tile.x() <= self.x2() - && tile.y() <= self.y2() + tile.x() >= self.left() + && tile.y() >= self.top() + && tile.x() <= self.right() + && tile.y() <= self.bottom() } } @@ -195,43 +247,70 @@ impl TileHashMap { } const VIEWPORT_DEFAULT_CAPACITY: usize = 24 * 12; +const VIEWPORT_SPIRAL_DEFAULT_CAPACITY: usize = 64; // This structure keeps the list of tiles that are in the pending list, the // ones that are going to be rendered. pub struct PendingTiles { pub list: Vec, + pub spiral: Vec, + pub spiral_rect: TileRect, } impl PendingTiles { - pub fn new_empty() -> Self { + pub fn new() -> Self { Self { list: Vec::with_capacity(VIEWPORT_DEFAULT_CAPACITY), + spiral: Vec::with_capacity(VIEWPORT_SPIRAL_DEFAULT_CAPACITY), + spiral_rect: TileRect::empty(), } } - // Generate tiles ordered by distance to the center (closest processed first). - fn generate_spiral(rect: &TileRect) -> Vec { - let cx = rect.center_x(); - let cy = rect.center_y(); + // Generate tiles in spiral order from center + fn generate_spiral(columns: usize, rows: usize) -> Vec { + let total = columns * rows; + let mut result = Vec::with_capacity(total); + let mut cx = 0; + let mut cy = 0; - // TileRect is inclusive (x1..=x2, y1..=y2). - let mut tiles = Vec::new(); - for x in rect.x1()..=rect.x2() { - for y in rect.y1()..=rect.y2() { - tiles.push(Tile(x, y)); + let ratio = (columns as f32 / rows as f32).ceil() as i32; + + let mut direction_current = 0; + let mut direction_total_x = ratio; + let mut direction_total_y = 1; + let mut direction = 0; + + result.push(Tile(cx, cy)); + while result.len() < total { + match direction { + 0 => cx += 1, + 1 => cy += 1, + 2 => cx -= 1, + 3 => cy -= 1, + _ => unreachable!("Invalid direction"), + } + + result.push(Tile(cx, cy)); + + direction_current += 1; + let direction_total = if direction % 2 == 0 { + direction_total_x + } else { + direction_total_y + }; + + if direction_current == direction_total { + if direction % 2 == 0 { + direction_total_x += 1; + } else { + direction_total_y += 1; + } + direction = (direction + 1) % 4; + direction_current = 0; } } - - // We pop() from the end, so keep nearest-to-center tiles at the end. - tiles.sort_unstable_by(|a, b| { - let da = (a.x() - cx).abs() + (a.y() - cy).abs(); - let db = (b.x() - cx).abs() + (b.y() - cy).abs(); - da.cmp(&db) - .then_with(|| a.x().cmp(&b.x())) - .then_with(|| a.y().cmp(&b.y())) - }); - tiles.reverse(); - tiles + result.reverse(); + result } pub fn update(&mut self, tile_viewbox: &TileViewbox, surfaces: &Surfaces, only_visible: bool) { @@ -247,7 +326,18 @@ impl PendingTiles { } else { &tile_viewbox.interest_rect }; - let spiral = Self::generate_spiral(spiral_rect); + + self.spiral_rect = *spiral_rect; + + // We do not regenerate spiral if the spiral_rect + // doesn't change. The spiral_rect is based on the + // viewbox so, if the viewbox doesn't change + // the spiral should not change. + let total = (spiral_rect.width() * spiral_rect.height()) as usize; + if self.spiral.len() < total { + self.spiral = + Self::generate_spiral(spiral_rect.width() as usize, spiral_rect.height() as usize); + } // Partition tiles into 4 priority groups (highest priority = processed last due to pop()): // 1. visible + cached (fastest - just blit from cache) @@ -259,7 +349,9 @@ impl PendingTiles { let mut interest_cached = Vec::new(); let mut interest_uncached = Vec::new(); - for tile in spiral { + let center_tile = Tile(spiral_rect.center_x(), spiral_rect.center_y()); + for spiral_tile in self.spiral.iter() { + let tile = Tile(spiral_tile.0 + center_tile.0, spiral_tile.1 + center_tile.1); let is_visible = tile_viewbox.visible_rect.contains(&tile); let is_cached = surfaces.has_cached_tile_surface(tile);