Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtquick3d-aa.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5
6\title Anti-Aliasing Best Practices
7\page quick3d-asset-conditioning-anti-aliasing
8
9Qt Quick 3D has multiple ways to combat aliasing (the jagged edges) while
10rendering 3D models. Each technique offers its own benefits and limitations.
11Multiple techniques can be combined, but with additional performance cost.
12
13\section1 Aliasing in General
14
15Aliasing occurs when there is more \e information present in the original than
16we can represent in the pixels on screen. Anti-aliasing techniques fall into
17three categories:
18
19\list
20
21\li Techniques that find additional information for a single pixel and
22represent them all at the same time.
23
24\li Image effects that attempt to find where things look bad and sweep the
25problems under the carpet.
26
27\li Techniques employed by artists that attempt to workaround the limitations.
28
29\endlist
30
31Although anti-aliasing is a useful tool in rendering graphics, it could affect
32performance of your application if not used wisely. The following sections
33describe a few different anti-aliasing techniques to choose from. Understanding
34which technique best targets your problems will help balance visual quality
35with sufficient rendering speed.
36
37\section2 Geometry Aliasing
38
39By default, all geometry is rendered one on-screen pixel at a time. As you can
40see on the left in the image below, this leaves harsh edges that may be easily
41noticeable in high-contrast cases, most noticeably here with black-and-white.
42
43\image AA-GeometryAliasing.png {Comparison of five antialiasing
44 techniques on rotated squares, circles, and text}
45\caption Effective techniques for reducing aliasing for geometry
46
47The most correct fix for this is to use \l{multisample-aa}{Multisample
48Anti-Aliasing}, as it gathers more geometric detail only as needed. Using
49\l{temporal-aa}{Temporal Anti-Aliasing} or \l{progressive-aa}{Progressive
50Anti-Aliasing} can also mitigate the issue in a correct manner.
51
52Finally, in certain cases you can use a \l{silhouette-opacity-maps}{silhouette
53opacity map} to smooth the edges of the geometry.
54
55\target texture-aliasing
56\section2 Texture Aliasing
57
58When a texture is sub-sampled, fewer pixels than in the original are displayed,
59resulting in undesirable artifacts based on which pixels are chosen. This
60effect is worsened when the model is moving, as different pixels are chosen at
61different times. In the image below, notice how the line between E3 and F3 is
62missing, strongly present between G3 and H3, then gone for the next 5 columns,
63and so on.
64
65\image AA-TextureAliasing.png {Comparison of four antialiasing
66 techniques on receding grid texture}
67\caption Effective techniques for reducing aliasing for textures
68
69The simplest (and arguably the best) fix for this problem is to use
70\l{mipmaps}{mipmapping in the image texture} itself. Alternative fixes include
71using either \l{temporal-aa}{Temporal AA} or \l{progressive-aa}{Progressive AA}
72to gather more information from the texture.
73
74Using \l{multisample-aa}{Multisample Anti-Aliasing} will not fix this problem.
75
76\target reflection-aliasing
77\section2 Reflection Aliasing
78
79Similar to \l{texture-aliasing}{Texture Aliasing}, a material reflecting the environment will sub-sample the image.
80In some cases, as seen on the left in the image below, it becomes obvious when fine details are being
81skipped.
82
83\image AA-ReflectionAliasing.png
84 {Comparison of four techniques for reducing reflection aliasing}
85\caption Effective techniques for reducing aliasing for reflections
86
87The most correct solution in this case is using \l{temporal-aa}{Temporal AA} or
88\l{progressive-aa}{Progressive AA} to find the extra information.
89
90A simple alternative solution that may be acceptable is to make the material less glossy, more
91rough. In this case lower-resolution mipmaps of the environment are automatically used, blending
92the sharp details together.
93
94\section1 Anti-Aliasing Techniques in Qt Quick 3D
95
96\note Check out the \l{Qt Quick 3D - Antialiasing Example}{Antialiasing
97Example} and the \l{Qt Quick 3D - Scene Effects Example}{Scene Effects Example}
98to exercise some of these features live. Keep in mind however that modern
99windowing systems are often configured to perform \l{High DPI}{High DPI
100scaling} with a high resolution screen connected. This means that the content
101of any window shown on screen is rendered at a higher resolution and is then
102scaled down by the system compositor or some other component of the platform.
103That is in effect a form of \l{supersample-aa}{Supersample Anti-aliasing}.
104Enabling antialiasing techniques in Qt Quick 3D may then show smaller, or
105sometimes hard-to-see improvements, because aliasing is already eliminated to a
106degree by the windowing system's automatic scaling. However, when deploying the
107same application on another system, it could well be that that particular
108system uses a platform or a screen where there is no such scaling, and so
109aliasing and jagged edges are more visible out of the box. Developers are
110advised to consider the potential presence, or lack of high DPI scaling in
111their target environments, and experiment with and tune antialiasing settings
112with this in mind.
113
114Below is an example rendering of the
115\l{https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Sponza}{Sponza}
116scene with some antialiasing methods enabled. The screenshots were taken
117without any system scaling applied to the window (no high DPI scaling), so the
118effects of the various methods are more pronounced.
119
120\table
121\header
122\li AA used
123\li Result
124\row
125\li No AA
126\li \image aa_disabled.jpg {Architectural scene with no antialiasing
127 showing jagged edges}
128\row
129\li Supersample AA, high (1.5x)
130\li \image aa_ssaa_high.jpg {Architectural scene with supersample
131 antialiasing at 1.5x}
132\row
133\li Multisample AA, high (4x)
134\li \image aa_msaa_high.jpg {Architectural scene with multisample
135 antialiasing at 4x}
136\row
137\li FXAA
138\li \image aa_fxaa.jpg
139 {Architectural scene with fast approximate antialiasing}
140\row
141\li Temporal AA, default strength (0.3)
142\li \image aa_temporal_default.jpg
143 {Architectural scene with temporal antialiasing}
144\endtable
145
146\target multisample-aa
147\section2 Multisample Anti-Aliasing
148
149Multisample AA (MSAA) operates either on the color buffer of the \l View3D item
150(this is the default), or, if a \l{View3D::renderMode}{renderMode} other than
151\c Offscreen is used, on the entire Qt Quick window (\l QQuickWindow, \l QQuickView,
152\l Window, \l ApplicationWindow).
153
154The edges of geometry are super-sampled, resulting in smoother silhouettes.
155This technique has no effect on the materials inside geometry, however.
156
157\list
158
159\li \b{Pros}: Good results on geometry silhouettes, where aliasing is often
160most noticeable. Works with fast animation without an issue. Many recent GPUs
161support 2x or 4x MSAA without any performance issue.
162
163\li \b{Cons}: Can be expensive to use, especially on older mobile and embedded
164hardware. Does not help with texture or reflection issues.
165
166\endlist
167
168When the View3D is using the default \l{View3D::renderMode}{renderMode} of \c
169Offscreen, the View3D itself is in full control of multisample antialiasing.
170Applications can configure this via the
171\l{SceneEnvironment::antialiasingMode}{antialiasingMode} and
172\l{SceneEnvironment::antialiasingQuality}{antialiasingQuality} properties of
173the environment (\l SceneEnvironment or \l ExtendedSceneEnvironment) associated
174with the \l View3D.
175
176The following example requests the commonly used 4x MSAA, because
177antialiasingQuality defaults to \c{SceneEnvironment.High}.
178
179\qml
180 View3D {
181 environment: SceneEnvironment {
182 antialiasingMode: SceneEnvironment.MSAA
183 }
184 }
185\endqml
186
187MSAA is not implemented by Qt itself, but is rather performed by the underlying
1883D API. Hence performance and quality may vary between different hardware and
189their 3D API implementations.
190
191\target temporal-aa
192\section2 Temporal Anti-Aliasing
193
194Temporal AA operates on the color buffer of the \l View3D. The camera is
195jiggled \e {very slightly} between frames, and the result of each new frame is
196blended with the previous frame.
197
198\list
199
200\li \b{Pros}: Due to the jiggling camera it finds real details that were
201otherwise lost. Low impact on performance.
202
203\li \b{Cons}: Fast-moving objects cause one-frame ghosting.
204
205\endlist
206
207Temporal AA has no effect when combined with Multisample AA. It can however be
208combined with Progressive AA.
209
210To control temporal anti-aliasing, use the environment's
211\l{SceneEnvironment::temporalAAEnabled}{temporalAAEnabled} and
212\l{SceneEnvironment::temporalAAStrength}{temporalAAStrength} properties.
213
214\qml
215 View3D {
216 environment: SceneEnvironment {
217 temporalAAEnabled: true
218 }
219 }
220\endqml
221
222\target progressive-aa
223\section2 Progressive Anti-Aliasing
224
225Progressive AA operates on the color buffer of the \l View3D. When all the
226content in the scene rendered by the View3D has stopped moving, the camera is
227jiggled \e {very slightly} between frames, and the result of each new frame is
228blended with the previous frames. The more frames you accumulate, better
229looking the result.
230
231\list
232
233\li \b{Pros}: Provides detailed static images with no performance cost.
234
235\li \b{Cons}: Does not take effect if any visual changes are occurring. 8x PAA
236takes one eighth of a second to finish rendering (at 60fps), which may be
237noticeable.
238
239\endlist
240
241\qml
242 View3D {
243 environment: SceneEnvironment {
244 antialiasingMode: SceneEnvironment.ProgressiveAA
245 }
246 }
247\endqml
248
249Use \l{SceneEnvironment::antialiasingQuality}{antialiasingQuality} to control
250how many frames are blended together (2, 4, or 8).
251
252\target supersample-aa
253\section2 Supersample Anti-Aliasing
254
255Supersample AA operates on the color buffer of a \l View3D. This involves
256creating a color buffer (texture) larger then its normal size, and then
257downsampling it. This means an increased resource usage and at large sizes the
258scaling operation can be costly.
259
260\list
261
262\li \b{Pros}: Provides full-scene anti-aliasing with no limitations on animation.
263
264\li \b{Cons}: Can severely degrade performance when your scene is already
265limited by the fill-rate of the graphics system, especially an older mobile and
266embedded hardware.
267
268\endlist
269
270\qml
271 View3D {
272 environment: SceneEnvironment {
273 antialiasingMode: SceneEnvironment.SSAA
274 }
275 }
276\endqml
277
278Use \l{SceneEnvironment::antialiasingQuality}{antialiasingQuality} to control
279the size multiplier (1.2, 1.5 or 2.0).
280
281\target mipmaps
282\section2 Mipmaps
283
284Mipmapping stores the texture along with its pre-calculated lower resolution
285versions. Whenever the texture is being displayed at a smaller size, the
286rendering system automatically uses these low-resolution images (which combine
287many details into fewer pixels).
288
289\list
290
291\li \b{Pros}: Low performance impact. Greatly improves image quality for
292textures.
293
294\li \b{Cons}: Requires potentially costly generation of the mipmap chain, or,
295with some image container formats, pre-generating the mipmap images in the
296image asset itself. Uses 33% more graphics memory than the same image without
297mipmaps.
298
299\endlist
300
301To have Qt generate mipmaps for a \l Texture and enable using the mipmap chain
302when performing texture sampling in the graphics shaders, set the
303\l{Texture::mipFilter}{mipFilter} and
304\l{Texture::generateMipmaps}{generateMipmaps} properties.
305
306\qml
307 Texture {
308 source: "image.png"
309 mipFilter: Texture.Linear
310 generateMipmaps: true
311 }
312\endqml
313
314\target specular-aa
315\section2 Specular Anti-Aliasing
316
317Artifacts from the specular lighting contribution may be reduced by enabling
318Specular Anti-aliasing. These artifacts typically show up as bright dots,
319perhaps with a flickering appearance.
320
321\table
322\header
323\li Specular AA disabled
324\li Specular AA enabled
325\row
326\li \image specular_aa_off.jpg
327 {Architectural scene showing specular highlights without
328 antialiasing}
329\li \image specular_aa_on.jpg
330 {Architectural scene showing smoothed specular highlights with
331 antialiasing}
332\endtable
333
334\qml
335 View3D {
336 environment: SceneEnvironment {
337 specularAAEnabled: true
338 }
339 }
340\endqml
341
342\note Materials with a very smooth appearance may change their appearance as if
343they had a \l{PrincipledMaterial::roughness}{more rough} surface when enabling
344specular AA. This is a result of the underlying lighting calculations.
345
346\target fx-aa
347\section2 Fast Approximate Anti-Aliasing
348
349\l ExtendedSceneEnvironment offers another method of anti-aliasing in form of a
350post-processing effect. To enable FXAA, set
351\l{ExtendedSceneEnvironment::fxaaEnabled}{fxaaEnabled} to true.
352
353\qml
354 import QtQuick3D.Helpers
355
356 View3D {
357 environment: ExtendedSceneEnvironment {
358 fxaaEnabled: true
359 }
360 }
361\endqml
362
363\section1 Artist-Employed Cheats
364
365\target silhouette-opacity-maps
366\section2 Silhouette Opacity Maps
367
368When your model has a consistent silhouette, you can apply an opacity map that makes the outer edge
369of the geometry transparent. Using a gradient for the opacity will let the edge of the object
370smoothly disappear. However, even if your opacity map transitions directly from fully-opaque
371to fully-transparent over the space of one pixel, the result will still provide anti-aliased edges
372as seen in the example above. This is because image maps, including opacity maps, use bilinear
373interpolation.
374
375\list
376\li
377 \b{Pros}: Can show softer transitions than normal AA. Can be applied per model instead of
378 per-layer.
379\li
380 \b{Cons}: Cannot be used if the silhouette of the object will ever change. Multiple overlapping
381 models that employ transparency consume fill rate performance, which is often at a premium.
382\endlist
383
384\target modifying-materials
385\section2 Modifying Materials or Geometry
386
387As demonstrated in the image for \l{reflection-aliasing}{Reflection Aliasing} above, sometimes the
388simplest fix for problems is to change the artwork. If you are getting distracting specular glints
389on the corner of your model, ask yourself: \e {Can I make the material softer? Can I modify the
390geometry to smooth or change the reflection angle? Can I edit the environment map to reduce
391sharp transitions?}
392
393*/