bevy/crates
Carter Anderson 2e79951659 Shader Imports. Decouple Mesh logic from PBR (#3137)
## 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
2021-11-18 03:45:02 +00:00
..
bevy_app Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_asset Shader Imports. Decouple Mesh logic from PBR (#3137) 2021-11-18 03:45:02 +00:00
bevy_audio Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_core Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_derive Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_diagnostic Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_dylib Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_dynamic_plugin Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_ecs Document the new pipelined renderer (#3094) 2021-11-16 03:37:48 +00:00
bevy_gilrs Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_gltf Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_input Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_internal WebGL2 support (#3039) 2021-10-29 00:46:18 +00:00
bevy_log fix tracing dependencies to before 0.3 update (#3103) 2021-11-10 22:17:54 +00:00
bevy_macro_utils Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_math Update vendored Crevice to 0.8.0 + PR for arrays (#3059) 2021-11-12 01:39:25 +00:00
bevy_pbr Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_reflect Update vendored Crevice to 0.8.0 + PR for arrays (#3059) 2021-11-12 01:39:25 +00:00
bevy_render Update vendored Crevice to 0.8.0 + PR for arrays (#3059) 2021-11-12 01:39:25 +00:00
bevy_scene Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_sprite use correct size of pixel instead of 4 (#2977) 2021-10-28 23:10:45 +00:00
bevy_tasks Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_text Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_transform Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_ui Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_utils Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_wgpu Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_window Update for edition 2021 (#2997) 2021-10-25 18:00:13 +00:00
bevy_winit WebGL2 support (#3039) 2021-10-29 00:46:18 +00:00
crevice Update vendored Crevice to 0.8.0 + PR for arrays (#3059) 2021-11-12 01:39:25 +00:00