Skip to content

Commit

Permalink
Do not try to decode in parallel on WASM
Browse files Browse the repository at this point in the history
(there's no threading at all)
  • Loading branch information
Speykious committed Jan 31, 2024
1 parent ed4887d commit f2225c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
7 changes: 3 additions & 4 deletions inox2d-opengl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::ops::Deref;
use gl_buffer::RenderCtxOpenglExt;
use glam::{uvec2, Mat4, UVec2, Vec2, Vec3};
use glow::HasContext;
use inox2d::texture::{parallel_decode_model_textures, TextureId};
use tracing::{debug, error};
use inox2d::texture::{decode_model_textures, TextureId};

use inox2d::math::camera::Camera;
use inox2d::model::{Model, ModelTexture};
Expand Down Expand Up @@ -188,11 +187,11 @@ impl OpenglRenderer {

fn upload_model_textures(&mut self, model_textures: &[ModelTexture]) -> Result<(), TextureError> {
// decode textures in parallel
let shalltexs = parallel_decode_model_textures(model_textures.iter(), None);
let shalltexs = decode_model_textures(model_textures.iter());

// upload textures
for (i, shalltex) in shalltexs.iter().enumerate() {
debug!("Uploading shallow texture {:?}", i);
tracing::debug!("Uploading shallow texture {:?}", i);
let tex = texture::Texture::from_shallow_texture(&self.gl, shalltex)?;
self.textures.push(tex);
}
Expand Down
4 changes: 2 additions & 2 deletions inox2d-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use inox2d::render::RenderCtxKind;

use encase::ShaderType;
use glam::{vec3, Mat4, UVec2, Vec2, Vec3};
use inox2d::texture::parallel_decode_model_textures;
use inox2d::texture::decode_model_textures;
use tracing::warn;
use wgpu::{util::DeviceExt, *};

Expand Down Expand Up @@ -52,7 +52,7 @@ impl Renderer {
..SamplerDescriptor::default()
});

let shalltexs = parallel_decode_model_textures(model.textures.iter(), None);
let shalltexs = decode_model_textures(model.textures.iter());
for shalltex in &shalltexs {
let texture_size = wgpu::Extent3d {
width: shalltex.width(),
Expand Down
34 changes: 23 additions & 11 deletions inox2d/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::io;
use std::sync::mpsc;

use image::{ImageBuffer, ImageError, ImageFormat, Rgba};
use tracing::error;
Expand Down Expand Up @@ -86,23 +85,36 @@ fn decode_texture(mtex: ModelTexture) -> Result<ShallowTexture, DecodeTextureErr
}
}

pub fn parallel_decode_model_textures<'a>(
#[cfg(target_arch = "wasm32")]
pub fn decode_model_textures<'a>(
model_textures: impl ExactSizeIterator<Item = &'a ModelTexture>,
n_threads: Option<usize>,
) -> Vec<ShallowTexture> {
(model_textures.cloned())
.map(decode_texture)
.inspect(|res| {
if let Err(e) = res {
tracing::error!("{}", e);
}
})
.filter_map(Result::ok)
.collect::<Vec<_>>()
}

/// Decodes model textures in parallel, using as many threads as we can use minus one.
#[cfg(not(target_arch = "wasm32"))]
pub fn decode_model_textures<'a>(
model_textures: impl ExactSizeIterator<Item = &'a ModelTexture>,
) -> Vec<ShallowTexture> {
use std::sync::mpsc;

// get number of optimal threads from computer
let mut max_num_threads = std::thread::available_parallelism().unwrap().get();
let mut num_threads = std::thread::available_parallelism().unwrap().get();

// remove at least one thread to not torture the computer
if max_num_threads > 1 {
max_num_threads -= 1;
if num_threads > 1 {
num_threads -= 1;
}

let mut num_threads = match n_threads {
Some(n_threads) => n_threads.min(max_num_threads),
None => max_num_threads,
};

// do not use more threads than there are images
if num_threads > model_textures.len() {
num_threads = model_textures.len();
Expand Down

0 comments on commit f2225c9

Please sign in to comment.