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.cpp
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
8
9#include <QtCore/qregularexpression.h>
10
11#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
12#include <QtQuick3DRuntimeRender/private/qssgrenderlight_p.h>
13#include "../qssgrendercontextcore.h"
14#include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h>
15#include <QtQuick3DRuntimeRender/private/qssgrendershaderlibrarymanager_p.h>
16#include <QtQuick3DRuntimeRender/private/qssgrendershadercodegenerator_p.h>
17#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterialshadergenerator_p.h>
18#include <QtQuick3DRuntimeRender/private/qssgshadermaterialadapter_p.h>
19
21
22QSSGMaterialVertexPipeline::QSSGMaterialVertexPipeline(QSSGProgramGenerator &programGen,
25 : m_programGenerator(&programGen)
26 , defaultMaterialShaderKeyProperties(materialProperties)
27 , materialAdapter(materialAdapter)
28 , hasCustomShadedMain(false)
29 , skipCustomFragmentSnippet(false)
30{
31}
32
33static inline void insertProcessorArgsFragmentMain(QByteArray &snippet, const char *argKey, const char* (*argListFunc)(),
34 QSSGShaderMaterialAdapter *materialAdapter = nullptr, bool isSharedInout = false)
35{
36 const int argKeyLen = int(strlen(argKey));
37 const int argKeyPos = snippet.indexOf(argKey);
38 if (argKeyPos >= 0) {
39 QByteArray flexArgs;
40 if (materialAdapter) {
41 if (materialAdapter->isClearcoatEnabled()) {
42 flexArgs += QByteArrayLiteral(", inout float CLEARCOAT_AMOUNT, inout float CLEARCOAT_FRESNEL_POWER, inout float CLEARCOAT_ROUGHNESS, inout vec3 CLEARCOAT_NORMAL");
43 if (materialAdapter->isClearcoatFresnelScaleBiasEnabled())
44 flexArgs += QByteArrayLiteral(", inout float CLEARCOAT_FRESNEL_SCALE, inout float CLEARCOAT_FRESNEL_BIAS");
45 }
46 if (materialAdapter->isFresnelScaleBiasEnabled())
47 flexArgs += QByteArrayLiteral(", inout float FRESNEL_SCALE, inout float FRESNEL_BIAS");
48 if (materialAdapter->isTransmissionEnabled())
49 flexArgs += QByteArrayLiteral(", inout float TRANSMISSION_FACTOR, inout float THICKNESS_FACTOR, inout vec3 ATTENUATION_COLOR, inout float ATTENUATION_DISTANCE");
50 if (materialAdapter->usesSharedVariables()) {
51 const char *inoutString = isSharedInout ? ", inout " : ", in ";
52 flexArgs += inoutString + QByteArrayLiteral("QT_SHARED_VARS SHARED");
53 }
54 }
55 snippet = snippet.left(argKeyPos) + argListFunc() + flexArgs + snippet.mid(argKeyPos + argKeyLen);
56 }
57}
58
59static inline void insertProcessorArgs(QByteArray &snippet, const char *argKey, const char* (*argListFunc)(),
60 QSSGShaderMaterialAdapter *materialAdapter = nullptr, bool isSharedInout = false)
61{
62 const int argKeyLen = int(strlen(argKey));
63 const int argKeyPos = snippet.indexOf(argKey);
64 if (argKeyPos >= 0) {
65 QByteArray flexArgs;
66 if (materialAdapter && materialAdapter->usesSharedVariables()) {
67 const char *inoutString = isSharedInout ? ", inout " : ", in ";
68 flexArgs += inoutString + QByteArrayLiteral("QT_SHARED_VARS SHARED");
69 }
70 snippet = snippet.left(argKeyPos) + argListFunc() + flexArgs + snippet.mid(argKeyPos + argKeyLen);
71 }
72}
73
74static inline void insertDirectionalLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
75{
76 insertProcessorArgs(snippet, "/*%QT_ARGS_DIRECTIONAL_LIGHT%*/", QSSGMaterialShaderGenerator::directionalLightProcessorArgumentList, materialAdapter, true);
77}
78
79static inline void insertPointLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
80{
81 insertProcessorArgs(snippet, "/*%QT_ARGS_POINT_LIGHT%*/", QSSGMaterialShaderGenerator::pointLightProcessorArgumentList, materialAdapter, true);
82}
83
84static inline void insertSpotLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
85{
86 insertProcessorArgs(snippet, "/*%QT_ARGS_SPOT_LIGHT%*/", QSSGMaterialShaderGenerator::spotLightProcessorArgumentList, materialAdapter, true);
87}
88
89static inline void insertAmbientLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
90{
91 insertProcessorArgs(snippet, "/*%QT_ARGS_AMBIENT_LIGHT%*/", QSSGMaterialShaderGenerator::ambientLightProcessorArgumentList, materialAdapter, true);
92}
93
94static inline void insertIblProbeProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
95{
96 insertProcessorArgs(snippet, "/*%QT_ARGS_IBL_PROBE%*/", QSSGMaterialShaderGenerator::iblProbeProcessorArgumentList, materialAdapter, true);
97}
98
99static inline void insertSpecularLightProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
100{
101 insertProcessorArgs(snippet, "/*%QT_ARGS_SPECULAR_LIGHT%*/", QSSGMaterialShaderGenerator::specularLightProcessorArgumentList, materialAdapter, true);
102}
103
104static inline void insertFragmentMainArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
105{
106 insertProcessorArgsFragmentMain(snippet, "/*%QT_ARGS_MAIN%*/", QSSGMaterialShaderGenerator::shadedFragmentMainArgumentList, materialAdapter, true);
107}
108
109static inline void insertPostProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
110{
111 insertProcessorArgs(snippet, "/*%QT_ARGS_POST_PROCESS%*/", QSSGMaterialShaderGenerator::postProcessorArgumentList, materialAdapter, false);
112}
113
114static inline void insertVertexMainArgs(QByteArray &snippet)
115{
116 insertProcessorArgs(snippet, "/*%QT_ARGS_MAIN%*/", QSSGMaterialShaderGenerator::vertexMainArgumentList);
117}
118
119static inline void insertVertexInstancedMainArgs(QByteArray &snippet)
120{
121 insertProcessorArgs(snippet, "/*%QT_ARGS_MAIN%*/", QSSGMaterialShaderGenerator::vertexInstancedMainArgumentList);
122}
123
124static inline const char *customMainCallWithArguments(bool usesInstancing)
125{
126 if (usesInstancing)
127 return " qt_customMain(qt_vertPosition.xyz, qt_vertNormal, qt_vertUV0, qt_vertUV1, qt_vertTangent, qt_vertBinormal, qt_vertJoints, qt_vertWeights, qt_vertColor, qt_instancedModelMatrix, qt_instancedMVPMatrix);";
128 else
129 return " qt_customMain(qt_vertPosition.xyz, qt_vertNormal, qt_vertUV0, qt_vertUV1, qt_vertTangent, qt_vertBinormal, qt_vertJoints, qt_vertWeights, qt_vertColor);\n";
130}
131
132static inline QByteArray extractSharedVarsTypeDefinition(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter) {
133 if (materialAdapter->usesSharedVariables()) {
134 // Extract shared variables from the custom shader snippet
135 static QRegularExpression re(QString::fromLocal8Bit(R"(struct\s+QT_SHARED_VARS\s*\{[\s\S]*?\};)"), QRegularExpression::DotMatchesEverythingOption);
136 QRegularExpressionMatch match = re.match(QString::fromLocal8Bit(snippet));
137 if (!match.hasMatch())
138 return QByteArray();
139 QString typeDefinition = match.captured(0);
140 snippet.remove(match.capturedStart(0), match.capturedLength(0));
141 return typeDefinition.toLocal8Bit();
142 }
143 return QByteArray();
144}
145
146void QSSGMaterialVertexPipeline::beginVertexGeneration(const QSSGShaderDefaultMaterialKey &inKey,
147 const QSSGShaderFeatures &inFeatureSet,
148 QSSGShaderLibraryManager &shaderLibraryManager)
149{
150 QSSGShaderGeneratorStageFlags theStages(QSSGProgramGenerator::defaultFlags());
151 programGenerator()->beginProgram(theStages);
152
153 QSSGStageGeneratorBase &vertexShader(vertex());
154
155 const bool meshHasNormals = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
156 QSSGShaderKeyVertexAttribute::Normal, inKey);
157 const bool meshHasTexCoord0 = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
158 QSSGShaderKeyVertexAttribute::TexCoord0, inKey);
159 const bool meshHasTexCoord1 = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
160 QSSGShaderKeyVertexAttribute::TexCoord1, inKey);
161 const bool meshHasTexCoordLightmap = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
162 QSSGShaderKeyVertexAttribute::TexCoordLightmap, inKey);
163 const bool meshHasTangents = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
164 QSSGShaderKeyVertexAttribute::Tangent, inKey);
165 const bool meshHasBinormals = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
166 QSSGShaderKeyVertexAttribute::Binormal, inKey);
167 const bool meshHasColors = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
168 QSSGShaderKeyVertexAttribute::Color, inKey);
169 const bool meshHasJointsAndWeights = defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(
170 QSSGShaderKeyVertexAttribute::JointAndWeight, inKey);
171 const bool overridesPosition = defaultMaterialShaderKeyProperties.m_overridesPosition.getValue(inKey);
172 const bool usesProjectionMatrix = defaultMaterialShaderKeyProperties.m_usesProjectionMatrix.getValue(inKey);
173 const bool usesInvProjectionMatrix = defaultMaterialShaderKeyProperties.m_usesInverseProjectionMatrix.getValue(inKey);
174 const bool usesPointsTopology = defaultMaterialShaderKeyProperties.m_usesPointsTopology.getValue(inKey);
175 const bool usesFloatJointIndices = defaultMaterialShaderKeyProperties.m_usesFloatJointIndices.getValue(inKey);
176 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(inKey);
177 usesInstancing = defaultMaterialShaderKeyProperties.m_usesInstancing.getValue(inKey);
178 m_hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(inKey) > 0;
179 const auto morphSize = defaultMaterialShaderKeyProperties.m_targetCount.getValue(inKey);
180 m_hasMorphing = morphSize > 0;
181 m_viewCount = inFeatureSet.isSet(QSSGShaderFeatures::Feature::DisableMultiView)
182 ? 1 : defaultMaterialShaderKeyProperties.m_viewCount.getValue(inKey);
183 const bool usesViewIndex = defaultMaterialShaderKeyProperties.m_usesViewIndex.getValue(inKey);
184
185 vertexShader.addIncoming("attr_pos", "vec3");
186 if (usesInstancing) {
187 vertexShader.addIncoming("qt_instanceTransform0", "vec4");
188 vertexShader.addIncoming("qt_instanceTransform1", "vec4");
189 vertexShader.addIncoming("qt_instanceTransform2", "vec4");
190 vertexShader.addIncoming("qt_instanceColor", "vec4");
191 vertexShader.addIncoming("qt_instanceData", "vec4");
192 }
193 if (blendParticles) {
194 vertexShader.addInclude("particles.glsllib");
195 vertexShader.addUniform("qt_particleTexture", "sampler2D");
196 vertexShader.addUniform("qt_countPerSlice", "uint");
197 vertexShader.addUniform("qt_oneOverParticleImageSize", "vec2");
198 vertexShader.addUniform("qt_particleMatrix", "mat4");
199 vertexShader.addUniform("qt_particleIndexOffset", "uint");
200 }
201
202 if (m_hasSkinning && meshHasJointsAndWeights) {
203 vertexShader.addInclude("skinanim.glsllib");
204 if (usesFloatJointIndices)
205 vertexShader.addIncoming("attr_joints", "vec4");
206 else
207 vertexShader.addIncoming("attr_joints", "ivec4");
208 vertexShader.addIncoming("attr_weights", "vec4");
209
210 vertexShader.addUniform("qt_boneTexture", "sampler2D");
211 }
212 if (m_hasMorphing) {
213 vertexShader.addInclude("morphanim.glsllib");
214 vertexShader.addUniformArray("qt_morphWeights", "float", morphSize);
215 vertexShader.addUniform("qt_morphTargetTexture", "sampler2DArray");
216 }
217
218 const bool hasCustomVertexShader = materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Vertex);
219 const bool hasCustomFragmentShader = materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment);
220 if (hasCustomVertexShader) {
221 QByteArray snippet = materialAdapter->customShaderSnippet(QSSGShaderCache::ShaderType::Vertex,
222 shaderLibraryManager,
223 m_viewCount >= 2);
224 if (materialAdapter->hasCustomShaderFunction(QSSGShaderCache::ShaderType::Vertex,
225 QByteArrayLiteral("qt_customMain"),
226 shaderLibraryManager))
227 {
228 if (usesInstancing)
229 insertVertexInstancedMainArgs(snippet);
230 else
231 insertVertexMainArgs(snippet);
232
233 if (materialAdapter->usesCustomSkinning()) {
234 vertexShader.addInclude("skinanim.glsllib");
235 vertexShader.addUniform("qt_boneTexture", "sampler2D");
236 }
237
238 if (materialAdapter->usesCustomMorphing()) {
239 vertexShader.addInclude("morphanim_custom.glsllib");
240 if (morphSize > 0)
241 vertexShader.addUniformArray("qt_morphWeights", "float", morphSize);
242 vertexShader.addUniform("qt_morphTargetTexture", "sampler2DArray");
243 m_hasMorphing = false;
244 }
245
246 if (!materialAdapter->isUnshaded()) {
247 hasCustomShadedMain = true;
248 }
249 }
250 vertexShader << snippet;
251 }
252
253 vertexShader << "void main()"
254 << "\n"
255 << "{"
256 << "\n";
257 // These local variables will be used for whole the pipeline
258 // instead of each attributes since it is more convenient
259 // for adding new routines.
260 vertexShader.append(" vec4 qt_vertPosition = vec4(attr_pos, 1.0);");
261 vertexShader.append(" vec3 qt_vertNormal = vec3(0.0);");
262 vertexShader.append(" vec3 qt_vertTangent = vec3(0.0);");
263 vertexShader.append(" vec3 qt_vertBinormal = vec3(0.0);");
264 if (meshHasTexCoord0 || hasCustomVertexShader)
265 vertexShader.append(" vec2 qt_vertUV0 = vec2(0.0);");
266 if (meshHasTexCoord1 || hasCustomVertexShader)
267 vertexShader.append(" vec2 qt_vertUV1 = vec2(0.0);");
268 if (m_hasSkinning || hasCustomVertexShader)
269 vertexShader.append(" ivec4 qt_vertJoints = ivec4(0);");
270 if (meshHasJointsAndWeights || m_hasSkinning || hasCustomVertexShader)
271 vertexShader.append(" vec4 qt_vertWeights = vec4(0.0);");
272 if (meshHasColors || usesInstancing || blendParticles || hasCustomVertexShader || hasCustomFragmentShader)
273 vertexShader.append(" vec4 qt_vertColor = vec4(1.0);"); // must be 1,1,1,1 to not alter when multiplying with it
274
275 if (!usesInstancing) {
276 if (m_viewCount < 2)
277 vertexShader.addUniform("qt_modelViewProjection", "mat4");
278 else
279 vertexShader.addUniformArray("qt_modelViewProjection", "mat4", m_viewCount);
280 } else {
281 // Must manualy calculate a MVP
282 vertexShader.addUniform("qt_modelMatrix", "mat4");
283 vertexShader.addUniform("qt_parentMatrix", "mat4");
284 if (m_viewCount < 2)
285 vertexShader.addUniform("qt_viewProjectionMatrix", "mat4");
286 else
287 vertexShader.addUniformArray("qt_viewProjectionMatrix", "mat4", m_viewCount);
288 }
289
290 // The custom fragment main should be skipped if this is a
291 // depth (or normal texture) pass, but not if it is also a OpaqueDepthPrePass
292 // because then we need to know the real alpha values
293 skipCustomFragmentSnippet = false;
294 const bool isDepthPass = inFeatureSet.isSet(QSSGShaderFeatures::Feature::DepthPass);
295 const bool isOpaqueDepthPrePass = inFeatureSet.isSet(QSSGShaderFeatures::Feature::OpaqueDepthPrePass);
296 skipCustomFragmentSnippet = (isDepthPass && !isOpaqueDepthPrePass);
297
298 if (hasCustomVertexShader || hasCustomFragmentShader) {
299 // This is both for unshaded and shaded. Regardless of any other
300 // condition we have to ensure the keywords (VIEW_MATRIX etc.) promised
301 // by the documentation are available in *both* the custom vertex and
302 // fragment shader snippets, even if only one of them is present.
303 if (m_viewCount < 2) {
304 vertexShader.addUniform("qt_viewProjectionMatrix", "mat4");
305 vertexShader.addUniform("qt_viewMatrix", "mat4");
306 vertexShader.addUniform("qt_cameraPosition", "vec3");
307 vertexShader.addUniform("qt_cameraDirection", "vec3");
308 if (usesProjectionMatrix)
309 vertexShader.addUniform("qt_projectionMatrix", "mat4");
310 if (usesInvProjectionMatrix)
311 vertexShader.addUniform("qt_inverseProjectionMatrix", "mat4");
312 } else {
313 vertexShader.addUniformArray("qt_viewProjectionMatrix", "mat4", m_viewCount);
314 vertexShader.addUniformArray("qt_viewMatrix", "mat4", m_viewCount);
315 vertexShader.addUniformArray("qt_cameraPosition", "vec3", m_viewCount);
316 vertexShader.addUniformArray("qt_cameraDirection", "vec3", m_viewCount);
317 if (usesProjectionMatrix)
318 vertexShader.addUniformArray("qt_projectionMatrix", "mat4", m_viewCount);
319 if (usesInvProjectionMatrix)
320 vertexShader.addUniformArray("qt_inverseProjectionMatrix", "mat4", m_viewCount);
321 }
322 vertexShader.addUniform("qt_modelMatrix", "mat4");
323 vertexShader.addUniform("qt_normalMatrix", "mat3");
324 vertexShader.addUniform("qt_cameraProperties", "vec2");
325 }
326
327 // With multiview, qt_viewIndex (aka VIEW_INDEX) is always present.
328 // Otherwise, we still make VIEW_INDEX functional (always 0) in custom
329 // materials, if the keyword is used.
330 if (m_viewCount >= 2) {
331 addFlatParameter("qt_viewIndex", "uint");
332 vertexShader.append(" qt_viewIndex = gl_ViewIndex;");
333 } else if (usesViewIndex) {
334 addFlatParameter("qt_viewIndex", "uint");
335 vertexShader.append(" qt_viewIndex = 0;");
336 }
337
338 if (meshHasNormals) {
339 vertexShader.append(" qt_vertNormal = attr_norm;");
340 vertexShader.addIncoming("attr_norm", "vec3");
341 }
342 if (meshHasTexCoord0) {
343 vertexShader.append(" qt_vertUV0 = attr_uv0;");
344 vertexShader.addIncoming("attr_uv0", "vec2");
345 }
346 if (meshHasTexCoord1) {
347 vertexShader.append(" qt_vertUV1 = attr_uv1;");
348 vertexShader.addIncoming("attr_uv1", "vec2");
349 }
350 if (meshHasTexCoordLightmap) {
351 vertexShader.append(" vec2 qt_vertLightmapUV = attr_lightmapuv;");
352 vertexShader.addIncoming("attr_lightmapuv", "vec2");
353 }
354 if (meshHasTangents) {
355 vertexShader.append(" qt_vertTangent = attr_textan;");
356 vertexShader.addIncoming("attr_textan", "vec3");
357 }
358 if (meshHasBinormals) {
359 vertexShader.append(" qt_vertBinormal = attr_binormal;");
360 vertexShader.addIncoming("attr_binormal", "vec3");
361 }
362 if (meshHasColors) {
363 vertexShader.append(" qt_vertColor = attr_color;");
364 vertexShader.addIncoming("attr_color", "vec4");
365 }
366
367 if (meshHasJointsAndWeights && (m_hasSkinning || hasCustomVertexShader)) {
368 if (usesFloatJointIndices) {
369 vertexShader.addIncoming("attr_joints", "vec4");
370 vertexShader.append(" qt_vertJoints = ivec4(attr_joints);");
371 } else {
372 vertexShader.addIncoming("attr_joints", "ivec4");
373 vertexShader.append(" qt_vertJoints = attr_joints;");
374 }
375 vertexShader.addIncoming("attr_weights", "vec4");
376 vertexShader.append(" qt_vertWeights = attr_weights;");
377 }
378
379 if (usesInstancing) {
380 vertexShader.append(" qt_vertColor *= qt_instanceColor;");
381 vertexShader.append(" mat4 qt_instanceMatrix = mat4(qt_instanceTransform0, qt_instanceTransform1, qt_instanceTransform2, vec4(0.0, 0.0, 0.0, 1.0));");
382 if (m_hasSkinning)
383 vertexShader.append(" mat4 qt_instancedModelMatrix = qt_parentMatrix * transpose(qt_instanceMatrix);");
384 else
385 vertexShader.append(" mat4 qt_instancedModelMatrix = qt_parentMatrix * transpose(qt_instanceMatrix) * qt_modelMatrix;");
386 vertexShader.append(" mat3 qt_instancedNormalMatrix = mat3(transpose(inverse(qt_instancedModelMatrix)));");
387 if (m_viewCount < 2)
388 vertexShader.append(" mat4 qt_instancedMVPMatrix = qt_viewProjectionMatrix * qt_instancedModelMatrix;");
389 else
390 vertexShader.append(" mat4 qt_instancedMVPMatrix = qt_viewProjectionMatrix[qt_viewIndex] * qt_instancedModelMatrix;");
391 }
392
393 if (!materialAdapter->isUnshaded() || !hasCustomVertexShader) {
394 vertexShader << " vec3 qt_uTransform;\n";
395 vertexShader << " vec3 qt_vTransform;\n";
396
397 if (hasCustomShadedMain)
398 vertexShader.append(customMainCallWithArguments(usesInstancing));
399
400 if (m_hasMorphing && !hasCustomVertexShader)
401 vertexShader.append(" qt_vertPosition.xyz = qt_getTargetPosition(qt_vertPosition.xyz);");
402
403 m_needsSkinning = m_hasSkinning && !materialAdapter->usesCustomSkinning();
404 if (m_needsSkinning) {
405 vertexShader.append(" mat4 skinMat = mat4(1);");
406 vertexShader.append(" if (qt_vertWeights != vec4(0.0)) {");
407 vertexShader.append(" skinMat = qt_getSkinMatrix(qt_vertJoints, qt_vertWeights);");
408 vertexShader.append(" qt_vertPosition = skinMat * qt_vertPosition;");
409 vertexShader.append(" }");
410 }
411 if (blendParticles) {
412 vertexShader.append(" qt_vertPosition.xyz = qt_applyParticle(qt_vertPosition.xyz, qt_vertNormal, qt_vertColor, qt_vertNormal, qt_vertColor, qt_particleMatrix);");
413 }
414
415 if (!hasCustomShadedMain || !overridesPosition) {
416 if (!usesInstancing) {
417 if (m_viewCount < 2)
418 vertexShader.append(" gl_Position = qt_modelViewProjection * qt_vertPosition;");
419 else
420 vertexShader.append(" gl_Position = qt_modelViewProjection[qt_viewIndex] * qt_vertPosition;");
421 } else {
422 vertexShader.append(" gl_Position = qt_instancedMVPMatrix * qt_vertPosition;");
423 }
424 }
425 }
426
427 if (usesPointsTopology && !hasCustomVertexShader) {
428 vertexShader.addUniform("qt_materialPointSize", "float");
429 vertexShader.append(" gl_PointSize = qt_materialPointSize;");
430 } // with a custom vertex shader it is up to it to set gl_PointSize (aka POINT_SIZE)
431}
432
433void QSSGMaterialVertexPipeline::beginFragmentGeneration(QSSGShaderLibraryManager &shaderLibraryManager, QSSGRenderLayer::OITMethod oitMethod)
434{
435 fragment().addUniform("qt_material_properties", "vec4");
436 fragment().addUniform("qt_rhi_properties", "vec4");
437
438 if (m_viewCount < 2) {
439 fragment().addUniform("qt_viewMatrix", "mat4");
440 } else {
441 fragment().addUniformArray("qt_viewMatrix", "mat4", m_viewCount);
442 }
443
444 if (!skipCustomFragmentSnippet && materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment)) {
445 QByteArray snippet = materialAdapter->customShaderSnippet(QSSGShaderCache::ShaderType::Fragment,
446 shaderLibraryManager,
447 m_viewCount >= 2);
448 if (!materialAdapter->isUnshaded()) {
449 insertAmbientLightProcessorArgs(snippet, materialAdapter);
450 insertIblProbeProcessorArgs(snippet, materialAdapter);
451 insertSpecularLightProcessorArgs(snippet, materialAdapter);
452 insertSpotLightProcessorArgs(snippet, materialAdapter);
453 insertPointLightProcessorArgs(snippet, materialAdapter);
454 insertDirectionalLightProcessorArgs(snippet, materialAdapter);
455 insertFragmentMainArgs(snippet, materialAdapter);
456 insertPostProcessorArgs(snippet, materialAdapter);
457 auto sharedVars = extractSharedVarsTypeDefinition(snippet, materialAdapter);
458 fragment().addTypeDeclaration("QT_SHARED_VARS", sharedVars);
459 }
460 fragment() << snippet;
461 }
462
463 if (oitMethod != QSSGRenderLayer::OITMethod::None) {
464 if (oitMethod == QSSGRenderLayer::OITMethod::LinkedList) {
465 // Early tests are required since we can't undo modifications to
466 // the image data structures after running the fragment shader.
467 // Modifying depth in fragment shader is not possible then.
468 fragment() << "layout(early_fragment_tests) in;" << "\n";
469 }
470 if (oitMethod == QSSGRenderLayer::OITMethod::WeightedBlended) {
471 fragment().addDefinition("QSSG_OIT_METHOD", "QSSG_OIT_WEIGHTED_BLENDED");
472 fragment() << "layout(location = 1) out vec4 revealageOutput;" << "\n";
473 } else if (oitMethod == QSSGRenderLayer::OITMethod::LinkedList) {
474 fragment() << "#extension GL_ARB_shading_language_packing : enable" << "\n";
475 fragment().addDefinition("QSSG_OIT_METHOD", "QSSG_OIT_LINKED_LIST");
476 }
477 }
478 fragment() << "void main()"
479 << "\n"
480 << "{"
481 << "\n";
482
483 if (!materialAdapter->isUnshaded() || !materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment))
484 fragment() << " float qt_objectOpacity = qt_material_properties.a;\n";
485}
486
487void QSSGMaterialVertexPipeline::assignOutput(const QByteArray &inVarName, const QByteArray &inVarValue)
488{
489 vertex() << " " << inVarName << " = " << inVarValue << ";\n";
490}
491
492void QSSGMaterialVertexPipeline::doGenerateWorldNormal(const QSSGShaderDefaultMaterialKey &inKey)
493{
494 QSSGStageGeneratorBase &vertexGenerator(vertex());
495 const bool usesInstancing = defaultMaterialShaderKeyProperties.m_usesInstancing.getValue(inKey);
496 if (!usesInstancing)
497 vertexGenerator.addUniform("qt_normalMatrix", "mat3");
498 if (m_hasMorphing)
499 vertexGenerator.append(" qt_vertNormal = qt_getTargetNormal(qt_vertNormal);");
500 if (m_hasSkinning) {
501 vertexGenerator.append(" if (qt_vertWeights != vec4(0.0))");
502 vertexGenerator.append(" qt_vertNormal = qt_getSkinNormalMatrix(qt_vertJoints, qt_vertWeights) * qt_vertNormal;");
503 }
504 // If new model->skin is used,
505 // both qt_normalMatrix and qt_modelMatrix are identity.
506 if (!usesInstancing) {
507 if (m_hasSkinning)
508 vertexGenerator.append(" vec3 qt_world_normal = normalize(qt_vertNormal);");
509 else
510 vertexGenerator.append(" vec3 qt_world_normal = normalize(qt_normalMatrix * qt_vertNormal);");
511 } else {
512 vertexGenerator.append(" vec3 qt_world_normal = normalize(qt_instancedNormalMatrix * qt_vertNormal);");
513 }
514 vertexGenerator.append(" qt_varNormal = qt_world_normal;");
515}
516
517void QSSGMaterialVertexPipeline::doGenerateVarTangent(const QSSGShaderDefaultMaterialKey &inKey)
518{
519 if (m_hasMorphing)
520 vertex() << " qt_vertTangent = qt_getTargetTangent(qt_vertTangent);\n";
521 if (m_needsSkinning) {
522 vertex() << " if (qt_vertWeights != vec4(0.0))\n"
523 << " qt_vertTangent = (skinMat * vec4(qt_vertTangent, 0.0)).xyz;\n";
524
525 }
526 const bool usesInstancing = defaultMaterialShaderKeyProperties.m_usesInstancing.getValue(inKey);
527 if (!usesInstancing) {
528 if (!m_hasSkinning)
529 vertex() << " qt_varTangent = (qt_modelMatrix * vec4(qt_vertTangent, 0.0)).xyz;\n";
530 else
531 vertex() << " qt_varTangent = qt_vertTangent;\n";
532 } else {
533 vertex() << " qt_varTangent = (qt_instancedModelMatrix * vec4(qt_vertTangent, 0.0)).xyz;\n";
534 }
535}
536
537void QSSGMaterialVertexPipeline::doGenerateVarBinormal(const QSSGShaderDefaultMaterialKey &inKey)
538{
539 if (m_hasMorphing)
540 vertex() << " qt_vertBinormal = qt_getTargetBinormal(qt_vertBinormal);\n";
541 if (m_needsSkinning) {
542 vertex() << " if (qt_vertWeights != vec4(0.0))\n"
543 << " qt_vertBinormal = (skinMat * vec4(qt_vertBinormal, 0.0)).xyz;\n";
544 }
545 const bool usesInstancing = defaultMaterialShaderKeyProperties.m_usesInstancing.getValue(inKey);
546 if (!usesInstancing) {
547 if (!m_hasSkinning)
548 vertex() << " qt_varBinormal = (qt_modelMatrix * vec4(qt_vertBinormal, 0.0)).xyz;\n";
549 else
550 vertex() << " qt_varBinormal = qt_vertBinormal;\n";
551 } else {
552 vertex() << " qt_varBinormal = (qt_instancedModelMatrix * vec4(qt_vertBinormal, 0.0)).xyz;\n";
553 }
554}
555
556bool QSSGMaterialVertexPipeline::hasAttributeInKey(QSSGShaderKeyVertexAttribute::VertexAttributeBits inAttr,
557 const QSSGShaderDefaultMaterialKey &inKey)
558{
559 return defaultMaterialShaderKeyProperties.m_vertexAttributes.getBitValue(inAttr, inKey);
560}
561
562void QSSGMaterialVertexPipeline::endVertexGeneration()
563{
564 if (materialAdapter->isUnshaded() && materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Vertex))
565 vertex() << customMainCallWithArguments(usesInstancing);
566 vertex().append("}");
567}
568
569void QSSGMaterialVertexPipeline::endFragmentGeneration()
570{
571 if (!skipCustomFragmentSnippet && materialAdapter->isUnshaded() && materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment))
572 fragment() << " qt_customMain();\n";
573
574 fragment().append("}");
575}
576
577void QSSGMaterialVertexPipeline::addInterpolationParameter(const QByteArray &inName, const QByteArray &inType)
578{
579 vertex().addOutgoing(inName, inType);
580 fragment().addIncoming(inName, inType);
581}
582
583void QSSGMaterialVertexPipeline::addFlatParameter(const QByteArray &inName, const QByteArray &inType)
584{
585 vertex().addFlatOutgoing(inName, inType);
586 fragment().addFlatIncoming(inName, inType);
587}
588
589QSSGStageGeneratorBase &QSSGMaterialVertexPipeline::activeStage()
590{
591 return vertex();
592}
593
594QT_END_NAMESPACE
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 const char * customMainCallWithArguments(bool usesInstancing)
static void insertProcessorArgs(QByteArray &snippet, const char *argKey, const char *(*argListFunc)(), QSSGShaderMaterialAdapter *materialAdapter=nullptr, bool isSharedInout=false)
static void insertPostProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
static void insertIblProbeProcessorArgs(QByteArray &snippet, QSSGShaderMaterialAdapter *materialAdapter)
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)