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
qssgvertexpipelineimpl_p.h
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6
7#ifndef QSSG_VERTEX_PIPELINE_IMPL_H
8#define QSSG_VERTEX_PIPELINE_IMPL_H
9
10//
11// W A R N I N G
12// -------------
13//
14// This file is not part of the Qt API. It exists purely as an
15// implementation detail. This header file may change from version to
16// version without notice, or even be removed.
17//
18// We mean it.
19//
20
21#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterialshadergenerator_p.h>
22#include <QtQuick3DRuntimeRender/private/qssgrendershaderkeys_p.h>
23
24#include <QtCore/QSharedPointer>
25
26QT_BEGIN_NAMESPACE
27
28struct QSSGMaterialVertexPipeline
29{
30 enum class GenerationFlag
31 {
32 UVCoords = 1,
33 EnvMapReflection = 1 << 1,
34 ViewVector = 1 << 2,
35 WorldNormal = 1 << 3,
36 ObjectNormal = 1 << 4,
37 WorldPosition = 1 << 5,
38 TangentBinormal = 1 << 6,
39 UVCoords1 = 1 << 7,
40 VertexColor = 1 << 8,
41 PerspDivDepth = 1 << 9,
42 PerspDivWorldPos = 1 << 10
43 };
44
45 typedef QHash<QByteArray, QByteArray> TStrTableStrMap;
46 typedef TStrTableStrMap::const_iterator TParamIter;
47 typedef QFlags<GenerationFlag> GenerationFlags;
48
49 QSSGProgramGenerator *m_programGenerator = nullptr;
50 QString m_tempString;
51
52 GenerationFlags m_generationFlags;
53 bool m_hasSkinning;
54 bool m_needsSkinning;
55 bool m_hasMorphing;
56 QList<QByteArray> m_addedFunctions;
57 int m_viewCount = 1;
58
59 const QSSGShaderDefaultMaterialKeyProperties &defaultMaterialShaderKeyProperties;
60 QSSGShaderMaterialAdapter *materialAdapter;
61 bool useFloatJointIndices;
62 bool hasCustomShadedMain;
63 bool usesInstanceBuffer;
64 bool usesBufferlessInstancing;
65 bool usesInstancing;
66 bool skipCustomFragmentSnippet;
67
68 QSSGMaterialVertexPipeline(QSSGProgramGenerator &inProgram,
69 const QSSGShaderDefaultMaterialKeyProperties &materialProperties,
70 QSSGShaderMaterialAdapter *materialAdapter);
71
72 ~QSSGMaterialVertexPipeline() = default;
73
74 // Trues true if the code was *not* set.
75 bool setCode(GenerationFlag inCode)
76 {
77 if (m_generationFlags & inCode)
78 return true;
79 m_generationFlags |= inCode;
80 return false;
81 }
82 bool hasCode(GenerationFlag inCode) { return (m_generationFlags & inCode); }
83 QSSGProgramGenerator *programGenerator() const { return m_programGenerator; }
84
85 QSSGStageGeneratorBase &vertex()
86 {
87 return *programGenerator()->getStage(QSSGShaderGeneratorStage::Vertex);
88 }
89 QSSGStageGeneratorBase &fragment()
90 {
91 return *programGenerator()->getStage(QSSGShaderGeneratorStage::Fragment);
92 }
93
94 /**
95 * @brief Generates UV coordinates in shader code
96 *
97 * @param[in] inUVSet index of UV data set
98 *
99 * @return no return
100 */
101 void generateUVCoords(quint32 inUVSet, const QSSGShaderDefaultMaterialKey &inKey)
102 {
103 if (inUVSet == 0 && setCode(GenerationFlag::UVCoords))
104 return;
105 if (inUVSet == 1 && setCode(GenerationFlag::UVCoords1))
106 return;
107
108 const bool meshHasUV0 = hasAttributeInKey(QSSGShaderKeyVertexAttribute::TexCoord0, inKey);
109 const bool meshHasUV1 = hasAttributeInKey(QSSGShaderKeyVertexAttribute::TexCoord1, inKey);
110
111 Q_ASSERT(inUVSet == 0 || inUVSet == 1);
112
113 if (inUVSet == 0) {
114 if (hasCustomShadedMain || meshHasUV0) {
115 addInterpolationParameter("qt_varTexCoord0", "vec2");
116 if (m_hasMorphing)
117 vertex().append(" qt_vertUV0 = qt_getTargetTex0(qt_vertUV0);");
118 vertex() << " qt_varTexCoord0 = qt_vertUV0;\n";
119 fragment() <<" vec2 qt_texCoord0 = qt_varTexCoord0;\n";
120 } else {
121 vertex() << " vec2 qt_varTexCoord0 = vec2(0.0);\n";
122 fragment() << " vec2 qt_texCoord0 = vec2(0.0);\n";
123 }
124 } else if (inUVSet == 1) {
125 if (hasCustomShadedMain || meshHasUV1) {
126 addInterpolationParameter("qt_varTexCoord1", "vec2");
127 if (m_hasMorphing)
128 vertex().append(" qt_vertUV1 = qt_getTargetTex0(qt_vertUV1);");
129 vertex() << " qt_varTexCoord1 = qt_vertUV1;\n";
130 fragment() <<" vec2 qt_texCoord1 = qt_varTexCoord1;\n";
131 } else {
132 vertex() << " vec2 qt_varTexCoord1 = vec2(0.0);\n";
133 fragment() << " vec2 qt_texCoord1 = vec2(0.0);\n";
134 }
135 }
136 }
137
138 void generateLightmapUVCoords(const QSSGShaderDefaultMaterialKey &inKey)
139 {
140 if (hasAttributeInKey(QSSGShaderKeyVertexAttribute::TexCoordLightmap, inKey)) {
141 addInterpolationParameter("qt_varTexCoordLightmap", "vec2");
142 vertex() << " qt_varTexCoordLightmap = qt_vertLightmapUV;\n";
143 fragment() <<" vec2 qt_texCoordLightmap = qt_varTexCoordLightmap;\n";
144 } else {
145 vertex() << " vec2 qt_varTexCoordLightmap = vec2(0.0);\n";
146 fragment() << " vec2 qt_texCoordLightmap = vec2(0.0);\n";
147 }
148 }
149
150 void generateEnvMapReflection(const QSSGShaderDefaultMaterialKey &inKey)
151 {
152 if (setCode(GenerationFlag::EnvMapReflection))
153 return;
154
155 generateWorldPosition(inKey);
156 generateWorldNormal(inKey);
157 QSSGStageGeneratorBase &activeGenerator(activeStage());
158 addInterpolationParameter("qt_var_object_to_camera", "vec3");
159
160 if (m_viewCount < 2) {
161 activeGenerator.addUniform("qt_cameraPosition", "vec3");
162 activeGenerator.append(" qt_var_object_to_camera = normalize( qt_local_model_world_position - qt_cameraPosition );");
163 } else {
164 activeGenerator.addUniformArray("qt_cameraPosition", "vec3", m_viewCount);
165 activeGenerator.append(" qt_var_object_to_camera = normalize( qt_local_model_world_position - qt_cameraPosition[qt_viewIndex] );");
166 }
167
168 // World normal cannot be relied upon in the vertex shader because of bump maps.
169 fragment().append(" vec3 environment_map_reflection = reflect( "
170 "normalize(qt_var_object_to_camera), qt_world_normal.xyz );");
171 fragment().append(" environment_map_reflection *= vec3( 0.5, 0.5, 0 );");
172 fragment().append(" environment_map_reflection += vec3( 0.5, 0.5, 1.0 );");
173 }
174
175 void generateViewVector(const QSSGShaderDefaultMaterialKey &inKey)
176 {
177 if (setCode(GenerationFlag::ViewVector))
178 return;
179
180 generateWorldPosition(inKey);
181
182 if (m_viewCount < 2) {
183 activeStage().addUniform("qt_cameraPosition", "vec3");
184 fragment() << " vec3 qt_view_vector = normalize(qt_cameraPosition - qt_varWorldPos);\n";
185 } else {
186 activeStage().addUniformArray("qt_cameraPosition", "vec3", m_viewCount);
187 fragment() << " vec3 qt_view_vector = normalize(qt_cameraPosition[qt_viewIndex] - qt_varWorldPos);\n";
188 }
189 }
190
191 // fragment shader expects varying vertex normal
192 // lighting in vertex pipeline expects qt_world_normal
193
194 // qt_world_normal in both vert and frag shader
195 void generateWorldNormal(const QSSGShaderDefaultMaterialKey &inKey)
196 {
197 if (setCode(GenerationFlag::WorldNormal))
198 return;
199
200 const bool meshHasNormal = hasAttributeInKey(QSSGShaderKeyVertexAttribute::Normal, inKey);
201
202 if (hasCustomShadedMain || meshHasNormal) {
203 addInterpolationParameter("qt_varNormal", "vec3");
204 doGenerateWorldNormal(inKey);
205 } else {
206 generateWorldPosition(inKey);
207 // qt_rhi_properties.x is used to correct for Y inversion for non OpenGL backends
208 fragment().append(" vec3 qt_varNormal = cross(dFdx(qt_varWorldPos), qt_rhi_properties.x * dFdy(qt_varWorldPos));");
209 }
210 fragment().append(" vec3 qt_world_normal = normalize(qt_varNormal);");
211 }
212
213 void generateObjectNormal()
214 {
215 if (setCode(GenerationFlag::ObjectNormal))
216 return;
217
218 addInterpolationParameter("qt_varObjectNormal", "vec3");
219 vertex().append(" qt_varObjectNormal = qt_vertNormal;");
220 fragment().append(" vec3 object_normal = normalize(qt_varObjectNormal);");
221 }
222
223 void generateWorldPosition(const QSSGShaderDefaultMaterialKey &inKey)
224 {
225 Q_UNUSED(inKey);
226 if (setCode(GenerationFlag::WorldPosition))
227 return;
228
229 activeStage().addUniform("qt_modelMatrix", "mat4");
230 addInterpolationParameter("qt_varWorldPos", "vec3");
231 if (!usesInstancing) {
232 if (m_hasSkinning)
233 vertex().append(" vec3 qt_local_model_world_position = qt_vertPosition.xyz;");
234 else
235 vertex().append(" vec3 qt_local_model_world_position = (qt_modelMatrix * qt_vertPosition).xyz;");
236 } else {
237 vertex().append(" vec3 qt_local_model_world_position = (qt_instancedModelMatrix * qt_vertPosition).xyz;");
238 }
239
240 assignOutput("qt_varWorldPos", "qt_local_model_world_position");
241 }
242
243 void generateDepth()
244 {
245 if (setCode(GenerationFlag::PerspDivDepth))
246 return;
247
248 addInterpolationParameter("qt_varDepth", "float");
249 vertex().append(" qt_varDepth = gl_Position.z / gl_Position.w;");
250 }
251
252 void generateShadowWorldPosition(const QSSGShaderDefaultMaterialKey &inKey)
253 {
254 Q_UNUSED(inKey);
255 if (setCode(GenerationFlag::PerspDivWorldPos))
256 return;
257
258 activeStage().addUniform("qt_modelMatrix", "mat4");
259 addInterpolationParameter("qt_varShadowWorldPos", "vec3");
260
261 if (!usesInstancing) {
262 if (m_hasSkinning)
263 vertex().append(" vec4 qt_shadow_world_tmp = qt_vertPosition;");
264 else
265 vertex().append(" vec4 qt_shadow_world_tmp = qt_modelMatrix * qt_vertPosition;");
266 } else {
267 vertex().append(" vec4 qt_shadow_world_tmp = qt_instancedModelMatrix * qt_vertPosition;");
268 }
269 vertex().append(" qt_varShadowWorldPos = qt_shadow_world_tmp.xyz / qt_shadow_world_tmp.w;");
270 }
271
272 void generateVarTangentAndBinormal(const QSSGShaderDefaultMaterialKey &inKey, bool &genTangent, bool &genBinormal)
273 {
274 if (setCode(GenerationFlag::TangentBinormal))
275 return;
276
277 const bool meshHasTangent = hasAttributeInKey(QSSGShaderKeyVertexAttribute::Tangent, inKey);
278 const bool meshHasBinormal = hasAttributeInKey(QSSGShaderKeyVertexAttribute::Binormal, inKey);
279
280 // I assume that there is no mesh having only binormal without tangent
281 // since it is an abnormal case
282 if (hasCustomShadedMain || meshHasTangent) {
283 addInterpolationParameter("qt_varTangent", "vec3");
284 doGenerateVarTangent(inKey);
285 fragment() << " vec3 qt_tangent = normalize(qt_varTangent);\n";
286
287 if (hasCustomShadedMain || meshHasBinormal) {
288 addInterpolationParameter("qt_varBinormal", "vec3");
289 doGenerateVarBinormal(inKey);
290 fragment() << " vec3 qt_binormal = normalize(qt_varBinormal);\n";
291 genBinormal = true;
292 } else {
293 fragment() << " vec3 qt_binormal = vec3(0.0);\n";
294 }
295 genTangent = true;
296 } else {
297 fragment() << " vec3 qt_tangent = vec3(0.0);\n"
298 << " vec3 qt_binormal = vec3(0.0);\n";
299 }
300 }
301 void generateVertexColor(const QSSGShaderDefaultMaterialKey &inKey)
302 {
303 if (setCode(GenerationFlag::VertexColor))
304 return;
305
306 const bool meshHasColor = hasAttributeInKey(QSSGShaderKeyVertexAttribute::Color, inKey);
307
308 const bool vColorEnabled = defaultMaterialShaderKeyProperties.m_vertexColorsEnabled.getValue(inKey);
309 const bool vColorMaskEnabled = defaultMaterialShaderKeyProperties.m_vertexColorsMaskEnabled.getValue(inKey);
310 const bool usesVarColor = defaultMaterialShaderKeyProperties.m_usesVarColor.getValue(inKey);
311 const bool usesInstanceBuffer = defaultMaterialShaderKeyProperties.m_usesInstanceBuffer.getValue(inKey);
312 const bool usesBlendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(inKey);
313
314 const bool vertexColorsEnabled = (vColorEnabled && meshHasColor) || usesInstanceBuffer || usesBlendParticles || usesVarColor;
315 const bool vertexColorsMaskEnabled = (vColorMaskEnabled && meshHasColor);
316
317 if (vertexColorsEnabled || vertexColorsMaskEnabled) {
318 addInterpolationParameter("qt_varColor", "vec4");
319 if (m_hasMorphing)
320 vertex().append(" qt_vertColor = qt_getTargetColor(qt_vertColor);");
321 vertex().append(" qt_varColor = qt_vertColor;");
322
323 fragment().append(" vec4 qt_vertColorMask = qt_varColor;\n");
324 if (vertexColorsEnabled)
325 fragment().append(" vec4 qt_vertColor = qt_varColor;\n");
326 else
327 fragment().append(" vec4 qt_vertColor = vec4(1.0);\n");
328 }else {
329 fragment().append(" vec4 qt_vertColorMask = vec4(1.0);\n");
330 fragment().append(" vec4 qt_vertColor = vec4(1.0);\n"); // must be 1,1,1,1 to not alter when multiplying with it
331 }
332 }
333
334 void addIncoming(const QByteArray &name, const QByteArray &type) { activeStage().addIncoming(name, type); }
335
336 void addOutgoing(const QByteArray &name, const QByteArray &type) { addInterpolationParameter(name, type); }
337
338 void addUniform(const QByteArray &name, const QByteArray &type) { activeStage().addUniform(name, type); }
339
340 void addUniformArray(const QByteArray &name, const QByteArray &type, quint32 size) { activeStage().addUniformArray(name, type, size); }
341
342 void addInclude(const QByteArray &name) { activeStage().addInclude(name); }
343
344 void addFunction(const QByteArray &functionName)
345 {
346 if (!m_addedFunctions.contains(functionName)) {
347 m_addedFunctions.push_back(functionName);
348 QByteArray includeName = "func" + functionName + ".glsllib";
349 addInclude(includeName);
350 }
351 }
352
353 void addConstantBuffer(const QByteArray &name, const QByteArray &layout)
354 {
355 activeStage().addConstantBuffer(name, layout);
356 }
357
358 void addConstantBufferParam(const QByteArray &cbName, const QByteArray &paramName, const QByteArray &type)
359 {
360 activeStage().addConstantBufferParam(cbName, paramName, type);
361 }
362
363 void addDefinition(const QByteArray &name, const QByteArray &value = QByteArray())
364 {
365 activeStage().addDefinition(name, value);
366 }
367
368 QSSGStageGeneratorBase &operator<<(const QByteArray &data)
369 {
370 activeStage() << data;
371 return activeStage();
372 }
373
374 void append(const QByteArray &data) { activeStage().append(data); }
375
376 QSSGShaderGeneratorStage stage() const
377 {
378 return const_cast<QSSGMaterialVertexPipeline *>(this)->activeStage().stage();
379 }
380
381 // Responsible for beginning all vertex and fragment generation (void main() { etc).
382 void beginVertexGeneration(const QSSGShaderDefaultMaterialKey &inKey,
383 const QSSGShaderFeatures &inFeatureSet,
384 QSSGShaderLibraryManager &shaderLibraryManager);
385 // The fragment shader expects a floating point constant, qt_objectOpacity to be defined
386 // post this method.
387 void beginFragmentGeneration(QSSGShaderLibraryManager &shaderLibraryManager, QSSGRenderLayer::OITMethod oitMethod);
388 // Output variables may be mangled in some circumstances so the shader generation system
389 // needs an abstraction
390 // mechanism around this.
391 void assignOutput(const QByteArray &inVarName, const QByteArray &inVarValueExpr);
392
393 // responsible for closing all vertex and fragment generation
394 void endVertexGeneration();
395 void endFragmentGeneration();
396
397 QSSGStageGeneratorBase &activeStage();
398 void addInterpolationParameter(const QByteArray &inParamName, const QByteArray &inParamType);
399 void addFlatParameter(const QByteArray &inParamName, const QByteArray &inParamType);
400
401 void doGenerateWorldNormal(const QSSGShaderDefaultMaterialKey &inKey);
402 void doGenerateVarTangent(const QSSGShaderDefaultMaterialKey &inKey);
403 void doGenerateVarBinormal(const QSSGShaderDefaultMaterialKey &inKey);
404 bool hasAttributeInKey(QSSGShaderKeyVertexAttribute::VertexAttributeBits inAttr, const QSSGShaderDefaultMaterialKey &inKey);
405};
406
407QT_END_NAMESPACE
408
409#endif
Combined button and popup list for selecting options.
static void insertDirectionalLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertSpecularLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertFragmentMainArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertProcessorArgs(QByteArray &snippet, const char *argKey, const char *(*argListFunc)(), QSSGShaderMaterialAdapter *materialAdapter=nullptr, bool isSharedInout=false)
static void insertVertexBufferlessInstancedMainArgs(QByteArray &snippet)
static void insertPostProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertIblProbeProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static const char * customMainCallWithArguments(bool usesInstanceBuffer, bool usesBufferlessInstancing)
static void insertAmbientLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertVertexInstancedMainArgs(QByteArray &snippet)
static void insertVertexMainArgs(QByteArray &snippet)
static void insertSpotLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static QByteArray extractSharedVarsTypeDefinition(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertProcessorArgsFragmentMain(QByteArray &snippet, const char *argKey, const char *(*argListFunc)(), QSSGShaderMaterialAdapter *materialAdapter=nullptr, bool isSharedInout=false)
static void insertPointLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)