34 QSSGShaderMaterialAdapter *materialAdapter =
nullptr,
bool isSharedInout =
false)
36 const int argKeyLen =
int(strlen(argKey));
37 const int argKeyPos = snippet.indexOf(argKey);
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");
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");
63 snippet = snippet.left(argKeyPos) + argListFunc() + flexArgs + snippet.mid(argKeyPos + argKeyLen);
161void QSSGMaterialVertexPipeline::beginVertexGeneration(
const QSSGShaderDefaultMaterialKey &inKey,
162 const QSSGShaderFeatures &inFeatureSet,
163 QSSGShaderLibraryManager &shaderLibraryManager)
165 QSSGShaderGeneratorStageFlags theStages(QSSGProgramGenerator::defaultFlags());
166 programGenerator()->beginProgram(theStages);
168 QSSGStageGeneratorBase &vertexShader(vertex());
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);
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);
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()) {
219 vertexShader.addDefinition(
"qt_instanceIndex",
"gl_InstanceIndex");
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");
230 if (m_hasSkinning && meshHasJointsAndWeights) {
231 vertexShader.addInclude(
"skinanim.glsllib");
232 if (usesFloatJointIndices)
233 vertexShader.addIncoming(
"attr_joints",
"vec4");
235 vertexShader.addIncoming(
"attr_joints",
"ivec4");
236 vertexShader.addIncoming(
"attr_weights",
"vec4");
238 vertexShader.addUniform(
"qt_boneTexture",
"sampler2D");
241 vertexShader.addInclude(
"morphanim.glsllib");
242 vertexShader.addUniformArray(
"qt_morphWeights",
"float", morphSize);
243 vertexShader.addUniform(
"qt_morphTargetTexture",
"sampler2DArray");
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,
252 if (materialAdapter->hasCustomShaderFunction(QSSGShaderCache::ShaderType::Vertex,
253 QByteArrayLiteral(
"qt_customMain"),
254 shaderLibraryManager))
256 if (usesInstanceBuffer)
257 insertVertexInstancedMainArgs(snippet);
258 else if (usesBufferlessInstancing)
259 insertVertexBufferlessInstancedMainArgs(snippet);
261 insertVertexMainArgs(snippet);
263 if (materialAdapter->usesCustomSkinning()) {
264 vertexShader.addInclude(
"skinanim.glsllib");
265 vertexShader.addUniform(
"qt_boneTexture",
"sampler2D");
268 if (materialAdapter->usesCustomMorphing()) {
269 vertexShader.addInclude(
"morphanim_custom.glsllib");
271 vertexShader.addUniformArray(
"qt_morphWeights",
"float", morphSize);
272 vertexShader.addUniform(
"qt_morphTargetTexture",
"sampler2DArray");
273 m_hasMorphing =
false;
276 if (!materialAdapter->isUnshaded()) {
277 hasCustomShadedMain =
true;
280 vertexShader << snippet;
283 vertexShader <<
"void main()"
290 if (meshHasPosition) {
291 vertexShader.append(
" vec4 qt_vertPosition = vec4(attr_pos, 1.0);");
294 vertexShader.append(
" vec4 qt_vertPosition = vec4(0.0, 0.0, 0.0, 1.0);");
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);");
311 if (!usesInstanceBuffer) {
313 vertexShader.addUniform(
"qt_modelViewProjection",
"mat4");
315 vertexShader.addUniformArray(
"qt_modelViewProjection",
"mat4", m_viewCount);
317 if (usesInstancing) {
319 vertexShader.addUniform(
"qt_modelMatrix",
"mat4");
320 vertexShader.addUniform(
"qt_parentMatrix",
"mat4");
322 vertexShader.addUniform(
"qt_viewProjectionMatrix",
"mat4");
324 vertexShader.addUniformArray(
"qt_viewProjectionMatrix",
"mat4", m_viewCount);
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);
335 if (hasCustomVertexShader || hasCustomFragmentShader) {
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");
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);
359 vertexShader.addUniform(
"qt_modelMatrix",
"mat4");
360 vertexShader.addUniform(
"qt_normalMatrix",
"mat3");
361 vertexShader.addUniform(
"qt_cameraProperties",
"vec2");
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;");
375 if (meshHasNormals) {
376 vertexShader.append(
" qt_vertNormal = attr_norm;");
377 vertexShader.addIncoming(
"attr_norm",
"vec3");
379 if (meshHasTexCoord0) {
380 vertexShader.append(
" qt_vertUV0 = attr_uv0;");
381 vertexShader.addIncoming(
"attr_uv0",
"vec2");
383 if (meshHasTexCoord1) {
384 vertexShader.append(
" qt_vertUV1 = attr_uv1;");
385 vertexShader.addIncoming(
"attr_uv1",
"vec2");
387 if (meshHasTexCoordLightmap) {
388 vertexShader.append(
" vec2 qt_vertLightmapUV = attr_lightmapuv;");
389 vertexShader.addIncoming(
"attr_lightmapuv",
"vec2");
391 if (meshHasTangents) {
392 vertexShader.append(
" qt_vertTangent = attr_textan;");
393 vertexShader.addIncoming(
"attr_textan",
"vec3");
395 if (meshHasBinormals) {
396 vertexShader.append(
" qt_vertBinormal = attr_binormal;");
397 vertexShader.addIncoming(
"attr_binormal",
"vec3");
400 vertexShader.append(
" qt_vertColor = attr_color;");
401 vertexShader.addIncoming(
"attr_color",
"vec4");
404 if (meshHasJointsAndWeights && (m_hasSkinning || hasCustomVertexShader)) {
405 if (usesFloatJointIndices) {
406 vertexShader.addIncoming(
"attr_joints",
"vec4");
407 vertexShader.append(
" qt_vertJoints = ivec4(attr_joints);");
409 vertexShader.addIncoming(
"attr_joints",
"ivec4");
410 vertexShader.append(
" qt_vertJoints = attr_joints;");
412 vertexShader.addIncoming(
"attr_weights",
"vec4");
413 vertexShader.append(
" qt_vertWeights = attr_weights;");
416 const auto appendInstancedMatrices = [&](
const char *instanceMatrix) {
418 vertexShader <<
" mat4 qt_instancedModelMatrix = qt_parentMatrix * " << instanceMatrix <<
";\n";
420 vertexShader <<
" mat4 qt_instancedModelMatrix = qt_parentMatrix * " << instanceMatrix <<
" * qt_modelMatrix;\n";
421 vertexShader.append(
" mat3 qt_instancedNormalMatrix = mat3(transpose(inverse(qt_instancedModelMatrix)));");
423 vertexShader.append(
" mat4 qt_instancedMVPMatrix = qt_viewProjectionMatrix * qt_instancedModelMatrix;");
425 vertexShader.append(
" mat4 qt_instancedMVPMatrix = qt_viewProjectionMatrix[qt_viewIndex] * qt_instancedModelMatrix;");
429 const bool customMainRunsLast = materialAdapter->isUnshaded() && hasCustomVertexShader;
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) {
437 vertexShader.append(
" mat4 qt_bufferlessInstanceTransform = mat4(1.0);");
438 if (customMainRunsLast)
439 appendInstancedMatrices(
"qt_bufferlessInstanceTransform");
442 if (!customMainRunsLast) {
443 vertexShader <<
" vec3 qt_uTransform;\n";
444 vertexShader <<
" vec3 qt_vTransform;\n";
446 if (hasCustomShadedMain)
450 if (usesBufferlessInstancing)
451 appendInstancedMatrices(
"qt_bufferlessInstanceTransform");
453 if (m_hasMorphing && !hasCustomVertexShader)
454 vertexShader.append(
" qt_vertPosition.xyz = qt_getTargetPosition(qt_vertPosition.xyz);");
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(
" }");
464 if (blendParticles) {
465 vertexShader.append(
" qt_vertPosition.xyz = qt_applyParticle(qt_vertPosition.xyz, qt_vertNormal, qt_vertColor, qt_vertNormal, qt_vertColor, qt_particleMatrix);");
468 if (!hasCustomShadedMain || !overridesPosition) {
469 if (!usesInstancing) {
471 vertexShader.append(
" gl_Position = qt_modelViewProjection * qt_vertPosition;");
473 vertexShader.append(
" gl_Position = qt_modelViewProjection[qt_viewIndex] * qt_vertPosition;");
475 vertexShader.append(
" gl_Position = qt_instancedMVPMatrix * qt_vertPosition;");
480 if (usesPointsTopology && !hasCustomVertexShader) {
481 vertexShader.addUniform(
"qt_materialPointSize",
"float");
482 vertexShader.append(
" gl_PointSize = qt_materialPointSize;");
486void QSSGMaterialVertexPipeline::beginFragmentGeneration(QSSGShaderLibraryManager &shaderLibraryManager, QSSGRenderLayer::OITMethod oitMethod)
488 fragment().addUniform(
"qt_material_properties",
"vec4");
489 fragment().addUniform(
"qt_rhi_properties",
"vec4");
491 if (m_viewCount < 2) {
492 fragment().addUniform(
"qt_viewMatrix",
"mat4");
494 fragment().addUniformArray(
"qt_viewMatrix",
"mat4", m_viewCount);
497 if (!skipCustomFragmentSnippet && materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment)) {
498 QByteArray snippet = materialAdapter->customShaderSnippet(QSSGShaderCache::ShaderType::Fragment,
499 shaderLibraryManager,
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);
513 fragment() << snippet;
516 if (oitMethod != QSSGRenderLayer::OITMethod::None) {
517 if (oitMethod == QSSGRenderLayer::OITMethod::LinkedList) {
521 fragment() <<
"layout(early_fragment_tests) in;" <<
"\n";
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");
531 fragment() <<
"void main()"
536 if (!materialAdapter->isUnshaded() || !materialAdapter->hasCustomShaderSnippet(QSSGShaderCache::ShaderType::Fragment))
537 fragment() <<
" float qt_objectOpacity = qt_material_properties.a;\n";