Skip to content

Commit 8bafb14

Browse files
committed
store compact coords as u8 memory to half memory usage, flag and skip invalid map id
1 parent a835be8 commit 8bafb14

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

large-scale-viz/high_perf_character_render/src/bin/render_compact_runs.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Result};
22
use clap::Parser;
3-
use sprite_video_renderer::data::CoordinateMapper;
3+
use sprite_video_renderer::data::{CoordinateMapper, INVALID_MAP_ID_FLAG};
44
use sprite_video_renderer::rendering::{GpuContext, SpriteInstance, SpriteRenderer, TextureAtlas};
55
use sprite_video_renderer::video::ProResEncoder;
66
use std::fs::File;
@@ -59,10 +59,18 @@ struct CompactCoord {
5959
map_id: u16,
6060
}
6161

62+
#[repr(C, packed)]
63+
#[derive(Debug, Clone, Copy)]
64+
struct UltraCompactCoordMem {
65+
x: u8,
66+
y: u8,
67+
map_id: u8,
68+
}
69+
6270
#[derive(Debug)]
6371
struct CompactRun {
6472
sprite_id: u8,
65-
coords: Vec<CompactCoord>,
73+
coords: Vec<UltraCompactCoordMem>,
6674
}
6775

6876
fn main() -> Result<()> {
@@ -170,6 +178,11 @@ async fn run() -> Result<()> {
170178
let current_pos = coordinate_mapper.convert_coords(&current_coords);
171179
let next_pos = coordinate_mapper.convert_coords(&next_coords);
172180

181+
// map id doesn't exist! (probably transitioning between world)
182+
if current_pos == INVALID_MAP_ID_FLAG || next_pos == INVALID_MAP_ID_FLAG {
183+
continue;
184+
}
185+
173186
// Check pixel distance - only interpolate if moving <= 16 pixels (1 tile)
174187
let pixel_dx = (next_pos[0] - current_pos[0]).abs();
175188
let pixel_dy = (next_pos[1] - current_pos[1]).abs();
@@ -290,7 +303,9 @@ fn load_compact_runs(path: &PathBuf) -> Result<Vec<CompactRun>> {
290303
let mut runs = Vec::new();
291304
let mut buffer = vec![0u8; 1024 * 1024]; // 1MB buffer for reading
292305

293-
loop {
306+
let mut skipped_runs: u32 = 0;
307+
308+
'runs_loop: loop {
294309
// Read sprite_id
295310
let mut sprite_id_buf = [0u8; 1];
296311
match reader.read_exact(&mut sprite_id_buf) {
@@ -319,13 +334,36 @@ fn load_compact_runs(path: &PathBuf) -> Result<Vec<CompactRun>> {
319334
let coord = unsafe {
320335
std::ptr::read_unaligned(buffer[offset..].as_ptr() as *const CompactCoord)
321336
};
322-
coords.push(coord);
337+
let x = coord.x;
338+
let y = coord.y;
339+
let map_id = coord.map_id;
340+
if x > u8::MAX as u16 {
341+
//log::error!("got x coord with value: {}", x);
342+
skipped_runs += 1;
343+
continue 'runs_loop;
344+
}
345+
if y > u8::MAX as u16 {
346+
//log::error!("got y coord with value: {}", y);
347+
skipped_runs += 1;
348+
continue 'runs_loop;
349+
}
350+
if map_id > u8::MAX as u16 {
351+
//log::error!("got map_id with value: {}", map_id);
352+
skipped_runs += 1;
353+
continue 'runs_loop;
354+
}
355+
let packy_coords = UltraCompactCoordMem {
356+
x: x as u8,
357+
y: y as u8,
358+
map_id: map_id as u8,
359+
};
360+
coords.push(packy_coords);
323361
}
324362

325363
runs.push(CompactRun { sprite_id, coords });
326364

327365
if runs.len() % 100000 == 0 {
328-
log::info!("Loaded {} runs...", runs.len());
366+
log::info!("Loaded {} runs, skipped {}...", runs.len(), skipped_runs);
329367
}
330368
}
331369

large-scale-viz/high_perf_character_render/src/data/coordinate_mapper.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct CoordinateMapper {
3030
regions: HashMap<i64, MapRegion>,
3131
}
3232

33+
pub const INVALID_MAP_ID_FLAG: [f32; 2] = [117117.0, 117117.0];
34+
3335
impl CoordinateMapper {
3436
/// Load map data from JSON file
3537
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
@@ -71,7 +73,7 @@ impl CoordinateMapper {
7173
[x, y]
7274
} else {
7375
log::warn!("No map coordinate location for id: {}", map_region_id);
74-
[4096.0, 4096.0] // Center of canvas if no region found
76+
INVALID_MAP_ID_FLAG // invalid for example 255 is seen sometimes
7577
}
7678
}
7779
}

large-scale-viz/high_perf_character_render/src/data/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ pub mod parquet_reader;
33
pub mod sprite_data;
44

55
pub use coordinate_mapper::CoordinateMapper;
6+
pub use coordinate_mapper::INVALID_MAP_ID_FLAG;
67
pub use parquet_reader::{ParquetFilter, ParquetReader};
78
pub use sprite_data::{AnimationState, Direction, SpriteFrame, SpriteInstance, SpriteSequence};

0 commit comments

Comments
 (0)