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-shadercache.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\title Persistent Caching of Shaders with Qt Quick 3D
6\page quick3d-shadercache
7
8\section1 Shaders and 3D Materials
9
10Rendering a 3D object with a material involves a vertex and fragment shader.
11
12The following happens at run time when a material is encountered for the
13first time during the lifetime of a 3D scene:
14
15\list
16
17\li The shader source texts are generated.
18
19\li The shaders are sent through \l{Qt Shader Tools}{the standard Qt shader pipeline}.
20 The result is an intermediate bytecode version of the shader (SPIR-V), a number
21 of source code variants for other 3D APIs (GLSL for OpenGL, HLSL for Direct3D, etc.),
22 and additional metadata.
23
24\li When the 3D engine creates a \l{QRhiGraphicsPipeline}{graphics pipeline}, a bytecode or
25 source variant suitable for the 3D API used is chosen, and then the underlying 3D API will
26 likely perform a second-phase compilation targeting the actual GPU instruction set.
27
28\endlist
29
30All this together can be a costly operation. To avoid this in subsequent runs of
31the application, two persistent, disk-based caches are employed.
32
33\section1 Low-level Cache
34
35Qt Quick 3D builds on Qt Quick and the scenegraph. When it comes to persistent,
36disk-based caching of OpenGL program binaries, Vulkan pipelines, etc., the same
37infrastructure is used.
38
39See \l{QQuickGraphicsConfiguration#Pipeline Cache Save and Load} for a detailed
40discussion of this.
41
42By default the shader disk cache is enabled. This means that even when the
43platform or 3D API implementation does not perform its own persistent caching of
44compiled shaders, Qt may still store the OpenGL program binaries or similar
45assets that get collected during the lifetime of a \l QQuickWindow. Writing the
46data to disk happens when the window is closed, and it is reloaded on the
47next run of the application. This way the creation of graphics pipelines may be
48significantly faster when the shaders were encountered during previous runs of
49the application.
50
51The shaders to 3D materials and post-processing effects participate in this too.
52
53\section1 Material Shader Cache
54
55Unlike Qt Quick, Qt Quick 3D currently relies on run time generation of the
56shader source texts for 3D materials and effects. The exception for this is
57when the \l{Shadergen Tool} is used, however that feature is experimental
58and is not recommended for production use at the moment.
59
60Generating the shader source texts and then performing the first-phase
61compilation and transpilation (see \l{Qt Shader Tools} for details) can be an
62expensive operation. For 2D materials and effects this typically happens at build time,
63whereas for 3D that is not an option currently.
64
65Therefore, Qt Quick 3D employs another disk-based cache. This stores the assets
66for the generated 3D materials and effects.
67
68This means that, in combination with the low-level cache, a 3D scene with a
69material that has been encountered during the previous runs of the application
70will not lead to an expensive, blocking operation when encountered again in
71subsequent runs.
72
73\note Just as with the low-level shader and pipeline cache, this requires a
74writable filesystem and that the platform provides a writable cache location via
75QStandardPaths.
76
77The application attribute Qt::AA_DisableShaderDiskCache and the corresponding
78environment variable apply to the material shader cache as well. When set, both
79Qt's low-level and Qt Quick 3D's material cache will get disabled.
80
81\note The \c qml tool sets this attribute by default. Pass the
82\c{--enable-shader-cache} command-line argument to get a behavior identical to
83standalone Qt applications.
84
85*/