Skip to content

Commit

Permalink
Improve processing speed (#7)
Browse files Browse the repository at this point in the history
* max cost

* add send and sync

* multi process

* fix

* comment
  • Loading branch information
nokonoko1203 authored Aug 21, 2024
1 parent 0bf28e2 commit 698097f
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 464 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ thiserror = "1.0.61"
hashbrown = { version = "0.14.5", features = ["rayon"] }
stretto = "0.8.4"
rayon = "1.10.0"
sys-info = "0.7.0"
clap = {version = "4.5.9", features = ["derive"] }

[dev-dependencies]
Expand Down
Binary file added examples/output/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/output/0.png
Binary file not shown.
40 changes: 23 additions & 17 deletions examples/test_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::Instant;

use atlas_packer::texture::{CroppedTexture, TextureSizeCache};
use rayon::prelude::*;

use atlas_packer::{
Expand Down Expand Up @@ -38,7 +39,7 @@ fn main() {
(0.3, 0.8),
(0.2, 0.7),
];
let path_string: String = format!("examples/assets/{}.png", j);
let path_string: String = format!("./examples/assets/{}.png", j);
let image_path = PathBuf::from(path_string.as_str());
polygons.push(Polygon {
id: format!("texture_{}_{}", i, j),
Expand All @@ -59,39 +60,44 @@ fn main() {
let exporter = JpegAtlasExporter::default();
let packer = Mutex::new(TexturePacker::new(placer, exporter));

// Texture cache
let texture_cache = TextureCache::new(100_000_000);

let start = Instant::now();
let packing_start = Instant::now();

// Add textures to the atlas
// cache image size
let texture_size_cache = TextureSizeCache::new();
// place textures on the atlas
polygons.par_iter().for_each(|polygon| {
let texture = texture_cache.get_or_insert(
&polygon.uv_coords,
let place_start = Instant::now();
let texture_size = texture_size_cache.get_or_insert(&polygon.texture_uri);
let cropped_texture = CroppedTexture::new(
&polygon.texture_uri,
&polygon.downsample_factor.value(),
texture_size,
&polygon.uv_coords,
polygon.downsample_factor.clone(),
);

let _ = packer
.lock()
.unwrap()
.add_texture(polygon.id.clone(), texture);
// println!("{:?}", info);
.add_texture(polygon.id.clone(), cropped_texture);
let place_duration = place_start.elapsed();
println!("{}, texture place process {:?}", polygon.id, place_duration);
});

let duration = start.elapsed();
println!("atlas process {:?}", duration);

let mut packer = packer.into_inner().unwrap();

packer.finalize();

let duration = packing_start.elapsed();
println!("all packing process {:?}", duration);

let start = Instant::now();

let output_dir = Path::new("examples/output/");
// Caches the original textures for exporting to an atlas.
let texture_cache = TextureCache::new(100_000_000);
let output_dir = Path::new("./examples/output/");
packer.export(output_dir, &texture_cache, config.width(), config.height());

let duration = start.elapsed();
println!("atlas export process {:?}", duration);
println!("all atlas export process {:?}", duration);

let duration = all_process_start.elapsed();
println!("all process {:?}", duration);
Expand Down
215 changes: 0 additions & 215 deletions examples/test_pack_dice.rs

This file was deleted.

Loading

0 comments on commit 698097f

Please sign in to comment.