bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.68k stars 3.53k forks source link

Failure to load asset with the asset_server before using said asset #2706

Closed eliotbo closed 1 year ago

eliotbo commented 3 years ago

Bevy version

0.5.0

Operating system & version

Ubuntu 20.04

What you did

I cloned the bevy repo from Github. Enter "cargo run --example hot_shader_reloading" in a terminal. On my machine, the shaders sometimes don't have the time to load before they are used when instantiating a mesh.

What actually happened

This error occurs about once every 10 to 20 times that I try to run the example.

    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/examples/hot_shader_reloading`
thread 'Compute Task Pool (0)' panicked at 'called `Option::unwrap()` on a `None` value', crates/bevy_render/src/pipeline/pipeline_compiler.rs:82:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'task has failed', /home/eliotbolduc/.cargo/registry/src/github.com-1ecc6299db9ec823/async-task-4.0.3/src/task.rs:368:45

This issue becomes worst as more assets are loaded in a big project.

Temporary fix

Either add a sleep time after all the asset_server.load() calls (100 milliseconds worked) or verifying the loading states with AssetServer::get_load_state(handle)

Note

This is my first bug report ever. I am not sure if I provided enough information, or even if this bug is relevant at all... Let me know!

alice-i-cecile commented 3 years ago

Looks like a bug in our example code. We should amend it to implement one of the solutions in #1701 to avoid this type of non-deterministic behavior (and model the correct way to handle safe asset loading).

We should also probably hunt down that unwrap and give it a more informative error (or better fallback behavior).

This is my first bug report ever. I am not sure if I provided enough information, or even if this bug is relevant at all... Let me know!

Looks like a real issue! The description was great; hopefully we can clean this up easily enough.

cart commented 3 years ago

Shaders not being loaded yet should be handled properly internally by Bevy. Users shouldn't need to do manual tracking for that. If they do, this is an internal bug that must be fixed.

TuckerBMorgan commented 3 years ago

I can think of two solutions, one more desirable then the other. First, changing the asset loading for shaders to be synchronous, in that we stop everything until shaders are loaded, not the best. Second, similar to a version mentioned in another thread, we bail on a render pass as early as possible if it contains shaders that are unloaded and just let that pass finally occur once it is loaded, I think the second one is more feasible.

cart commented 3 years ago

Yeah "not drawing" things without loaded shaders is the right way to go. I'll just handle this correctly in the new renderer once we re-add pipelines as assets.

alice-i-cecile commented 2 years ago

@cart is this still valid? Your last comment says you were planning to handle this yourself :)

cart commented 1 year ago

Yeah the new renderer properly "doesn't draw" in this case, as far as I know. And in the unlikely event that this is still a problem (we havent seen this pop up anywhere), the internals are so different now that we'd want a new issue anyway.