Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_untyped has issues loading scenes from gltf files #10436

Closed
NiklasEi opened this issue Nov 7, 2023 · 3 comments · Fixed by #10514
Closed

load_untyped has issues loading scenes from gltf files #10436

NiklasEi opened this issue Nov 7, 2023 · 3 comments · Fixed by #10514
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash
Milestone

Comments

@NiklasEi
Copy link
Member

NiklasEi commented Nov 7, 2023

Bevy version

0.12.0

What you did

Load a scene using AssetServer#load_untyped

The following code crashes with a type Id miss match:

use std::any::TypeId;
use bevy::asset::LoadedUntypedAsset;
use bevy::gltf::Gltf;
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, load)
        .run();
}

#[derive(Resource)]
struct MyHandle(Handle<LoadedUntypedAsset>);

fn load(mut commands: Commands, asset_server: Res<AssetServer>) {
    println!("untyped {:?} scene {:?} gltf {:?}", TypeId::of::<LoadedUntypedAsset>(), TypeId::of::<Scene>(), TypeId::of::<Gltf>());
    commands.insert_resource(MyHandle(asset_server.load_untyped("models/AlienCake/alien.glb#Scene0")))
}

The app does not crash when directly loading the asset as a Handle<Scene> (as expected).

What went wrong

This crashes because at some point it expects a handle Id to have a different type id than it actually has (seems to be Gltf instead of the expected Scene).

Log and partial stack trace from running the code above as an example:

2023-11-08T16:58:33.116118Z  INFO bevy_winit::system: Creating new window "App" (0v0)
2023-11-08T16:58:33.116695Z  INFO winit::platform_impl::platform::x11::window: Guessed window scale factor: 1    
2023-11-08T16:58:33.320571Z  INFO bevy_render::renderer: AdapterInfo { name: "NVIDIA RTX 2000 Ada Generation Laptop GPU", vendor: 4318, device: 10424, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "535.129.03", backend: Vulkan }
untyped TypeId { t: 52159753730012975514151558583186680988 } scene TypeId { t: 312902742491752564684897985457935864593 } gltf TypeId { t: 129217342951176499515112392938364371523 }
2023-11-08T16:58:33.575293Z  INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "Linux 22.04 Ubuntu", kernel: "6.2.0-36-generic", cpu: "13th Gen Intel(R) Core(TM) i9-13950HX", core_count: "24", memory: "62.5 GiB" }
thread 'main' panicked at /home/niklas/code/bevy/crates/bevy_asset/src/id.rs:266:9:
assertion `left == right` failed: The target AssetId<bevy_scene::scene::Scene>'s TypeId does not match the TypeId of this UntypedAssetId
  left: TypeId { t: 129217342951176499515112392938364371523 }
 right: TypeId { t: 312902742491752564684897985457935864593 }
stack backtrace:
  <-- snip assert failure/panicing and unwinding --> 
  18:     0x55c495afe2c9 - bevy_asset::id::UntypedAssetId::typed::h23a6c230e3c26f34
                               at /home/niklas/code/bevy/crates/bevy_asset/src/id.rs:266:9
  19:     0x55c494f3b3b9 - <A as bevy_asset::loader::AssetContainer>::insert::h699f3d5e5a4a5014
                               at /home/niklas/code/bevy/crates/bevy_asset/src/loader.rs:238:50
  20:     0x55c497093e92 - bevy_asset::server::info::AssetInfos::process_asset_load::h1bfe159cce1417a5
                               at /home/niklas/code/bevy/crates/bevy_asset/src/server/info.rs:309:9
  21:     0x55c49711bb8d - bevy_asset::server::handle_internal_asset_events::{{closure}}::h50063b5502c785a4
                               at /home/niklas/code/bevy/crates/bevy_asset/src/server/mod.rs:856:21
  22:     0x55c497027e88 - bevy_ecs::world::World::resource_scope::h49aa2bd32f19e8ad
                               at /home/niklas/code/bevy/crates/bevy_ecs/src/world/mod.rs:1571:22
  23:     0x55c49702d05b - bevy_asset::server::handle_internal_asset_events::h323538561b60f547
                               at /home/niklas/code/bevy/crates/bevy_asset/src/server/mod.rs:851:5
  <-- snip system calling --> 
Encountered a panic in exclusive system `bevy_asset::server::handle_internal_asset_events`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

