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

pipelined-rendering sometimes panics with "Pipeline cound not be compiled because the following shader is not loaded yet" #3132

Closed
parasyte opened this issue Nov 15, 2021 · 3 comments
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Milestone

Comments

@parasyte
Copy link
Contributor

parasyte commented Nov 15, 2021

Bevy version

pipelined-rendering branch @ 029a7c0

Operating system & version

Windows 10

What you did

Rare panic when starting custom_shader_pipelined example. May happen on others, but I usually see it on this example.

Running in debug mode:

RUST_BACKTRACE=1 cargo run --example custom_shader_pipelined

What you expected to happen

No race conditions between shader loading and pipeline compiling.

What actually happened

Expand for full Backtrace:
Nov 14 18:26:59.226  INFO bevy_render2::renderer: AdapterInfo { name: "NVIDIA GeForce RTX 3090", vendor: 4318, device: 8708, device_type: DiscreteGpu, backend: Vulkan }
thread 'main' panicked at 'Pipeline cound not be compiled because the following shader is not loaded yet: WeakHandle<Shader>(AssetPathId(AssetPathId(SourcePathId(12319989330922190869), LabelId(12919124369935483856))))', pipelined\bevy_render2\src\render_resource\pipeline_cache.rs:132:46
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\/library\std\src\panicking.rs:498
   1: core::panicking::panic_fmt
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\/library\core\src\panicking.rs:106
   2: core::panicking::panic_display<ref$<enum$<bevy_render2::render_resource::pipeline_cache::RenderPipelineError> > >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\panicking.rs:62
   3: enum$<bevy_render2::render_resource::pipeline_cache::CachedPipelineState>::unwrap
             at .\pipelined\bevy_render2\src\render_resource\pipeline_cache.rs:132
   4: bevy_render2::render_phase::draw::impl$4::render<bevy_core_pipeline::Transparent3d>
             at .\pipelined\bevy_render2\src\render_phase\draw.rs:140
   5: bevy_render2::render_phase::draw::impl$21::render<bevy_core_pipeline::Transparent3d,bevy_render2::render_phase::draw::SetItemPipeline,bevy_pbr2::render::SetMeshViewBindGroup<0>,bevy_pbr2::render::SetTransformBindGroup<1>,custom_shader_pipelined::SetCustom
             at .\pipelined\bevy_render2\src\render_phase\draw.rs:160
   6: bevy_render2::render_phase::draw::impl$6::draw<bevy_core_pipeline::Transparent3d,tuple$<bevy_render2::render_phase::draw::SetItemPipeline,bevy_pbr2::render::SetMeshViewBindGroup<0>,bevy_pbr2::render::SetTransformBindGroup<1>,custom_shader_pipelined::SetCu
             at .\pipelined\bevy_render2\src\render_phase\draw.rs:192
   7: bevy_core_pipeline::main_pass_3d::impl$1::run
             at .\pipelined\bevy_core_pipeline\src\main_pass_3d.rs:95
   8: bevy_render2::renderer::graph_runner::RenderGraphRunner::run_graph
             at .\pipelined\bevy_render2\src\renderer\graph_runner.rs:178
   9: bevy_render2::renderer::graph_runner::RenderGraphRunner::run_graph
             at .\pipelined\bevy_render2\src\renderer\graph_runner.rs:187
  10: bevy_render2::renderer::graph_runner::RenderGraphRunner::run
             at .\pipelined\bevy_render2\src\renderer\graph_runner.rs:60
  11: bevy_render2::renderer::render_system
             at .\pipelined\bevy_render2\src\renderer\mod.rs:23
  12: core::ops::function::FnMut::call_mut<void (*)(ref_mut$<bevy_ecs::world::World>),tuple$<ref_mut$<bevy_ecs::world::World> > >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\ops\function.rs:150
  13: alloc::boxed::impl$45::call_mut<tuple$<ref_mut$<bevy_ecs::world::World> >,dyn$<core::ops::function::FnMut<tuple$<ref_mut$<bevy_ecs::world::World> >,assoc$<Output,tuple$<> > >,core::marker::Send,core::marker::Sync>,alloc::alloc::Global>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\alloc\src\boxed.rs:1698
  14: bevy_ecs::system::exclusive_system::impl$0::run
             at .\crates\bevy_ecs\src\system\exclusive_system.rs:42
  15: bevy_ecs::schedule::stage::impl$1::run
             at .\crates\bevy_ecs\src\schedule\stage.rs:863
  16: bevy_render2::impl$2::build::closure$1
             at .\pipelined\bevy_render2\src\lib.rs:245
  17: alloc::boxed::impl$46::call<tuple$<ref_mut$<bevy_ecs::world::World>,ref_mut$<bevy_app::app::App> >,dyn$<core::ops::function::Fn<tuple$<ref_mut$<bevy_ecs::world::World>,ref_mut$<bevy_app::app::App> >,assoc$<Output,tuple$<> > > >,alloc::alloc::Global>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\alloc\src\boxed.rs:1705
  18: bevy_app::app::App::update
             at .\crates\bevy_app\src\app.rs:95
  19: bevy_winit::winit_runner_with::closure$1
             at .\crates\bevy_winit\src\lib.rs:489
  20: winit::platform_impl::platform::event_loop::impl$2::run_return::closure$0<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:206
  21: alloc::boxed::impl$45::call_mut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> > >,dyn$<core::ops::function::FnMut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\alloc\src\boxed.rs:1698
  22: winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:245
  23: core::panic::unwind_safe::impl$23::call_once<tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\panic\unwind_safe.rs:271
  24: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panicking.rs:406
  25: winit::platform_impl::platform::raw_input::get_raw_mouse_button_state
  26: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panicking.rs:370
  27: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panic.rs:133
  28: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:152
  29: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::call_event_handler<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:239
  30: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::move_state_to<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:341
  31: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::main_events_cleared<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:227
  32: winit::platform_impl::platform::event_loop::flush_paint_messages<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:703
  33: winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:891
  34: core::ops::function::FnOnce::call_once<winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0,tuple$<> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\ops\function.rs:227
  35: core::panic::unwind_safe::impl$23::call_once<isize,winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\panic\unwind_safe.rs:271
  36: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0>,isize>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panicking.rs:406
  37: std::panicking::try::do_catch<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0>,isize>
  38: std::panicking::try<isize,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panicking.rs:370
  39: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0>,isize>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\std\src\panic.rs:133
  40: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,isize,winit::platform_impl::platform::event_loop::public_window_callback_inner::closure$0>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop\runner.rs:152
  41: winit::platform_impl::platform::event_loop::public_window_callback_inner<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:1980
  42: winit::platform_impl::platform::event_loop::public_window_callback<tuple$<> >
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:791
  43: DefSubclassProc
  44: DefSubclassProc
  45: CallWindowProcW
  46: DispatchMessageW
  47: SendMessageTimeoutW
  48: KiUserCallbackDispatcher
  49: NtUserDispatchMessage
  50: DispatchMessageW
  51: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run_return<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:221
  52: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\platform_impl\windows\event_loop.rs:191
  53: winit::event_loop::EventLoop<tuple$<> >::run<tuple$<>,bevy_winit::winit_runner_with::closure$1>
             at C:\Users\jay\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.25.0\src\event_loop.rs:154
  54: bevy_winit::run<bevy_winit::winit_runner_with::closure$1>
             at .\crates\bevy_winit\src\lib.rs:165
  55: bevy_winit::winit_runner_with
             at .\crates\bevy_winit\src\lib.rs:498
  56: bevy_winit::winit_runner
             at .\crates\bevy_winit\src\lib.rs:205
  57: core::ops::function::Fn::call<void (*)(bevy_app::app::App),tuple$<bevy_app::app::App> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\ops\function.rs:70
  58: alloc::boxed::impl$46::call<tuple$<bevy_app::app::App>,dyn$<core::ops::function::Fn<tuple$<bevy_app::app::App>,assoc$<Output,tuple$<> > > >,alloc::alloc::Global>
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\alloc\src\boxed.rs:1705
  59: bevy_app::app::App::run
             at .\crates\bevy_app\src\app.rs:123
  60: custom_shader_pipelined::main
             at .\examples\shader\custom_shader_pipelined.rs:35
  61: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/0727994435c75fdedd3e9d226cf434089b0ab585\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\examples\custom_shader_pipelined.exe` (exit code: 101)

Additional information

The panic message contains a typo. s/cound/could

@parasyte parasyte added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Nov 15, 2021
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Nov 15, 2021
@alice-i-cecile alice-i-cecile added this to the Bevy 0.6 milestone Nov 15, 2021
@cart
Copy link
Member

cart commented Nov 15, 2021

Awesome this is happening in the draw call function, which fortunately means that this should be fixed by my "shader imports" pr, which happens to add error handling to draw calls (and already handles this error cleanly).

@cart
Copy link
Member

cart commented Nov 15, 2021

Should hopefully have that out today at some point.

bors bot pushed a commit that referenced this issue Nov 18, 2021
## Shader Imports

This adds "whole file" shader imports. These come in two flavors:

### Asset Path Imports

```rust
// /assets/shaders/custom.wgsl

