Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtquick3d-tool-shadergen.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-tool-shadergen.html
6\title Shadergen Tool
7\brief Command line tool for generating Qt Quick 3D material shaders at build-time.
8
9The shadergen tool is a command line application that is part of Qt Quick 3D's
10asset conditioning pipeline. It can be enable per project or run manually from the command line.
11Pre-generating the material shaders can have a significant impact on the start-up time and/or avoid
12unwanted stalls at run-time, as the processes of creating a material shader at run-time can be costly.
13
14\note This tool is experimental and under development, but most common use-cases should work already.
15
16One of the biggest obstacles for the offline shader generator is the amount of different materials
17that can be generated, not only based on the material properties themeself but also how the rest of
18the scene is set-up; for example, light count, light type, shadows, etc. all affect the generated
19shader(s). When we also take \l{Dynamic properties} {dynamic properties} into account, the share amount
20of material shader permutations can very quickly make it unfeasible to generate them all at build-time.
21To limit the amount of shaders the tool needs to generate, the tool tries to only generate the shaders
22it think the application needs. The heuristics used in the tool might not always be able to detect
23which materials should be generate, this is espcecially true for properties that change at run-time.
24To verify that the material shaders were successfully, and correctly, generated, the tool should have
25generated a \l{Generated content}{.qsbc} file which can be inspected to verify that the content
26matches the material used by the application. It's also possible to verify that the material
27was loaded from the pre-built cache by setting the environment variable \b QT_RHI_SHADER_DEBUG=1
28and looking at the debug output for mentions of the engine loading the \b pregenerated
29shader successfully.
30
31Known limitations are:
32\list
33 \li Scenes with more then one \l{View3D}.
34 \li Dynamically adding or removing lights are not supported when using generating materials.
35 \li The generated shaders are strictly tied to the Qt version used due to its dependency on
36 the internals of the renderer. Compatibility of the generated shaders can therefore
37 not be guaranteed between versions.
38\endlist
39
40\section1 Usage
41
42To enable offline generation of material shaders in your project, add the following to your
43project file:
44
45CMake:
46\code
47qt6_add_materials(offlineshaders "shaders"
48 PREFIX
49 "/"
50 FILES
51 ${qml_resource_files}
52)
53\endcode
54
55Alternatively the shadergen tool can be invoked manually from the command line, like this:
56
57\code
58shadergen main.qml Material.qml
59\endcode
60
61Normally the shadergen tool should be run from your application's project folder, but it's also
62possible to instruct the tool to change its current working directory through the \c -C argument.
63
64If no output path is provided then the tool will write the generated files to the current
65directory. The output path can be changed with the \c -o option.
66
67Note that for the tool to generate the expected materials it will need to know about the whole
68scene and not just the material(s), for example the number of lights in the scene does also
69affect how the materials get generated, so all relevant qml files should be added to the list of
70files the tool needs to process.
71
72\section2 Command line arguments
73
74\table
75 \header
76 \li
77 Short
78 \li
79 Full
80 \li
81 Description
82 \row
83 \li
84 -C <PATH>
85 \li
86 --directory <PATH>
87 \li
88 Changes the current directory to \c <PATH>. This argument is optional.
89 \row
90 \li
91 -o <PATH>
92 \li
93 --output-dir <PATH>
94 \li
95 Sets the output path to <PATH>. This is the location where the files generated by the tool
96 will be placed under. If no path is given the path is current directory.
97 \row
98 \li
99 -r <NAME>
100 \li
101 --resource-file <NAME>
102 \li
103 Changes the name of the generated resource file to \c <NAME>. This argument is optional.
104 \row
105 \li
106 -l <FILE>
107 \li
108 --list-qsbc <FILE>
109 \li
110 List the content of the qsbc file.
111\endtable
112
113\section1 Generated content
114
115The shadergen tools main output file is a .qsbc file. The .qsbc file contains a collection of
116\l {Qt Shader Tools} {.qsb} files plus some meta-data about the various material shaders,
117like the unique property string for each material.
118
119\sa {Qt Shader Tools} {QtShaderTools}
120
121The .qsbc file can be inspected by calling the shadergen tool with the \c -d argument:
122
123\code
124shadergen -d qtappshaders.qsbc
125\endcode
126
127\section1 Dynamic properties
128
129Since the tool is run at build-time it has limited ability to reason about which properties
130might change at run-time. Properties where a value is only changing within the properties range, for
131example the roughness value, will not have any affect on the generated material shader, but properties
132that are either \b on or \b off, e.g., setting an image map at run-time, would require a different type of
133material to be generated. It is therefore recommended that all variants of a material, which enables
134or disables features in the material, or the scene, are declared as individual components, this will
135help the tool to generated the correct material shaders.
136
137The following example shows a contrived example of a material where we want to add a base color
138map to a material at run-time. Note that the component \c MaterialRedExtended is never used in the example,
139it's purely defined to help the shadergen tool generate the shaders needed to dynamically set the \c baseColorMap at run-time.
140
141MaterialRed.qml
142
143\snippet offlineshaders/MaterialRed.qml baseMaterial
144
145MaterialRedExtended.qml
146
147\snippet offlineshaders/MaterialRedExtended.qml extendedMaterial
148
149main.qml
150
151\snippet offlineshaders/main.qml redMaterial
152
153\snippet offlineshaders/main.qml setMap
154
155*/