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
qssgrenderuserpass.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7#include <QtQuick3DRuntimeRender/private/qssgrendercommands_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrenderbuffer_p.h>
9#include <QtQuick3DRuntimeRender/private/qssgrenderimage_p.h>
10#include <QtQuick3DRuntimeRender/private/qssgrendershadertokenizer_p.h>
11#include <QtCore/qcryptographichash.h>
12
13#include <ssg/qssgrendercontextcore.h>
14
15#include <iterator>
16
18
19Q_STATIC_LOGGING_CATEGORY(lcRenderUserPass, "qt.quick3d.runtimerender.renderuserpass")
20
21namespace
22{
23
37
39{
40 switch (macro) {
41 case AugmentMacro::BASE_COLOR:
42 return QSSGUserShaderAugmentation::FeatureFlags::BaseColor;
43 case AugmentMacro::ROUGHNESS:
44 return QSSGUserShaderAugmentation::FeatureFlags::Roughness;
45 case AugmentMacro::METALNESS:
46 return QSSGUserShaderAugmentation::FeatureFlags::Metalness;
47 case AugmentMacro::DIFFUSE_LIGHT:
48 return QSSGUserShaderAugmentation::FeatureFlags::DiffuseLight;
49 case AugmentMacro::SPECULAR_LIGHT:
50 return QSSGUserShaderAugmentation::FeatureFlags::SpecularLight;
51 case AugmentMacro::EMISSIVE_LIGHT:
52 return QSSGUserShaderAugmentation::FeatureFlags::EmissiveLight;
53 case AugmentMacro::WORLD_NORMAL:
54 return QSSGUserShaderAugmentation::FeatureFlags::WorldNormal;
55 case AugmentMacro::WORLD_TANGENT:
56 return QSSGUserShaderAugmentation::FeatureFlags::WorldTangent;
57 case AugmentMacro::WORLD_BINORMAL:
58 return QSSGUserShaderAugmentation::FeatureFlags::WorldBinormal;
59 case AugmentMacro::F0:
60 return QSSGUserShaderAugmentation::FeatureFlags::F0;
61 case AugmentMacro::F90:
62 return QSSGUserShaderAugmentation::FeatureFlags::F90;
63 }
64
65 Q_UNREACHABLE_RETURN(QSSGUserShaderAugmentation::FeatureFlags::None);
66}
67
70
71struct MacroPair {
72 const char macroName[QSSGMaxMacroLength];
73 const char replacement[QSSGMaxReplacementLength];
74};
75
76static constexpr MacroPair augmentMacroPairs[] = {
77 { "BASE_COLOR", "qt_diffuseColor" },
78 { "ROUGHNESS", "qt_roughnessAmount" },
79 { "METALNESS", "qt_metalnessAmount" },
80 { "DIFFUSE_LIGHT", "global_diffuse_light.rgb" },
81 { "SPECULAR_LIGHT", "global_specular_light" },
82 { "EMISSIVE_LIGHT", "global_emission" },
83 { "WORLD_NORMAL", "qt_world_normal" },
84 { "WORLD_TANGENT", "qt_tangent" },
85 { "WORLD_BINORMAL", "qt_binormal" },
86 { "F0", "qt_f0" },
87 { "F90", "qt_f90" }
88};
89
91{
92 return QByteArrayView(augmentMacroPairs[static_cast<size_t>(macro)].macroName);
93}
94
96{
97 return QByteArrayView(augmentMacroPairs[static_cast<size_t>(macro)].replacement);
98}
99
100}
101
102// NOTE: While this class doesn't have graphics resources itself, it manages commands that do!
103QSSGRenderUserPassBase::~QSSGRenderUserPassBase() = default;
104
105QSSGRenderUserPass::QSSGRenderUserPass()
106 : QSSGRenderUserPassBase(Type::RenderPass)
107{
108
109}
110
111QSSGRenderUserPass::~QSSGRenderUserPass()
112{
113 resetCommands();
114}
115
116void QSSGRenderUserPass::replaceAugmentMacros(QByteArray &shaderCode, QSSGUserShaderAugmentation &augmentation)
117{
118 // Tokenize the shader code and replace any identifier that matches one of the augment
119 // macros (e.g. BASE_COLOR) with the corresponding built-in variable name. Using the
120 // tokenizer means we only ever match whole identifiers - never a substring of a larger
121 // identifier - and macros inside comments are skipped, all without re-implementing that
122 // logic here.
123 QSSGShaderTokenizer tok;
124 tok.initialize(shaderCode.constData());
125
126 const char *const begin = shaderCode.constData();
127 const char *const end = begin + shaderCode.size();
128 const char *lastPos = begin; // Start of the region not yet copied to the result.
129
130 QByteArray result;
131 result.reserve(shaderCode.size());
132
133 for (QSSGShaderTokenizer::Token t = tok.next(); t != QSSGShaderTokenizer::Token_EOF; t = tok.next()) {
134 if (t != QSSGShaderTokenizer::Token_Identifier)
135 continue;
136
137 const QByteArrayView id(tok.identifier, tok.pos - tok.identifier);
138 for (size_t i = 0, n = std::size(augmentMacroPairs); i < n; ++i) {
139 const AugmentMacro macro = static_cast<AugmentMacro>(i);
140 if (id != augmentMacroName(macro))
141 continue;
142
143 // Copy everything up to the macro verbatim (preserving comments, whitespace and
144 // any non-matching code), then append the replacement in its place.
145 const QByteArrayView replacement = augmentMacroReplacement(macro);
146 result.append(lastPos, tok.identifier - lastPos);
147 result.append(replacement.data(), replacement.size());
148 lastPos = tok.pos;
149 augmentation.setFeatureFlag(featureForMacro(macro));
150 break;
151 }
152 }
153
154 // No macros were found, leave the shader code untouched.
155 if (lastPos == begin)
156 return;
157
158 // Copy the trailing remainder after the last replacement.
159 result.append(lastPos, end - lastPos);
160 shaderCode = std::move(result);
161}
162
163void QSSGRenderUserPass::finalizeShaders(const QSSGRenderContextInterface &ctx)
164{
165 Q_UNUSED(ctx);
166
167 if (materialMode != AugmentMaterial)
168 return;
169
170 if (!isDirty(DirtyFlag::ShaderDirty))
171 return;
172
173 clearDirty(DirtyFlag::ShaderDirty);
174
175 shaderAugmentation.featureFlags = 0;
176 shaderAugmentation.hash.clear();
177
178 const bool hasShaderCode = (shaderAugmentation.hasUserAugmentation());
179
180 if (hasShaderCode) {
181 qCDebug(lcRenderUserPass) << "Finalizing shaders for user pass with augment shader code.";
182
183 // Replace any instances of the AugmentMacros with the appropriate built-in variable names
184 replaceAugmentMacros(shaderAugmentation.preamble, shaderAugmentation);
185 replaceAugmentMacros(shaderAugmentation.body, shaderAugmentation);
186 // Used to build the unique shader cache key. The shader key goes in the generated
187 // shader as a one line comment so it can't be multiline. Generate hash from the
188 // augmented shader preamble and body instead, which gets combined with the material key.
189 QCryptographicHash hashData(QCryptographicHash::Algorithm::Sha1);
190 hashData.addData(shaderAugmentation.preamble);
191 hashData.addData(shaderAugmentation.body);
192 // The uniform, image and buffer declarations are generated from the
193 // properties, not taken from the preamble (processMetadata() leaves only
194 // a marker for images and buffers there), so they are part of the key too.
195 for (const auto &prop : std::as_const(shaderAugmentation.propertyUniforms)) {
196 QByteArray u(prop.name + ':' + prop.typeName);
197 if (prop.shaderDataType == QSSGRenderShaderValue::StorageBuffer) {
198 const QSSGShaderBuffer buffer(prop.value.value<QSSGShaderBuffer>());
199 u += ':' + buffer.dataType + ':' + QSSGBaseTypeHelpers::toString(buffer.qualifiers);
200 } else if (prop.shaderDataType == QSSGRenderShaderValue::Image) {
201 const QSSGShaderImage image(prop.value.value<QSSGShaderImage>());
202 u += ':' + image.imageType + ':' + image.format + ':' + QSSGBaseTypeHelpers::toString(image.qualifiers);
203 }
204 u += ';';
205 hashData.addData(u);
206 }
207 shaderAugmentation.hash = hashData.result().toHex();
208
209 m_state = State::Ready;
210 } else {
211 m_state = State::Error;
212
213 qWarning() << "No augment shader code provided for user pass.";
214 }
215
216 // Finalize shaders for all commands
217 qCDebug(lcRenderUserPass) << "Finalizing shaders for user pass commands";
218}
219
220void QSSGRenderUserPassBase::resetCommands()
221{
222 qDeleteAll(commands);
223 commands.clear();
224}
225
226QT_END_NAMESPACE
constexpr QByteArrayView augmentMacroReplacement(AugmentMacro macro)
static constexpr MacroPair augmentMacroPairs[]
static constexpr QSSGUserShaderAugmentation::FeatureFlags featureForMacro(AugmentMacro macro)
constexpr QByteArrayView augmentMacroName(AugmentMacro macro)
constexpr size_t QSSGMaxReplacementLength
constexpr size_t QSSGMaxMacroLength
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
const char replacement[QSSGMaxReplacementLength]
const char macroName[QSSGMaxMacroLength]