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