bevy/examples/3d
Patrick Walton ace4c6023b
Implement subpixel morphological antialiasing, or SMAA. (#13423)
This commit implements a large subset of [*subpixel morphological
antialiasing*], better known as SMAA. SMAA is a 2011 antialiasing
technique that detects jaggies in an aliased image and smooths them out.
Despite its age, it's been a continual staple of games for over a
decade. Four quality presets are available: *low*, *medium*, *high*, and
*ultra*. I set the default to *high*, on account of modern GPUs being
significantly faster than they were in 2011.

Like the already-implemented FXAA, SMAA works on an unaliased image.
Unlike FXAA, it requires three passes: (1) edge detection; (2) blending
weight calculation; (3) neighborhood blending. Each of the first two
passes writes an intermediate texture for use by the next pass. The
first pass also writes to a stencil buffer in order to dramatically
reduce the number of pixels that the second pass has to examine. Also
unlike FXAA, two built-in lookup textures are required; I bundle them
into the library in compressed KTX2 format.

The [reference implementation of SMAA] is in HLSL, with abundant use of
preprocessor macros to achieve GLSL compatibility. Unfortunately, the
reference implementation predates WGSL by over a decade, so I had to
translate the HLSL to WGSL manually. As much as was reasonably possible
without sacrificing readability, I tried to translate line by line,
preserving comments, both to aid reviewing and to allow patches to the
HLSL to more easily apply to the WGSL. Most of SMAA's features are
supported, but in the interests of making this patch somewhat less huge,
I skipped a few of the more exotic ones:

* The temporal variant is currently unsupported. This is and has been
used in shipping games, so supporting temporal SMAA would be useful
follow-up work. It would, however, require some significant work on TAA
to ensure compatibility, so I opted to skip it in this patch.

* Depth- and chroma-based edge detection are unimplemented; only luma
is. Depth is lower-quality, but faster; chroma is higher-quality, but
slower. Luma is the suggested default edge detection algorithm. (Note
that depth-based edge detection wouldn't work on WebGL 2 anyway, because
of the Naga bug whereby depth sampling is miscompiled in GLSL. This is
the same bug that prevents depth of field from working on that
platform.)

* Predicated thresholding is currently unsupported.

* My implementation is incompatible with SSAA and MSAA, unlike the
original; MSAA must be turned off to use SMAA in Bevy. I believe this
feature was rarely used in practice.

The `anti_aliasing` example has been updated to allow experimentation
with and testing of the different SMAA quality presets. Along the way, I
refactored the example's help text rendering code a bit to eliminate
code repetition.

SMAA is fully supported on WebGL 2.

Fixes #9819.

[*subpixel morphological antialiasing*]: https://www.iryoku.com/smaa/

[reference implementation of SMAA]: https://github.com/iryoku/smaa

## Changelog

### Added

* Subpixel morphological antialiasing, or SMAA, is now available. To use
it, add the `SmaaSettings` component to your `Camera`.

![Screenshot 2024-05-18
134311](https://github.com/bevyengine/bevy/assets/157897/ffbd611c-1b32-4491-b2e2-e410688852ee)

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-06-04 17:07:34 +00:00
..
3d_scene.rs Swapping back to using From<Color> for StandardMaterial in examples (#13566) 2024-05-29 13:50:28 +00:00
3d_shapes.rs Add subdivisions to PlaneMeshBuilder (#13580) 2024-06-03 13:57:07 +00:00
3d_viewport_to_world.rs Swapping back to using From<Color> for StandardMaterial in examples (#13566) 2024-05-29 13:50:28 +00:00
animated_material.rs Add hue traits (#12399) 2024-03-22 00:36:46 +00:00
anisotropy.rs Implement PBR anisotropy per KHR_materials_anisotropy. (#13450) 2024-06-03 23:46:06 +00:00
anti_aliasing.rs Implement subpixel morphological antialiasing, or SMAA. (#13423) 2024-06-04 17:07:34 +00:00
atmospheric_fog.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
auto_exposure.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
blend_modes.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
bloom_3d.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
clearcoat.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
color_grading.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
deferred_rendering.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
depth_of_field.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
fog.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
generate_custom_mesh.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
irradiance_volumes.rs Normalise matrix naming (#13489) 2024-06-03 16:56:53 +00:00
lighting.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
lightmaps.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
lines.rs Intern mesh vertex buffer layouts so that we don't have to compare them over and over. (#12216) 2024-03-01 20:56:21 +00:00
load_gltf.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
load_gltf_extras.rs add handling of all missing gltf extras: scene, mesh & materials (#13453) 2024-06-03 13:16:38 +00:00
meshlet.rs revert reflections PR changes to the meshlet example (#13539) 2024-05-27 19:48:18 +00:00
motion_blur.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
orthographic.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
parallax_mapping.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
parenting.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
pbr.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
reflection_probes.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
render_to_texture.rs example render_to_texture: remove extra light (#13398) 2024-05-16 23:26:55 +00:00
shadow_biases.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
shadow_caster_receiver.rs Remove redundant imports (#12817) 2024-04-01 19:59:08 +00:00
skybox.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
spherical_area_lights.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
split_screen.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
spotlight.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
ssao.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
ssr.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
texture.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
tonemapping.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
transmission.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
transparency_3d.rs Implement alpha to coverage (A2C) support. (#12970) 2024-04-15 20:37:52 +00:00
two_passes.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
update_gltf_scene.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
vertex_colors.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
visibility_range.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
volumetric_fog.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
wireframe.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00