Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions sites/docs/src/content/perf/antialiasing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Impeller anti-aliasing
description: How does Impeller perform anti-aliasing?
---

Aliasing is the visual artifacts that result from drawing geometry to a grid of
Comment thread
gaaclarke marked this conversation as resolved.
pixels (rasterization). The artifacts show up as jagged or rough edges on
shapes. Impeller employs a couple techniques to smooth out the mapping to raster
graphics (anti-aliasing).

## Techniques

### Multisample anti-aliasing (MSAA)

[MSAA][] is a global anti-aliasing technique that operates on the whole contents
of the screen. It is an optimization over rendering the whole screen at a larger
Comment thread
sfshaza2 marked this conversation as resolved.
scale and shrinking it down ([SSAA][]). Instead of doing the fragment operation
for each sub-sample, the calculation for the pixel is duplicated across the
sub-samples where a bounds check happens. This limits smoothing to edges. Mobile
phone GPUs have special hardware to optimize this process ( [Tiled
rendering][]). It comes in varying degrees of how many samples to consider.

On desktop and mobile 4x MSAA is used for all rendering calls.
Comment thread
gaaclarke marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a comma after "mobile" for better readability and grammatical correctness.

Suggested change
On desktop and mobile 4x MSAA is used for all rendering calls.
On desktop and mobile, 4x MSAA is used for all rendering calls.


### Signed distance fields ([SDFs][])

Typically, hardware accelerated computer graphics define a series of points and
edges (a [mesh][]) and [shaders][]. Instead, SDF renders shapes in the fragment
Comment thread
gaaclarke marked this conversation as resolved.
shader program as signed distance fields. Since the shape is defined in the
fragment shader the edges can be smoothed at the fragment level instead of
relying on the rasterization of a mesh.
Comment thread
gaaclarke marked this conversation as resolved.
Comment on lines +27 to +31

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Improve clarity, grammar, and technical accuracy:

  • Hyphenate "hardware-accelerated".
  • Clarify "SDF renders" to "Impeller renders ... using signed distance fields (SDFs)" since SDF is the representation, not the renderer.
  • Add a comma after the introductory conditional clause ("Since the shape is defined in the fragment shader,").
Suggested change
Typically, hardware accelerated computer graphics define a series of points and
edges (a [mesh][]) and [shaders][]. Instead, SDF renders shapes in the fragment
shader program as signed distance fields. Since the shape is defined in the
fragment shader the edges can be smoothed at the fragment level instead of
relying on the rasterization of a mesh.
Typically, hardware-accelerated computer graphics define a series of points and
edges (a [mesh][]) and [shaders][]. Instead, Impeller renders shapes in the
fragment shader program using signed distance fields (SDFs). Since the shape is
defined in the fragment shader, the edges can be smoothed at the fragment level
instead of relying on the rasterization of a mesh.


On desktop, rendering with SDFs is enabled by default. On mobile platforms, SDFs
are an option that defaults to false.

This technique is prioritized on desktop because SDF rendering puts more demand
on the GPU and Flutter supports older mobile phones. Also, the physical pixel
sizes on desktop computers are typically bigger than those of mobile phones. So
Comment thread
gaaclarke marked this conversation as resolved.
any imperfection will be more evident there.
Comment on lines +36 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Refine the phrasing for a more professional and technical tone:

  • Use "places higher demands on" instead of "puts more demand on".
  • Use "larger" instead of "bigger" for pixel sizes.
  • Avoid starting a sentence with "So" in technical documentation; combine the clauses to improve flow.
Suggested change
This technique is prioritized on desktop because SDF rendering puts more demand
on the GPU and Flutter supports older mobile phones. Also, the physical pixel
sizes on desktop computers are typically bigger than those of mobile phones. So
any imperfection will be more evident there.
This technique is prioritized on desktop because SDF rendering places higher
demands on the GPU, and Flutter supports older mobile phones. Also, the physical
pixel sizes on desktop computers are typically larger than those of mobile
phones, making any imperfections more noticeable.


### Examples

