From 9e38da25e1a74e0d04884c874c3e48b263d53a22 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 11 Apr 2019 18:54:03 -0700 Subject: [PATCH] Remove the `TileObjectPrimitive` structure in favor of a flat list of backdrops. This reduces memory usage a bit and adds the infrastructure needed to support a tile map. --- Cargo.lock | 21 ------------------ renderer/src/builder.rs | 22 ++++++++++-------- renderer/src/gpu_data.rs | 48 ++++++++++++++-------------------------- renderer/src/scene.rs | 1 + renderer/src/tiles.rs | 11 ++++----- 5 files changed, 36 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cea8c7c9..fac3793c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,15 +169,6 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "crossbeam-channel" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "crossbeam-deque" version = "0.2.0" @@ -209,15 +200,6 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "crossbeam-utils" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "deflate" version = "0.7.19" @@ -717,7 +699,6 @@ name = "pathfinder_renderer" version = "0.1.0" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "pathfinder_geometry 0.3.0", @@ -1425,11 +1406,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum color_quant 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" "checksum combine 3.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d2623b3542b48f4427e15ddd4995186decb594ebbd70271463886584b4a114b9" "checksum crc32fast 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e91d5240c6975ef33aeb5f148f35275c25eda8e8a5f95abe421978b05b8bf192" -"checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" -"checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum deflate 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)" = "8a6abb26e16e8d419b5c78662aa9f82857c2386a073da266840e474d5055ec86" "checksum egl 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a373bc9844200b1ff15bd1b245931d1c20d09d06e4ec09f361171f29a4b0752d" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" diff --git a/renderer/src/builder.rs b/renderer/src/builder.rs index 7f7ce9e7..cd5d9cb5 100644 --- a/renderer/src/builder.rs +++ b/renderer/src/builder.rs @@ -164,8 +164,8 @@ impl SceneAssemblyThread { } // See whether we have room for the alpha tiles. If we don't, then flush. - let mut alpha_tile_count = 0; - for tile_index in 0..object.tiles.len() { + let (tile_count, mut alpha_tile_count) = (object.tile_count() as usize, 0); + for tile_index in 0..(object.tile_count() as usize) { if !object.solid_tiles[tile_index] { alpha_tile_count += 1; } @@ -177,8 +177,8 @@ impl SceneAssemblyThread { // Copy alpha tiles. let mut current_pass = &mut self.info.as_mut().unwrap().current_pass; - let mut object_tile_index_to_batch_alpha_tile_index = vec![u16::MAX; object.tiles.len()]; - for (tile_index, tile) in object.tiles.iter().enumerate() { + let mut object_tile_index_to_batch_alpha_tile_index = vec![u16::MAX; tile_count]; + for (tile_index, tile_backdrop) in object.tile_backdrops.iter().cloned().enumerate() { // Skip solid tiles. if object.solid_tiles[tile_index] { continue; @@ -187,9 +187,12 @@ impl SceneAssemblyThread { let batch_alpha_tile_index = current_pass.alpha_tiles.len() as u16; object_tile_index_to_batch_alpha_tile_index[tile_index] = batch_alpha_tile_index; + let tile_coords = object.tile_index_to_coords(tile_index as u32); current_pass.alpha_tiles.push(AlphaTileBatchPrimitive { - tile: *tile, + tile_x: tile_coords.x() as i16, + tile_y: tile_coords.y() as i16, object_index: current_pass.object_range.end as u16, + backdrop: tile_backdrop, }); } @@ -244,15 +247,16 @@ impl SceneAssemblyThread { fn cull_alpha_tiles(&mut self) { let info = self.info.as_mut().unwrap(); for alpha_tile in &mut info.current_pass.alpha_tiles { - let scene_tile_index = scene::scene_tile_index(alpha_tile.tile.tile_x as i32, - alpha_tile.tile.tile_y as i32, + let scene_tile_index = scene::scene_tile_index(alpha_tile.tile_x as i32, + alpha_tile.tile_y as i32, info.tile_rect); if info.z_buffer.test(scene_tile_index, alpha_tile.object_index as u32) { continue; } + // FIXME(pcwalton): Hack! - alpha_tile.tile.tile_x = -1; - alpha_tile.tile.tile_y = -1; + alpha_tile.tile_x = -1; + alpha_tile.tile_y = -1; } } } diff --git a/renderer/src/gpu_data.rs b/renderer/src/gpu_data.rs index 0aadd0fa..9d5a7b16 100644 --- a/renderer/src/gpu_data.rs +++ b/renderer/src/gpu_data.rs @@ -14,7 +14,7 @@ use crate::scene::ObjectShader; use crate::tiles::{self, TILE_HEIGHT, TILE_WIDTH}; use fixedbitset::FixedBitSet; use pathfinder_geometry::basic::line_segment::{LineSegmentF32, LineSegmentU4, LineSegmentU8}; -use pathfinder_geometry::basic::point::{Point2DF32, Point3DF32}; +use pathfinder_geometry::basic::point::{Point2DF32, Point2DI32, Point3DF32}; use pathfinder_geometry::basic::rect::{RectF32, RectI32}; use pathfinder_geometry::util; use pathfinder_simd::default::{F32x4, I32x4}; @@ -25,7 +25,7 @@ use std::ops::Add; pub struct BuiltObject { pub bounds: RectF32, pub tile_rect: RectI32, - pub tiles: Vec, + pub tile_backdrops: Vec, pub fills: Vec, pub solid_tiles: FixedBitSet, } @@ -53,14 +53,6 @@ pub struct FillObjectPrimitive { pub tile_y: i16, } -#[derive(Clone, Copy, Debug)] -#[repr(C)] -pub struct TileObjectPrimitive { - pub tile_x: i16, - pub tile_y: i16, - pub backdrop: i16, -} - // FIXME(pcwalton): Move `subpx` before `px` and remove `repr(packed)`. #[derive(Clone, Copy, Debug)] #[repr(packed)] @@ -81,7 +73,9 @@ pub struct SolidTileBatchPrimitive { #[derive(Clone, Copy, Debug)] #[repr(C)] pub struct AlphaTileBatchPrimitive { - pub tile: TileObjectPrimitive, + pub tile_x: i16, + pub tile_y: i16, + pub backdrop: i16, pub object_index: u16, } @@ -102,17 +96,16 @@ impl BuiltObject { // Allocate tiles. let tile_count = tile_rect.size().x() as usize * tile_rect.size().y() as usize; - let mut tiles = Vec::with_capacity(tile_count); - for y in tile_rect.min_y()..tile_rect.max_y() { - for x in tile_rect.min_x()..tile_rect.max_x() { - tiles.push(TileObjectPrimitive::new(x as i16, y as i16)); - } - } - + let tile_backdrops = vec![0; tile_count]; let mut solid_tiles = FixedBitSet::with_capacity(tile_count); solid_tiles.insert_range(..); - BuiltObject { bounds, tile_rect, tiles, fills: vec![], solid_tiles } + BuiltObject { bounds, tile_rect, tile_backdrops, fills: vec![], solid_tiles } + } + + #[inline] + pub fn tile_count(&self) -> u32 { + self.tile_rect.size().x() as u32 * self.tile_rect.size().y() as u32 } // TODO(pcwalton): SIMD-ify `tile_x` and `tile_y`. @@ -249,12 +242,10 @@ impl BuiltObject { } } - pub fn get_tile_mut(&mut self, tile_x: i32, tile_y: i32) -> Option<&mut TileObjectPrimitive> { - let tile_index = self.tile_coords_to_index(tile_x, tile_y); - match tile_index { - None => None, - Some(tile_index) => Some(&mut self.tiles[tile_index as usize]), - } + #[inline] + pub fn tile_index_to_coords(&self, tile_index: u32) -> Point2DI32 { + let (width, tile_index) = (self.tile_rect.size().x(), tile_index as i32); + self.tile_rect.origin() + Point2DI32::new(tile_index % width, tile_index / width) } } @@ -289,13 +280,6 @@ impl Debug for RenderCommand { } } -impl TileObjectPrimitive { - #[inline] - fn new(tile_x: i16, tile_y: i16) -> TileObjectPrimitive { - TileObjectPrimitive { tile_x, tile_y, backdrop: 0 } - } -} - impl Add for Stats { type Output = Stats; fn add(self, other: Stats) -> Stats { diff --git a/renderer/src/scene.rs b/renderer/src/scene.rs index ec836bdb..498501dc 100644 --- a/renderer/src/scene.rs +++ b/renderer/src/scene.rs @@ -194,6 +194,7 @@ pub struct ObjectShader { pub fill_color: ColorU, } +// FIXME(pcwalton): This duplicates code in `BuiltObject::tile_coords_to_index`! // TODO(pcwalton): Use a `Point2DI32` here? #[inline] pub fn scene_tile_index(tile_x: i32, tile_y: i32, tile_rect: RectI32) -> u32 { diff --git a/renderer/src/tiles.rs b/renderer/src/tiles.rs index 79454e61..e7eca791 100644 --- a/renderer/src/tiles.rs +++ b/renderer/src/tiles.rs @@ -95,9 +95,9 @@ impl<'o, 'z> Tiler<'o, 'z> { fn cull(&self) { for solid_tile_index in self.built_object.solid_tiles.ones() { - let tile = &self.built_object.tiles[solid_tile_index]; - if tile.backdrop != 0 { - self.z_buffer.update(tile.tile_x as i32, tile.tile_y as i32, self.object_index); + if self.built_object.tile_backdrops[solid_tile_index] != 0 { + let tile_coords = self.built_object.tile_index_to_coords(solid_tile_index as u32); + self.z_buffer.update(tile_coords.x(), tile_coords.y(), self.object_index); } } } @@ -163,8 +163,9 @@ impl<'o, 'z> Tiler<'o, 'z> { // Move over to the correct tile, filling in as we go. while current_tile_x < segment_tile_x { //println!("... emitting backdrop {} @ tile {}", current_winding, current_tile_x); - if let Some(tile) = self.built_object.get_tile_mut(current_tile_x, tile_y) { - tile.backdrop = current_winding; + if let Some(tile_index) = + self.built_object.tile_coords_to_index(current_tile_x, tile_y) { + self.built_object.tile_backdrops[tile_index as usize] = current_winding; } current_tile_x += 1;