Additional information

This currently breaks loading scenes in bevy_asset_loader

@NiklasEi NiklasEi added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled A-Assets Load files from disk to use for things like images, models, and sounds P-Crash A sudden unexpected crash and removed S-Needs-Triage This issue needs to be labelled labels Nov 7, 2023
@NiklasEi
Copy link
Member Author

NiklasEi commented Nov 7, 2023

I think it's failing because get_or_create_path_handle_untyped uses the type id of the loader (here Gltf), although the type id for the requested handle might be different if loaded as a "side product" by said loader.

@rparrett rparrett added this to the 0.12.1 milestone Nov 8, 2023
@Selene-Amanita
Copy link
Member

Selene-Amanita commented Nov 8, 2023

It seems to happen here:

id: labeled_asset.handle.id(),

For some reason just before the bug happens, labeled_asset.asset.asset_type_id() and labeled_asset.handle.id().type_id are different

Edit: diving deeper:

untyped TypeId { t: 237053916966532227674992742134880052298 } scene TypeId { t: 109858301795979688433023594662162045982 } gltf TypeId { t: 125106640790734373050133807998316083405 }
<...>
[/home/nocta/Travail/Perso/Contributions/bevy/crates/bevy_asset/src/loader.rs:376] handle.clone() = StrongHandle<Scene>{ id: Index(AssetIndex { generation: 0, index: 0 }), path: Some(cup.glb#Scene0) }
[/home/nocta/Travail/Perso/Contributions/bevy/crates/bevy_asset/src/loader.rs:377] handle.clone().untyped() = StrongHandle{ type_id: TypeId { t: 125106640790734373050133807998316083405 }, id: Index(AssetIndex { generation: 0, index: 0 }), path: Some(cup.glb#Scene0) }

Edit 2: here:

loader.asset_type_id(),

path is cup.glb#Scene0
loader.asset_type_id() is 125106640790734373050133807998316083405
loader.asset_type_name() is bevy_gltf::Gltf

@Rust-Ninja-Sabi
Copy link

Rust-Ninja-Sabi commented Nov 13, 2023

have same error with gltf

github-merge-queue bot pushed a commit that referenced this issue Nov 14, 2023
# Objective

Fixes #10436
Alternative to #10465 

## Solution

`load_untyped_async` / `load_internal` currently has a bug. In
`load_untyped_async`, we pass None into `load_internal` for the
`UntypedHandle` of the labeled asset path. This results in a call to
`get_or_create_path_handle_untyped` with `loader.asset_type_id()`
This is a new code path that wasn't hit prior to the newly added
`load_untyped` because `load_untyped_async` was a private method only
used in the context of the `load_folder` impl (which doesn't have
labels)

The fix required some refactoring to catch that case and defer handle
retrieval. I have also made `load_untyped_async` public as it is now
"ready for public use" and unlocks new scenarios.
cart added a commit that referenced this issue Nov 30, 2023
# Objective

Fixes #10436
Alternative to #10465 

## Solution

`load_untyped_async` / `load_internal` currently has a bug. In
`load_untyped_async`, we pass None into `load_internal` for the
`UntypedHandle` of the labeled asset path. This results in a call to
`get_or_create_path_handle_untyped` with `loader.asset_type_id()`
This is a new code path that wasn't hit prior to the newly added
`load_untyped` because `load_untyped_async` was a private method only
used in the context of the `load_folder` impl (which doesn't have
labels)

The fix required some refactoring to catch that case and defer handle
retrieval. I have also made `load_untyped_async` public as it is now
"ready for public use" and unlocks new scenarios.
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this issue Jan 9, 2024
# Objective

Fixes bevyengine#10436
Alternative to bevyengine#10465 

## Solution

`load_untyped_async` / `load_internal` currently has a bug. In
`load_untyped_async`, we pass None into `load_internal` for the
`UntypedHandle` of the labeled asset path. This results in a call to
`get_or_create_path_handle_untyped` with `loader.asset_type_id()`
This is a new code path that wasn't hit prior to the newly added
`load_untyped` because `load_untyped_async` was a private method only
used in the context of the `load_folder` impl (which doesn't have
labels)

The fix required some refactoring to catch that case and defer handle
retrieval. I have also made `load_untyped_async` public as it is now
"ready for public use" and unlocks new scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash
Projects
None yet
4 participants