| No AA | MSAA 4x | MSAA 4x + SDF |
| ------------------------------------------- | ---------------------------------------------- | ------------------------------------------------------- |
| ![No AA](/assets/images/docs/perf/noaa.png) | ![MSAA 4x](/assets/images/docs/perf/msaa4.png) | ![MSAA 4x + SDF](/assets/images/docs/perf/msaa4sdf.png) |
Comment thread
gaaclarke marked this conversation as resolved.

## Working with anti-aliasing

### SDFs with the FragmentShader API

Standard primitive shapes in Flutter are drawn by default with SDFs. If a
Flutter developer wants to define their own custom graphics with SDFs they can
do so with the [FragmentShader API][]. Using the [drawPath()][] is sufficient
for most use cases without resorting to high quality SDF rendering. Not all
drawn paths are guaranteed to result in SDF rendering though.
Comment thread
sfshaza2 marked this conversation as resolved.
Comment on lines +53 to +55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Improve phrasing and grammar:

  • Remove the unnecessary article "the" before [drawPath()][].
  • Hyphenate "high-quality" when used as a compound adjective.
  • Avoid ending the sentence with "though" to maintain a professional tone.
Suggested change
do so with the [FragmentShader API][]. Using the [drawPath()][] is sufficient
for most use cases without resorting to high quality SDF rendering. Not all
drawn paths are guaranteed to result in SDF rendering though.
do so with the [FragmentShader API][]. Using [drawPath()][] is sufficient
for most use cases without resorting to high-quality SDF rendering. However,
not all drawn paths are guaranteed to result in SDF rendering.


An example of rendering SDFs with the FragmentShader API can be found at
[`simple_sdf`][].

### Enabling SDFs on iOS

SDFs can be enabled on iOS by adding a new field to the `Info.plist` for the
project.
Comment on lines +60 to +63

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The section only describes how to enable SDFs on iOS. Since the PR description mentions opting in to SDFs on "mobile devices" (plural), please clarify if there is an equivalent configuration for Android (e.g., in AndroidManifest.xml), or explicitly state if this feature is currently iOS-only.


```xml
<key>FLTEnableSDFs</key>
<true/>
```

[MSAA]: https://en.wikipedia.org/wiki/Multisample_anti-aliasing
[SSAA]: https://en.wikipedia.org/wiki/Supersampling
[Tiled rendering]: https://en.wikipedia.org/wiki/Tiled_rendering
[SDFs]: https://en.wikipedia.org/wiki/Signed_distance_function
[mesh]: https://en.wikipedia.org/wiki/Polygon_mesh
[shaders]: https://en.wikipedia.org/wiki/Shader
[FragmentShader API]: /ui/design/graphics/fragment-shaders
[drawPath()]: {{site.api}}/flutter/dart-ui/Canvas/drawPath.html
[`simple_sdf`]: https://github.com/flutter/samples/tree/main/simple_sdf
2 changes: 2 additions & 0 deletions sites/docs/src/content/perf/impeller.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ check out the [README.md][] file in the source tree.

## Additional information

* [Impeller anti-aliasing][impeller-antialiasing]
* [Frequently asked questions][impeller-faq]
* [Impeller's coordinate system][impeller-coords]
* [How to set up Xcode for GPU frame captures with metal][impeller-xcode-capture]
Expand All @@ -187,6 +188,7 @@ check out the [README.md][] file in the source tree.
* [Guidance for writing efficient shaders][impeller-shader-optimization]
* [How color blending works in Impeller][impeller-blending]

[impeller-antialiasing]: /perf/antialiasing
[impeller-faq]: {{site.repo.flutter}}/blob/main/docs/engine/impeller/docs/faq.md
[impeller-coords]: {{site.repo.flutter}}/blob/main/docs/engine/impeller/docs/coordinate_system.md
[impeller-xcode-capture]: {{site.repo.flutter}}/blob/main/docs/engine/impeller/docs/xcode_frame_capture.md
Expand Down
2 changes: 2 additions & 0 deletions sites/docs/src/data/sidenav/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@
permalink: /perf
- title: Impeller
permalink: /perf/impeller
- title: Impeller anti-aliasing
permalink: /perf/antialiasing
- title: Performance best practices
permalink: /perf/best-practices
- title: App size
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sites/docs/web/assets/images/docs/perf/noaa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading