bevy/crates
Marco Buono 4465f256eb
Add MAY_DISCARD shader def, enabling early depth tests for most cases (#6697)
# Objective

- Right now we can't really benefit from [early depth
testing](https://www.khronos.org/opengl/wiki/Early_Fragment_Test) in our
PBR shader because it includes codepaths with `discard`, even for
situations where they are not necessary.

## Solution

- This PR introduces a new `MeshPipelineKey` and shader def,
`MAY_DISCARD`;
- All possible material/mesh options that that may result in `discard`s
being needed must set `MAY_DISCARD` ahead of time:
- Right now, this is only `AlphaMode::Mask(f32)`, but in the future
might include other options/effects; (e.g. one effect I'm personally
interested in is bayer dither pseudo-transparency for LOD transitions of
opaque meshes)
- Shader codepaths that can `discard` are guarded by an `#ifdef
MAY_DISCARD` preprocessor directive:
  - Right now, this is just one branch in `alpha_discard()`;
- If `MAY_DISCARD` is _not_ set, the `@early_depth_test` attribute is
added to the PBR fragment shader. This is a not yet documented, possibly
non-standard WGSL extension I found browsing Naga's source code. [I
opened a PR to document it
there](https://github.com/gfx-rs/naga/pull/2132). My understanding is
that for backends where this attribute is supported, it will force an
explicit opt-in to early depth test. (e.g. via
`layout(early_fragment_tests) in;` in GLSL)

## Caveats

- I included `@early_depth_test` for the sake of us being explicit, and
avoiding the need for the driver to be “smart” about enabling this
feature. That way, if we make a mistake and include a `discard`
unguarded by `MAY_DISCARD`, it will either produce errors or noticeable
visual artifacts so that we'll catch early, instead of causing a
performance regression.
- I'm not sure explicit early depth test is supported on the naga Metal
backend, which is what I'm currently using, so I can't really test the
explicit early depth test enable, I would like others with Vulkan/GL
hardware to test it if possible;
- I would like some guidance on how to measure/verify the performance
benefits of this;
- If I understand it correctly, this, or _something like this_ is needed
to fully reap the performance gains enabled by #6284;
- This will _most definitely_ conflict with #6284 and #6644. I can fix
the conflicts as needed, depending on whether/the order they end up
being merging in.

---

## Changelog

### Changed

- Early depth tests are now enabled whenever possible for meshes using
`StandardMaterial`, reducing the number of fragments evaluated for
scenes with lots of occlusions.
2023-05-29 15:15:01 +00:00
..
bevy_a11y Suppress the clippy::type_complexity lint (#8313) 2023-04-06 21:27:36 +00:00
bevy_animation Fixed several missing links in docs. (#8117) 2023-04-23 17:28:36 +00:00
bevy_app fix warning: variable does not need to be mutable (#8688) 2023-05-27 20:50:40 +00:00
bevy_asset update ahash and hashbrown (#8623) 2023-05-23 02:17:07 +00:00
bevy_audio Ability to set a Global Volume (#7706) 2023-04-10 14:08:43 +00:00
bevy_core Add Reflect and FromReflect for AssetPath (#8531) 2023-05-08 19:19:19 +00:00
bevy_core_pipeline Fix bloom wasm support (#8631) 2023-05-19 20:11:41 +00:00
bevy_derive bevy_derive: Add #[deref] attribute (#8552) 2023-05-16 18:29:09 +00:00
bevy_diagnostic Update sysinfo requirement from 0.28.1 to 0.29.0 (#8650) 2023-05-22 18:11:33 +00:00
bevy_dylib Suppress the clippy::type_complexity lint (#8313) 2023-04-06 21:27:36 +00:00
bevy_dynamic_plugin Update libloading requirement from 0.7 to 0.8 (#8649) 2023-05-22 20:04:15 +00:00
bevy_ecs Add documentation to UnsafeWorldCell::increment_change_tick (#8697) 2023-05-28 12:46:24 +00:00
bevy_ecs_compile_fail_tests Fix 1.69 CI clippy lints (#8450) 2023-04-20 16:51:21 +00:00
bevy_encase_derive update syn, encase, glam and hexasphere (#8573) 2023-05-16 01:24:17 +00:00
bevy_gilrs Add gamepad rumble support to bevy_input (#8398) 2023-04-24 15:28:53 +00:00
bevy_gizmos Webgpu support (#8336) 2023-05-04 22:07:57 +00:00
bevy_gltf Add support for custom glTF vertex attributes. (#5370) 2023-04-24 14:20:13 +00:00
bevy_hierarchy Expose sorting methods in Children (#8522) 2023-05-01 15:57:25 +00:00
bevy_input Fix typos in gamepad AxisSettings (#8542) 2023-05-04 23:23:45 +00:00
bevy_internal Add support for pnm textures (#8601) 2023-05-16 23:51:47 +00:00
bevy_log Hide naga info logs & Derive PartialEq on Timer and Stopwatch (#8664) 2023-05-24 15:16:15 +00:00
bevy_macro_utils update syn, encase, glam and hexasphere (#8573) 2023-05-16 01:24:17 +00:00
bevy_macros_compile_fail_tests bevy_derive: Add #[deref] attribute (#8552) 2023-05-16 18:29:09 +00:00
bevy_math update syn, encase, glam and hexasphere (#8573) 2023-05-16 01:24:17 +00:00
bevy_mikktspace update syn, encase, glam and hexasphere (#8573) 2023-05-16 01:24:17 +00:00
bevy_pbr Add MAY_DISCARD shader def, enabling early depth tests for most cases (#6697) 2023-05-29 15:15:01 +00:00
bevy_ptr Fixed several missing links in docs. (#8117) 2023-04-23 17:28:36 +00:00
bevy_reflect update syn, encase, glam and hexasphere (#8573) 2023-05-16 01:24:17 +00:00
bevy_reflect_compile_fail_tests Fix 1.69 CI clippy lints (#8450) 2023-04-20 16:51:21 +00:00
bevy_render Fix screenshots on Wayland + Nvidia (#8701) 2023-05-29 07:22:13 +00:00
bevy_scene Rename map_entities and map_specific_entities (#7570) 2023-05-01 21:40:19 +00:00
bevy_sprite Make Material2d pipeline systems public (#8642) 2023-05-23 02:16:39 +00:00
bevy_tasks Fixed several missing links in docs. (#8117) 2023-04-23 17:28:36 +00:00
bevy_text Remove unused field in FontAtlasSet (#8639) 2023-05-19 18:55:12 +00:00
bevy_time Hide naga info logs & Derive PartialEq on Timer and Stopwatch (#8664) 2023-05-24 15:16:15 +00:00
bevy_transform Fix look_to variable naming (#8627) 2023-05-23 02:17:33 +00:00
bevy_ui Move bevy_ui accessibility systems to PostUpdate. (#8653) 2023-05-23 23:50:48 +00:00
bevy_utils Allow missing docs on wasm implementation of BoxedFuture (#8674) 2023-05-26 00:29:26 +00:00
bevy_window Correct RequestRedraw documentation (#8640) 2023-05-23 02:16:56 +00:00
bevy_winit Move bevy_ui accessibility systems to PostUpdate. (#8653) 2023-05-23 23:50:48 +00:00