Skip to content

Commit

Permalink
Add summary messages
Browse files Browse the repository at this point in the history
  • Loading branch information
neocturne committed Sep 19, 2023
1 parent a8eb2da commit 7d37f6a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 34 deletions.
38 changes: 29 additions & 9 deletions src/core/region_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{ffi::OsStr, path::Path, time::SystemTime};
use anyhow::{Context, Result};
use indexmap::IndexSet;
use rayon::prelude::*;
use tracing::{debug, error};
use tracing::{debug, error, info};

use super::common::*;
use crate::{
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'a> RegionProcessor<'a> {
}

/// Processes a single region file
fn process_region(&self, coords: TileCoords) -> Result<()> {
fn process_region(&self, coords: TileCoords) -> Result<bool> {
/// Width/height of the region data
const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32;

Expand All @@ -150,7 +150,7 @@ impl<'a> RegionProcessor<'a> {
if Some(input_timestamp) <= output_timestamp && Some(input_timestamp) <= lightmap_timestamp
{
debug!("Skipping unchanged region r.{}.{}.mca", coords.x, coords.z);
return Ok(());
return Ok(false);
}

debug!("Processing region r.{}.{}.mca", coords.x, coords.z);
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<'a> RegionProcessor<'a> {
Self::save_lightmap(&lightmap_path, &lightmap, input_timestamp)?;
}

Ok(())
Ok(true)
}

/// Iterates over all region files of a Minecraft save directory
Expand All @@ -203,11 +203,31 @@ impl<'a> RegionProcessor<'a> {
fs::create_dir_all(&self.config.processed_dir)?;
fs::create_dir_all(&self.config.tile_dir(TileKind::Lightmap, 0))?;

regions.par_iter().for_each(|&coords| {
if let Err(err) = self.process_region(coords) {
error!("Failed to process region {:?}: {:?}", coords, err);
}
});
info!("Processing region files...");

let mut results = vec![];
regions
.par_iter()
.map(|&coords| {
let result = self.process_region(coords);
if let Err(err) = &result {
error!("Failed to process region {:?}: {:?}", coords, err);
}
result
})
.collect_into_vec(&mut results);

let processed = results
.iter()
.filter(|result| matches!(result, Ok(true)))
.count();
let errors = results.iter().filter(|result| result.is_err()).count();
info!(
"Processed region files ({} processed, {} unchanged, {} errors)",
processed,
results.len() - processed,
errors,
);

Ok(regions)
}
Expand Down
60 changes: 43 additions & 17 deletions src/core/tile_mipmapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use anyhow::{Context, Result};
use rayon::prelude::*;
use tracing::{debug, warn};
use tracing::{debug, info, warn};

use super::common::*;
use crate::{io::fs, types::*};
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<'a> TileMipmapper<'a> {
level: usize,
coords: TileCoords,
prev: &TileCoordMap,
) -> Result<()>
) -> Result<(bool, bool)>
where
[P::Subpixel]: image::EncodableLayout,
image::ImageBuffer<P, Vec<P::Subpixel>>: Into<image::DynamicImage>,
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'a> TileMipmapper<'a> {
.collect();

let Some(input_timestamp) = sources.iter().map(|(_, _, ts)| *ts).max() else {
return Ok(());
return Ok((false, false));
};

if Some(input_timestamp) <= output_timestamp {
Expand All @@ -104,7 +104,7 @@ impl<'a> TileMipmapper<'a> {
.expect("tile path must be in output directory")
.display(),
);
return Ok(());
return Ok((true, false));
}

debug!(
Expand Down Expand Up @@ -143,7 +143,9 @@ impl<'a> TileMipmapper<'a> {
image
.write_to(file, image::ImageFormat::Png)
.context("Failed to save image")
})
})?;

Ok((true, true))
}

/// Runs the mipmap generation
Expand All @@ -165,24 +167,48 @@ impl<'a> TileMipmapper<'a> {
break;
}

info!("Generating level {} mipmaps...", level);

fs::create_dir_all(&self.config.tile_dir(TileKind::Map, level))?;
fs::create_dir_all(&self.config.tile_dir(TileKind::Lightmap, level))?;

let next = Self::map_coords(prev);

next.0.par_iter().try_for_each(|(&z, xs)| {
xs.par_iter().try_for_each(|&x| {
let coords = TileCoords { x, z };
self.render_mipmap::<image::Rgba<u8>>(TileKind::Map, level, coords, prev)?;
self.render_mipmap::<image::LumaA<u8>>(
TileKind::Lightmap,
level,
coords,
prev,
)?;
anyhow::Ok(())
let (total, processed) = next
.0
.par_iter()
.flat_map(|(&z, xs)| {
let mipmapper = &self;
xs.par_iter().map(move |&x| {
let coords = TileCoords { x, z };
let (found_map, processed_map) = mipmapper
.render_mipmap::<image::Rgba<u8>>(TileKind::Map, level, coords, prev)?;
let (found_light, processed_light) = mipmapper
.render_mipmap::<image::LumaA<u8>>(
TileKind::Lightmap,
level,
coords,
prev,
)?;
anyhow::Ok((
usize::from(found_map) + usize::from(found_light),
usize::from(processed_map) + usize::from(processed_light),
))
})
})
})?;
.try_reduce(
|| (0, 0),
|(found_a, processed_a), (found_b, processed_b)| {
Ok((found_a + found_b, processed_a + processed_b))
},
)?;

info!(
"Generated level {} mipmaps ({} processed, {} unchanged)",
level,
processed,
total - processed,
);

tile_stack.push(next);
}
Expand Down
33 changes: 25 additions & 8 deletions src/core/tile_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use glam::Vec3;
use lru::LruCache;
use rayon::prelude::*;
use tokio::sync::OnceCell;
use tracing::debug;
use tracing::{debug, info};

use super::{common::*, region_group::RegionGroup};
use crate::{
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<'a> TileRenderer<'a> {
}

/// Renders and saves a region tile image
fn render_tile(&self, coords: TileCoords) -> Result<()> {
fn render_tile(&self, coords: TileCoords) -> Result<bool> {
/// Width/height of a tile image
const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32;

Expand All @@ -280,7 +280,7 @@ impl<'a> TileRenderer<'a> {
.expect("tile path must be in output directory")
.display(),
);
return Ok(());
return Ok(false);
}

debug!(
Expand All @@ -307,18 +307,35 @@ impl<'a> TileRenderer<'a> {
.write_to(file, image::ImageFormat::Png)
.context("Failed to save image")
},
)
)?;

Ok(true)
}

/// Runs the tile generation
pub fn run(self) -> Result<()> {
fs::create_dir_all(&self.config.tile_dir(TileKind::Map, 0))?;

info!("Rendering map tiles...");

// Use par_bridge to process items in order (for better use of region cache)
self.regions.iter().par_bridge().try_for_each(|&coords| {
self.render_tile(coords)
.with_context(|| format!("Failed to render tile {:?}", coords))
})?;
let processed = self
.regions
.iter()
.par_bridge()
.map(|&coords| {
anyhow::Ok(usize::from(
self.render_tile(coords)
.with_context(|| format!("Failed to render tile {:?}", coords))?,
))
})
.try_reduce(|| 0, |a, b| Ok(a + b))?;

info!(
"Rendered map tiles ({} processed, {} unchanged)",
processed,
self.regions.len() - processed,
);

Ok(())
}
Expand Down

0 comments on commit 7d37f6a

Please sign in to comment.