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-architecture.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick3d-architecture.html
6\title Qt Quick 3D Architecture
7\brief An overview of the architecture of Qt Quick 3D
8\ingroup explanations-2dand3dgraphics
9
10Qt Quick 3D extends Qt Quick to support the rendering of 3D content. It adds
11extensive functionality, including several new public QML imports, as well as
12a new internal scene graph and renderer. This document describes the
13architecture of Qt Quick 3D from the public API to the details of how the
14rendering pipeline works.
15
16\section1 Module Overview
17
18Qt Quick 3D consists of several modules and plugins that expose the
19additional 3D APIs as well as utilities for conditioning and importing
20existing 3D assets.
21
22\section2 QML Imports
23
24\list
25 \li QtQuick3D - The main import which contains all the core components of
26 Qt Quick 3D
27 \li \l{QtQuick3D.AssetUtils QML Types}{QtQuick3D.AssetUtils} - A library for importing 3D assets at runtime
28 \li \l{Qt Quick 3D Helpers QML Types}{QtQuick3D.Helpers} - A library of additional components which can be
29 used to help design 3D and debug 3D scenes.
30\endlist
31
32\section2 C++ Libraries
33
34\list
35 \li \l{Qt Quick 3D C++ Classes}{QtQuick3D} - The only public C++ module.
36 Contains the definitions of all types exposed to the QtQuick3D QML import
37 as well as a few C++ APIs
38 \list
39 \li QQuick3DGeometry - Subclass to create procedural mesh data
40 \li QQuick3DTextureData - Subclass to create procedural texture data
41 \li QQuick3D::idealSurfaceFormat - used to get the ideal surface format
42 \endlist
43 \li \c QtQuick3DAssetImport - An internal and private library to aid in
44 importing assets and convert assets to QML.
45 \li \c QtQuick3DRuntimeRender - An internal and private library that
46 contains the spatial scene graph nodes and renderer.
47 \li \c QtQuick3DUtils - An internal and private library used as a common
48 utility library by all of the other C++ modules.
49\endlist
50
51\section2 AssetImporters Plugins
52The asset import tooling is implemented using a plugin based architecture. The
53plugins shipped with Qt Quick 3D extend the functionality of the asset importer
54library and tool, \l{Balsam Asset Import Tool}{Balsam}.
55\list
56 \li Assimp - This plugin uses the 3rd party library libAssimp to convert
57 3D assets in 3D interchange formats to Qt Quick 3D QML components.
58\endlist
59
60\section1 How does Qt Quick 3D fit into the Qt Graphics Stack
61
62\image quick3d-graphics-stack.drawio.svg
63 {Qt Quick 3D graphics stack architecture diagram}
64
65The above diagram illustrates how Qt Quick 3D fits into the larger Qt
66graphics stack. Qt Quick 3D works as an extension to the 2D Qt Quick API, and
67when using 3D scene items in conjunction with View3D the scene will be
68rendered via the Qt Rendering Hardware Interface (RHI). The RHI will
69translate API calls into the correct native rendering hardware API calls for
70a given platform. The diagram above shows the options available for
71each platform. If no native backend is explicitly defined, then Qt Quick will
72default to a sensible native backend for rendering for each platform.
73
74The integration between the Qt Quick 3D components of the stack and the Qt Quick
75stack are described below in the next sections.
76
77\section1 3D in 2D Integration
78
79Displaying 3D content in 2D is the primary purpose of the Qt Quick 3D API. The
80primary interface for integrating 3D content into 2D is the View3D component.
81
82The View3D component works like any other QQuickItem derived class with
83content and implements the virtual function QQuickItem::updatePaintNode. Qt
84Quick calls updatePaintNode for all "dirty" items in the Qt Quick scenegraph
85during the synchronization phase. This includes the 3D items managed by a
86View3D, which also undergo their synchronization phase as a result of the
87updatePaintNode call.
88
89The updatePaintNode method of View3D performs the following actions:
90\list
91 \li Set up a renderer and render target if one doesn't exist already
92 \li Synchronize items in the 3D scene via SceneManager
93 \li Update any "dynamic" textures that were rendered by Qt Quick (\l {Texture Path}{2D in 3D Texture path} below)
94\endlist
95
96The rendering of the 3D scene, however, does not occur in the View3D
97updatePaintNode method. Instead updatePaintNode returns a QSGNode subclass
98containing the renderer for Qt Quick 3D, which will render the 3D scene during
99the preprocess phase of the Qt Quick render process.
100
101The plumbing for how Qt Quick 3D will render depends on which
102View3D::renderMode is used:
103
104\section2 Offscreen
105
106The default mode for View3D is \l {View3D::renderMode}{Offscreen}. When using offscreen mode
107View3D becomes a texture provider by creating an offscreen surface and
108rendering to it. This surface can be mapped as a texture in Qt Quick and
109rendered with a QSGSimpleTextureNode.
110
111This pattern is very close to how QSGLayerNodes work already in Qt Quick.
112
113\section2 Underlay
114
115When using the \l {View3D::renderMode}{Underlay} mode the 3D scene is directly rendered to the
116QQuickWindow containing the View3D. Rendering occurs as a result of the signal
117QQuickWindow::beforeRenderPassRecording() which means that everything else in
118Qt Quick will be rendered on top of the 3D content.
119
120\section2 Overlay
121
122When using the \l {View3D::renderMode}{Overlay} mode the 3D scene is directly rendered to the
123QQuickWindow containing the View3D. Rendering occurs as a result of the signal
124QQuickWindow::afterRenderPassRecording() which means that the 3D content will
125be rendered on top of all other Qt Quick content.
126
127\section2 Inline
128
129The \l {View3D::renderMode}{Inline} render mode uses QSGRenderNode, which enables direct
130rendering to Qt Quick's render target without using an offscreen surface. It
131does this by injecting the render commands inline during the 2D rendering of
132the Qt Quick Scene.
133
134This mode can be problematic because it uses the same depth buffer as the
135Qt Quick renderer, and z values mean completely different things in Qt Quick
136vs Qt Quick 3D.
137
138\section1 2D in 3D Integration
139
140When rendering a 3D scene, there are many scenarios where there is a need to
141embed 2D elements into 3D. There are two different ways to integrate 2D
142content inside of 3D scenes, each of which has its own path to get to the
143screen.
144
145\section2 Direct Path
146
147The direct path is used to render 2D Qt Quick content as if it existed as an
148flat item in the 3D scene. For example, consider the following scene
149definition:
150
151\code
152Node {
153 Text {
154 text: "Hello world!"
155 }
156}
157\endcode
158
159What happens here is: when a child component is set on
160a spatial node of type QQuickItem, it is first wrapped by a
161QQuick3DItem2D, which is just a container that adds 3D coordinates to a 2D item.
162This sets the base 3D transformation for how all further 2D children are
163rendered so that they appear correctly in the 3D scene.
164
165When the time comes to render the scene, these 2D items' QSGNodes are passed to
166the Qt Quick Renderer to generate the appropriate render commands. Because the
167commands are done inline and take the current 3D transformation into
168consideration, they are rendered exactly the same as in the 2D renderer, but
169show up as if they were rendered in 3D.
170
171The drawback of this approach is that no lighting information of the 3D scene
172can be used to shade the 2D content, because the Qt Quick 2D renderer has no
173concept of lighting.
174
175\section2 Texture Path
176
177The texture path uses a 2D Qt Quick scene to create dynamic texture
178content. Consider the following Texture definition:
179
180\code
181Texture {
182 sourceItem: Item {
183 width: 256
184 height: 256
185 Text {
186 anchors.centerIn: parent
187 text: "Hello World!"
188 }
189 }
190}
191\endcode
192
193This approach works in the same way that Layer items work in Qt Quick, in that
194everything is rendered to an offscreen surface the size of the top-level Item,
195and that offscreen surface is then usable as a texture that can be reused
196elsewhere.
197
198This Texture can then be used by materials in the scene to render Qt Quick
199content on items.
200
201\section1 Scene Synchronization
202
203\section2 Scene Manager
204
205The scene manager in Qt Quick 3D is responsible for keeping track of spatial
206items in a 3D scene, and for making sure that items are updating their
207corresponding scene graph nodes during the synchronize phase. In Qt Quick,
208this role is performed by QQuickWindow for the 2D case. The scene manager is
209the primary interface between the frontend nodes and the backend scene graph
210objects.
211
212Each View3D item will have at least one Scene Manager, as one is created and
213associated with the built-in scene root on construction. When spatial nodes
214are added as children of the View3D, they are registered with the View3D's
215scene manager. When using an imported scene, a second SceneManager is created
216(or referenced if one exists already) to manage the nodes that are not direct
217children of the View3D. This is needed because, unlike the View3D, an
218imported scene doesn't exist on a QQuickWindow until it is referenced. The
219additional SceneManager makes sure that assets belonging to the imported
220scene are created at least once per QQuickWindow they are referenced in.
221
222While the scene manager is an internal API, it is important to know that the
223scene manager is responsible for calling updateSpatialNode on all objects that
224have been marked dirty by calling the update() method.
225
226\section2 Frontend/Backend Synchronization
227
228The objective of synchronization is to make sure that the states set on the
229frontend (Qt Quick) match what is set on the backend (Qt Quick Spatial Scene
230Graph Renderer). By default the frontend and backend live in separate threads:
231the frontend in the Qt Main thread, and the backend in Qt Quick's render thread. The
232synchronization phase is where the main thread and render thread can safely
233exchange data. During this phase, the scene manager will call updateSpatialNode for each dirty
234node in the scene. This will either create a new backend node or update an
235existing one for use by the renderer.
236
237\section2 Qt Quick Spatial Scene Graph
238
239Qt Quick 3D is designed to use the same frontend/backend separation pattern
240as Qt Quick: frontend objects are controlled by the Qt Quick engine, while
241backend objects contain state data for rendering the scene. Frontend objects
242inherit from QObject and are exposed to the Qt Quick engine. Items in QML
243source files map directly to frontend objects.
244
245As the properties of these frontend objects are updated, one or more backend nodes
246are created and placed into a scenegraph. Because rendering 3D scenes
247involves a lot more state than rendering 2D, there is a separate set of specialized scene
248graph nodes for representing the state of the 3D scene objects.
249This scene graph is know as the Qt Quick Spatial Scene Graph.
250
251Both the frontend objects and backend nodes can be categorized into two classes.
252The first are spatial, in the sense that they exist somewhere in the in 3D space.
253What this means in practice is that each of these types contains a transform
254matrix. For spatial items the parent child relationship is significant because
255each child item inherits the transform of its parents.
256
257The other class of items are resources. Resource items do not have a position
258in 3D space, but rather are just state that is used by other items. There can
259be a parent-child relationship between these items, but it has no other meaning
260than ownership.
261
262Unlike the 2D scene graph in Qt Quick, the spatial scene graph exposes resource
263nodes to the user directly. So for example in Qt Quick, while QSGTexture is
264public API, there is no QQuickItem that exposes this object directly. Instead
265the user must either use an Image item, which describes both where the texture
266comes from as well as how to render it, or write C++ code to operate on the
267QSGTexture itself. In Qt Quick 3D these resources are exposed directly in the
268QML API. This is necessary because resources are an important part of the scene
269state. These resources can be referenced by many objects in the scene: for
270example, many Materials could use the same Texture. It is also possible to
271set properties of a Texture at runtime that would directly change how a texture
272is sampled, for example.
273
274\section3 Spatial Objects
275
276All spatial Objects are subclasses of the Node component, which contains the
277properties defining the position, rotation, and scale in 3D space.
278
279\list
280 \li \l [QtQuick3D QML] {Node}{Node}
281 \li \l [QtQuick3D QML] {Light}{Light}
282 \list
283 \li DirectionalLight
284 \li PointLight
285 \li SpotLight
286 \endlist
287 \li \l [QtQuick3D QML] {Camera}{Camera}
288 \list
289 \li PerspectiveCamera
290 \li OrthographicCamera
291 \li FrustumCamera
292 \li CustomCamera
293 \endlist
294 \li \l [QtQuick3D QML] {Model}{Model}
295 \li Loader3D
296 \li Repeater3D
297 \li \l [QtQuick3D QML] {Skeleton}{Skeleton}
298 \li \l [QtQuick3D QML] {Joint}{Joint}
299\endlist
300
301\section3 Resource Objects
302
303Resource objects are subclasses of the Object3D component. Object3D is just a
304QObject subclass with some special helpers for use with the scene manager.
305Resource objects do have parent/child associations, but these are mostly useful
306for resource ownership.
307
308\list
309 \li \l [QtQuick3D QML] {Texture}{Texture}
310 \li \l [QtQuick3D QML] {TextureData}{TextureData}
311 \li \l [QtQuick3D QML] {Geometry}{Geometry}
312 \li \l [QtQuick3D QML] {Material}{Material}
313 \list
314 \li DefaultMaterial
315 \li PrincipledMaterial
316 \li CustomMaterial
317 \endlist
318 \li \l [QtQuick3D QML] {Effect}{Effect}
319 \li SceneEnvironment
320\endlist
321
322\section3 View3D and Render Layers
323
324With regard to the frontend/backend separation, View3D is the separation
325point from the user perspective because a View3D is what defines what scene
326content to render. In the Qt Quick Spatial Scene Graph, the root node for a
327scene that will be rendered is a Layer node. Layer nodes are created by the
328View3D using a combination of the the View3D's properties and the properties
329of the SceneEnvironment. When rendering a scene for a View3D, it is this Layer
330node that is being passed to the renderer to render a scene.
331
332\section1 Scene Rendering
333
334\image qtquick3d-rendergraph.drawio.svg {Qt Quick 3D render graph flow diagram}
335
336\section2 Set up Render Target
337
338The first step in the rendering process is to determine and set up the scene
339render target. Depending on which properties are set in the SceneEnvironment,
340the actual render target will vary. The first decision is whether content is
341being rendered directly to a window surface, or to an offscreen texture.
342By default, View3D will render to an offscreen texture. When using post
343processing effects, rendering to an offscreen texture is mandatory.
344
345Once a scene render target is determined, then some global states are set.
346\list
347 \li window size - if rendering to a window
348 \li viewport - the size of the scene area being rendered
349 \li scissor rect - the subset of a window that the viewport should be
350 clipped to
351 \li clear color - what color to clear the render target with, if any.
352\endlist
353
354\section2 Prepare for Render
355
356The next stage of rendering is the prepare stage where the renderer does
357house-keeping to figure out what needs to be rendered for a given frame,
358and that all necessary resources are available and up to date.
359
360The prepare stage itself has two phases: the high-level preparation of
361determining what is to be rendered and what resources are needed; and the
362low-level preparation that uses RHI to actually set up rendering pipelines and
363buffers, as well as setting up the rendering dependencies of the main scene pass.
364
365\section3 High level render preparation
366
367The purpose of this phase is to extract the state of the spatial scene graph
368into something that can be used to create render commands. The overview here is
369that the renderer is creating lists of geometry and material combinations to
370render from the perspective of a single camera with a set of lighting states.
371
372The first thing that is done is to determine the global common state for all
373content. If the SceneEnvironment defines a \l {SceneEnvironment::lightProbe}{lightProbe}, then it checks if the
374environment map associated with that light probe texture is loaded, and if its
375not, a new environment map is is loaded or generated. The generation of
376an environment will itself be a set of passes to convolve the source texture
377into a cube map. This cube map will contain both specular reflection information
378as well as irradiance, which is used for material shading.
379
380The next thing is that the renderer needs to determine which camera in the
381scene to use. If an active camera is not explicitly defined by a View3D, the
382first camera available in the scene is used. If there are no cameras
383in the scene, then no content is rendered and the renderer bails out.
384
385With a camera determined, it is possible to calculate the projection matrix
386for this frame. The calculation is done at this point because each renderable
387needs to know how to be projected. This also means that it is now possible to
388calculate which renderable items should be rendered. Starting with the list of
389all renderable items, we remove all items that are not visible because they
390are either disabled or completely transparent. Then, if frustum culling is
391enabled on the active camera, each renderable item is checked to see if it is
392completely outside of the view of the camera's frustum, and if so it is
393removed from the renderable list.
394
395In addition to the camera projection, the camera direction is also calculated
396as this is necessary for lighting calculations in the shading code.
397
398If there are light nodes in the scene, these are then gathered into a list the
399length of the maximum available lights available. If more light nodes exist in
400the scene than the amount of lights the renderer supports, any additional
401light nodes over that limit are ignored and don't contribute to the lighting of
402the scene. It is possible to specify the scope of light nodes, but note that
403even when setting a scope the lighting state of each light is still sent to
404every material which has lighting, but for lights not in scope the brightness
405will be set to 0, so in practice those lights will not contribute to the
406lighting of those materials.
407
408Now with a hopefully shorter list of renderables, each of these items need to
409be updated to reflect the current state of the scene. For each renderable we
410check that a suitable material is loaded, and if not a new one is created.
411A material is a combination of shaders and a rendering pipeline, and it is needed
412for creating a draw call. In addition the renderer makes sure that any
413resources needed to render a renderable is loaded, for example geometry and
414textures set on the Model. Resources that are not loaded already are
415loaded here.
416
417See \l{quick3d-shadercache}{Disk-based caching of shaders} for a discussion
418on the disk-based caching of shaders for materials and effects.
419
420The renderables list is then sorted into 3 lists.
421\list
422 \li Opaque Items: these are sorted from front to back, or in other words
423 from items that are closest to the camera to items that are furthest from the
424 camera. This is done to take advantage of hardware occlusion culling or
425 early z detection in the fragment shader.
426 \li 2D Items: these are QtQuick Items that are rendered by the Qt Quick
427 renderer.
428 \li Transparent Items: these are sorted from back to front, or in other
429 words from items that are farthest from the camera to items that are nearest
430 to the camera. This is done because transparent items need to be blended
431 with all items that are behind them.
432\endlist
433
434\section3 Low Level render preparation
435
436Now that everything that needs to be considered for this frame has been
437determined, the plumbing and dependencies for the main render pass can be
438addressed. The first thing that is done in this phase is to render any
439pre-passes that are required for the main pass.
440
441\list
442 \li Render DepthPass - Certain features like Screen Space Ambient Occlusion
443 and Shadowing require a depth pre-pass. This pass consists of all opaque
444 items being rendered to a depth texture.
445
446 \li Render SSAOPass - The objective of the Screen Space Ambient Occlusion
447 pass is to generate an ambient occlusion texture. This texture is used
448 later by materials to darken certain areas when shading.
449
450 \li Render ShadowPasses - Each light in the scene that has shadow enabled,
451 contributes to an additional shadow pass. There are two different shadowing
452 techniques employed by the renderer, so depending on the light types there
453 will be different passes. When rendering shadows from a directional light,
454 the scene is rendered to a 2D occlusion texture from a combination of the
455 directional light's direction and the size of the camera frustum. When
456 rendering shadows from a point or spot light the light's occlusion texture is
457 a cube map representing the occlusion contribution relative to each face
458 direction of the light.
459
460 \li Render ScreenTexture - This pass will only occur when using a
461 CustomMaterial that requires a screen texture, which can be used for
462 rendering tecniques such as refraction. This pass works like a depth pass,
463 but instead renders all opaque items to a color texture.
464\endlist
465
466After the dependency renders are done, the rest of the passes are prepared but
467not rendered. This preparation involves taking the state gathered in the
468high-level prep stage and translating that to graphics primitives like
469creating/updating uniform buffers values, associating samplers with dependency
470textures, setup for shader resource bindings, and everything else involved in
471creating a pipeline state necessary for performing a draw call.
472
473\section2 Scene Rendering
474
475Now that the hard work of preperation is done, the easy part is running the
476commands that contribute to the main scene's content. That rendering works
477in this order:
478
479\list
480 \li Clear Pass - This isn't really a pass, but depending on what
481 backgroundMode is set on SceneEnvironment, different things can happen here.
482 If the background mode is either transparent or color, then the color buffer
483 will be cleared with either transparency or the color specified. If, however,
484 the background mode is set to SkyBox, then a pass will be run that renders
485 the SkyBox from the perspective of the camera, which will also fill the buffer
486 with initial data.
487
488 \li Opaque Pass - Next all opaque items will be drawn. This just involves
489 setting the pipeline state, and running the draw command for each item in
490 the order in the list since they are already sorted at this point.
491
492 \li 2D Pass - If there are any 2D items in the scene, then the Qt Quick
493 renderer is invoked to generate the render commands necessary to render
494 those items.
495
496 \li Transparent Pass - Then finally the transparent items in the scene are
497 rendered one by one in the same manner as the opaque items.
498\endlist
499
500This concludes the rendering of the scene.
501
502\section2 Post-Processing
503
504If any post-processing functionality is enabled, then it can be assumed that the
505result of the scene renderer was a texture that is an input for the post
506processing phase. All post-processing methods are additional passes that
507operate on this scene input texture.
508
509All steps of the Post-Processing phase are optional, and if no built-in
510features and no user-defined effects are enabled, the output of the scene
511render is what is used by the final render target. Note however that
512\l{ExtendedSceneEnvironment::tonemapMode}{tonemapping} is enabled by default.
513
514\image qtquick3d-postprocess-graph.drawio.svg
515 {Post-processing render pass graph}
516
517\section3 Built-in Post-Processing
518
519\l ExtendedSceneEnvironment and its parent type \l SceneEnvironment offer the
520most common effects used in 3D scenes, as well as tonemapping that is used to
521map the high dynamic range color values generated by the renderer to the 0-1
522LDR range. The effects include depth of field, glow/bloom, lens flare,
523vignette, color adjustment and grading, fog, and ambient occlusion.
524
525\section3 Post-Processing Effects
526
527Applications can specify their own custom post-processing effects as an ordered
528list in the SceneEnvironment::effects property. When this list is non-empty,
529the effects in it are applied \e before the built-in effects provided by \l
530ExtendedSceneEnvironment. Each post-processing effect is part of a chain such
531that the output of the previous effect is the input for the next. The first
532effect in this chain gets its input directly from the output of the scene
533renderer step. It is also possible for effects to access the depth texture
534output of the scene renderer.
535
536Each effect in this process can consist of multiple sub-passes, which means it
537is possible to render content into intermediate buffers. The final pass of a
538multi-pass effect is expected to output a single texture containing the color
539data to be used by the next steps of the post-processing phase.
540
541\section3 Temporal and Progressive Antialiasing
542
543The Temporal and Progressive antialiasing steps are optionally enabled by
544setting properties in the SceneEnvironment. While not strictly a part of the
545post-processing phase, the actual results of Temporal and Progressive
546antialiasing are realized during the post-processing phase.
547
548Temporal Antialiasing is performed when a scene is being actively updated.
549When enabled, the active camera makes very small adjustments to the camera
550direction for each frame while drawing the scene. The current frame is then
551blended with the previously rendered frame to smooth out what was rendered.
552
553Progressive Antialiasing is only performed when a scene is not being updated.
554When enabled, an update is forced and the current state of the scene is
555rendered with very small adjustments to the active cameras direction. Up to 8
556frames are accumulated and blended together with pre-defined weights. This has
557the effect of smoothing out a non-animating scene, but comes at a
558performance cost because several extra frames will be rendered for each update.
559
560\section3 Super Sampling Antialiasing (SSAA)
561
562Super Sampling Antialiasing is a brute force way of smoothing out a scene. It
563works by rendering to a texture that is a multiple of the requested size of
564the scene, and then afterwards downsampling it to the target size. So for
565example if 2X SSAA is requested, then the scene would be rendered to a texture
566that is 2 times the intended size, and then downsampled as part of this
567phase. This can have a huge impact on performance and resource usage so
568should be avoided if possible. It's also possible for the View3D size to be
569too large to use this method, since the texture needed for this method may be
570larger than what is supported by the rendering hardware.
571
572*/