#import "shaders/custom_material.wgsl"

[[stage(fragment)]]
fn fragment() -> [[location(0)]] vec4<f32> {
    return get_color();
}
```

```rust
// /assets/shaders/custom_material.wgsl

[[block]]
struct CustomMaterial {
    color: vec4<f32>;
};
[[group(1), binding(0)]]
var<uniform> material: CustomMaterial;
```

### Custom Path Imports

Enables defining custom import paths. These are intended to be used by crates to export shader functionality:

```rust
// bevy_pbr2/src/render/pbr.wgsl

#import bevy_pbr::mesh_view_bind_group
#import bevy_pbr::mesh_bind_group

[[block]]
struct StandardMaterial {
    base_color: vec4<f32>;
    emissive: vec4<f32>;
    perceptual_roughness: f32;
    metallic: f32;
    reflectance: f32;
    flags: u32;
};

/* rest of PBR fragment shader here */
```

```rust
impl Plugin for MeshRenderPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        let mut shaders = app.world.get_resource_mut::<Assets<Shader>>().unwrap();
        shaders.set_untracked(
            MESH_BIND_GROUP_HANDLE,
            Shader::from_wgsl(include_str!("mesh_bind_group.wgsl"))
                .with_import_path("bevy_pbr::mesh_bind_group"),
        );
        shaders.set_untracked(
            MESH_VIEW_BIND_GROUP_HANDLE,
            Shader::from_wgsl(include_str!("mesh_view_bind_group.wgsl"))
                .with_import_path("bevy_pbr::mesh_view_bind_group"),
        );
