bevy/assets/shaders
IceSentry b3224e135b Add depth and normal prepass (#6284)
# Objective

- Add a configurable prepass
- A depth prepass is useful for various shader effects and to reduce overdraw. It can be expansive depending on the scene so it's important to be able to disable it if you don't need any effects that uses it or don't suffer from excessive overdraw.
- The goal is to eventually use it for things like TAA, Ambient Occlusion, SSR and various other techniques that can benefit from having a prepass.

## Solution

The prepass node is inserted before the main pass. It runs for each `Camera3d` with a prepass component (`DepthPrepass`, `NormalPrepass`). The presence of one of those components is used to determine which textures are generated in the prepass. When any prepass is enabled, the depth buffer generated will be used by the main pass to reduce overdraw.

The prepass runs for each `Material` created with the `MaterialPlugin::prepass_enabled` option set to `true`. You can overload the shader used by the prepass by using `Material::prepass_vertex_shader()` and/or `Material::prepass_fragment_shader()`. It will also use the `Material::specialize()` for more advanced use cases. It is enabled by default on all materials.

The prepass works on opaque materials and materials using an alpha mask. Transparent materials are ignored.

The `StandardMaterial` overloads the prepass fragment shader to support alpha mask and normal maps.

---

## Changelog

- Add a new `PrepassNode` that runs before the main pass
- Add a `PrepassPlugin` to extract/prepare/queue the necessary data
- Add a `DepthPrepass` and `NormalPrepass` component to control which textures will be created by the prepass and available in later passes.
- Add a new `prepass_enabled` flag to the `MaterialPlugin` that will control if a material uses the prepass or not.
- Add a new `prepass_enabled` flag to the `PbrPlugin` to control if the StandardMaterial uses the prepass. Currently defaults to false.
- Add `Material::prepass_vertex_shader()` and `Material::prepass_fragment_shader()` to control the prepass from the `Material`

## Notes

In bevy's sample 3d scene, the performance is actually worse when enabling the prepass, but on more complex scenes the performance is generally better. I would like more testing on this, but @DGriffin91 has reported a very noticeable improvements in some scenes.

The prepass is also used by @JMS55 for TAA and GTAO

discord thread: <https://discord.com/channels/691052431525675048/1011624228627419187>

This PR was built on top of the work of multiple people

Co-Authored-By: @superdump 
Co-Authored-By: @robtfm 
Co-Authored-By: @JMS55 

Co-authored-by: Charles <IceSentry@users.noreply.github.com>
Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com>
2023-01-19 22:11:13 +00:00
..
animate_shader.wgsl add globals to mesh view bind group (#5409) 2022-09-28 04:20:27 +00:00
array_texture.wgsl bevy_pbr: Fix incorrect and unnecessary normal-mapping code (#5766) 2022-11-03 20:37:32 +00:00
cubemap_unlit.wgsl Support array / cubemap / cubemap array textures in KTX2 (#5325) 2022-07-30 07:02:58 +00:00
custom_material.frag Update shader_material_glsl example to include texture sampling (#5215) 2022-07-08 01:14:22 +00:00
custom_material.vert Update shader_material_glsl example to include texture sampling (#5215) 2022-07-08 01:14:22 +00:00
custom_material.wgsl update wgpu to 0.13 (#5168) 2022-07-14 21:17:16 +00:00
custom_material_chromatic_aberration.wgsl adjust cluster index for viewport origin (#5947) 2022-09-15 21:58:14 +00:00
custom_material_screenspace_texture.wgsl adjust cluster index for viewport origin (#5947) 2022-09-15 21:58:14 +00:00
custom_vertex_attribute.wgsl update wgpu to 0.13 (#5168) 2022-07-14 21:17:16 +00:00
game_of_life.wgsl Remove unused code in game of life shader (#5349) 2022-07-17 15:24:24 +00:00
instancing.wgsl update wgpu to 0.13 (#5168) 2022-07-14 21:17:16 +00:00
line_material.wgsl Fix line material shader (#5348) 2022-07-17 15:02:57 +00:00
shader_defs.wgsl update wgpu to 0.13 (#5168) 2022-07-14 21:17:16 +00:00
show_prepass.wgsl Add depth and normal prepass (#6284) 2023-01-19 22:11:13 +00:00