Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMAA (Sub-pixel morphological antialiasing) support #9819

Closed
yhslai opened this issue Sep 15, 2023 · 3 comments · Fixed by #13423
Closed

SMAA (Sub-pixel morphological antialiasing) support #9819

yhslai opened this issue Sep 15, 2023 · 3 comments · Fixed by #13423
Labels
A-Rendering Drawing game state to the screen C-Enhancement A new feature

Comments

@yhslai
Copy link

yhslai commented Sep 15, 2023

What problem does this solve or what need does it fill?

Feature: Support SMAA (Sub-pixel morphological antialiasing). It's a pracital implementation of MLAA (Morphological antialiasing).
Use Case: As far as I know, It's the best antialiasing that based on full-screen postprocessing only. It has the following advantages:

  1. No temporal blur. TAA is a quite big trade-off: sometimes it looks absolutely amazing, sometimes horribly blur. There is even a dedicate subreddit for complaining about TAA: /r/fuckTAA.
  2. Render pipeline independent: Unlike MSAA, SMAA doesn't need you to change anything on the render pipeline. It's truly just a post-processing effect. Forward or deferred rendering doesn't matter.
  3. Anti-aliasing on texture: MSAA only anti-aliases the boundaries of triangles. SMAA anti-aliases the aliasing cause by low-res textures too. (Of course it's a trade-off as well. Sometimes you don't want that.)
  4. Looks better than FXAA: well never mind, anything looks better than FXAA.

What solution would you like?

Implement a SMAA post processing effect.

What alternative(s) have you considered?

TAA, MSAA, FXAA. But as I stated, SMAA has unique advantages over them.

Additional context

Official SMAA implementation: https://github.com/iryoku/smaa (MIT license)
Paper: http://www.iryoku.com/smaa/
3D Graphics Discussion: #4630 (I'm not sure if I should just posted there, but some items in that post link to separate issuse so I assume it's fine to open a new issue for further discussion/details)

@yhslai yhslai added C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels Sep 15, 2023
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Sep 15, 2023
@alice-i-cecile
Copy link
Member

Pulling this out into its own issue was the right idea :) Much easier to track this way.

@superdump
Copy link
Contributor

I think @jakobhellermann had implemented SMAA a while ago.

@Elabajaba
Copy link
Contributor

CMAA2 is a newer and faster SMAA alternative, but is worse at actually removing aliasing (and has a few other tradeoffs vs SMAA).

Also note that SMAA won't fix specular aliasing or temporal instability (though there is SMAA T2x which iirc uses the previous frame and motion vectors for a basic single history frame TAA), and specular aliasing tends to be a major problem with PBR materials.

https://www.intel.com/content/www/us/en/developer/articles/technical/conservative-morphological-anti-aliasing-20.html
https://github.com/GameTechDev/CMAA2

github-merge-queue bot pushed a commit that referenced this issue Jun 4, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Enhancement A new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants