977void QSSGLayerRenderData::prepareImageForRender(QSSGRenderImage &inImage,
978 QSSGRenderableImage::Type inMapType,
979 QSSGRenderableImage *&ioFirstImage,
980 QSSGRenderableImage *&ioNextImage,
981 QSSGRenderableObjectFlags &ioFlags,
982 QSSGShaderDefaultMaterialKey &inShaderKey,
983 quint32 inImageIndex,
984 QSSGRenderDefaultMaterial *inMaterial)
986 QSSGRenderContextInterface &contextInterface = *renderer->contextInterface();
987 const auto &bufferManager = contextInterface.bufferManager();
989 if (inImage.clearDirty())
990 ioFlags |= QSSGRenderableObjectFlag::Dirty;
1001 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(&inImage);
1003 if (texture.m_texture) {
1004 if (texture.m_flags.hasTransparency()
1005 && (inMapType == QSSGRenderableImage::Type::Diffuse
1006 || inMapType == QSSGRenderableImage::Type::Opacity
1007 || inMapType == QSSGRenderableImage::Type::Translucency))
1009 ioFlags |= QSSGRenderableObjectFlag::HasTransparency;
1012 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, inMapType, inImage, texture);
1013 QSSGShaderKeyImageMap &theKeyProp = defaultMaterialShaderKeyProperties.m_imageMaps[inImageIndex];
1015 theKeyProp.setEnabled(inShaderKey,
true);
1016 switch (inImage.m_mappingMode) {
1017 case QSSGRenderImage::MappingModes::Normal:
1019 case QSSGRenderImage::MappingModes::Environment:
1020 theKeyProp.setEnvMap(inShaderKey,
true);
1022 case QSSGRenderImage::MappingModes::LightProbe:
1023 theKeyProp.setLightProbe(inShaderKey,
true);
1033 switch (texture.m_texture->format()) {
1034 case QRhiTexture::Format::RED_OR_ALPHA8:
1035 hasA = !renderer->contextInterface()->rhiContext()->rhi()->isFeatureSupported(QRhi::RedOrAlpha8IsRed);
1037 case QRhiTexture::Format::R8:
1047 if (inImage.isImageTransformIdentity())
1048 theKeyProp.setIdentityTransform(inShaderKey,
true);
1050 if (inImage.m_indexUV == 1)
1051 theKeyProp.setUsesUV1(inShaderKey,
true);
1053 if (texture.m_flags.isLinear())
1054 theKeyProp.setLinear(inShaderKey,
true);
1056 if (texture.m_flags.isPreMultipliedAlpha())
1057 theKeyProp.setPreMultipliedAlpha(inShaderKey,
true);
1059 if (ioFirstImage ==
nullptr)
1060 ioFirstImage = theImage;
1062 ioNextImage->m_nextImage = theImage;
1064 ioNextImage = theImage;
1066 if (inMaterial && inImageIndex >= QSSGShaderDefaultMaterialKeyProperties::SingleChannelImagesFirst) {
1067 QSSGRenderDefaultMaterial::TextureChannelMapping value = QSSGRenderDefaultMaterial::R;
1069 const quint32 scIndex = inImageIndex - QSSGShaderDefaultMaterialKeyProperties::SingleChannelImagesFirst;
1070 QSSGShaderKeyTextureChannel &channelKey = defaultMaterialShaderKeyProperties.m_textureChannels[scIndex];
1071 switch (inImageIndex) {
1072 case QSSGShaderDefaultMaterialKeyProperties::OpacityMap:
1073 value = inMaterial->opacityChannel;
1075 case QSSGShaderDefaultMaterialKeyProperties::RoughnessMap:
1076 value = inMaterial->roughnessChannel;
1078 case QSSGShaderDefaultMaterialKeyProperties::MetalnessMap:
1079 value = inMaterial->metalnessChannel;
1081 case QSSGShaderDefaultMaterialKeyProperties::OcclusionMap:
1082 value = inMaterial->occlusionChannel;
1084 case QSSGShaderDefaultMaterialKeyProperties::TranslucencyMap:
1085 value = inMaterial->translucencyChannel;
1087 case QSSGShaderDefaultMaterialKeyProperties::HeightMap:
1088 value = inMaterial->heightChannel;
1090 case QSSGShaderDefaultMaterialKeyProperties::ClearcoatMap:
1091 value = inMaterial->clearcoatChannel;
1093 case QSSGShaderDefaultMaterialKeyProperties::ClearcoatRoughnessMap:
1094 value = inMaterial->clearcoatRoughnessChannel;
1096 case QSSGShaderDefaultMaterialKeyProperties::TransmissionMap:
1097 value = inMaterial->transmissionChannel;
1099 case QSSGShaderDefaultMaterialKeyProperties::ThicknessMap:
1100 value = inMaterial->thicknessChannel;
1102 case QSSGShaderDefaultMaterialKeyProperties::BaseColorMap:
1103 value = inMaterial->baseColorChannel;
1105 case QSSGShaderDefaultMaterialKeyProperties::SpecularAmountMap:
1106 value = inMaterial->specularAmountChannel;
1108 case QSSGShaderDefaultMaterialKeyProperties::EmissiveMap:
1109 value = inMaterial->emissiveChannel;
1114 bool useDefault =
false;
1116 case QSSGRenderDefaultMaterial::TextureChannelMapping::G:
1119 case QSSGRenderDefaultMaterial::TextureChannelMapping::B:
1122 case QSSGRenderDefaultMaterial::TextureChannelMapping::A:
1129 value = QSSGRenderDefaultMaterial::R;
1130 channelKey.setTextureChannel(QSSGShaderKeyTextureChannel::TexturChannelBits(value), inShaderKey);
1160QSSGDefaultMaterialPreparationResult QSSGLayerRenderData::prepareDefaultMaterialForRender(
1161 QSSGRenderDefaultMaterial &inMaterial,
1162 QSSGRenderableObjectFlags &inExistingFlags,
1163 float inOpacity,
bool hasAnyLights,
1164 bool anyLightHasShadows,
1165 QSSGLayerRenderPreparationResultFlags &ioFlags)
1167 QSSGRenderDefaultMaterial *theMaterial = &inMaterial;
1168 QSSGDefaultMaterialPreparationResult retval(QSSGShaderDefaultMaterialKey(qHash(features)));
1169 retval.renderableFlags = inExistingFlags;
1170 QSSGRenderableObjectFlags &renderableFlags(retval.renderableFlags);
1171 QSSGShaderDefaultMaterialKey &theGeneratedKey(retval.materialKey);
1172 retval.opacity = inOpacity;
1173 float &subsetOpacity(retval.opacity);
1175 if (theMaterial->isDirty())
1176 renderableFlags |= QSSGRenderableObjectFlag::Dirty;
1178 subsetOpacity *= theMaterial->opacity;
1180 QSSGRenderableImage *firstImage =
nullptr;
1182 const bool lighting = theMaterial->lighting != QSSGRenderDefaultMaterial::MaterialLighting::NoLighting;
1183 defaultMaterialShaderKeyProperties.m_hasLighting.setValue(theGeneratedKey, lighting);
1185 defaultMaterialShaderKeyProperties.m_hasPunctualLights.setValue(theGeneratedKey, hasAnyLights);
1186 defaultMaterialShaderKeyProperties.m_hasShadows.setValue(theGeneratedKey, anyLightHasShadows);
1187 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey, layer.lightProbe !=
nullptr);
1190 defaultMaterialShaderKeyProperties.m_specularAAEnabled.setValue(theGeneratedKey, layer.specularAAEnabled);
1193 defaultMaterialShaderKeyProperties.m_isDoubleSided.setValue(theGeneratedKey, theMaterial->cullMode == QSSGCullFaceMode::Disabled);
1196 defaultMaterialShaderKeyProperties.m_overridesPosition.setValue(theGeneratedKey,
false);
1199 defaultMaterialShaderKeyProperties.m_usesProjectionMatrix.setValue(theGeneratedKey,
false);
1200 defaultMaterialShaderKeyProperties.m_usesInverseProjectionMatrix.setValue(theGeneratedKey,
false);
1202 defaultMaterialShaderKeyProperties.m_usesVarColor.setValue(theGeneratedKey,
false);
1205 defaultMaterialShaderKeyProperties.m_alphaMode.setValue(theGeneratedKey, theMaterial->alphaMode);
1208 setVertexInputPresence(renderableFlags, theGeneratedKey);
1211 defaultMaterialShaderKeyProperties.m_usesPointsTopology.setValue(theGeneratedKey, renderableFlags.isPointsTopology());
1214 defaultMaterialShaderKeyProperties.m_lightmapEnabled.setValue(theGeneratedKey, renderableFlags.rendersWithLightmap());
1215 defaultMaterialShaderKeyProperties.m_metallicRoughnessEnabled.setValue(theGeneratedKey, theMaterial->type == QSSGRenderDefaultMaterial::Type::PrincipledMaterial);
1216 defaultMaterialShaderKeyProperties.m_specularGlossyEnabled.setValue(theGeneratedKey, theMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial);
1220 defaultMaterialShaderKeyProperties.m_debugMode.setValue(theGeneratedKey,
int(layer.debugMode));
1223 defaultMaterialShaderKeyProperties.m_fogEnabled.setValue(theGeneratedKey, layer.fog.enabled);
1226 defaultMaterialShaderKeyProperties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
1227 defaultMaterialShaderKeyProperties.m_usesViewIndex.setValue(theGeneratedKey, layer.viewCount >= 2);
1229 if (!defaultMaterialShaderKeyProperties.m_hasIbl.getValue(theGeneratedKey) && theMaterial->iblProbe) {
1230 features.set(QSSGShaderFeatures::Feature::LightProbe,
true);
1231 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey,
true);
1236 if (subsetOpacity >= QSSGRendererPrivate::minimumRenderOpacity) {
1252 if (theMaterial->blendMode != QSSGRenderDefaultMaterial::MaterialBlendMode::SourceOver
1253 || theMaterial->opacityMap
1254 || theMaterial->alphaMode == QSSGRenderDefaultMaterial::Blend)
1256 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1259 const bool specularEnabled = theMaterial->isSpecularEnabled();
1260 const bool metalnessEnabled = theMaterial->isMetalnessEnabled();
1261 defaultMaterialShaderKeyProperties.m_specularEnabled.setValue(theGeneratedKey, specularEnabled || metalnessEnabled);
1262 defaultMaterialShaderKeyProperties.m_specularModel.setSpecularModel(theGeneratedKey, theMaterial->specularModel);
1263 defaultMaterialShaderKeyProperties.m_diffuseModel.setDiffuseModel(theGeneratedKey, theMaterial->diffuseModel);
1264 defaultMaterialShaderKeyProperties.m_fresnelScaleBiasEnabled.setValue(theGeneratedKey, theMaterial->isFresnelScaleBiasEnabled());
1265 defaultMaterialShaderKeyProperties.m_clearcoatFresnelScaleBiasEnabled.setValue(theGeneratedKey, theMaterial->isClearcoatFresnelScaleBiasEnabled());
1266 defaultMaterialShaderKeyProperties.m_fresnelEnabled.setValue(theGeneratedKey, theMaterial->isFresnelEnabled());
1267 defaultMaterialShaderKeyProperties.m_baseColorSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isBaseColorSingleChannelEnabled());
1268 defaultMaterialShaderKeyProperties.m_specularSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isSpecularAmountSingleChannelEnabled());
1269 defaultMaterialShaderKeyProperties.m_emissiveSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isEmissiveSingleChannelEnabled());
1270 defaultMaterialShaderKeyProperties.m_invertOpacityMapValue.setValue(theGeneratedKey, theMaterial->isInvertOpacityMapValue());
1271 defaultMaterialShaderKeyProperties.m_vertexColorsEnabled.setValue(theGeneratedKey, theMaterial->isVertexColorsEnabled());
1272 defaultMaterialShaderKeyProperties.m_vertexColorsMaskEnabled.setValue(theGeneratedKey, theMaterial->isVertexColorsMaskEnabled());
1273 defaultMaterialShaderKeyProperties.m_vertexColorRedMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorRedMask.toInt()));
1274 defaultMaterialShaderKeyProperties.m_vertexColorGreenMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorGreenMask.toInt()));
1275 defaultMaterialShaderKeyProperties.m_vertexColorBlueMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorBlueMask.toInt()));
1276 defaultMaterialShaderKeyProperties.m_vertexColorAlphaMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorAlphaMask.toInt()));
1277 defaultMaterialShaderKeyProperties.m_clearcoatEnabled.setValue(theGeneratedKey, theMaterial->isClearcoatEnabled());
1278 defaultMaterialShaderKeyProperties.m_transmissionEnabled.setValue(theGeneratedKey, theMaterial->isTransmissionEnabled());
1283 QSSGRenderableImage *nextImage =
nullptr;
1284#define CHECK_IMAGE_AND_PREPARE(img, imgtype, shadercomponent)
1286 prepareImageForRender(*(img), imgtype, firstImage, nextImage, renderableFlags,
1287 theGeneratedKey, shadercomponent, &inMaterial)
1289 if (theMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
1290 theMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial) {
1292 QSSGRenderableImage::Type::BaseColor,
1293 QSSGShaderDefaultMaterialKeyProperties::BaseColorMap);
1295 QSSGRenderableImage::Type::Occlusion,
1296 QSSGShaderDefaultMaterialKeyProperties::OcclusionMap);
1298 QSSGRenderableImage::Type::Height,
1299 QSSGShaderDefaultMaterialKeyProperties::HeightMap);
1301 QSSGRenderableImage::Type::Clearcoat,
1302 QSSGShaderDefaultMaterialKeyProperties::ClearcoatMap);
1304 QSSGRenderableImage::Type::ClearcoatRoughness,
1305 QSSGShaderDefaultMaterialKeyProperties::ClearcoatRoughnessMap);
1307 QSSGRenderableImage::Type::ClearcoatNormal,
1308 QSSGShaderDefaultMaterialKeyProperties::ClearcoatNormalMap);
1310 QSSGRenderableImage::Type::Transmission,
1311 QSSGShaderDefaultMaterialKeyProperties::TransmissionMap);
1313 QSSGRenderableImage::Type::Thickness,
1314 QSSGShaderDefaultMaterialKeyProperties::ThicknessMap);
1315 if (theMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial) {
1317 QSSGRenderableImage::Type::Metalness,
1318 QSSGShaderDefaultMaterialKeyProperties::MetalnessMap);
1322 QSSGRenderableImage::Type::Diffuse,
1323 QSSGShaderDefaultMaterialKeyProperties::DiffuseMap);
1325 CHECK_IMAGE_AND_PREPARE(theMaterial->emissiveMap, QSSGRenderableImage::Type::Emissive, QSSGShaderDefaultMaterialKeyProperties::EmissiveMap);
1327 QSSGRenderableImage::Type::Specular,
1328 QSSGShaderDefaultMaterialKeyProperties::SpecularMap);
1330 QSSGRenderableImage::Type::Roughness,
1331 QSSGShaderDefaultMaterialKeyProperties::RoughnessMap);
1332 CHECK_IMAGE_AND_PREPARE(theMaterial->opacityMap, QSSGRenderableImage::Type::Opacity, QSSGShaderDefaultMaterialKeyProperties::OpacityMap);
1333 CHECK_IMAGE_AND_PREPARE(theMaterial->bumpMap, QSSGRenderableImage::Type::Bump, QSSGShaderDefaultMaterialKeyProperties::BumpMap);
1335 QSSGRenderableImage::Type::SpecularAmountMap,
1336 QSSGShaderDefaultMaterialKeyProperties::SpecularAmountMap);
1337 CHECK_IMAGE_AND_PREPARE(theMaterial->normalMap, QSSGRenderableImage::Type::Normal, QSSGShaderDefaultMaterialKeyProperties::NormalMap);
1339 QSSGRenderableImage::Type::Translucency,
1340 QSSGShaderDefaultMaterialKeyProperties::TranslucencyMap);
1342#undef CHECK_IMAGE_AND_PREPARE
1344 if (subsetOpacity < QSSGRendererPrivate::minimumRenderOpacity) {
1345 subsetOpacity = 0.0f;
1349 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1350 renderableFlags |= QSSGRenderableObjectFlag::CompletelyTransparent;
1353 if (subsetOpacity > 1.f - QSSGRendererPrivate::minimumRenderOpacity)
1354 subsetOpacity = 1.f;
1356 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1358 if (inMaterial.isTransmissionEnabled()) {
1359 ioFlags.setRequiresScreenTexture(
true);
1360 ioFlags.setRequiresMipmapsForScreenTexture(
true);
1361 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1364 if (renderableFlags.hasTransparency()) {
1365 if (orderIndependentTransparencyEnabled)
1366 defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
1367 if (layer.oitMethodDirty)
1368 renderableFlags |= QSSGRenderableObjectFlag::Dirty;
1371 retval.firstImage = firstImage;
1372 if (retval.renderableFlags.isDirty())
1373 retval.dirty =
true;
1375 renderer->addMaterialDirtyClear(&inMaterial);
1379QSSGDefaultMaterialPreparationResult QSSGLayerRenderData::prepareCustomMaterialForRender(
1380 QSSGRenderCustomMaterial &inMaterial, QSSGRenderableObjectFlags &inExistingFlags,
1381 float inOpacity,
bool alreadyDirty,
bool hasAnyLights,
bool anyLightHasShadows,
1382 QSSGLayerRenderPreparationResultFlags &ioFlags)
1384 QSSGDefaultMaterialPreparationResult retval(QSSGShaderDefaultMaterialKey(qHash(features)));
1385 retval.renderableFlags = inExistingFlags;
1386 QSSGRenderableObjectFlags &renderableFlags(retval.renderableFlags);
1387 QSSGShaderDefaultMaterialKey &theGeneratedKey(retval.materialKey);
1388 retval.opacity = inOpacity;
1389 float &subsetOpacity(retval.opacity);
1391 if (subsetOpacity < QSSGRendererPrivate::minimumRenderOpacity) {
1392 subsetOpacity = 0.0f;
1396 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1397 renderableFlags |= QSSGRenderableObjectFlag::CompletelyTransparent;
1400 if (subsetOpacity > 1.f - QSSGRendererPrivate::minimumRenderOpacity)
1401 subsetOpacity = 1.f;
1403 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1405 defaultMaterialShaderKeyProperties.m_hasLighting.setValue(theGeneratedKey,
true);
1406 defaultMaterialShaderKeyProperties.m_hasPunctualLights.setValue(theGeneratedKey, hasAnyLights);
1407 defaultMaterialShaderKeyProperties.m_hasShadows.setValue(theGeneratedKey, anyLightHasShadows);
1408 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey, layer.lightProbe !=
nullptr);
1409 defaultMaterialShaderKeyProperties.m_specularEnabled.setValue(theGeneratedKey,
true);
1411 defaultMaterialShaderKeyProperties.m_specularAAEnabled.setValue(theGeneratedKey, layer.specularAAEnabled);
1414 defaultMaterialShaderKeyProperties.m_isDoubleSided.setValue(theGeneratedKey, inMaterial.m_cullMode == QSSGCullFaceMode::Disabled);
1417 defaultMaterialShaderKeyProperties.m_metallicRoughnessEnabled.setValue(theGeneratedKey,
true);
1420 const bool overridesPosition = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::OverridesPosition);
1421 defaultMaterialShaderKeyProperties.m_overridesPosition.setValue(theGeneratedKey, overridesPosition);
1424 const bool usesProjectionMatrix = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ProjectionMatrix);
1425 defaultMaterialShaderKeyProperties.m_usesProjectionMatrix.setValue(theGeneratedKey, usesProjectionMatrix);
1426 const bool usesInvProjectionMatrix = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::InverseProjectionMatrix);
1427 defaultMaterialShaderKeyProperties.m_usesInverseProjectionMatrix.setValue(theGeneratedKey, usesInvProjectionMatrix);
1430 setVertexInputPresence(renderableFlags, theGeneratedKey);
1433 defaultMaterialShaderKeyProperties.m_usesPointsTopology.setValue(theGeneratedKey, renderableFlags.isPointsTopology());
1436 defaultMaterialShaderKeyProperties.m_lightmapEnabled.setValue(theGeneratedKey, renderableFlags.rendersWithLightmap());
1439 defaultMaterialShaderKeyProperties.m_debugMode.setValue(theGeneratedKey,
int(layer.debugMode));
1442 defaultMaterialShaderKeyProperties.m_fogEnabled.setValue(theGeneratedKey, layer.fog.enabled);
1445 defaultMaterialShaderKeyProperties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
1446 defaultMaterialShaderKeyProperties.m_usesViewIndex.setValue(theGeneratedKey,
1447 inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ViewIndex));
1452 const bool usesVarColor = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::VarColor);
1453 defaultMaterialShaderKeyProperties.m_usesVarColor.setValue(theGeneratedKey, usesVarColor);
1455 const bool usesClearcoat = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Clearcoat);
1456 defaultMaterialShaderKeyProperties.m_clearcoatEnabled.setValue(theGeneratedKey, usesClearcoat);
1458 const bool usesClearcoatFresnelScaleBias = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ClearcoatFresnelScaleBias);
1459 defaultMaterialShaderKeyProperties.m_clearcoatFresnelScaleBiasEnabled.setValue(theGeneratedKey, usesClearcoatFresnelScaleBias);
1461 const bool usesFresnelScaleBias = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::FresnelScaleBias);
1462 defaultMaterialShaderKeyProperties.m_fresnelScaleBiasEnabled.setValue(theGeneratedKey, usesFresnelScaleBias);
1464 const bool usesTransmission = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Transmission);
1465 defaultMaterialShaderKeyProperties.m_transmissionEnabled.setValue(theGeneratedKey, usesTransmission);
1467 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Blending))
1468 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1470 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ScreenTexture)) {
1471 ioFlags.setRequiresScreenTexture(
true);
1472 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1475 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ScreenMipTexture)) {
1476 ioFlags.setRequiresScreenTexture(
true);
1477 ioFlags.setRequiresMipmapsForScreenTexture(
true);
1478 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1481 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::DepthTexture))
1482 ioFlags.setRequiresDepthTexture(
true);
1484 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::NormalTexture)) {
1485 ioFlags.setRequiresNormalTexture(
true);
1486 renderableFlags |= QSSGRenderableObjectFlag::RequiresNormalTexture;
1489 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::AoTexture)) {
1490 ioFlags.setRequiresDepthTexture(
true);
1491 ioFlags.setRequiresSsaoPass(
true);
1493 if (orderIndependentTransparencyEnabled && renderableFlags.hasTransparency())
1494 defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
1496 retval.firstImage =
nullptr;
1498 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::MotionVectorTexture))
1499 ioFlags.setRequiresMotionVectorPass(
true);
1501 if (retval.dirty || alreadyDirty)
1502 renderer->addMaterialDirtyClear(&inMaterial);
1560bool QSSGLayerRenderData::prepareModelsForRender(QSSGRenderContextInterface &contextInterface,
1561 const RenderableNodeEntries &renderableModels,
1562 QSSGLayerRenderPreparationResultFlags &ioFlags,
1563 const QSSGRenderCameraList &allCameras,
1564 const QSSGRenderCameraDataList &allCameraData,
1565 TModelContextPtrList &modelContexts,
1566 QSSGRenderableObjectList &opaqueObjects,
1567 QSSGRenderableObjectList &transparentObjects,
1568 QSSGRenderableObjectList &screenTextureObjects,
1571 const auto &rhiCtx = contextInterface.rhiContext();
1572 const auto &bufferManager = contextInterface.bufferManager();
1574 const auto &debugDrawSystem = contextInterface.debugDrawSystem();
1575 const bool maybeDebugDraw = debugDrawSystem && debugDrawSystem->isEnabled();
1577 bool wasDirty =
false;
1579 for (
const QSSGRenderableNodeEntry &renderable : renderableModels) {
1580 if ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::Disabled) != 0)
1583 const QSSGRenderModel &model = *
static_cast<QSSGRenderModel *>(renderable.node);
1584 const auto &lights = renderable.lights;
1585 QSSGRenderMesh *theMesh = modelData->getMesh(model);
1589 const bool altGlobalTransform = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::GlobalTransform) != 0);
1590 const auto &globalTransform = altGlobalTransform ? renderable.extOverrides.globalTransform : getGlobalTransform(model);
1591 QMatrix3x3 normalMatrix { Qt::Uninitialized };
1592 QSSGLayerRenderData::ModelViewProjections mvps;
1593 if (altGlobalTransform) {
1594 QSSGRenderNode::calculateNormalMatrix(globalTransform, normalMatrix);
1595 size_t mvpCount = 0;
1596 for (
const auto &cameraData : allCameraData) {
1597 QSSGRenderNode::calculateMVPAndNormalMatrix(globalTransform, cameraData.viewProjection, mvps[mvpCount++], normalMatrix);
1600 if (model.usesBoneTexture()) {
1604 size_t mvpCount = 0;
1605 for (
const QSSGRenderCameraData &cameraData : allCameraData) {
1606 mvps[mvpCount++] = cameraData.viewProjection;
1607 normalMatrix = QMatrix3x3();
1610 normalMatrix = getNormalMatrix(model);
1611 mvps = getModelMvps(model);
1614 const bool altModelOpacity = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::GlobalOpacity) != 0);
1615 const float modelGlobalOpacity = altModelOpacity ? renderable.extOverrides.globalOpacity : getGlobalOpacity(model);
1616 QSSGModelContext &theModelContext = *RENDER_FRAME_NEW<QSSGModelContext>(contextInterface, model, globalTransform, normalMatrix, mvps);
1617 modelContexts.push_back(&theModelContext);
1620 const auto &meshSubsets = theMesh->subsets;
1621 const auto meshSubsetCount = meshSubsets.size();
1622 theModelContext.subsets = RENDER_FRAME_NEW_BUFFER<QSSGSubsetRenderable>(contextInterface, meshSubsetCount);
1626 auto boneTexture = bufferManager->loadSkinmap(model.skin);
1627 setBonemapTexture(theModelContext, boneTexture.m_texture);
1628 }
else if (model.skeleton) {
1629 auto boneTexture = bufferManager->loadSkinmap(&(model.skeleton->boneTexData));
1630 setBonemapTexture(theModelContext, boneTexture.m_texture);
1634 QSSGRenderableObjectFlags renderableFlagsForModel;
1636 if (meshSubsetCount > 0) {
1637 const QSSGRenderSubset &theSubset = meshSubsets.at(0);
1639 renderableFlagsForModel.setMotionVectorParticipant(model.motionVectorEnabled);
1641 renderableFlagsForModel.setCastsShadows(model.castsShadows);
1642 renderableFlagsForModel.setReceivesShadows(model.receivesShadows);
1643 renderableFlagsForModel.setReceivesReflections(model.receivesReflections);
1644 renderableFlagsForModel.setCastsReflections(model.castsReflections);
1646 renderableFlagsForModel.setUsedInBakedLighting(model.usedInBakedLighting);
1647 if (model.hasLightmap()) {
1648 QSSGRenderImageTexture lmImageTexture = bufferManager->loadLightmap(model);
1649 if (lmImageTexture.m_texture) {
1650 renderableFlagsForModel.setRendersWithLightmap(
true);
1651 setLightmapTexture(theModelContext, lmImageTexture.m_texture);
1660 bool hasJoint =
false;
1661 bool hasWeight =
false;
1662 bool hasMorphTarget = theSubset.rhi.targetsTexture !=
nullptr;
1663 for (
const QSSGRhiInputAssemblerState::InputSemantic &sem : std::as_const(theSubset.rhi.ia.inputs)) {
1664 if (sem == QSSGRhiInputAssemblerState::PositionSemantic) {
1665 renderableFlagsForModel.setHasAttributePosition(
true);
1666 }
else if (sem == QSSGRhiInputAssemblerState::NormalSemantic) {
1667 renderableFlagsForModel.setHasAttributeNormal(
true);
1668 }
else if (sem == QSSGRhiInputAssemblerState::TexCoord0Semantic) {
1669 renderableFlagsForModel.setHasAttributeTexCoord0(
true);
1670 }
else if (sem == QSSGRhiInputAssemblerState::TexCoord1Semantic) {
1671 renderableFlagsForModel.setHasAttributeTexCoord1(
true);
1672 }
else if (sem == QSSGRhiInputAssemblerState::TexCoordLightmapSemantic) {
1673 renderableFlagsForModel.setHasAttributeTexCoordLightmap(
true);
1674 }
else if (sem == QSSGRhiInputAssemblerState::TangentSemantic) {
1675 renderableFlagsForModel.setHasAttributeTangent(
true);
1676 }
else if (sem == QSSGRhiInputAssemblerState::BinormalSemantic) {
1677 renderableFlagsForModel.setHasAttributeBinormal(
true);
1678 }
else if (sem == QSSGRhiInputAssemblerState::ColorSemantic) {
1679 renderableFlagsForModel.setHasAttributeColor(
true);
1682 }
else if (sem == QSSGRhiInputAssemblerState::JointSemantic) {
1684 }
else if (sem == QSSGRhiInputAssemblerState::WeightSemantic) {
1688 renderableFlagsForModel.setHasAttributeJointAndWeight(hasJoint && hasWeight);
1689 renderableFlagsForModel.setHasAttributeMorphTarget(hasMorphTarget);
1692 QSSGRenderableObjectList bakedLightingObjects;
1693 bool usesBlendParticles = particlesEnabled && theModelContext.model.particleBuffer !=
nullptr
1694 && model.particleBuffer->particleCount();
1695 const bool anyLightHasShadows = std::find_if(lights.begin(),
1697 [](
const QSSGShaderLight &light) {
return light.shadows; })
1699 const bool hasAnyLights = !lights.isEmpty();
1700 QSSGRenderLight::SoftShadowQuality maxSoftShadowQuality = QSSGRenderLight::SoftShadowQuality::Hard;
1701 if (anyLightHasShadows) {
1703 for (
const QSSGShaderLight &light : lights) {
1704 if (light.shadows && light.light->m_softShadowQuality > maxSoftShadowQuality)
1705 maxSoftShadowQuality = light.light->m_softShadowQuality;
1711 auto &renderableSubsets = theModelContext.subsets;
1712 const bool hasMaterialOverrides = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::Materials) != 0);
1713 const auto &materials = hasMaterialOverrides ? renderable.extOverrides.materials : modelData->getMaterials(model);
1714 const auto materialCount = materials.size();
1715 QSSGRenderGraphObject *lastMaterial = !materials.isEmpty() ? materials.last() :
nullptr;
1716 int idx = 0, subsetIdx = 0;
1717 for (; idx < meshSubsetCount; ++idx) {
1719 QSSGRenderGraphObject *theMaterialObject = (idx >= materialCount) ? lastMaterial : materials[idx];
1720 if (!theMaterialObject)
1723 const QSSGRenderSubset &theSubset = meshSubsets.at(idx);
1724 QSSGRenderableObjectFlags renderableFlags = renderableFlagsForModel;
1725 float subsetOpacity = modelGlobalOpacity;
1727 renderableFlags.setPointsTopology(theSubset.rhi.ia.topology == QRhiGraphicsPipeline::Points);
1728 QSSGRenderableObject *theRenderableObject = &renderableSubsets[subsetIdx++];
1730 const bool usesInstancing = theModelContext.model.instancing()
1731 && rhiCtx->rhi()->isFeatureSupported(QRhi::Instancing);
1732 if (usesInstancing && theModelContext.model.instanceTable->hasTransparency())
1733 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1734 if (theModelContext.model.hasTransparency)
1735 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1738 quint32 subsetLevelOfDetail = 0;
1739 if (!theSubset.lods.isEmpty() && lodThreshold > 0.0f) {
1741 float lodDistanceMultiplier = camerasView[0]->getLevelOfDetailMultiplier();
1742 float distanceThreshold = 0.0f;
1743 const auto scale = QSSGUtils::mat44::getScale(globalTransform);
1744 float modelScale = qMax(scale.x(), qMax(scale.y(), scale.z()));
1745 QSSGBounds3 transformedBounds = theSubset.bounds;
1746 if (camerasView[0]->type != QSSGRenderGraphObject::Type::OrthographicCamera) {
1747 transformedBounds.transform(globalTransform);
1748 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1749 debugDrawSystem->drawBounds(transformedBounds, QColor(Qt::red));
1750 const QMatrix4x4 cameraGlobalTranform = getGlobalTransform(*camerasView[0]);
1751 const QVector3D cameraNormal = QSSGRenderNode::getScalingCorrectDirection(cameraGlobalTranform);
1752 const QVector3D cameraPosition = QSSGRenderNode::getGlobalPos(cameraGlobalTranform);
1753 const QSSGPlane cameraPlane = QSSGPlane(cameraPosition, cameraNormal);
1754 const QVector3D lodSupportMin = transformedBounds.getSupport(-cameraNormal);
1755 const QVector3D lodSupportMax = transformedBounds.getSupport(cameraNormal);
1756 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1757 debugDrawSystem->drawPoint(lodSupportMin, QColor(
"orange"));
1759 const float distanceMin = cameraPlane.distance(lodSupportMin);
1760 const float distanceMax = cameraPlane.distance(lodSupportMax);
1762 if (distanceMin * distanceMax < 0.0)
1763 distanceThreshold = 0.0;
1764 else if (distanceMin >= 0.0)
1765 distanceThreshold = distanceMin;
1766 else if (distanceMax <= 0.0)
1767 distanceThreshold = -distanceMax;
1771 distanceThreshold = 1.0;
1774 int currentLod = -1;
1775 if (model.levelOfDetailBias > 0.0f) {
1776 const float threshold = distanceThreshold * lodDistanceMultiplier;
1777 const float modelBias = 1 / model.levelOfDetailBias;
1778 for (qsizetype i = 0; i < theSubset.lods.count(); ++i) {
1779 float subsetDistance = theSubset.lods[i].distance * modelScale * modelBias;
1780 float screenSize = subsetDistance / threshold;
1781 if (screenSize > lodThreshold)
1786 if (currentLod == -1)
1787 subsetLevelOfDetail = 0;
1789 subsetLevelOfDetail = currentLod + 1;
1790 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1791 debugDrawSystem->drawBounds(transformedBounds, QSSGDebugDrawSystem::levelOfDetailColor(subsetLevelOfDetail));
1794 QVector3D theModelCenter(theSubset.bounds.center());
1795 theModelCenter = QSSGUtils::mat44::transform(globalTransform, theModelCenter);
1796 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLodNormal)) {
1797 const QMatrix4x4 allCamera0GlobalTransform = getGlobalTransform(*allCameras[0]);
1798 debugDrawSystem->debugNormals(*bufferManager, theModelContext, theSubset, subsetLevelOfDetail, (theModelCenter - QSSGRenderNode::getGlobalPos(allCamera0GlobalTransform)).length() * 0.01);
1801 auto checkF32TypeIndex = [&rhiCtx](QRhiVertexInputAttribute::Format f) {
1802 if ((f == QRhiVertexInputAttribute::Format::Float4)
1803 || (f == QRhiVertexInputAttribute::Format::Float3)
1804 || (f == QRhiVertexInputAttribute::Format::Float2)
1805 || (f == QRhiVertexInputAttribute::Format::Float)) {
1808 if (!rhiCtx->rhi()->isFeatureSupported(QRhi::IntAttributes))
1809 qWarning() <<
"WARN: Model has non-integer type indices for skinning but current RHI backend doesn't support it!";
1813 if (theMaterialObject->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
1814 theMaterialObject->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
1815 theMaterialObject->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial) {
1816 QSSGRenderDefaultMaterial &theMaterial(
static_cast<QSSGRenderDefaultMaterial &>(*theMaterialObject));
1817 QSSGDefaultMaterialPreparationResult theMaterialPrepResult(prepareDefaultMaterialForRender(theMaterial, renderableFlags, subsetOpacity, hasAnyLights, anyLightHasShadows, ioFlags));
1818 QSSGShaderDefaultMaterialKey &theGeneratedKey(theMaterialPrepResult.materialKey);
1819 subsetOpacity = theMaterialPrepResult.opacity;
1820 QSSGRenderableImage *firstImage(theMaterialPrepResult.firstImage);
1821 wasDirty |= theMaterialPrepResult.dirty;
1822 renderableFlags = theMaterialPrepResult.renderableFlags;
1823 if (renderableFlags.hasTransparency())
1824 ioFlags.setHasCustomBlendMode(theMaterial.blendMode != QSSGRenderDefaultMaterial::MaterialBlendMode::SourceOver);
1827 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey, usesBlendParticles);
1829 if (defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.getValue(theGeneratedKey) ==
int(QSSGRenderLayer::OITMethod::LinkedList))
1830 defaultMaterialShaderKeyProperties.m_oitMSAA.setValue(theGeneratedKey, rhiCtx->mainPassSampleCount() > 1);
1833 const auto boneCount = model.skin ? model.skin->boneCount :
1834 model.skeleton ? model.skeleton->boneCount : 0;
1835 defaultMaterialShaderKeyProperties.m_boneCount.setValue(theGeneratedKey, boneCount);
1836 if (
auto idJoint = theSubset.rhi.ia.inputs.indexOf(QSSGRhiInputAssemblerState::JointSemantic); idJoint != -1) {
1837 const auto attr = theSubset.rhi.ia.inputLayout.attributeAt(idJoint);
1838 defaultMaterialShaderKeyProperties.m_usesFloatJointIndices.setValue(theGeneratedKey, checkF32TypeIndex(attr->format()));
1842 defaultMaterialShaderKeyProperties.m_shadowSoftness.setShadowSoftness(theGeneratedKey, maxSoftShadowQuality);
1845 defaultMaterialShaderKeyProperties.m_usesInstancing.setValue(theGeneratedKey, usesInstancing);
1847 defaultMaterialShaderKeyProperties.m_targetCount.setValue(theGeneratedKey,
1848 theSubset.rhi.ia.targetCount);
1849 defaultMaterialShaderKeyProperties.m_targetPositionOffset.setValue(theGeneratedKey,
1850 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::PositionSemantic]);
1851 defaultMaterialShaderKeyProperties.m_targetNormalOffset.setValue(theGeneratedKey,
1852 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::NormalSemantic]);
1853 defaultMaterialShaderKeyProperties.m_targetTangentOffset.setValue(theGeneratedKey,
1854 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TangentSemantic]);
1855 defaultMaterialShaderKeyProperties.m_targetBinormalOffset.setValue(theGeneratedKey,
1856 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::BinormalSemantic]);
1857 defaultMaterialShaderKeyProperties.m_targetTexCoord0Offset.setValue(theGeneratedKey,
1858 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord0Semantic]);
1859 defaultMaterialShaderKeyProperties.m_targetTexCoord1Offset.setValue(theGeneratedKey,
1860 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord1Semantic]);
1861 defaultMaterialShaderKeyProperties.m_targetColorOffset.setValue(theGeneratedKey,
1862 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::ColorSemantic]);
1864 new (theRenderableObject) QSSGSubsetRenderable(QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset,
1871 subsetLevelOfDetail,
1876 anyLightHasShadows);
1877 wasDirty = wasDirty || renderableFlags.isDirty();
1878 }
else if (theMaterialObject->type == QSSGRenderGraphObject::Type::CustomMaterial) {
1879 QSSGRenderCustomMaterial &theMaterial(
static_cast<QSSGRenderCustomMaterial &>(*theMaterialObject));
1881 const auto &theMaterialSystem(contextInterface.customMaterialSystem());
1882 wasDirty |= theMaterialSystem->prepareForRender(theModelContext.model, theSubset, theMaterial);
1884 if (theMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Blending))
1885 ioFlags.setHasCustomBlendMode(!hasCustomBlendMode(theMaterial));
1887 QSSGDefaultMaterialPreparationResult theMaterialPrepResult(
1888 prepareCustomMaterialForRender(theMaterial, renderableFlags, subsetOpacity, wasDirty,
1889 hasAnyLights, anyLightHasShadows, ioFlags));
1890 QSSGShaderDefaultMaterialKey &theGeneratedKey(theMaterialPrepResult.materialKey);
1891 subsetOpacity = theMaterialPrepResult.opacity;
1892 QSSGRenderableImage *firstImage(theMaterialPrepResult.firstImage);
1893 renderableFlags = theMaterialPrepResult.renderableFlags;
1895 if (model.particleBuffer && model.particleBuffer->particleCount())
1896 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey,
true);
1898 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey,
false);
1900 if (defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.getValue(theGeneratedKey) ==
int(QSSGRenderLayer::OITMethod::LinkedList))
1901 defaultMaterialShaderKeyProperties.m_oitMSAA.setValue(theGeneratedKey, rhiCtx->mainPassSampleCount() > 1);
1904 defaultMaterialShaderKeyProperties.m_shadowSoftness.setShadowSoftness(theGeneratedKey, maxSoftShadowQuality);
1907 const auto boneCount = model.skin ? model.skin->boneCount :
1908 model.skeleton ? model.skeleton->boneCount : 0;
1909 defaultMaterialShaderKeyProperties.m_boneCount.setValue(theGeneratedKey, boneCount);
1910 if (
auto idJoint = theSubset.rhi.ia.inputs.indexOf(QSSGRhiInputAssemblerState::JointSemantic); idJoint != -1) {
1911 const auto attr = theSubset.rhi.ia.inputLayout.attributeAt(idJoint);
1912 defaultMaterialShaderKeyProperties.m_usesFloatJointIndices.setValue(theGeneratedKey, checkF32TypeIndex(attr->format()));
1916 bool usesInstancing = theModelContext.model.instancing()
1917 && rhiCtx->rhi()->isFeatureSupported(QRhi::Instancing);
1918 defaultMaterialShaderKeyProperties.m_usesInstancing.setValue(theGeneratedKey, usesInstancing);
1920 defaultMaterialShaderKeyProperties.m_targetCount.setValue(theGeneratedKey,
1921 theSubset.rhi.ia.targetCount);
1922 defaultMaterialShaderKeyProperties.m_targetPositionOffset.setValue(theGeneratedKey,
1923 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::PositionSemantic]);
1924 defaultMaterialShaderKeyProperties.m_targetNormalOffset.setValue(theGeneratedKey,
1925 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::NormalSemantic]);
1926 defaultMaterialShaderKeyProperties.m_targetTangentOffset.setValue(theGeneratedKey,
1927 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TangentSemantic]);
1928 defaultMaterialShaderKeyProperties.m_targetBinormalOffset.setValue(theGeneratedKey,
1929 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::BinormalSemantic]);
1930 defaultMaterialShaderKeyProperties.m_targetTexCoord0Offset.setValue(theGeneratedKey,
1931 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord0Semantic]);
1932 defaultMaterialShaderKeyProperties.m_targetTexCoord1Offset.setValue(theGeneratedKey,
1933 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord1Semantic]);
1934 defaultMaterialShaderKeyProperties.m_targetColorOffset.setValue(theGeneratedKey,
1935 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::ColorSemantic]);
1937 if (theMaterial.m_iblProbe)
1938 theMaterial.m_iblProbe->clearDirty();
1940 new (theRenderableObject) QSSGSubsetRenderable(QSSGSubsetRenderable::Type::CustomMaterialMeshSubset,
1947 subsetLevelOfDetail,
1952 anyLightHasShadows);
1954 if (theRenderableObject)
1955 theRenderableObject->camdistSq = getCameraDistanceSq(*theRenderableObject, allCameraData[0]);
1959 if (Q_UNLIKELY(idx != subsetIdx))
1960 renderableSubsets.mSize = subsetIdx;
1962 for (
auto &ro : renderableSubsets) {
1963 const auto depthMode = ro.depthWriteMode;
1964 hasDepthWriteObjects |= (depthMode == QSSGDepthDrawMode::Always || depthMode == QSSGDepthDrawMode::OpaqueOnly);
1965 enum ObjectType : quint8 { ScreenTexture, Transparent, Opaque };
1966 static constexpr DepthPrepassObject ppState[][2] = { {DepthPrepassObject::None, DepthPrepassObject::ScreenTexture},
1967 {DepthPrepassObject::None, DepthPrepassObject::Transparent},
1968 {DepthPrepassObject::None, DepthPrepassObject::Opaque} };
1970 if (ro.renderableFlags.requiresScreenTexture()) {
1971 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::ScreenTexture][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1972 screenTextureObjects.push_back({&ro, ro.camdistSq, model.tag});
1973 }
else if (ro.renderableFlags.hasTransparency()) {
1974 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::Transparent][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1975 transparentObjects.push_back({&ro, ro.camdistSq, model.tag});
1977 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::Opaque][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1978 opaqueObjects.push_back({&ro, ro.camdistSq, model.tag});
1981 if (ro.renderableFlags.usedInBakedLighting())
1982 bakedLightingObjects.push_back({&ro, ro.camdistSq, model.tag});
1985 if (!bakedLightingObjects.isEmpty())
1986 bakedLightingModels.push_back(QSSGBakedLightingModel(&model, bakedLightingObjects));
1992bool QSSGLayerRenderData::prepareParticlesForRender(
const RenderableNodeEntries &renderableParticles,
const QSSGRenderCameraData &cameraData, QSSGLayerRenderPreparationResultFlags &ioFlags)
1994 QSSG_ASSERT(particlesEnabled,
return false);
1996 QSSGRenderContextInterface &contextInterface = *renderer->contextInterface();
2001 auto &opaqueObjects = opaqueObjectStore[0];
2002 auto &transparentObjects = transparentObjectStore[0];
2003 auto &screenTextureObjects = screenTextureObjectStore[0];
2005 for (
auto &renderable : renderableParticles) {
2006 QSSGShaderParticleMaterialKeyProperties &properties = particleMaterialShaderKeyProperties;
2008 QSSGRenderParticles &particles = *
static_cast<QSSGRenderParticles *>(renderable.node);
2009 QSSGShaderParticleMaterialKey &theGeneratedKey(particles.materialKey);
2010 const auto &lights = renderable.lights;
2012 QSSGRenderableObjectFlags renderableFlags;
2013 renderableFlags.setCastsShadows(
false);
2014 renderableFlags.setReceivesShadows(
false);
2015 renderableFlags.setHasAttributePosition(
true);
2016 renderableFlags.setHasAttributeNormal(
true);
2017 renderableFlags.setHasAttributeTexCoord0(
true);
2018 renderableFlags.setHasAttributeColor(
true);
2019 renderableFlags.setHasTransparency(particles.m_hasTransparency);
2020 renderableFlags.setCastsReflections(particles.m_castsReflections);
2021 if (particles.m_hasTransparency && particles.m_blendMode != QSSGRenderParticles::BlendMode::SourceOver)
2022 ioFlags.setHasCustomBlendMode(
true);
2024 properties.m_isLineParticle.setValue(theGeneratedKey, particles.m_featureLevel >= QSSGRenderParticles::FeatureLevel::Line);
2025 const bool animated = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimated
2026 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::Animated
2027 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::AnimatedVLight
2028 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimatedVLight;
2029 const bool mapped = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMapped
2030 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::Mapped
2031 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::MappedVLight
2032 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMappedVLight;
2033 const bool lit = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineVLight
2034 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::AnimatedVLight
2035 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::MappedVLight
2036 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMappedVLight
2037 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimatedVLight
2038 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::SimpleVLight;
2039 properties.m_isAnimated.setValue(theGeneratedKey, animated);
2040 properties.m_isMapped.setValue(theGeneratedKey, mapped);
2041 properties.m_hasLighting.setValue(theGeneratedKey, lit);
2042 properties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
2043 if (renderableFlags.hasTransparency() && orderIndependentTransparencyEnabled) {
2044 properties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
2045 if (layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
2046 properties.m_oitMSAA.setValue(theGeneratedKey, contextInterface.rhiContext()->mainPassSampleCount() > 1);
2048 properties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(0));
2051 float opacity = getGlobalOpacity(particles);
2052 QVector3D center(particles.m_particleBuffer.bounds().center());
2053 center = QSSGUtils::mat44::transform(getGlobalTransform(particles), center);
2055 QSSGRenderableImage *firstImage =
nullptr;
2056 if (particles.m_sprite) {
2057 const auto &bufferManager = contextInterface.bufferManager();
2059 if (particles.m_sprite->clearDirty())
2062 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(particles.m_sprite);
2063 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, QSSGRenderableImage::Type::Diffuse, *particles.m_sprite, texture);
2064 firstImage = theImage;
2065 properties.m_isSpriteLinear.setValue(theGeneratedKey, texture.m_flags.isLinear());
2068 QSSGRenderableImage *colorTable =
nullptr;
2069 if (particles.m_colorTable) {
2070 const auto &bufferManager = contextInterface.bufferManager();
2072 if (particles.m_colorTable->clearDirty())
2075 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(particles.m_colorTable);
2077 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, QSSGRenderableImage::Type::Diffuse, *particles.m_colorTable, texture);
2078 colorTable = theImage;
2079 properties.m_isColorTableLinear.setValue(theGeneratedKey, texture.m_flags.isLinear());
2082 if (opacity > 0.0f && particles.m_particleBuffer.particleCount()) {
2083 const auto globalTransform = getGlobalTransform(particles);
2084 auto *theRenderableObject = RENDER_FRAME_NEW<QSSGParticlesRenderable>(contextInterface,
2095 if (theRenderableObject) {
2096 if (theRenderableObject->renderableFlags.requiresScreenTexture())
2097 screenTextureObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2098 else if (theRenderableObject->renderableFlags.hasTransparency())
2099 transparentObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2101 opaqueObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2388void QSSGLayerRenderData::prepareForRender()
2390 QSSG_ASSERT_X(layerPrepResult.isNull(),
"Prep-result was not reset for render!", layerPrepResult = {});
2392 QRect theViewport(renderer->viewport());
2396 frameData.m_ctx = renderer->contextInterface();
2401 ps.viewport = {
float(theViewport.x()),
float(theViewport.y()),
float(theViewport.width()),
float(theViewport.height()), 0.0f, 1.0f };
2402 if (layer.scissorRect.isValid()) {
2403 ps.flags |= QSSGRhiGraphicsPipelineState::Flag::UsesScissor;
2404 ps.scissor = { layer.scissorRect.x(),
2405 theViewport.height() - (layer.scissorRect.y() + layer.scissorRect.height()),
2406 layer.scissorRect.width(),
2407 layer.scissorRect.height() };
2410 ps.depthFunc = QRhiGraphicsPipeline::LessOrEqual;
2411 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::BlendEnabled,
false);
2414 ps.polygonMode = layer.wireframeMode ? QRhiGraphicsPipeline::Line : QRhiGraphicsPipeline::Fill;
2416 bool wasDirty =
false;
2417 bool wasDataDirty =
false;
2418 wasDirty = layer.isDirty();
2420 const bool shouldDisableInternalPasses = (layer.renderOverrides & size_t(QSSGRenderLayer::RenderOverrides::DisableInternalPasses)) != 0;
2421 wasDirty |= (shouldDisableInternalPasses != disableMainPasses);
2422 disableMainPasses = shouldDisableInternalPasses;
2425 layerPrepResult = { theViewport, layer };
2428 const bool SSAOEnabled = layer.ssaoEnabled();
2429 layerPrepResult.flags.setRequiresSsaoPass(SSAOEnabled);
2430 features.set(QSSGShaderFeatures::Feature::Ssao, SSAOEnabled);
2433 bool requiresDepthTexture = SSAOEnabled;
2434 bool requiresNormalTexture =
false;
2435 bool requiresMotionVectorTexture =
false;
2436 for (QSSGRenderEffect *theEffect = layer.firstEffect; theEffect; theEffect = theEffect->m_nextEffect) {
2437 if (theEffect->isDirty()) {
2439 theEffect->clearDirty();
2441 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesDepthTexture))
2442 requiresDepthTexture =
true;
2443 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesNormalTexture))
2444 requiresNormalTexture =
true;
2445 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesMotionVectorTexture))
2446 requiresMotionVectorTexture =
true;
2449 const auto &rhiCtx = renderer->contextInterface()->rhiContext();
2450 orderIndependentTransparencyEnabled = (layer.oitMethod != QSSGRenderLayer::OITMethod::None);
2451 if (layer.oitMethod == QSSGRenderLayer::OITMethod::WeightedBlended) {
2452 orderIndependentTransparencyEnabled = rhiCtx->rhi()->isFeatureSupported(QRhi::PerRenderTargetBlending);
2453 if (rhiCtx->mainPassSampleCount() > 1)
2454 orderIndependentTransparencyEnabled |= rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch) && rhiCtx->rhi()->isFeatureSupported(QRhi::SampleVariables);
2455 if (!orderIndependentTransparencyEnabled && !oitWarningUnsupportedShown) {
2456 qCWarning(lcQuick3DRender) <<
"WeightedBlended OIT is requested, but it is not supported.";
2457 oitWarningUnsupportedShown =
true;
2459 }
else if (layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList) {
2460 if (rhiCtx->mainPassSampleCount() > 1)
2461 orderIndependentTransparencyEnabled |= rhiCtx->rhi()->isFeatureSupported(QRhi::SampleVariables);
2462 if (!orderIndependentTransparencyEnabled && !oitWarningUnsupportedShown) {
2463 qCWarning(lcQuick3DRender) <<
"LinkedList OIT is requested, but it is not supported.";
2464 oitWarningUnsupportedShown =
true;
2467 if (layer.oitMethodDirty) {
2468 oitRenderContext.reset();
2469 for (
auto &renderResult : renderResults)
2470 renderResult.reset();
2473 layerPrepResult.flags.setRequiresDepthTexture(requiresDepthTexture);
2475 layerPrepResult.flags.setRequiresNormalTexture(requiresNormalTexture);
2477 layerPrepResult.flags.setRequiresMotionVectorPass(requiresMotionVectorTexture);
2481 if (!layer.firstEffect)
2482 QSSGLayerRenderData::setTonemapFeatures(features, layer.tonemapMode);
2489 if (rhiCtx->rhi()->resourceLimit(QRhi::MaxUniformBufferRange) < REDUCED_MAX_LIGHT_COUNT_THRESHOLD_BYTES) {
2490 features.set(QSSGShaderFeatures::Feature::ReduceMaxNumLights,
true);
2491 static bool notified =
false;
2494 qCDebug(lcQuick3DRender,
"Qt Quick 3D maximum number of lights has been reduced from %d to %d due to the graphics driver's limitations",
2495 QSSG_MAX_NUM_LIGHTS, QSSG_REDUCED_MAX_NUM_LIGHTS);
2500 QSSGRenderImageTexture lightProbeTexture;
2501 if (layer.lightProbe) {
2502 const auto &lightProbeSettings = layer.lightProbeSettings;
2503 if (layer.lightProbe->m_format == QSSGRenderTextureFormat::Unknown) {
2506 if (renderer->contextInterface()->rhiContext()->rhi()->isTextureFormatSupported(QRhiTexture::RGBA16F))
2507 layer.lightProbe->m_format = QSSGRenderTextureFormat::RGBA16F;
2509 layer.lightProbe->m_format = QSSGRenderTextureFormat::RGBE8;
2512 if (layer.lightProbe->clearDirty())
2513 wasDataDirty =
true;
2516 lightProbeTexture = renderer->contextInterface()->bufferManager()->loadRenderImage(layer.lightProbe, QSSGBufferManager::MipModeBsdf);
2517 if (lightProbeTexture.m_texture) {
2519 features.set(QSSGShaderFeatures::Feature::LightProbe,
true);
2520 features.set(QSSGShaderFeatures::Feature::IblOrientation, !lightProbeSettings.probeOrientation.isIdentity());
2524 if (lightProbeTexture.m_flags.isRgbe8())
2525 features.set(QSSGShaderFeatures::Feature::RGBELightProbe,
true);
2527 layer.lightProbe =
nullptr;
2530 const bool forceIblExposureValues = (features.isSet(QSSGShaderFeatures::Feature::LightProbe) && layer.tonemapMode == QSSGRenderLayer::TonemapMode::Custom);
2531 features.set(QSSGShaderFeatures::Feature::ForceIblExposure, forceIblExposureValues);
2534 frameData.m_ctx->bufferManager()->setLightmapSource(layer.lightmapSource);
2538 version = nodeData->version();
2543 auto &globalTransforms = nodeData->globalTransforms;
2544 auto &globalOpacities = nodeData->globalOpacities;
2545 auto &instanceTransforms = nodeData->instanceTransforms;
2548 QSSGRenderDataHelpers::GlobalStateResultT globalStateResult = QSSGRenderDataHelpers::GlobalStateResult::None;
2550 const bool layerTreeWasDirty = layer.isDirty(QSSGRenderLayer::DirtyFlag::TreeDirty);
2551 layer.clearDirty(QSSGRenderLayer::DirtyFlag::TreeDirty);
2552 if (layerTreeWasDirty) {
2553 wasDataDirty =
true;
2554 layerNodes = nodeData->getLayerNodeView(layer);
2556 for (
auto &node : layerNodes)
2557 globalStateResult |= QSSGRenderDataHelpers::updateGlobalNodeState(node, version);
2559 bool transformAndOpacityDirty =
false;
2560 for (
auto &node : layerNodes)
2561 transformAndOpacityDirty |= QSSGRenderDataHelpers::calcGlobalNodeData<QSSGRenderDataHelpers::Strategy::Update>(node, version, globalTransforms, globalOpacities);
2564 if (transformAndOpacityDirty) {
2565 for (
const auto &node : layerNodes)
2566 wasDataDirty |= QSSGRenderDataHelpers::calcInstanceTransforms(node, version, globalTransforms, instanceTransforms);
2569 wasDataDirty |= transformAndOpacityDirty;
2575 const bool hasExplicitCamera = (layer.explicitCameras.size() != 0);
2576 bool cameraLayerMaskDirty =
false;
2577 quint32 layerMask = QSSGRenderCamera::LayerMaskAll;
2578 if (hasExplicitCamera) {
2579 QSSGRenderCamera *explicitCamera = layer.explicitCameras[0];
2582 layerMask = explicitCamera->tag.value() | QSSGRenderCamera::ReservedLayerMask;
2583 cameraLayerMaskDirty = explicitCamera->isDirty(QSSGRenderCamera::DirtyFlag::LayerMaskDirty);
2584 explicitCamera->clearDirty(QSSGRenderCamera::DirtyFlag::LayerMaskDirty);
2587 const bool restatNodes = (layerTreeWasDirty || (globalStateResult & QSSGRenderDataHelpers::GlobalStateResult::ActiveChanged) || cameraLayerMaskDirty);
2590 updateFilteredLayerNodes(layerMask);
2596 QSSGRenderCamera::Configuration cameraConfig { renderer->dpr(), layer.isSsaaEnabled() ? layer.ssaaMultiplier : 1.0f };
2597 renderedCameras.clear();
2598 if (!layer.explicitCameras.isEmpty()) {
2599 for (QSSGRenderCamera *cam : std::as_const(layer.explicitCameras)) {
2601 if (cam->getGlobalState(QSSGRenderCamera::GlobalState::Active)) {
2602 const bool computeFrustumSucceeded = cam->calculateProjection(theViewport, cameraConfig);
2603 if (Q_LIKELY(computeFrustumSucceeded))
2604 renderedCameras.append(cam);
2606 qCCritical(INTERNAL_ERROR,
"Failed to calculate camera frustum");
2610 }
else if (QSSG_GUARD_X(layer.viewCount == 1,
"Multiview rendering requires explicit cameras to be set!.")) {
2615 for (
auto iter = camerasView.begin(); renderedCameras.isEmpty() && iter != camerasView.end(); iter++) {
2616 QSSGRenderCamera *theCamera = *iter;
2617 if (theCamera->getGlobalState(QSSGRenderCamera::GlobalState::Active)) {
2618 const bool computeFrustumSucceeded = theCamera->calculateProjection(theViewport, cameraConfig);
2619 if (Q_LIKELY(computeFrustumSucceeded))
2620 renderedCameras.append(theCamera);
2622 qCCritical(INTERNAL_ERROR,
"Failed to calculate camera frustum");
2630 if (renderedCameras.size() > 0 && (renderedCameras[0]->layerMask & QSSGRenderCamera::LayerMaskUserAll) != QSSGRenderCamera::LayerMaskUserAll) {
2631 if (!nonExplicitCameraWithLayerMaskWarningShown) {
2632 nonExplicitCameraWithLayerMaskWarningShown =
true;
2633 qWarning() <<
"Scenes with non-explicit cameras with a layer detected!"
2634 " Set the camera explicitly to avoid unnecessary evaluation of scene nodes!";
2638 layerMask = renderedCameras[0]->layerMask;
2639 updateFilteredLayerNodes(layerMask);
2643 float meshLodThreshold = 1.0f;
2644 if (!renderedCameras.isEmpty())
2645 meshLodThreshold = renderedCameras[0]->levelOfDetailPixelThreshold / theViewport.width();
2647 layer.renderedCamerasMutex.lock();
2648 layer.renderedCameras = renderedCameras;
2649 layer.renderedCamerasMutex.unlock();
2652 const QSSGRenderCameraDataList &renderCameraData = getCachedCameraDatas();
2653 modelData->updateModelData(modelsView, renderer, renderCameraData);
2656 item2DData->updateItem2DData(item2DsView, renderer, renderCameraData);
2659 prepareResourceLoaders();
2662 updateDirtySkeletons(*
this, modelsView);
2665 int directionalLightsCount = 0;
2666 int positionalLightsCount = 0;
2667 const int maxLightCount = effectiveMaxLightCount(features);
2668 const int maxDirectionalLights = effectiveMaxDirectionalLightCount(features);
2669 QSSGShaderLightList renderableLights;
2670 int shadowMapCount = 0;
2671 bool hasScopedLights =
false;
2677 auto it = std::make_reverse_iterator(lightsView.end());
2678 const auto end = it + qMin(maxLightCount, lightsView.size());
2679 for (; it != end; ++it) {
2680 QSSGRenderLight *renderLight = (*it);
2681 QMatrix4x4 renderLightTransform = getGlobalTransform(*renderLight);
2682 if (renderLight->type == QSSGRenderGraphObject::Type::DirectionalLight)
2683 directionalLightsCount++;
2685 positionalLightsCount++;
2687 if (positionalLightsCount > maxLightCount)
2689 if (directionalLightsCount > maxDirectionalLights)
2693 hasScopedLights |= (renderLight->m_scope !=
nullptr);
2694 const bool castShadows = renderLight->m_castShadow && !renderLight->m_fullyBaked;
2695 shadowMapCount +=
int(castShadows);
2696 const auto &direction = renderLight->getScalingCorrectDirection(renderLightTransform);
2697 renderableLights.push_back(QSSGShaderLight{ renderLight, castShadows, direction });
2701 const bool showLightCountWarning = !tooManyLightsWarningShown && (positionalLightsCount > maxLightCount);
2702 if (showLightCountWarning) {
2703 qWarning(
"Too many lights in scene, maximum is %d", maxLightCount);
2704 tooManyLightsWarningShown =
true;
2707 const bool showDirectionalLightCountWarning = !tooManyDirectionalLightsWarningShown && (directionalLightsCount > maxDirectionalLights);
2708 if (showDirectionalLightCountWarning) {
2709 qWarning(
"Too many directional lights in scene, maximum is %d", maxDirectionalLights);
2710 tooManyDirectionalLightsWarningShown =
true;
2713 if (shadowMapCount > 0) {
2714 requestShadowMapManager();
2715 layerPrepResult.flags.setRequiresShadowMapPass(
true);
2720 features.set(QSSGShaderFeatures::Feature::Ssm,
true);
2721 shadowMapManager->addShadowMaps(renderableLights);
2722 }
else if (shadowMapManager) {
2724 shadowMapManager->releaseCachedResources();
2730 QSSG_ASSERT(globalLights.isEmpty(), globalLights.clear());
2731 if (hasScopedLights) {
2732 for (
const auto &shaderLight : std::as_const(renderableLights)) {
2733 if (!shaderLight.light->m_scope)
2734 globalLights.push_back(shaderLight);
2737 const auto prepareLightsWithScopedLights = [&renderableLights,
this](QVector<QSSGRenderableNodeEntry> &renderableNodes) {
2738 for (qint32 idx = 0, end = renderableNodes.size(); idx < end; ++idx) {
2739 QSSGRenderableNodeEntry &theNodeEntry(renderableNodes[idx]);
2740 QSSGShaderLightList filteredLights;
2741 for (
const auto &light : std::as_const(renderableLights)) {
2742 if (light.light->m_scope && !scopeLight(theNodeEntry.node, light.light->m_scope))
2744 filteredLights.push_back(light);
2747 if (filteredLights.isEmpty()) {
2748 theNodeEntry.lights = QSSGDataView(globalLights);
2753 auto customLightList = RENDER_FRAME_NEW_BUFFER<QSSGShaderLight>(*renderer->contextInterface(), filteredLights.size());
2754 std::copy(filteredLights.cbegin(), filteredLights.cend(), customLightList.begin());
2755 theNodeEntry.lights = customLightList;
2760 prepareLightsWithScopedLights(renderableModels);
2761 prepareLightsWithScopedLights(renderableParticles);
2763 globalLights = renderableLights;
2765 const auto prepareLights = [
this](QVector<QSSGRenderableNodeEntry> &renderableNodes) {
2766 for (qint32 idx = 0, end = renderableNodes.size(); idx < end; ++idx) {
2767 QSSGRenderableNodeEntry &theNodeEntry(renderableNodes[idx]);
2768 theNodeEntry.lights = QSSGDataView(globalLights);
2772 prepareLights(renderableModels);
2773 prepareLights(renderableParticles);
2779 Q_STATIC_ASSERT(USERPASSES == size_t(QSSGRenderLayer::RenderExtensionStage::Count));
2780 for (size_t i = 0; i != size_t(QSSGRenderLayer::RenderExtensionStage::Count); ++i) {
2781 const auto &renderExtensions = layer.renderExtensions[i];
2782 auto &userPass = userPasses[i];
2783 for (
auto rit = renderExtensions.crbegin(), rend = renderExtensions.crend(); rit != rend; ++rit) {
2784 if ((*rit)->prepareData(frameData)) {
2786 userPass.extensions.push_back(*rit);
2794 const auto &userRenderPassManager = requestUserRenderPassManager();
2795 userRenderPassManager->updateUserPassOrder(layerTreeWasDirty);
2796 userRenderPasses.userPasses = userRenderPassManager->scheduledUserPasses();
2799 auto &opaqueObjects = opaqueObjectStore[0];
2800 auto &transparentObjects = transparentObjectStore[0];
2801 auto &screenTextureObjects = screenTextureObjectStore[0];
2803 if (!renderedCameras.isEmpty()) {
2804 wasDirty |= prepareModelsForRender(*renderer->contextInterface(), renderableModels, layerPrepResult.flags, renderedCameras, getCachedCameraDatas(), modelContexts, opaqueObjects, transparentObjects, screenTextureObjects, meshLodThreshold);
2805 if (particlesEnabled) {
2806 const auto &cameraDatas = getCachedCameraDatas();
2807 wasDirty |= prepareParticlesForRender(renderableParticles, cameraDatas[0], layerPrepResult.flags);
2810 wasDirty |= (item2DsView.size() != 0);
2812 if (orderIndependentTransparencyEnabled) {
2814 if (transparentObjects.size() > 0 && !layerPrepResult.flags.hasCustomBlendMode()) {
2815 if (layer.oitMethod == QSSGRenderLayer::OITMethod::WeightedBlended) {
2816 if (rhiCtx->mainPassSampleCount() > 1)
2817 layerPrepResult.flags.setRequiresDepthTextureMS(
true);
2819 layerPrepResult.flags.setRequiresDepthTexture(
true);
2822 orderIndependentTransparencyEnabled =
false;
2823 if (!oitWarningInvalidBlendModeShown) {
2824 qCWarning(lcQuick3DRender) <<
"Order Independent Transparency requested, but disabled due to invalid blend modes.";
2825 qCWarning(lcQuick3DRender) <<
"Use SourceOver blend mode for Order Independent Transparency.";
2826 oitWarningInvalidBlendModeShown =
true;
2830 layer.oitMethodDirty =
false;
2832 prepareReflectionProbesForRender();
2834 wasDirty = wasDirty || wasDataDirty;
2835 layerPrepResult.flags.setWasDirty(wasDirty);
2836 layerPrepResult.flags.setLayerDataDirty(wasDataDirty);
2839 const bool animating = wasDirty;
2841 layer.progAAPassIndex = 0;
2843 const bool progressiveAA = layer.isProgressiveAAEnabled() && !animating;
2844 layer.progressiveAAIsActive = progressiveAA;
2845 const bool temporalAA = layer.isTemporalAAEnabled() && !progressiveAA;
2847 layer.temporalAAIsActive = temporalAA;
2849 QVector2D vertexOffsetsAA;
2851 if (progressiveAA && layer.progAAPassIndex > 0 && layer.progAAPassIndex < quint32(layer.antialiasingQuality)) {
2852 int idx = layer.progAAPassIndex - 1;
2853 vertexOffsetsAA = s_ProgressiveAAVertexOffsets[idx] / QVector2D{
float(theViewport.width()/2.0),
float(theViewport.height()/2.0) };
2857 if (layer.temporalAAMode == QSSGRenderLayer::TAAMode::MotionVector && layer.tempAAPassIndex > 0) {
2858 if (layer.tempAAPassIndex >= quint32(MAX_AA_LEVELS) + 1)
2859 layer.tempAAPassIndex = 1;
2860 int idx = layer.tempAAPassIndex - 1;
2861 vertexOffsetsAA = s_ProgressiveAAVertexOffsets[idx] / QVector2D{
float(theViewport.width()/2.0),
float(theViewport.height()/2.0) };
2862 layer.currentAndLastJitter = QVector4D(vertexOffsetsAA, layer.currentAndLastJitter.x(), layer.currentAndLastJitter.y());
2863 layerPrepResult.flags.setRequiresMotionVectorPass(
true);
2864 layerPrepResult.flags.setRequiresDepthTexture(
true);
2866 const int t = 1 - 2 * (layer.tempAAPassIndex % 2);
2867 const float f = t * layer.temporalAAStrength;
2868 vertexOffsetsAA = { f /
float(theViewport.width()/2.0), f /
float(theViewport.height()/2.0) };
2872 if (!renderedCameras.isEmpty()) {
2873 if (temporalAA || progressiveAA ) {
2874 QMatrix4x4 offsetProjection = renderedCameras[0]->projection;
2875 QMatrix4x4 invProjection = renderedCameras[0]->projection.inverted();
2876 if (renderedCameras[0]->type == QSSGRenderCamera::Type::OrthographicCamera) {
2877 offsetProjection(0, 3) -= vertexOffsetsAA.x();
2878 offsetProjection(1, 3) -= vertexOffsetsAA.y();
2879 }
else if (renderedCameras[0]->type == QSSGRenderCamera::Type::PerspectiveCamera) {
2880 offsetProjection(0, 2) += vertexOffsetsAA.x();
2881 offsetProjection(1, 2) += vertexOffsetsAA.y();
2883 for (
auto &modelContext : std::as_const(modelContexts)) {
2884 for (
int mvpIdx = 0; mvpIdx < renderedCameras.count(); ++mvpIdx)
2885 modelContext->modelViewProjections[mvpIdx] = offsetProjection * invProjection * modelContext->modelViewProjections[mvpIdx];
2890 const bool hasItem2Ds = (item2DsView.size() > 0);
2891 const bool layerEnableDepthTest = layer.layerFlags.testFlag(QSSGRenderLayer::LayerFlag::EnableDepthTest);
2892 const bool layerEnabledDepthPrePass = layer.layerFlags.testFlag(QSSGRenderLayer::LayerFlag::EnableDepthPrePass);
2893 const bool depthTestEnableDefault = layerEnableDepthTest && (!opaqueObjects.isEmpty() || depthPrepassObjectsState || hasDepthWriteObjects);
2894 const bool zPrePassForced = (depthPrepassObjectsState != 0);
2895 zPrePassActive = zPrePassForced || (layerEnabledDepthPrePass && layerEnableDepthTest && (hasDepthWriteObjects || hasItem2Ds));
2896 const bool depthWriteEnableDefault = depthTestEnableDefault && (!layerEnabledDepthPrePass || !zPrePassActive);
2898 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, depthTestEnableDefault);
2899 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled, depthWriteEnableDefault);
2903 layerPrepResult.setState(QSSGLayerRenderPreparationResult::State::Done);
2906 QSSG_ASSERT(activePasses.isEmpty(), activePasses.clear());
2910 if (layerPrepResult.flags.requiresDepthTexture())
2911 activePasses.push_back(&depthMapPass);
2912 if (layerPrepResult.flags.requiresDepthTextureMS())
2913 activePasses.push_back(&depthMapPassMS);
2915 if (layerPrepResult.flags.requiresNormalTexture())
2916 activePasses.push_back(&normalPass);
2919 if (layerPrepResult.flags.requiresSsaoPass())
2920 activePasses.push_back(&ssaoMapPass);
2923 if (layerPrepResult.flags.requiresShadowMapPass())
2924 activePasses.push_back(&shadowMapPass);
2926 if (zPrePassActive && !disableMainPasses)
2927 activePasses.push_back(&zPrePassPass);
2930 if (layerPrepResult.flags.requiresScreenTexture())
2931 activePasses.push_back(&screenMapPass);
2934 activePasses.push_back(&reflectionMapPass);
2936 auto &textureExtensionPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::TextureProviders)];
2937 if (textureExtensionPass.hasData())
2938 activePasses.push_back(&textureExtensionPass);
2940 auto &underlayPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::Underlay)];
2941 if (underlayPass.hasData())
2942 activePasses.push_back(&underlayPass);
2945 if (userRenderPasses.hasData())
2946 activePasses.push_back(&userRenderPasses);
2948 const bool hasOpaqueObjects = (opaqueObjects.size() > 0);
2950 if (hasOpaqueObjects && !disableMainPasses)
2951 activePasses.push_back(&opaquePass);
2954 if (layerPrepResult.flags.requiresMotionVectorPass())
2955 activePasses.push_back(&motionVectorMapPass);
2959 if (renderer->contextInterface()->rhiContext()->rhi()->isFeatureSupported(QRhi::TexelFetch)) {
2960 if (layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap && layer.skyBoxCubeMap && !disableMainPasses)
2961 activePasses.push_back(&skyboxCubeMapPass);
2962 else if (layer.background == QSSGRenderLayer::Background::SkyBox && layer.lightProbe && !disableMainPasses)
2963 activePasses.push_back(&skyboxPass);
2966 if (hasItem2Ds && !disableMainPasses)
2967 activePasses.push_back(&item2DPass);
2969 if (layerPrepResult.flags.requiresScreenTexture())
2970 activePasses.push_back(&reflectionPass);
2973 if ((transparentObjects.size() > 0 || (!layerEnableDepthTest && hasOpaqueObjects)) && !disableMainPasses) {
2974 if (orderIndependentTransparencyEnabled) {
2975 activePasses.push_back(&oitRenderPass);
2976 activePasses.push_back(&oitCompositePass);
2977 oitRenderPass.setMethod(layer.oitMethod);
2978 oitCompositePass.setMethod(layer.oitMethod);
2980 activePasses.push_back(&transparentPass);
2984 auto &overlayPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::Overlay)];
2985 if (overlayPass.hasData())
2986 activePasses.push_back(&overlayPass);
2988 if (layer.gridEnabled)
2989 activePasses.push_back(&infiniteGridPass);
2991 if (
const auto &dbgDrawSystem = renderer->contextInterface()->debugDrawSystem(); dbgDrawSystem && dbgDrawSystem->isEnabled() && !disableMainPasses)
2992 activePasses.push_back(&debugDrawPass);