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