```

By convention these should use rust-style module paths that start with the crate name. Ultimately we might enforce this convention.

Note that this feature implements _run time_ import resolution. Ultimately we should move the import logic into an asset preprocessor once Bevy gets support for that.

## Decouple Mesh Logic from PBR Logic via MeshRenderPlugin

This breaks out mesh rendering code from PBR material code, which improves the legibility of the code, decouples mesh logic from PBR logic, and opens the door for a future `MaterialPlugin<T: Material>` that handles all of the pipeline setup for arbitrary shader materials.

## Removed `RenderAsset<Shader>` in favor of extracting shaders into RenderPipelineCache

This simplifies the shader import implementation and removes the need to pass around `RenderAssets<Shader>`.

##  RenderCommands are now fallible

This allows us to cleanly handle pipelines+shaders not being ready yet. We can abort a render command early in these cases, preventing bevy from trying to bind group / do draw calls for pipelines that couldn't be bound. This could also be used in the future for things like "components not existing on entities yet". 

# Next Steps

* Investigate using Naga for "partial typed imports" (ex: `#import bevy_pbr::material::StandardMaterial`, which would import only the StandardMaterial struct)
* Implement `MaterialPlugin<T: Material>` for low-boilerplate custom material shaders
* Move shader import logic into the asset preprocessor once bevy gets support for that.

Fixes #3132
@cart
Copy link
Member

cart commented Nov 18, 2021

Resolved by #3137

@cart cart closed this as completed Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

3 participants