bevy/crates/bevy_render/src
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
..
camera Windows as Entities (#5589) 2023-01-19 00:38:28 +00:00
color Improve Color::hex performance (#6940) 2023-01-17 13:26:43 +00:00
mesh Update Box vertices comment (#7055) 2022-12-29 23:45:07 +00:00
primitives Very minor doc formatting changes (#5287) 2022-07-12 13:06:16 +00:00
render_graph Replace UUID based IDs with a atomic-counted ones (#6988) 2022-12-25 00:23:15 +00:00
render_phase Reduce the use of atomics in the render phase (#7084) 2023-01-18 02:19:19 +00:00
render_resource Make PipelineCache internally mutable. (#7205) 2023-01-16 15:41:14 +00:00
renderer Reduce branching in TrackedRenderPass (#7053) 2023-01-09 19:24:56 +00:00
texture Add depth and normal prepass (#6284) 2023-01-19 22:11:13 +00:00
view Windows as Entities (#5589) 2023-01-19 00:38:28 +00:00
extract_component.rs ExtractComponent output optional associated type (#6699) 2022-11-21 13:19:44 +00:00
extract_param.rs Implement ReadOnlySystemParam for Extract<> (#7182) 2023-01-13 22:35:43 +00:00
extract_resource.rs gate an import used only for a debug assert (#7165) 2023-01-11 20:52:04 +00:00
globals.rs bevy_reflect: Register missing reflected types for bevy_render (#6725) 2022-11-23 00:41:21 +00:00
lib.rs Windows as Entities (#5589) 2023-01-19 00:38:28 +00:00
render_asset.rs Fix clippy::iter_with_drain (#6485) 2022-11-06 01:42:15 +00:00
settings.rs Replace WgpuAdapterInfo with RenderAdapterInfo in the documentation. (#7036) 2022-12-26 19:47:01 +00:00
spatial_bundle.rs enum Visibility component (#6320) 2022-12-25 00:39:29 +00:00