Skip to content

Commit

Permalink
don't parallelize gltf texture loads
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Sep 25, 2024
1 parent 4cebd5e commit 985cbb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
4 changes: 4 additions & 0 deletions crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub enum AssetServerMode {
}

impl AssetServer {
pub fn get_tasks(&self) -> Vec<UntypedAssetId> {
self.data.infos.read().pending_tasks.keys().cloned().collect()
}

/// Create a new instance of [`AssetServer`]. If `watch_for_changes` is true, the [`AssetReader`] storage will watch for changes to
/// asset sources and hot-reload them.
pub fn new(sources: AssetSources, mode: AssetServerMode, watching_for_changes: bool) -> Self {
Expand Down
73 changes: 36 additions & 37 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use bevy_render::{
},
};
use bevy_scene::Scene;
#[cfg(not(target_arch = "wasm32"))]
use bevy_tasks::IoTaskPool;
// #[cfg(not(target_arch = "wasm32"))]
// use bevy_tasks::IoTaskPool;
use bevy_transform::components::Transform;
use bevy_utils::tracing::{error, info_span, warn};
use bevy_utils::{HashMap, HashSet};
Expand Down Expand Up @@ -373,7 +373,7 @@ async fn load_gltf<'a, 'b, 'c>(
// later in the loader when looking up handles for materials. However this would mean
// that the material's load context would no longer track those images as dependencies.
let mut _texture_handles = Vec::new();
if gltf.textures().len() == 1 || cfg!(target_arch = "wasm32") {
// if gltf.textures().len() == 1 || cfg!(target_arch = "wasm32") {
for texture in gltf.textures() {
let parent_path = load_context.path().parent().unwrap();
let image = load_image(
Expand All @@ -383,41 +383,39 @@ async fn load_gltf<'a, 'b, 'c>(
parent_path,
loader.supported_compressed_formats,
settings.load_materials,
)
.await?;
)?;
process_loaded_texture(load_context, &mut _texture_handles, image);
}
} else {
#[cfg(not(target_arch = "wasm32"))]
IoTaskPool::get()
.scope(|scope| {
gltf.textures().for_each(|gltf_texture| {
let parent_path = load_context.path().parent().unwrap();
let linear_textures = &linear_textures;
let buffer_data = &buffer_data;
scope.spawn(async move {
load_image(
gltf_texture,
buffer_data,
linear_textures,
parent_path,
loader.supported_compressed_formats,
settings.load_materials,
)
.await
});
});
})
.into_iter()
.for_each(|result| match result {
Ok(image) => {
process_loaded_texture(load_context, &mut _texture_handles, image);
}
Err(err) => {
warn!("Error loading glTF texture: {}", err);
}
});
}
// } else {
// #[cfg(not(target_arch = "wasm32"))]
// IoTaskPool::get()
// .scope(|scope| {
// gltf.textures().for_each(|gltf_texture| {
// let parent_path = load_context.path().parent().unwrap();
// let linear_textures = &linear_textures;
// let buffer_data = &buffer_data;
// scope.spawn(async move {
// load_image(
// gltf_texture,
// buffer_data,
// linear_textures,
// parent_path,
// loader.supported_compressed_formats,
// settings.load_materials,
// )
// });
// });
// })
// .into_iter()
// .for_each(|result| match result {
// Ok(image) => {
// process_loaded_texture(load_context, &mut _texture_handles, image);
// }
// Err(err) => {
// warn!("Error loading glTF texture: {}", err);
// }
// });
// }

let mut materials = vec![];
let mut named_materials = HashMap::default();
Expand Down Expand Up @@ -662,6 +660,7 @@ async fn load_gltf<'a, 'b, 'c>(
);
if result.is_err() {
err = Some(result);
println!("gltf {file_name} err exit load node");
return;
}
}
Expand Down Expand Up @@ -802,7 +801,7 @@ fn paths_recur(
}

/// Loads a glTF texture as a bevy [`Image`] and returns it together with its label.
async fn load_image<'a, 'b>(
fn load_image<'a, 'b>(
gltf_texture: gltf::Texture<'a>,
buffer_data: &[Vec<u8>],
linear_textures: &HashSet<usize>,
Expand Down

0 comments on commit 985cbb2

Please sign in to comment.