896void QSSGLayerRenderData::prepareImageForRender(QSSGRenderImage &inImage,
897 QSSGRenderableImage::Type inMapType,
898 QSSGRenderableImage *&ioFirstImage,
899 QSSGRenderableImage *&ioNextImage,
900 QSSGRenderableObjectFlags &ioFlags,
901 QSSGShaderDefaultMaterialKey &inShaderKey,
902 quint32 inImageIndex,
903 QSSGRenderDefaultMaterial *inMaterial)
905 QSSGRenderContextInterface &contextInterface = *renderer->contextInterface();
906 const auto &bufferManager = contextInterface.bufferManager();
908 if (inImage.clearDirty())
909 ioFlags |= QSSGRenderableObjectFlag::Dirty;
920 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(&inImage);
922 if (texture.m_texture) {
923 if (texture.m_flags.hasTransparency()
924 && (inMapType == QSSGRenderableImage::Type::Diffuse
925 || inMapType == QSSGRenderableImage::Type::Opacity
926 || inMapType == QSSGRenderableImage::Type::Translucency))
928 ioFlags |= QSSGRenderableObjectFlag::HasTransparency;
931 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, inMapType, inImage, texture);
932 QSSGShaderKeyImageMap &theKeyProp = defaultMaterialShaderKeyProperties.m_imageMaps[inImageIndex];
934 theKeyProp.setEnabled(inShaderKey,
true);
935 switch (inImage.m_mappingMode) {
936 case QSSGRenderImage::MappingModes::Normal:
938 case QSSGRenderImage::MappingModes::Environment:
939 theKeyProp.setEnvMap(inShaderKey,
true);
941 case QSSGRenderImage::MappingModes::LightProbe:
942 theKeyProp.setLightProbe(inShaderKey,
true);
952 switch (texture.m_texture->format()) {
953 case QRhiTexture::Format::RED_OR_ALPHA8:
954 hasA = !renderer->contextInterface()->rhiContext()->rhi()->isFeatureSupported(QRhi::RedOrAlpha8IsRed);
956 case QRhiTexture::Format::R8:
966 if (inImage.isImageTransformIdentity())
967 theKeyProp.setIdentityTransform(inShaderKey,
true);
969 if (inImage.m_indexUV == 1)
970 theKeyProp.setUsesUV1(inShaderKey,
true);
972 if (texture.m_flags.isLinear())
973 theKeyProp.setLinear(inShaderKey,
true);
975 if (texture.m_flags.isPreMultipliedAlpha())
976 theKeyProp.setPreMultipliedAlpha(inShaderKey,
true);
978 if (ioFirstImage ==
nullptr)
979 ioFirstImage = theImage;
981 ioNextImage->m_nextImage = theImage;
983 ioNextImage = theImage;
985 if (inMaterial && inImageIndex >= QSSGShaderDefaultMaterialKeyProperties::SingleChannelImagesFirst) {
986 QSSGRenderDefaultMaterial::TextureChannelMapping value = QSSGRenderDefaultMaterial::R;
988 const quint32 scIndex = inImageIndex - QSSGShaderDefaultMaterialKeyProperties::SingleChannelImagesFirst;
989 QSSGShaderKeyTextureChannel &channelKey = defaultMaterialShaderKeyProperties.m_textureChannels[scIndex];
990 switch (inImageIndex) {
991 case QSSGShaderDefaultMaterialKeyProperties::OpacityMap:
992 value = inMaterial->opacityChannel;
994 case QSSGShaderDefaultMaterialKeyProperties::RoughnessMap:
995 value = inMaterial->roughnessChannel;
997 case QSSGShaderDefaultMaterialKeyProperties::MetalnessMap:
998 value = inMaterial->metalnessChannel;
1000 case QSSGShaderDefaultMaterialKeyProperties::OcclusionMap:
1001 value = inMaterial->occlusionChannel;
1003 case QSSGShaderDefaultMaterialKeyProperties::TranslucencyMap:
1004 value = inMaterial->translucencyChannel;
1006 case QSSGShaderDefaultMaterialKeyProperties::HeightMap:
1007 value = inMaterial->heightChannel;
1009 case QSSGShaderDefaultMaterialKeyProperties::ClearcoatMap:
1010 value = inMaterial->clearcoatChannel;
1012 case QSSGShaderDefaultMaterialKeyProperties::ClearcoatRoughnessMap:
1013 value = inMaterial->clearcoatRoughnessChannel;
1015 case QSSGShaderDefaultMaterialKeyProperties::TransmissionMap:
1016 value = inMaterial->transmissionChannel;
1018 case QSSGShaderDefaultMaterialKeyProperties::ThicknessMap:
1019 value = inMaterial->thicknessChannel;
1021 case QSSGShaderDefaultMaterialKeyProperties::BaseColorMap:
1022 value = inMaterial->baseColorChannel;
1024 case QSSGShaderDefaultMaterialKeyProperties::SpecularAmountMap:
1025 value = inMaterial->specularAmountChannel;
1027 case QSSGShaderDefaultMaterialKeyProperties::EmissiveMap:
1028 value = inMaterial->emissiveChannel;
1033 bool useDefault =
false;
1035 case QSSGRenderDefaultMaterial::TextureChannelMapping::G:
1038 case QSSGRenderDefaultMaterial::TextureChannelMapping::B:
1041 case QSSGRenderDefaultMaterial::TextureChannelMapping::A:
1048 value = QSSGRenderDefaultMaterial::R;
1049 channelKey.setTextureChannel(QSSGShaderKeyTextureChannel::TexturChannelBits(value), inShaderKey);
1079QSSGDefaultMaterialPreparationResult QSSGLayerRenderData::prepareDefaultMaterialForRender(
1080 QSSGRenderDefaultMaterial &inMaterial,
1081 QSSGRenderableObjectFlags &inExistingFlags,
1082 float inOpacity,
bool hasAnyLights,
1083 bool anyLightHasShadows,
1084 QSSGLayerRenderPreparationResultFlags &ioFlags)
1086 QSSGRenderDefaultMaterial *theMaterial = &inMaterial;
1087 QSSGDefaultMaterialPreparationResult retval(QSSGShaderDefaultMaterialKey(qHash(features)));
1088 retval.renderableFlags = inExistingFlags;
1089 QSSGRenderableObjectFlags &renderableFlags(retval.renderableFlags);
1090 QSSGShaderDefaultMaterialKey &theGeneratedKey(retval.materialKey);
1091 retval.opacity = inOpacity;
1092 float &subsetOpacity(retval.opacity);
1094 if (theMaterial->isDirty())
1095 renderableFlags |= QSSGRenderableObjectFlag::Dirty;
1097 subsetOpacity *= theMaterial->opacity;
1099 QSSGRenderableImage *firstImage =
nullptr;
1101 const bool lighting = theMaterial->lighting != QSSGRenderDefaultMaterial::MaterialLighting::NoLighting;
1102 defaultMaterialShaderKeyProperties.m_hasLighting.setValue(theGeneratedKey, lighting);
1104 defaultMaterialShaderKeyProperties.m_hasPunctualLights.setValue(theGeneratedKey, hasAnyLights);
1105 defaultMaterialShaderKeyProperties.m_hasShadows.setValue(theGeneratedKey, anyLightHasShadows);
1106 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey, layer.lightProbe !=
nullptr);
1109 defaultMaterialShaderKeyProperties.m_specularAAEnabled.setValue(theGeneratedKey, layer.specularAAEnabled);
1112 defaultMaterialShaderKeyProperties.m_isDoubleSided.setValue(theGeneratedKey, theMaterial->cullMode == QSSGCullFaceMode::Disabled);
1115 defaultMaterialShaderKeyProperties.m_overridesPosition.setValue(theGeneratedKey,
false);
1118 defaultMaterialShaderKeyProperties.m_usesProjectionMatrix.setValue(theGeneratedKey,
false);
1119 defaultMaterialShaderKeyProperties.m_usesInverseProjectionMatrix.setValue(theGeneratedKey,
false);
1121 defaultMaterialShaderKeyProperties.m_usesVarColor.setValue(theGeneratedKey,
false);
1124 defaultMaterialShaderKeyProperties.m_alphaMode.setValue(theGeneratedKey, theMaterial->alphaMode);
1127 setVertexInputPresence(renderableFlags, theGeneratedKey);
1130 defaultMaterialShaderKeyProperties.m_usesPointsTopology.setValue(theGeneratedKey, renderableFlags.isPointsTopology());
1133 defaultMaterialShaderKeyProperties.m_lightmapEnabled.setValue(theGeneratedKey, renderableFlags.rendersWithLightmap());
1134 defaultMaterialShaderKeyProperties.m_metallicRoughnessEnabled.setValue(theGeneratedKey, theMaterial->type == QSSGRenderDefaultMaterial::Type::PrincipledMaterial);
1135 defaultMaterialShaderKeyProperties.m_specularGlossyEnabled.setValue(theGeneratedKey, theMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial);
1139 defaultMaterialShaderKeyProperties.m_debugMode.setValue(theGeneratedKey,
int(layer.debugMode));
1142 defaultMaterialShaderKeyProperties.m_fogEnabled.setValue(theGeneratedKey, layer.fog.enabled);
1145 defaultMaterialShaderKeyProperties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
1146 defaultMaterialShaderKeyProperties.m_usesViewIndex.setValue(theGeneratedKey, layer.viewCount >= 2);
1148 if (!defaultMaterialShaderKeyProperties.m_hasIbl.getValue(theGeneratedKey) && theMaterial->iblProbe) {
1149 features.set(QSSGShaderFeatures::Feature::LightProbe,
true);
1150 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey,
true);
1155 if (subsetOpacity >= QSSGRendererPrivate::minimumRenderOpacity) {
1171 if (theMaterial->blendMode != QSSGRenderDefaultMaterial::MaterialBlendMode::SourceOver
1172 || theMaterial->opacityMap
1173 || theMaterial->alphaMode == QSSGRenderDefaultMaterial::Blend)
1175 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1178 const bool specularEnabled = theMaterial->isSpecularEnabled();
1179 const bool metalnessEnabled = theMaterial->isMetalnessEnabled();
1180 defaultMaterialShaderKeyProperties.m_specularEnabled.setValue(theGeneratedKey, specularEnabled || metalnessEnabled);
1181 defaultMaterialShaderKeyProperties.m_specularModel.setSpecularModel(theGeneratedKey, theMaterial->specularModel);
1182 defaultMaterialShaderKeyProperties.m_diffuseModel.setDiffuseModel(theGeneratedKey, theMaterial->diffuseModel);
1183 defaultMaterialShaderKeyProperties.m_fresnelScaleBiasEnabled.setValue(theGeneratedKey, theMaterial->isFresnelScaleBiasEnabled());
1184 defaultMaterialShaderKeyProperties.m_clearcoatFresnelScaleBiasEnabled.setValue(theGeneratedKey, theMaterial->isClearcoatFresnelScaleBiasEnabled());
1185 defaultMaterialShaderKeyProperties.m_fresnelEnabled.setValue(theGeneratedKey, theMaterial->isFresnelEnabled());
1186 defaultMaterialShaderKeyProperties.m_baseColorSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isBaseColorSingleChannelEnabled());
1187 defaultMaterialShaderKeyProperties.m_specularSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isSpecularAmountSingleChannelEnabled());
1188 defaultMaterialShaderKeyProperties.m_emissiveSingleChannelEnabled.setValue(theGeneratedKey, theMaterial->isEmissiveSingleChannelEnabled());
1189 defaultMaterialShaderKeyProperties.m_invertOpacityMapValue.setValue(theGeneratedKey, theMaterial->isInvertOpacityMapValue());
1190 defaultMaterialShaderKeyProperties.m_vertexColorsEnabled.setValue(theGeneratedKey, theMaterial->isVertexColorsEnabled());
1191 defaultMaterialShaderKeyProperties.m_vertexColorsMaskEnabled.setValue(theGeneratedKey, theMaterial->isVertexColorsMaskEnabled());
1192 defaultMaterialShaderKeyProperties.m_vertexColorRedMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorRedMask.toInt()));
1193 defaultMaterialShaderKeyProperties.m_vertexColorGreenMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorGreenMask.toInt()));
1194 defaultMaterialShaderKeyProperties.m_vertexColorBlueMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorBlueMask.toInt()));
1195 defaultMaterialShaderKeyProperties.m_vertexColorAlphaMask.setValue(theGeneratedKey, quint16(theMaterial->vertexColorAlphaMask.toInt()));
1196 defaultMaterialShaderKeyProperties.m_clearcoatEnabled.setValue(theGeneratedKey, theMaterial->isClearcoatEnabled());
1197 defaultMaterialShaderKeyProperties.m_transmissionEnabled.setValue(theGeneratedKey, theMaterial->isTransmissionEnabled());
1202 QSSGRenderableImage *nextImage =
nullptr;
1203#define CHECK_IMAGE_AND_PREPARE(img, imgtype, shadercomponent)
1205 prepareImageForRender(*(img), imgtype, firstImage, nextImage, renderableFlags,
1206 theGeneratedKey, shadercomponent, &inMaterial)
1208 if (theMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
1209 theMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial) {
1211 QSSGRenderableImage::Type::BaseColor,
1212 QSSGShaderDefaultMaterialKeyProperties::BaseColorMap);
1214 QSSGRenderableImage::Type::Occlusion,
1215 QSSGShaderDefaultMaterialKeyProperties::OcclusionMap);
1217 QSSGRenderableImage::Type::Height,
1218 QSSGShaderDefaultMaterialKeyProperties::HeightMap);
1220 QSSGRenderableImage::Type::Clearcoat,
1221 QSSGShaderDefaultMaterialKeyProperties::ClearcoatMap);
1223 QSSGRenderableImage::Type::ClearcoatRoughness,
1224 QSSGShaderDefaultMaterialKeyProperties::ClearcoatRoughnessMap);
1226 QSSGRenderableImage::Type::ClearcoatNormal,
1227 QSSGShaderDefaultMaterialKeyProperties::ClearcoatNormalMap);
1229 QSSGRenderableImage::Type::Transmission,
1230 QSSGShaderDefaultMaterialKeyProperties::TransmissionMap);
1232 QSSGRenderableImage::Type::Thickness,
1233 QSSGShaderDefaultMaterialKeyProperties::ThicknessMap);
1234 if (theMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial) {
1236 QSSGRenderableImage::Type::Metalness,
1237 QSSGShaderDefaultMaterialKeyProperties::MetalnessMap);
1241 QSSGRenderableImage::Type::Diffuse,
1242 QSSGShaderDefaultMaterialKeyProperties::DiffuseMap);
1244 CHECK_IMAGE_AND_PREPARE(theMaterial->emissiveMap, QSSGRenderableImage::Type::Emissive, QSSGShaderDefaultMaterialKeyProperties::EmissiveMap);
1246 QSSGRenderableImage::Type::Specular,
1247 QSSGShaderDefaultMaterialKeyProperties::SpecularMap);
1249 QSSGRenderableImage::Type::Roughness,
1250 QSSGShaderDefaultMaterialKeyProperties::RoughnessMap);
1251 CHECK_IMAGE_AND_PREPARE(theMaterial->opacityMap, QSSGRenderableImage::Type::Opacity, QSSGShaderDefaultMaterialKeyProperties::OpacityMap);
1252 CHECK_IMAGE_AND_PREPARE(theMaterial->bumpMap, QSSGRenderableImage::Type::Bump, QSSGShaderDefaultMaterialKeyProperties::BumpMap);
1254 QSSGRenderableImage::Type::SpecularAmountMap,
1255 QSSGShaderDefaultMaterialKeyProperties::SpecularAmountMap);
1256 CHECK_IMAGE_AND_PREPARE(theMaterial->normalMap, QSSGRenderableImage::Type::Normal, QSSGShaderDefaultMaterialKeyProperties::NormalMap);
1258 QSSGRenderableImage::Type::Translucency,
1259 QSSGShaderDefaultMaterialKeyProperties::TranslucencyMap);
1261#undef CHECK_IMAGE_AND_PREPARE
1263 if (subsetOpacity < QSSGRendererPrivate::minimumRenderOpacity) {
1264 subsetOpacity = 0.0f;
1268 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1269 renderableFlags |= QSSGRenderableObjectFlag::CompletelyTransparent;
1272 if (subsetOpacity > 1.f - QSSGRendererPrivate::minimumRenderOpacity)
1273 subsetOpacity = 1.f;
1275 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1277 if (inMaterial.isTransmissionEnabled()) {
1278 ioFlags.setRequiresScreenTexture(
true);
1279 ioFlags.setRequiresMipmapsForScreenTexture(
true);
1280 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1283 if (renderableFlags.hasTransparency()) {
1284 if (orderIndependentTransparencyEnabled)
1285 defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
1286 if (layer.oitMethodDirty)
1287 renderableFlags |= QSSGRenderableObjectFlag::Dirty;
1290 retval.firstImage = firstImage;
1291 if (retval.renderableFlags.isDirty())
1292 retval.dirty =
true;
1294 renderer->addMaterialDirtyClear(&inMaterial);
1298QSSGDefaultMaterialPreparationResult QSSGLayerRenderData::prepareCustomMaterialForRender(
1299 QSSGRenderCustomMaterial &inMaterial, QSSGRenderableObjectFlags &inExistingFlags,
1300 float inOpacity,
bool alreadyDirty,
bool hasAnyLights,
bool anyLightHasShadows,
1301 QSSGLayerRenderPreparationResultFlags &ioFlags)
1303 QSSGDefaultMaterialPreparationResult retval(QSSGShaderDefaultMaterialKey(qHash(features)));
1304 retval.renderableFlags = inExistingFlags;
1305 QSSGRenderableObjectFlags &renderableFlags(retval.renderableFlags);
1306 QSSGShaderDefaultMaterialKey &theGeneratedKey(retval.materialKey);
1307 retval.opacity = inOpacity;
1308 float &subsetOpacity(retval.opacity);
1310 if (subsetOpacity < QSSGRendererPrivate::minimumRenderOpacity) {
1311 subsetOpacity = 0.0f;
1315 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1316 renderableFlags |= QSSGRenderableObjectFlag::CompletelyTransparent;
1319 if (subsetOpacity > 1.f - QSSGRendererPrivate::minimumRenderOpacity)
1320 subsetOpacity = 1.f;
1322 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1324 defaultMaterialShaderKeyProperties.m_hasLighting.setValue(theGeneratedKey,
true);
1325 defaultMaterialShaderKeyProperties.m_hasPunctualLights.setValue(theGeneratedKey, hasAnyLights);
1326 defaultMaterialShaderKeyProperties.m_hasShadows.setValue(theGeneratedKey, anyLightHasShadows);
1327 defaultMaterialShaderKeyProperties.m_hasIbl.setValue(theGeneratedKey, layer.lightProbe !=
nullptr);
1328 defaultMaterialShaderKeyProperties.m_specularEnabled.setValue(theGeneratedKey,
true);
1330 defaultMaterialShaderKeyProperties.m_specularAAEnabled.setValue(theGeneratedKey, layer.specularAAEnabled);
1333 defaultMaterialShaderKeyProperties.m_isDoubleSided.setValue(theGeneratedKey, inMaterial.m_cullMode == QSSGCullFaceMode::Disabled);
1336 defaultMaterialShaderKeyProperties.m_metallicRoughnessEnabled.setValue(theGeneratedKey,
true);
1339 const bool overridesPosition = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::OverridesPosition);
1340 defaultMaterialShaderKeyProperties.m_overridesPosition.setValue(theGeneratedKey, overridesPosition);
1343 const bool usesProjectionMatrix = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ProjectionMatrix);
1344 defaultMaterialShaderKeyProperties.m_usesProjectionMatrix.setValue(theGeneratedKey, usesProjectionMatrix);
1345 const bool usesInvProjectionMatrix = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::InverseProjectionMatrix);
1346 defaultMaterialShaderKeyProperties.m_usesInverseProjectionMatrix.setValue(theGeneratedKey, usesInvProjectionMatrix);
1349 setVertexInputPresence(renderableFlags, theGeneratedKey);
1352 defaultMaterialShaderKeyProperties.m_usesPointsTopology.setValue(theGeneratedKey, renderableFlags.isPointsTopology());
1355 defaultMaterialShaderKeyProperties.m_lightmapEnabled.setValue(theGeneratedKey, renderableFlags.rendersWithLightmap());
1358 defaultMaterialShaderKeyProperties.m_debugMode.setValue(theGeneratedKey,
int(layer.debugMode));
1361 defaultMaterialShaderKeyProperties.m_fogEnabled.setValue(theGeneratedKey, layer.fog.enabled);
1364 defaultMaterialShaderKeyProperties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
1365 defaultMaterialShaderKeyProperties.m_usesViewIndex.setValue(theGeneratedKey,
1366 inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ViewIndex));
1371 const bool usesVarColor = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::VarColor);
1372 defaultMaterialShaderKeyProperties.m_usesVarColor.setValue(theGeneratedKey, usesVarColor);
1374 const bool usesClearcoat = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Clearcoat);
1375 defaultMaterialShaderKeyProperties.m_clearcoatEnabled.setValue(theGeneratedKey, usesClearcoat);
1377 const bool usesClearcoatFresnelScaleBias = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ClearcoatFresnelScaleBias);
1378 defaultMaterialShaderKeyProperties.m_clearcoatFresnelScaleBiasEnabled.setValue(theGeneratedKey, usesClearcoatFresnelScaleBias);
1380 const bool usesFresnelScaleBias = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::FresnelScaleBias);
1381 defaultMaterialShaderKeyProperties.m_fresnelScaleBiasEnabled.setValue(theGeneratedKey, usesFresnelScaleBias);
1383 const bool usesTransmission = inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Transmission);
1384 defaultMaterialShaderKeyProperties.m_transmissionEnabled.setValue(theGeneratedKey, usesTransmission);
1386 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Blending))
1387 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1389 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ScreenTexture)) {
1390 ioFlags.setRequiresScreenTexture(
true);
1391 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1394 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::ScreenMipTexture)) {
1395 ioFlags.setRequiresScreenTexture(
true);
1396 ioFlags.setRequiresMipmapsForScreenTexture(
true);
1397 renderableFlags |= QSSGRenderableObjectFlag::RequiresScreenTexture;
1400 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::DepthTexture))
1401 ioFlags.setRequiresDepthTexture(
true);
1403 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::NormalTexture)) {
1404 ioFlags.setRequiresNormalTexture(
true);
1405 renderableFlags |= QSSGRenderableObjectFlag::RequiresNormalTexture;
1408 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::AoTexture)) {
1409 ioFlags.setRequiresDepthTexture(
true);
1410 ioFlags.setRequiresSsaoPass(
true);
1412 if (orderIndependentTransparencyEnabled && renderableFlags.hasTransparency())
1413 defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
1415 retval.firstImage =
nullptr;
1417 if (inMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::MotionVectorTexture))
1418 ioFlags.setRequiresMotionVectorPass(
true);
1420 if (retval.dirty || alreadyDirty)
1421 renderer->addMaterialDirtyClear(&inMaterial);
1479bool QSSGLayerRenderData::prepareModelsForRender(QSSGRenderContextInterface &contextInterface,
1480 const RenderableNodeEntries &renderableModels,
1481 QSSGLayerRenderPreparationResultFlags &ioFlags,
1482 const QSSGRenderCameraList &allCameras,
1483 const QSSGRenderCameraDataList &allCameraData,
1484 TModelContextPtrList &modelContexts,
1485 QSSGRenderableObjectList &opaqueObjects,
1486 QSSGRenderableObjectList &transparentObjects,
1487 QSSGRenderableObjectList &screenTextureObjects,
1490 const auto &rhiCtx = contextInterface.rhiContext();
1491 const auto &bufferManager = contextInterface.bufferManager();
1493 const auto &debugDrawSystem = contextInterface.debugDrawSystem();
1494 const bool maybeDebugDraw = debugDrawSystem && debugDrawSystem->isEnabled();
1496 bool wasDirty =
false;
1498 for (
const QSSGRenderableNodeEntry &renderable : renderableModels) {
1499 if ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::Disabled) != 0)
1502 const QSSGRenderModel &model = *
static_cast<QSSGRenderModel *>(renderable.node);
1503 const auto &lights = renderable.lights;
1504 QSSGRenderMesh *theMesh = modelData->getMesh(model);
1508 const bool altGlobalTransform = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::GlobalTransform) != 0);
1509 const auto &globalTransform = altGlobalTransform ? renderable.extOverrides.globalTransform : getGlobalTransform(model);
1510 QMatrix3x3 normalMatrix { Qt::Uninitialized };
1511 QSSGLayerRenderData::ModelViewProjections mvps;
1512 if (altGlobalTransform) {
1513 QSSGRenderNode::calculateNormalMatrix(globalTransform, normalMatrix);
1514 size_t mvpCount = 0;
1515 for (
const auto &cameraData : allCameraData) {
1516 QSSGRenderNode::calculateMVPAndNormalMatrix(globalTransform, cameraData.viewProjection, mvps[mvpCount++], normalMatrix);
1519 if (model.usesBoneTexture()) {
1523 size_t mvpCount = 0;
1524 for (
const QSSGRenderCameraData &cameraData : allCameraData) {
1525 mvps[mvpCount++] = cameraData.viewProjection;
1526 normalMatrix = QMatrix3x3();
1529 normalMatrix = getNormalMatrix(model);
1530 mvps = getModelMvps(model);
1533 const bool altModelOpacity = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::GlobalOpacity) != 0);
1534 const float modelGlobalOpacity = altModelOpacity ? renderable.extOverrides.globalOpacity : getGlobalOpacity(model);
1535 QSSGModelContext &theModelContext = *RENDER_FRAME_NEW<QSSGModelContext>(contextInterface, model, globalTransform, normalMatrix, mvps);
1536 modelContexts.push_back(&theModelContext);
1539 const auto &meshSubsets = theMesh->subsets;
1540 const auto meshSubsetCount = meshSubsets.size();
1541 theModelContext.subsets = RENDER_FRAME_NEW_BUFFER<QSSGSubsetRenderable>(contextInterface, meshSubsetCount);
1545 auto boneTexture = bufferManager->loadSkinmap(model.skin);
1546 setBonemapTexture(theModelContext, boneTexture.m_texture);
1547 }
else if (model.skeleton) {
1548 auto boneTexture = bufferManager->loadSkinmap(&(model.skeleton->boneTexData));
1549 setBonemapTexture(theModelContext, boneTexture.m_texture);
1553 QSSGRenderableObjectFlags renderableFlagsForModel;
1555 if (meshSubsetCount > 0) {
1556 const QSSGRenderSubset &theSubset = meshSubsets.at(0);
1558 renderableFlagsForModel.setMotionVectorParticipant(model.motionVectorEnabled);
1560 renderableFlagsForModel.setCastsShadows(model.castsShadows);
1561 renderableFlagsForModel.setReceivesShadows(model.receivesShadows);
1562 renderableFlagsForModel.setReceivesReflections(model.receivesReflections);
1563 renderableFlagsForModel.setCastsReflections(model.castsReflections);
1565 renderableFlagsForModel.setUsedInBakedLighting(model.usedInBakedLighting);
1566 if (model.hasLightmap()) {
1567 QSSGRenderImageTexture lmImageTexture = bufferManager->loadLightmap(model);
1568 if (lmImageTexture.m_texture) {
1569 renderableFlagsForModel.setRendersWithLightmap(
true);
1570 setLightmapTexture(theModelContext, lmImageTexture.m_texture);
1579 bool hasJoint =
false;
1580 bool hasWeight =
false;
1581 bool hasMorphTarget = theSubset.rhi.targetsTexture !=
nullptr;
1582 for (
const QSSGRhiInputAssemblerState::InputSemantic &sem : std::as_const(theSubset.rhi.ia.inputs)) {
1583 if (sem == QSSGRhiInputAssemblerState::PositionSemantic) {
1584 renderableFlagsForModel.setHasAttributePosition(
true);
1585 }
else if (sem == QSSGRhiInputAssemblerState::NormalSemantic) {
1586 renderableFlagsForModel.setHasAttributeNormal(
true);
1587 }
else if (sem == QSSGRhiInputAssemblerState::TexCoord0Semantic) {
1588 renderableFlagsForModel.setHasAttributeTexCoord0(
true);
1589 }
else if (sem == QSSGRhiInputAssemblerState::TexCoord1Semantic) {
1590 renderableFlagsForModel.setHasAttributeTexCoord1(
true);
1591 }
else if (sem == QSSGRhiInputAssemblerState::TexCoordLightmapSemantic) {
1592 renderableFlagsForModel.setHasAttributeTexCoordLightmap(
true);
1593 }
else if (sem == QSSGRhiInputAssemblerState::TangentSemantic) {
1594 renderableFlagsForModel.setHasAttributeTangent(
true);
1595 }
else if (sem == QSSGRhiInputAssemblerState::BinormalSemantic) {
1596 renderableFlagsForModel.setHasAttributeBinormal(
true);
1597 }
else if (sem == QSSGRhiInputAssemblerState::ColorSemantic) {
1598 renderableFlagsForModel.setHasAttributeColor(
true);
1601 }
else if (sem == QSSGRhiInputAssemblerState::JointSemantic) {
1603 }
else if (sem == QSSGRhiInputAssemblerState::WeightSemantic) {
1607 renderableFlagsForModel.setHasAttributeJointAndWeight(hasJoint && hasWeight);
1608 renderableFlagsForModel.setHasAttributeMorphTarget(hasMorphTarget);
1611 QSSGRenderableObjectList bakedLightingObjects;
1612 bool usesBlendParticles = particlesEnabled && theModelContext.model.particleBuffer !=
nullptr
1613 && model.particleBuffer->particleCount();
1614 const bool anyLightHasShadows = std::find_if(lights.begin(),
1616 [](
const QSSGShaderLight &light) {
return light.shadows; })
1618 const bool hasAnyLights = !lights.isEmpty();
1619 QSSGRenderLight::SoftShadowQuality maxSoftShadowQuality = QSSGRenderLight::SoftShadowQuality::Hard;
1620 if (anyLightHasShadows) {
1622 for (
const QSSGShaderLight &light : lights) {
1623 if (light.shadows && light.light->m_softShadowQuality > maxSoftShadowQuality)
1624 maxSoftShadowQuality = light.light->m_softShadowQuality;
1630 auto &renderableSubsets = theModelContext.subsets;
1631 const bool hasMaterialOverrides = ((renderable.overridden & QSSGRenderableNodeEntry::Overridden::Materials) != 0);
1632 const auto &materials = hasMaterialOverrides ? renderable.extOverrides.materials : modelData->getMaterials(model);
1633 const auto materialCount = materials.size();
1634 QSSGRenderGraphObject *lastMaterial = !materials.isEmpty() ? materials.last() :
nullptr;
1635 int idx = 0, subsetIdx = 0;
1636 for (; idx < meshSubsetCount; ++idx) {
1638 QSSGRenderGraphObject *theMaterialObject = (idx >= materialCount) ? lastMaterial : materials[idx];
1639 if (!theMaterialObject)
1642 const QSSGRenderSubset &theSubset = meshSubsets.at(idx);
1643 QSSGRenderableObjectFlags renderableFlags = renderableFlagsForModel;
1644 float subsetOpacity = modelGlobalOpacity;
1646 renderableFlags.setPointsTopology(theSubset.rhi.ia.topology == QRhiGraphicsPipeline::Points);
1647 QSSGRenderableObject *theRenderableObject = &renderableSubsets[subsetIdx++];
1649 const bool usesInstancing = theModelContext.model.instancing()
1650 && rhiCtx->rhi()->isFeatureSupported(QRhi::Instancing);
1651 if (usesInstancing && theModelContext.model.instanceTable->hasTransparency())
1652 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1653 if (theModelContext.model.hasTransparency)
1654 renderableFlags |= QSSGRenderableObjectFlag::HasTransparency;
1657 quint32 subsetLevelOfDetail = 0;
1658 if (!theSubset.lods.isEmpty() && lodThreshold > 0.0f) {
1660 float lodDistanceMultiplier = camerasView[0]->getLevelOfDetailMultiplier();
1661 float distanceThreshold = 0.0f;
1662 const auto scale = QSSGUtils::mat44::getScale(globalTransform);
1663 float modelScale = qMax(scale.x(), qMax(scale.y(), scale.z()));
1664 QSSGBounds3 transformedBounds = theSubset.bounds;
1665 if (camerasView[0]->type != QSSGRenderGraphObject::Type::OrthographicCamera) {
1666 transformedBounds.transform(globalTransform);
1667 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1668 debugDrawSystem->drawBounds(transformedBounds, QColor(Qt::red));
1669 const QMatrix4x4 cameraGlobalTranform = getGlobalTransform(*camerasView[0]);
1670 const QVector3D cameraNormal = QSSGRenderNode::getScalingCorrectDirection(cameraGlobalTranform);
1671 const QVector3D cameraPosition = QSSGRenderNode::getGlobalPos(cameraGlobalTranform);
1672 const QSSGPlane cameraPlane = QSSGPlane(cameraPosition, cameraNormal);
1673 const QVector3D lodSupportMin = transformedBounds.getSupport(-cameraNormal);
1674 const QVector3D lodSupportMax = transformedBounds.getSupport(cameraNormal);
1675 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1676 debugDrawSystem->drawPoint(lodSupportMin, QColor(
"orange"));
1678 const float distanceMin = cameraPlane.distance(lodSupportMin);
1679 const float distanceMax = cameraPlane.distance(lodSupportMax);
1681 if (distanceMin * distanceMax < 0.0)
1682 distanceThreshold = 0.0;
1683 else if (distanceMin >= 0.0)
1684 distanceThreshold = distanceMin;
1685 else if (distanceMax <= 0.0)
1686 distanceThreshold = -distanceMax;
1690 distanceThreshold = 1.0;
1693 int currentLod = -1;
1694 if (model.levelOfDetailBias > 0.0f) {
1695 const float threshold = distanceThreshold * lodDistanceMultiplier;
1696 const float modelBias = 1 / model.levelOfDetailBias;
1697 for (qsizetype i = 0; i < theSubset.lods.count(); ++i) {
1698 float subsetDistance = theSubset.lods[i].distance * modelScale * modelBias;
1699 float screenSize = subsetDistance / threshold;
1700 if (screenSize > lodThreshold)
1705 if (currentLod == -1)
1706 subsetLevelOfDetail = 0;
1708 subsetLevelOfDetail = currentLod + 1;
1709 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLod))
1710 debugDrawSystem->drawBounds(transformedBounds, QSSGDebugDrawSystem::levelOfDetailColor(subsetLevelOfDetail));
1713 QVector3D theModelCenter(theSubset.bounds.center());
1714 theModelCenter = QSSGUtils::mat44::transform(globalTransform, theModelCenter);
1715 if (maybeDebugDraw && debugDrawSystem->isEnabled(QSSGDebugDrawSystem::Mode::MeshLodNormal)) {
1716 const QMatrix4x4 allCamera0GlobalTransform = getGlobalTransform(*allCameras[0]);
1717 debugDrawSystem->debugNormals(*bufferManager, theModelContext, theSubset, subsetLevelOfDetail, (theModelCenter - QSSGRenderNode::getGlobalPos(allCamera0GlobalTransform)).length() * 0.01);
1720 auto checkF32TypeIndex = [&rhiCtx](QRhiVertexInputAttribute::Format f) {
1721 if ((f == QRhiVertexInputAttribute::Format::Float4)
1722 || (f == QRhiVertexInputAttribute::Format::Float3)
1723 || (f == QRhiVertexInputAttribute::Format::Float2)
1724 || (f == QRhiVertexInputAttribute::Format::Float)) {
1727 if (!rhiCtx->rhi()->isFeatureSupported(QRhi::IntAttributes))
1728 qWarning() <<
"WARN: Model has non-integer type indices for skinning but current RHI backend doesn't support it!";
1732 if (theMaterialObject->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
1733 theMaterialObject->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
1734 theMaterialObject->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial) {
1735 QSSGRenderDefaultMaterial &theMaterial(
static_cast<QSSGRenderDefaultMaterial &>(*theMaterialObject));
1736 QSSGDefaultMaterialPreparationResult theMaterialPrepResult(prepareDefaultMaterialForRender(theMaterial, renderableFlags, subsetOpacity, hasAnyLights, anyLightHasShadows, ioFlags));
1737 QSSGShaderDefaultMaterialKey &theGeneratedKey(theMaterialPrepResult.materialKey);
1738 subsetOpacity = theMaterialPrepResult.opacity;
1739 QSSGRenderableImage *firstImage(theMaterialPrepResult.firstImage);
1740 wasDirty |= theMaterialPrepResult.dirty;
1741 renderableFlags = theMaterialPrepResult.renderableFlags;
1742 if (renderableFlags.hasTransparency())
1743 ioFlags.setHasCustomBlendMode(theMaterial.blendMode != QSSGRenderDefaultMaterial::MaterialBlendMode::SourceOver);
1746 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey, usesBlendParticles);
1748 if (defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.getValue(theGeneratedKey) ==
int(QSSGRenderLayer::OITMethod::LinkedList))
1749 defaultMaterialShaderKeyProperties.m_oitMSAA.setValue(theGeneratedKey, rhiCtx->mainPassSampleCount() > 1);
1752 const auto boneCount = model.skin ? model.skin->boneCount :
1753 model.skeleton ? model.skeleton->boneCount : 0;
1754 defaultMaterialShaderKeyProperties.m_boneCount.setValue(theGeneratedKey, boneCount);
1755 if (
auto idJoint = theSubset.rhi.ia.inputs.indexOf(QSSGRhiInputAssemblerState::JointSemantic); idJoint != -1) {
1756 const auto attr = theSubset.rhi.ia.inputLayout.attributeAt(idJoint);
1757 defaultMaterialShaderKeyProperties.m_usesFloatJointIndices.setValue(theGeneratedKey, checkF32TypeIndex(attr->format()));
1761 defaultMaterialShaderKeyProperties.m_shadowSoftness.setShadowSoftness(theGeneratedKey, maxSoftShadowQuality);
1764 defaultMaterialShaderKeyProperties.m_usesInstancing.setValue(theGeneratedKey, usesInstancing);
1766 defaultMaterialShaderKeyProperties.m_targetCount.setValue(theGeneratedKey,
1767 theSubset.rhi.ia.targetCount);
1768 defaultMaterialShaderKeyProperties.m_targetPositionOffset.setValue(theGeneratedKey,
1769 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::PositionSemantic]);
1770 defaultMaterialShaderKeyProperties.m_targetNormalOffset.setValue(theGeneratedKey,
1771 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::NormalSemantic]);
1772 defaultMaterialShaderKeyProperties.m_targetTangentOffset.setValue(theGeneratedKey,
1773 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TangentSemantic]);
1774 defaultMaterialShaderKeyProperties.m_targetBinormalOffset.setValue(theGeneratedKey,
1775 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::BinormalSemantic]);
1776 defaultMaterialShaderKeyProperties.m_targetTexCoord0Offset.setValue(theGeneratedKey,
1777 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord0Semantic]);
1778 defaultMaterialShaderKeyProperties.m_targetTexCoord1Offset.setValue(theGeneratedKey,
1779 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord1Semantic]);
1780 defaultMaterialShaderKeyProperties.m_targetColorOffset.setValue(theGeneratedKey,
1781 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::ColorSemantic]);
1783 new (theRenderableObject) QSSGSubsetRenderable(QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset,
1790 subsetLevelOfDetail,
1795 anyLightHasShadows);
1796 wasDirty = wasDirty || renderableFlags.isDirty();
1797 }
else if (theMaterialObject->type == QSSGRenderGraphObject::Type::CustomMaterial) {
1798 QSSGRenderCustomMaterial &theMaterial(
static_cast<QSSGRenderCustomMaterial &>(*theMaterialObject));
1800 const auto &theMaterialSystem(contextInterface.customMaterialSystem());
1801 wasDirty |= theMaterialSystem->prepareForRender(theModelContext.model, theSubset, theMaterial);
1803 if (theMaterial.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Blending))
1804 ioFlags.setHasCustomBlendMode(!hasCustomBlendMode(theMaterial));
1806 QSSGDefaultMaterialPreparationResult theMaterialPrepResult(
1807 prepareCustomMaterialForRender(theMaterial, renderableFlags, subsetOpacity, wasDirty,
1808 hasAnyLights, anyLightHasShadows, ioFlags));
1809 QSSGShaderDefaultMaterialKey &theGeneratedKey(theMaterialPrepResult.materialKey);
1810 subsetOpacity = theMaterialPrepResult.opacity;
1811 QSSGRenderableImage *firstImage(theMaterialPrepResult.firstImage);
1812 renderableFlags = theMaterialPrepResult.renderableFlags;
1814 if (model.particleBuffer && model.particleBuffer->particleCount())
1815 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey,
true);
1817 defaultMaterialShaderKeyProperties.m_blendParticles.setValue(theGeneratedKey,
false);
1819 if (defaultMaterialShaderKeyProperties.m_orderIndependentTransparency.getValue(theGeneratedKey) ==
int(QSSGRenderLayer::OITMethod::LinkedList))
1820 defaultMaterialShaderKeyProperties.m_oitMSAA.setValue(theGeneratedKey, rhiCtx->mainPassSampleCount() > 1);
1823 defaultMaterialShaderKeyProperties.m_shadowSoftness.setShadowSoftness(theGeneratedKey, maxSoftShadowQuality);
1826 const auto boneCount = model.skin ? model.skin->boneCount :
1827 model.skeleton ? model.skeleton->boneCount : 0;
1828 defaultMaterialShaderKeyProperties.m_boneCount.setValue(theGeneratedKey, boneCount);
1829 if (
auto idJoint = theSubset.rhi.ia.inputs.indexOf(QSSGRhiInputAssemblerState::JointSemantic); idJoint != -1) {
1830 const auto attr = theSubset.rhi.ia.inputLayout.attributeAt(idJoint);
1831 defaultMaterialShaderKeyProperties.m_usesFloatJointIndices.setValue(theGeneratedKey, checkF32TypeIndex(attr->format()));
1835 bool usesInstancing = theModelContext.model.instancing()
1836 && rhiCtx->rhi()->isFeatureSupported(QRhi::Instancing);
1837 defaultMaterialShaderKeyProperties.m_usesInstancing.setValue(theGeneratedKey, usesInstancing);
1839 defaultMaterialShaderKeyProperties.m_targetCount.setValue(theGeneratedKey,
1840 theSubset.rhi.ia.targetCount);
1841 defaultMaterialShaderKeyProperties.m_targetPositionOffset.setValue(theGeneratedKey,
1842 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::PositionSemantic]);
1843 defaultMaterialShaderKeyProperties.m_targetNormalOffset.setValue(theGeneratedKey,
1844 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::NormalSemantic]);
1845 defaultMaterialShaderKeyProperties.m_targetTangentOffset.setValue(theGeneratedKey,
1846 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TangentSemantic]);
1847 defaultMaterialShaderKeyProperties.m_targetBinormalOffset.setValue(theGeneratedKey,
1848 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::BinormalSemantic]);
1849 defaultMaterialShaderKeyProperties.m_targetTexCoord0Offset.setValue(theGeneratedKey,
1850 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord0Semantic]);
1851 defaultMaterialShaderKeyProperties.m_targetTexCoord1Offset.setValue(theGeneratedKey,
1852 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::TexCoord1Semantic]);
1853 defaultMaterialShaderKeyProperties.m_targetColorOffset.setValue(theGeneratedKey,
1854 theSubset.rhi.ia.targetOffsets[QSSGRhiInputAssemblerState::ColorSemantic]);
1856 if (theMaterial.m_iblProbe)
1857 theMaterial.m_iblProbe->clearDirty();
1859 new (theRenderableObject) QSSGSubsetRenderable(QSSGSubsetRenderable::Type::CustomMaterialMeshSubset,
1866 subsetLevelOfDetail,
1871 anyLightHasShadows);
1873 if (theRenderableObject)
1874 theRenderableObject->camdistSq = getCameraDistanceSq(*theRenderableObject, allCameraData[0]);
1878 if (Q_UNLIKELY(idx != subsetIdx))
1879 renderableSubsets.mSize = subsetIdx;
1881 for (
auto &ro : renderableSubsets) {
1882 const auto depthMode = ro.depthWriteMode;
1883 hasDepthWriteObjects |= (depthMode == QSSGDepthDrawMode::Always || depthMode == QSSGDepthDrawMode::OpaqueOnly);
1884 enum ObjectType : quint8 { ScreenTexture, Transparent, Opaque };
1885 static constexpr DepthPrepassObject ppState[][2] = { {DepthPrepassObject::None, DepthPrepassObject::ScreenTexture},
1886 {DepthPrepassObject::None, DepthPrepassObject::Transparent},
1887 {DepthPrepassObject::None, DepthPrepassObject::Opaque} };
1889 if (ro.renderableFlags.requiresScreenTexture()) {
1890 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::ScreenTexture][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1891 screenTextureObjects.push_back({&ro, ro.camdistSq, model.tag});
1892 }
else if (ro.renderableFlags.hasTransparency()) {
1893 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::Transparent][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1894 transparentObjects.push_back({&ro, ro.camdistSq, model.tag});
1896 depthPrepassObjectsState |= DepthPrepassObjectStateT(ppState[ObjectType::Opaque][size_t(depthMode == QSSGDepthDrawMode::OpaquePrePass)]);
1897 opaqueObjects.push_back({&ro, ro.camdistSq, model.tag});
1900 if (ro.renderableFlags.usedInBakedLighting())
1901 bakedLightingObjects.push_back({&ro, ro.camdistSq, model.tag});
1904 if (!bakedLightingObjects.isEmpty())
1905 bakedLightingModels.push_back(QSSGBakedLightingModel(&model, bakedLightingObjects));
1911bool QSSGLayerRenderData::prepareParticlesForRender(
const RenderableNodeEntries &renderableParticles,
const QSSGRenderCameraData &cameraData, QSSGLayerRenderPreparationResultFlags &ioFlags)
1913 QSSG_ASSERT(particlesEnabled,
return false);
1915 QSSGRenderContextInterface &contextInterface = *renderer->contextInterface();
1920 auto &opaqueObjects = opaqueObjectStore[0];
1921 auto &transparentObjects = transparentObjectStore[0];
1922 auto &screenTextureObjects = screenTextureObjectStore[0];
1924 for (
auto &renderable : renderableParticles) {
1925 QSSGShaderParticleMaterialKeyProperties &properties = particleMaterialShaderKeyProperties;
1927 QSSGRenderParticles &particles = *
static_cast<QSSGRenderParticles *>(renderable.node);
1928 QSSGShaderParticleMaterialKey &theGeneratedKey(particles.materialKey);
1929 const auto &lights = renderable.lights;
1931 QSSGRenderableObjectFlags renderableFlags;
1932 renderableFlags.setCastsShadows(
false);
1933 renderableFlags.setReceivesShadows(
false);
1934 renderableFlags.setHasAttributePosition(
true);
1935 renderableFlags.setHasAttributeNormal(
true);
1936 renderableFlags.setHasAttributeTexCoord0(
true);
1937 renderableFlags.setHasAttributeColor(
true);
1938 renderableFlags.setHasTransparency(particles.m_hasTransparency);
1939 renderableFlags.setCastsReflections(particles.m_castsReflections);
1940 if (particles.m_hasTransparency && particles.m_blendMode != QSSGRenderParticles::BlendMode::SourceOver)
1941 ioFlags.setHasCustomBlendMode(
true);
1943 properties.m_isLineParticle.setValue(theGeneratedKey, particles.m_featureLevel >= QSSGRenderParticles::FeatureLevel::Line);
1944 const bool animated = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimated
1945 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::Animated
1946 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::AnimatedVLight
1947 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimatedVLight;
1948 const bool mapped = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMapped
1949 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::Mapped
1950 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::MappedVLight
1951 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMappedVLight;
1952 const bool lit = particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineVLight
1953 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::AnimatedVLight
1954 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::MappedVLight
1955 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineMappedVLight
1956 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::LineAnimatedVLight
1957 || particles.m_featureLevel == QSSGRenderParticles::FeatureLevel::SimpleVLight;
1958 properties.m_isAnimated.setValue(theGeneratedKey, animated);
1959 properties.m_isMapped.setValue(theGeneratedKey, mapped);
1960 properties.m_hasLighting.setValue(theGeneratedKey, lit);
1961 properties.m_viewCount.setValue(theGeneratedKey, layer.viewCount);
1962 if (renderableFlags.hasTransparency() && orderIndependentTransparencyEnabled) {
1963 properties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(layer.oitMethod));
1964 if (layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
1965 properties.m_oitMSAA.setValue(theGeneratedKey, contextInterface.rhiContext()->mainPassSampleCount() > 1);
1967 properties.m_orderIndependentTransparency.setValue(theGeneratedKey,
int(0));
1970 float opacity = getGlobalOpacity(particles);
1971 QVector3D center(particles.m_particleBuffer.bounds().center());
1972 center = QSSGUtils::mat44::transform(getGlobalTransform(particles), center);
1974 QSSGRenderableImage *firstImage =
nullptr;
1975 if (particles.m_sprite) {
1976 const auto &bufferManager = contextInterface.bufferManager();
1978 if (particles.m_sprite->clearDirty())
1981 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(particles.m_sprite);
1982 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, QSSGRenderableImage::Type::Diffuse, *particles.m_sprite, texture);
1983 firstImage = theImage;
1984 properties.m_isSpriteLinear.setValue(theGeneratedKey, texture.m_flags.isLinear());
1987 QSSGRenderableImage *colorTable =
nullptr;
1988 if (particles.m_colorTable) {
1989 const auto &bufferManager = contextInterface.bufferManager();
1991 if (particles.m_colorTable->clearDirty())
1994 const QSSGRenderImageTexture texture = bufferManager->loadRenderImage(particles.m_colorTable);
1996 QSSGRenderableImage *theImage = RENDER_FRAME_NEW<QSSGRenderableImage>(contextInterface, QSSGRenderableImage::Type::Diffuse, *particles.m_colorTable, texture);
1997 colorTable = theImage;
1998 properties.m_isColorTableLinear.setValue(theGeneratedKey, texture.m_flags.isLinear());
2001 if (opacity > 0.0f && particles.m_particleBuffer.particleCount()) {
2002 const auto globalTransform = getGlobalTransform(particles);
2003 auto *theRenderableObject = RENDER_FRAME_NEW<QSSGParticlesRenderable>(contextInterface,
2014 if (theRenderableObject) {
2015 if (theRenderableObject->renderableFlags.requiresScreenTexture())
2016 screenTextureObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2017 else if (theRenderableObject->renderableFlags.hasTransparency())
2018 transparentObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2020 opaqueObjects.push_back({theRenderableObject, getCameraDistanceSq(*theRenderableObject, cameraData), particles.tag});
2307void QSSGLayerRenderData::prepareForRender()
2309 QSSG_ASSERT_X(layerPrepResult.isNull(),
"Prep-result was not reset for render!", layerPrepResult = {});
2311 QRect theViewport(renderer->viewport());
2315 frameData.m_ctx = renderer->contextInterface();
2320 ps.viewport = {
float(theViewport.x()),
float(theViewport.y()),
float(theViewport.width()),
float(theViewport.height()), 0.0f, 1.0f };
2321 if (layer.scissorRect.isValid()) {
2322 ps.flags |= QSSGRhiGraphicsPipelineState::Flag::UsesScissor;
2323 ps.scissor = { layer.scissorRect.x(),
2324 theViewport.height() - (layer.scissorRect.y() + layer.scissorRect.height()),
2325 layer.scissorRect.width(),
2326 layer.scissorRect.height() };
2329 ps.depthFunc = QRhiGraphicsPipeline::LessOrEqual;
2330 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::BlendEnabled,
false);
2333 ps.polygonMode = layer.wireframeMode ? QRhiGraphicsPipeline::Line : QRhiGraphicsPipeline::Fill;
2335 bool wasDirty =
false;
2336 bool wasDataDirty =
false;
2337 wasDirty = layer.isDirty();
2339 const bool shouldDisableInternalPasses = (layer.renderOverrides & size_t(QSSGRenderLayer::RenderOverrides::DisableInternalPasses)) != 0;
2340 wasDirty |= (shouldDisableInternalPasses != disableMainPasses);
2341 disableMainPasses = shouldDisableInternalPasses;
2344 layerPrepResult = { theViewport, layer };
2347 const bool SSAOEnabled = layer.ssaoEnabled();
2348 layerPrepResult.flags.setRequiresSsaoPass(SSAOEnabled);
2349 features.set(QSSGShaderFeatures::Feature::Ssao, SSAOEnabled);
2352 bool requiresDepthTexture = SSAOEnabled;
2353 bool requiresNormalTexture =
false;
2354 bool requiresMotionVectorTexture =
false;
2355 for (QSSGRenderEffect *theEffect = layer.firstEffect; theEffect; theEffect = theEffect->m_nextEffect) {
2356 if (theEffect->isDirty()) {
2358 theEffect->clearDirty();
2360 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesDepthTexture))
2361 requiresDepthTexture =
true;
2362 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesNormalTexture))
2363 requiresNormalTexture =
true;
2364 if (theEffect->testFlag(QSSGRenderEffect::Flags::UsesMotionVectorTexture))
2365 requiresMotionVectorTexture =
true;
2368 const auto &rhiCtx = renderer->contextInterface()->rhiContext();
2369 orderIndependentTransparencyEnabled = (layer.oitMethod != QSSGRenderLayer::OITMethod::None);
2370 if (layer.oitMethod == QSSGRenderLayer::OITMethod::WeightedBlended) {
2371 orderIndependentTransparencyEnabled = rhiCtx->rhi()->isFeatureSupported(QRhi::PerRenderTargetBlending);
2372 if (rhiCtx->mainPassSampleCount() > 1)
2373 orderIndependentTransparencyEnabled |= rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch) && rhiCtx->rhi()->isFeatureSupported(QRhi::SampleVariables);
2374 if (!orderIndependentTransparencyEnabled && !oitWarningUnsupportedShown) {
2375 qCWarning(lcQuick3DRender) <<
"WeightedBlended OIT is requested, but it is not supported.";
2376 oitWarningUnsupportedShown =
true;
2378 }
else if (layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList) {
2379 if (rhiCtx->mainPassSampleCount() > 1)
2380 orderIndependentTransparencyEnabled |= rhiCtx->rhi()->isFeatureSupported(QRhi::SampleVariables);
2381 if (!orderIndependentTransparencyEnabled && !oitWarningUnsupportedShown) {
2382 qCWarning(lcQuick3DRender) <<
"LinkedList OIT is requested, but it is not supported.";
2383 oitWarningUnsupportedShown =
true;
2386 if (layer.oitMethodDirty) {
2387 oitRenderContext.reset();
2388 for (
auto &renderResult : renderResults)
2389 renderResult.reset();
2392 layerPrepResult.flags.setRequiresDepthTexture(requiresDepthTexture);
2394 layerPrepResult.flags.setRequiresNormalTexture(requiresNormalTexture);
2396 layerPrepResult.flags.setRequiresMotionVectorPass(requiresMotionVectorTexture);
2400 if (!layer.firstEffect)
2401 QSSGLayerRenderData::setTonemapFeatures(features, layer.tonemapMode);
2408 if (rhiCtx->rhi()->resourceLimit(QRhi::MaxUniformBufferRange) < REDUCED_MAX_LIGHT_COUNT_THRESHOLD_BYTES) {
2409 features.set(QSSGShaderFeatures::Feature::ReduceMaxNumLights,
true);
2410 static bool notified =
false;
2413 qCDebug(lcQuick3DRender,
"Qt Quick 3D maximum number of lights has been reduced from %d to %d due to the graphics driver's limitations",
2414 QSSG_MAX_NUM_LIGHTS, QSSG_REDUCED_MAX_NUM_LIGHTS);
2419 QSSGRenderImageTexture lightProbeTexture;
2420 if (layer.lightProbe) {
2421 const auto &lightProbeSettings = layer.lightProbeSettings;
2422 if (layer.lightProbe->m_format == QSSGRenderTextureFormat::Unknown) {
2425 if (renderer->contextInterface()->rhiContext()->rhi()->isTextureFormatSupported(QRhiTexture::RGBA16F))
2426 layer.lightProbe->m_format = QSSGRenderTextureFormat::RGBA16F;
2428 layer.lightProbe->m_format = QSSGRenderTextureFormat::RGBE8;
2431 if (layer.lightProbe->clearDirty())
2432 wasDataDirty =
true;
2435 lightProbeTexture = renderer->contextInterface()->bufferManager()->loadRenderImage(layer.lightProbe, QSSGBufferManager::MipModeBsdf);
2436 if (lightProbeTexture.m_texture) {
2438 features.set(QSSGShaderFeatures::Feature::LightProbe,
true);
2439 features.set(QSSGShaderFeatures::Feature::IblOrientation, !lightProbeSettings.probeOrientation.isIdentity());
2443 if (lightProbeTexture.m_flags.isRgbe8())
2444 features.set(QSSGShaderFeatures::Feature::RGBELightProbe,
true);
2446 layer.lightProbe =
nullptr;
2449 const bool forceIblExposureValues = (features.isSet(QSSGShaderFeatures::Feature::LightProbe) && layer.tonemapMode == QSSGRenderLayer::TonemapMode::Custom);
2450 features.set(QSSGShaderFeatures::Feature::ForceIblExposure, forceIblExposureValues);
2453 frameData.m_ctx->bufferManager()->setLightmapSource(layer.lightmapSource);
2457 version = nodeData->version();
2462 auto &globalTransforms = nodeData->globalTransforms;
2463 auto &globalOpacities = nodeData->globalOpacities;
2464 auto &instanceTransforms = nodeData->instanceTransforms;
2467 QSSGRenderDataHelpers::GlobalStateResultT globalStateResult = QSSGRenderDataHelpers::GlobalStateResult::None;
2469 const bool layerTreeWasDirty = layer.isDirty(QSSGRenderLayer::DirtyFlag::TreeDirty);
2470 layer.clearDirty(QSSGRenderLayer::DirtyFlag::TreeDirty);
2471 if (layerTreeWasDirty) {
2472 wasDataDirty =
true;
2473 layerNodes = nodeData->getLayerNodeView(layer);
2475 for (
auto &node : layerNodes)
2476 globalStateResult |= QSSGRenderDataHelpers::updateGlobalNodeState(node, version);
2478 bool transformAndOpacityDirty =
false;
2479 for (
auto &node : layerNodes)
2480 transformAndOpacityDirty |= QSSGRenderDataHelpers::calcGlobalNodeData<QSSGRenderDataHelpers::Strategy::Update>(node, version, globalTransforms, globalOpacities);
2483 if (transformAndOpacityDirty) {
2484 for (
const auto &node : layerNodes)
2485 wasDataDirty |= QSSGRenderDataHelpers::calcInstanceTransforms(node, version, globalTransforms, instanceTransforms);
2488 wasDataDirty |= transformAndOpacityDirty;
2494 const bool hasExplicitCamera = (layer.explicitCameras.size() != 0);
2495 bool cameraLayerMaskDirty =
false;
2496 quint32 layerMask = QSSGRenderCamera::LayerMaskAll;
2497 if (hasExplicitCamera) {
2498 QSSGRenderCamera *explicitCamera = layer.explicitCameras[0];
2499 layerMask = explicitCamera->tag.value();
2500 cameraLayerMaskDirty = explicitCamera->isDirty(QSSGRenderCamera::DirtyFlag::LayerMaskDirty);
2501 explicitCamera->clearDirty(QSSGRenderCamera::DirtyFlag::LayerMaskDirty);
2504 const bool restatNodes = (layerTreeWasDirty || (globalStateResult & QSSGRenderDataHelpers::GlobalStateResult::ActiveChanged) || cameraLayerMaskDirty);
2507 updateFilteredLayerNodes(layerMask);
2513 QSSGRenderCamera::Configuration cameraConfig { renderer->dpr(), layer.isSsaaEnabled() ? layer.ssaaMultiplier : 1.0f };
2514 renderedCameras.clear();
2515 if (!layer.explicitCameras.isEmpty()) {
2516 for (QSSGRenderCamera *cam : std::as_const(layer.explicitCameras)) {
2518 if (cam->getGlobalState(QSSGRenderCamera::GlobalState::Active)) {
2519 const bool computeFrustumSucceeded = cam->calculateProjection(theViewport, cameraConfig);
2520 if (Q_LIKELY(computeFrustumSucceeded))
2521 renderedCameras.append(cam);
2523 qCCritical(INTERNAL_ERROR,
"Failed to calculate camera frustum");
2527 }
else if (QSSG_GUARD_X(layer.viewCount == 1,
"Multiview rendering requires explicit cameras to be set!.")) {
2532 for (
auto iter = camerasView.begin(); renderedCameras.isEmpty() && iter != camerasView.end(); iter++) {
2533 QSSGRenderCamera *theCamera = *iter;
2534 if (theCamera->getGlobalState(QSSGRenderCamera::GlobalState::Active)) {
2535 const bool computeFrustumSucceeded = theCamera->calculateProjection(theViewport, cameraConfig);
2536 if (Q_LIKELY(computeFrustumSucceeded))
2537 renderedCameras.append(theCamera);
2539 qCCritical(INTERNAL_ERROR,
"Failed to calculate camera frustum");
2547 if (renderedCameras.size() > 0 && renderedCameras[0]->layerMask != QSSGRenderCamera::LayerMaskAll) {
2548 if (!nonExplicitCameraWithLayerMaskWarningShown) {
2549 nonExplicitCameraWithLayerMaskWarningShown =
true;
2550 qWarning() <<
"Scenes with non-explicit cameras with a layer detected!"
2551 " Set the camera explicitly to avoid unecessery evaluation of scene nodes!";
2555 layerMask = renderedCameras[0]->layerMask;
2556 updateFilteredLayerNodes(layerMask);
2560 float meshLodThreshold = 1.0f;
2561 if (!renderedCameras.isEmpty())
2562 meshLodThreshold = renderedCameras[0]->levelOfDetailPixelThreshold / theViewport.width();
2564 layer.renderedCamerasMutex.lock();
2565 layer.renderedCameras = renderedCameras;
2566 layer.renderedCamerasMutex.unlock();
2569 const QSSGRenderCameraDataList &renderCameraData = getCachedCameraDatas();
2570 modelData->updateModelData(modelsView, renderer, renderCameraData);
2573 item2DData->updateItem2DData(item2DsView, renderer, renderCameraData);
2576 prepareResourceLoaders();
2579 updateDirtySkeletons(*
this, modelsView);
2582 int directionalLightsCount = 0;
2583 int positionalLightsCount = 0;
2584 const int maxLightCount = effectiveMaxLightCount(features);
2585 const int maxDirectionalLights = effectiveMaxDirectionalLightCount(features);
2586 QSSGShaderLightList renderableLights;
2587 int shadowMapCount = 0;
2588 bool hasScopedLights =
false;
2594 auto it = std::make_reverse_iterator(lightsView.end());
2595 const auto end = it + qMin(maxLightCount, lightsView.size());
2596 for (; it != end; ++it) {
2597 QSSGRenderLight *renderLight = (*it);
2598 QMatrix4x4 renderLightTransform = getGlobalTransform(*renderLight);
2599 if (renderLight->type == QSSGRenderGraphObject::Type::DirectionalLight)
2600 directionalLightsCount++;
2602 positionalLightsCount++;
2604 if (positionalLightsCount > maxLightCount)
2606 if (directionalLightsCount > maxDirectionalLights)
2610 hasScopedLights |= (renderLight->m_scope !=
nullptr);
2611 const bool castShadows = renderLight->m_castShadow && !renderLight->m_fullyBaked;
2612 shadowMapCount +=
int(castShadows);
2613 const auto &direction = renderLight->getScalingCorrectDirection(renderLightTransform);
2614 renderableLights.push_back(QSSGShaderLight{ renderLight, castShadows, direction });
2618 const bool showLightCountWarning = !tooManyLightsWarningShown && (positionalLightsCount > maxLightCount);
2619 if (showLightCountWarning) {
2620 qWarning(
"Too many lights in scene, maximum is %d", maxLightCount);
2621 tooManyLightsWarningShown =
true;
2624 const bool showDirectionalLightCountWarning = !tooManyDirectionalLightsWarningShown && (directionalLightsCount > maxDirectionalLights);
2625 if (showDirectionalLightCountWarning) {
2626 qWarning(
"Too many directional lights in scene, maximum is %d", maxDirectionalLights);
2627 tooManyDirectionalLightsWarningShown =
true;
2630 if (shadowMapCount > 0) {
2631 requestShadowMapManager();
2632 layerPrepResult.flags.setRequiresShadowMapPass(
true);
2637 features.set(QSSGShaderFeatures::Feature::Ssm,
true);
2638 shadowMapManager->addShadowMaps(renderableLights);
2639 }
else if (shadowMapManager) {
2641 shadowMapManager->releaseCachedResources();
2647 QSSG_ASSERT(globalLights.isEmpty(), globalLights.clear());
2648 if (hasScopedLights) {
2649 for (
const auto &shaderLight : std::as_const(renderableLights)) {
2650 if (!shaderLight.light->m_scope)
2651 globalLights.push_back(shaderLight);
2654 const auto prepareLightsWithScopedLights = [&renderableLights,
this](QVector<QSSGRenderableNodeEntry> &renderableNodes) {
2655 for (qint32 idx = 0, end = renderableNodes.size(); idx < end; ++idx) {
2656 QSSGRenderableNodeEntry &theNodeEntry(renderableNodes[idx]);
2657 QSSGShaderLightList filteredLights;
2658 for (
const auto &light : std::as_const(renderableLights)) {
2659 if (light.light->m_scope && !scopeLight(theNodeEntry.node, light.light->m_scope))
2661 filteredLights.push_back(light);
2664 if (filteredLights.isEmpty()) {
2665 theNodeEntry.lights = QSSGDataView(globalLights);
2670 auto customLightList = RENDER_FRAME_NEW_BUFFER<QSSGShaderLight>(*renderer->contextInterface(), filteredLights.size());
2671 std::copy(filteredLights.cbegin(), filteredLights.cend(), customLightList.begin());
2672 theNodeEntry.lights = customLightList;
2677 prepareLightsWithScopedLights(renderableModels);
2678 prepareLightsWithScopedLights(renderableParticles);
2680 globalLights = renderableLights;
2682 const auto prepareLights = [
this](QVector<QSSGRenderableNodeEntry> &renderableNodes) {
2683 for (qint32 idx = 0, end = renderableNodes.size(); idx < end; ++idx) {
2684 QSSGRenderableNodeEntry &theNodeEntry(renderableNodes[idx]);
2685 theNodeEntry.lights = QSSGDataView(globalLights);
2689 prepareLights(renderableModels);
2690 prepareLights(renderableParticles);
2696 Q_STATIC_ASSERT(USERPASSES == size_t(QSSGRenderLayer::RenderExtensionStage::Count));
2697 for (size_t i = 0; i != size_t(QSSGRenderLayer::RenderExtensionStage::Count); ++i) {
2698 const auto &renderExtensions = layer.renderExtensions[i];
2699 auto &userPass = userPasses[i];
2700 for (
auto rit = renderExtensions.crbegin(), rend = renderExtensions.crend(); rit != rend; ++rit) {
2701 if ((*rit)->prepareData(frameData)) {
2703 userPass.extensions.push_back(*rit);
2711 const auto &userRenderPassManager = requestUserRenderPassManager();
2712 userRenderPassManager->updateUserPassOrder(layerTreeWasDirty);
2713 userRenderPasses.userPasses = userRenderPassManager->scheduledUserPasses();
2716 auto &opaqueObjects = opaqueObjectStore[0];
2717 auto &transparentObjects = transparentObjectStore[0];
2718 auto &screenTextureObjects = screenTextureObjectStore[0];
2720 if (!renderedCameras.isEmpty()) {
2721 wasDirty |= prepareModelsForRender(*renderer->contextInterface(), renderableModels, layerPrepResult.flags, renderedCameras, getCachedCameraDatas(), modelContexts, opaqueObjects, transparentObjects, screenTextureObjects, meshLodThreshold);
2722 if (particlesEnabled) {
2723 const auto &cameraDatas = getCachedCameraDatas();
2724 wasDirty |= prepareParticlesForRender(renderableParticles, cameraDatas[0], layerPrepResult.flags);
2727 wasDirty |= (item2DsView.size() != 0);
2729 if (orderIndependentTransparencyEnabled) {
2731 if (transparentObjects.size() > 0 && !layerPrepResult.flags.hasCustomBlendMode()) {
2732 if (layer.oitMethod == QSSGRenderLayer::OITMethod::WeightedBlended) {
2733 if (rhiCtx->mainPassSampleCount() > 1)
2734 layerPrepResult.flags.setRequiresDepthTextureMS(
true);
2736 layerPrepResult.flags.setRequiresDepthTexture(
true);
2739 orderIndependentTransparencyEnabled =
false;
2740 if (!oitWarningInvalidBlendModeShown) {
2741 qCWarning(lcQuick3DRender) <<
"Order Independent Transparency requested, but disabled due to invalid blend modes.";
2742 qCWarning(lcQuick3DRender) <<
"Use SourceOver blend mode for Order Independent Transparency.";
2743 oitWarningInvalidBlendModeShown =
true;
2747 layer.oitMethodDirty =
false;
2749 prepareReflectionProbesForRender();
2751 wasDirty = wasDirty || wasDataDirty;
2752 layerPrepResult.flags.setWasDirty(wasDirty);
2753 layerPrepResult.flags.setLayerDataDirty(wasDataDirty);
2756 const bool animating = wasDirty;
2758 layer.progAAPassIndex = 0;
2760 const bool progressiveAA = layer.isProgressiveAAEnabled() && !animating;
2761 layer.progressiveAAIsActive = progressiveAA;
2762 const bool temporalAA = layer.isTemporalAAEnabled() && !progressiveAA;
2764 layer.temporalAAIsActive = temporalAA;
2766 QVector2D vertexOffsetsAA;
2768 if (progressiveAA && layer.progAAPassIndex > 0 && layer.progAAPassIndex < quint32(layer.antialiasingQuality)) {
2769 int idx = layer.progAAPassIndex - 1;
2770 vertexOffsetsAA = s_ProgressiveAAVertexOffsets[idx] / QVector2D{
float(theViewport.width()/2.0),
float(theViewport.height()/2.0) };
2774 if (layer.temporalAAMode == QSSGRenderLayer::TAAMode::MotionVector && layer.tempAAPassIndex > 0) {
2775 if (layer.tempAAPassIndex >= quint32(MAX_AA_LEVELS) + 1)
2776 layer.tempAAPassIndex = 1;
2777 int idx = layer.tempAAPassIndex - 1;
2778 vertexOffsetsAA = s_ProgressiveAAVertexOffsets[idx] / QVector2D{
float(theViewport.width()/2.0),
float(theViewport.height()/2.0) };
2779 layer.currentAndLastJitter = QVector4D(vertexOffsetsAA, layer.currentAndLastJitter.x(), layer.currentAndLastJitter.y());
2780 layerPrepResult.flags.setRequiresMotionVectorPass(
true);
2781 layerPrepResult.flags.setRequiresDepthTexture(
true);
2783 const int t = 1 - 2 * (layer.tempAAPassIndex % 2);
2784 const float f = t * layer.temporalAAStrength;
2785 vertexOffsetsAA = { f /
float(theViewport.width()/2.0), f /
float(theViewport.height()/2.0) };
2789 if (!renderedCameras.isEmpty()) {
2790 if (temporalAA || progressiveAA ) {
2791 QMatrix4x4 offsetProjection = renderedCameras[0]->projection;
2792 QMatrix4x4 invProjection = renderedCameras[0]->projection.inverted();
2793 if (renderedCameras[0]->type == QSSGRenderCamera::Type::OrthographicCamera) {
2794 offsetProjection(0, 3) -= vertexOffsetsAA.x();
2795 offsetProjection(1, 3) -= vertexOffsetsAA.y();
2796 }
else if (renderedCameras[0]->type == QSSGRenderCamera::Type::PerspectiveCamera) {
2797 offsetProjection(0, 2) += vertexOffsetsAA.x();
2798 offsetProjection(1, 2) += vertexOffsetsAA.y();
2800 for (
auto &modelContext : std::as_const(modelContexts)) {
2801 for (
int mvpIdx = 0; mvpIdx < renderedCameras.count(); ++mvpIdx)
2802 modelContext->modelViewProjections[mvpIdx] = offsetProjection * invProjection * modelContext->modelViewProjections[mvpIdx];
2807 const bool hasItem2Ds = (item2DsView.size() > 0);
2808 const bool layerEnableDepthTest = layer.layerFlags.testFlag(QSSGRenderLayer::LayerFlag::EnableDepthTest);
2809 const bool layerEnabledDepthPrePass = layer.layerFlags.testFlag(QSSGRenderLayer::LayerFlag::EnableDepthPrePass);
2810 const bool depthTestEnableDefault = layerEnableDepthTest && (!opaqueObjects.isEmpty() || depthPrepassObjectsState || hasDepthWriteObjects);
2811 const bool zPrePassForced = (depthPrepassObjectsState != 0);
2812 zPrePassActive = zPrePassForced || (layerEnabledDepthPrePass && layerEnableDepthTest && (hasDepthWriteObjects || hasItem2Ds));
2813 const bool depthWriteEnableDefault = depthTestEnableDefault && (!layerEnabledDepthPrePass || !zPrePassActive);
2815 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, depthTestEnableDefault);
2816 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled, depthWriteEnableDefault);
2820 layerPrepResult.setState(QSSGLayerRenderPreparationResult::State::Done);
2823 QSSG_ASSERT(activePasses.isEmpty(), activePasses.clear());
2827 if (layerPrepResult.flags.requiresDepthTexture())
2828 activePasses.push_back(&depthMapPass);
2829 if (layerPrepResult.flags.requiresDepthTextureMS())
2830 activePasses.push_back(&depthMapPassMS);
2832 if (layerPrepResult.flags.requiresNormalTexture())
2833 activePasses.push_back(&normalPass);
2836 if (layerPrepResult.flags.requiresSsaoPass())
2837 activePasses.push_back(&ssaoMapPass);
2840 if (layerPrepResult.flags.requiresShadowMapPass())
2841 activePasses.push_back(&shadowMapPass);
2843 if (zPrePassActive && !disableMainPasses)
2844 activePasses.push_back(&zPrePassPass);
2847 if (layerPrepResult.flags.requiresScreenTexture())
2848 activePasses.push_back(&screenMapPass);
2851 activePasses.push_back(&reflectionMapPass);
2853 auto &textureExtensionPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::TextureProviders)];
2854 if (textureExtensionPass.hasData())
2855 activePasses.push_back(&textureExtensionPass);
2857 auto &underlayPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::Underlay)];
2858 if (underlayPass.hasData())
2859 activePasses.push_back(&underlayPass);
2862 if (userRenderPasses.hasData())
2863 activePasses.push_back(&userRenderPasses);
2865 const bool hasOpaqueObjects = (opaqueObjects.size() > 0);
2867 if (hasOpaqueObjects && !disableMainPasses)
2868 activePasses.push_back(&opaquePass);
2871 if (layerPrepResult.flags.requiresMotionVectorPass())
2872 activePasses.push_back(&motionVectorMapPass);
2876 if (renderer->contextInterface()->rhiContext()->rhi()->isFeatureSupported(QRhi::TexelFetch)) {
2877 if (layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap && layer.skyBoxCubeMap && !disableMainPasses)
2878 activePasses.push_back(&skyboxCubeMapPass);
2879 else if (layer.background == QSSGRenderLayer::Background::SkyBox && layer.lightProbe && !disableMainPasses)
2880 activePasses.push_back(&skyboxPass);
2883 if (hasItem2Ds && !disableMainPasses)
2884 activePasses.push_back(&item2DPass);
2886 if (layerPrepResult.flags.requiresScreenTexture())
2887 activePasses.push_back(&reflectionPass);
2890 if ((transparentObjects.size() > 0 || (!layerEnableDepthTest && hasOpaqueObjects)) && !disableMainPasses) {
2891 if (orderIndependentTransparencyEnabled) {
2892 activePasses.push_back(&oitRenderPass);
2893 activePasses.push_back(&oitCompositePass);
2894 oitRenderPass.setMethod(layer.oitMethod);
2895 oitCompositePass.setMethod(layer.oitMethod);
2897 activePasses.push_back(&transparentPass);
2901 auto &overlayPass = userPasses[size_t(QSSGRenderLayer::RenderExtensionStage::Overlay)];
2902 if (overlayPass.hasData())
2903 activePasses.push_back(&overlayPass);
2905 if (layer.gridEnabled)
2906 activePasses.push_back(&infiniteGridPass);
2908 if (
const auto &dbgDrawSystem = renderer->contextInterface()->debugDrawSystem(); dbgDrawSystem && dbgDrawSystem->isEnabled() && !disableMainPasses)
2909 activePasses.push_back(&debugDrawPass);