251 const QSSGRenderCamera &inCamera,
252 const QSSGRenderLight *inLight,
253 const int shadowMapResolution,
254 const float pcfRadius,
255 const float clipNear,
257 const QSSGBounds3 &castingObjectsBox,
258 const QSSGBounds3 &receivingObjectsBox,
259 bool lockShadowmapTexels,
260 QSSGDebugDrawSystem *debugDrawSystem,
262 bool drawSceneCascadeIntersection)
264 Q_ASSERT(inLight->type == QSSGRenderLight::Type::DirectionalLight);
265 QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> result;
267 if (clipNear >= clipFar || qFuzzyCompare(clipNear, clipFar))
270 const QMatrix4x4 lightGlobalTransform = data.getGlobalTransform(*inLight);
271 const QVector3D lightDir = inLight->getDirection(lightGlobalTransform);
272 const QVector3D lightPivot = inLight->pivot;
274 const QVector3D forward = lightDir.normalized();
275 const QVector3D right = qFuzzyCompare(qAbs(forward.y()), 1.0f)
276 ? QVector3D::crossProduct(forward, QVector3D(1, 0, 0)).normalized()
277 : QVector3D::crossProduct(forward, QVector3D(0, 1, 0)).normalized();
278 const QVector3D up = QVector3D::crossProduct(right, forward).normalized();
280 QMatrix4x4 lightMatrix;
281 lightMatrix.setRow(0, QVector4D(right, 0.0f));
282 lightMatrix.setRow(1, QVector4D(up, 0.0f));
283 lightMatrix.setRow(2, QVector4D(forward, 0.0f));
284 lightMatrix.setRow(3, QVector4D(0.0f, 0.0f, 0.0f, 1.0f));
285 QMatrix4x4 lightMatrixInverted = lightMatrix.inverted();
287 const float farScale = (clipFar - clipNear) / (inCamera.clipPlanes.clipFar() - inCamera.clipPlanes.clipNear());
289 const QMatrix4x4 cameraGlobalTransform = data.getGlobalTransform(inCamera);
290 QMatrix4x4 viewProjection(Qt::Uninitialized);
291 inCamera.calculateViewProjectionMatrix(cameraGlobalTransform, viewProjection);
292 const QSSGBoxPoints frustum = computeFrustumBounds(viewProjection);
293 const QSSGBoxPoints frustumUntransformed = lockShadowmapTexels ? computeFrustumBounds(inCamera.projection) : QSSGBoxPoints();
297 const auto calcFrustumRadius = [&](
float t0,
float t1) ->
float {
298 const QSSGBoxPoints pts = sliceFrustum(frustumUntransformed, t0 * farScale, t1 * farScale);
300 QVector3D center = QVector3D(0.f, 0.f, 0.f);
301 for (QVector3D point : pts) {
304 center = center * 0.125f;
306 float radiusSquared = 0;
307 for (QVector3D point : pts) {
308 radiusSquared = qMax(radiusSquared, (point - center).lengthSquared());
311 return std::sqrt(radiusSquared);
314 const auto computeSplitRanges = [inLight](
const QVarLengthArray<
float, 3> &splits) -> QVarLengthArray<QPair<
float,
float>, 4> {
315 QVarLengthArray<QPair<
float,
float>, 4> ranges;
316 const float csmBlendRatio = inLight->m_csmBlendRatio;
318 for (qsizetype i = 0; i < splits.length(); i++) {
319 const float tI = qBound(qMin(t0 + 0.01f, 1.0f), splits[i], 1.0f);
320 ranges.emplace_back(t0, qMin(1.0f, tI + csmBlendRatio));
323 ranges.emplace_back(t0, 1.0f);
327 const auto computeFrustums = [&](
const QVarLengthArray<
float, 3> &splits) {
328 for (
const auto &range : computeSplitRanges(splits)) {
329 const float frustumRadius = lockShadowmapTexels ? calcFrustumRadius(range.first, range.second) : 0.0f;
330 auto camera = computeShadowCameraFromFrustum(lightMatrix,
338 range.first * farScale,
339 range.second * farScale,
346 drawSceneCascadeIntersection);
347 result.emplace_back(std::move(camera));
351 switch (inLight->m_csmNumSplits) {
357 computeFrustums({ inLight->m_csmSplit1 });
361 computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2 });
365 computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2, inLight->m_csmSplit3 });
407 QSSGRhiShaderPipeline *shaderPipeline,
408 QSSGRenderableImage *renderableImage,
409 QSSGRhiShaderResourceBindingList &bindings,
410 bool isCustomMaterialMeshSubset =
false)
412 static const auto imageAffectsAlpha = [](QSSGRenderableImage::Type mapType) {
413 return mapType == QSSGRenderableImage::Type::BaseColor ||
414 mapType == QSSGRenderableImage::Type::Diffuse ||
415 mapType == QSSGRenderableImage::Type::Translucency ||
416 mapType == QSSGRenderableImage::Type::Opacity;
419 while (renderableImage) {
420 const auto mapType = renderableImage->m_mapType;
421 if (imageAffectsAlpha(mapType)) {
422 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(mapType);
423 const int samplerHint =
int(mapType);
424 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
425 if (samplerBinding >= 0) {
426 QRhiTexture *texture = renderableImage->m_texture.m_texture;
427 if (samplerBinding >= 0 && texture) {
428 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
429 QRhiSampler *sampler = rhiCtx->sampler({ QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
430 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
431 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
432 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
433 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
434 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
436 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
440 renderableImage = renderableImage->m_nextImage;
443 if (isCustomMaterialMeshSubset) {
444 QVector<QShaderDescription::InOutVariable> samplerVars =
445 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
446 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
447 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
448 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
449 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
450 if (it == samplerVars.cend())
451 samplerVars.append(var);
454 int maxSamplerBinding = -1;
455 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
456 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
465 QBitArray samplerBindingsSpecified(maxSamplerBinding + 1);
467 if (maxSamplerBinding >= 0) {
469 int customTexCount = shaderPipeline->extraTextureCount();
470 for (
int i = 0; i < customTexCount; ++i) {
471 const QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
472 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
473 if (samplerBinding >= 0) {
474 samplerBindingsSpecified.setBit(samplerBinding);
475 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
476 bindings.addTexture(samplerBinding,
477 RENDERER_VISIBILITY_ALL,
485 QRhiSampler *dummySampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
486 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
487 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
488 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
489 QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
490 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
492 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars)) {
493 if (!samplerBindingsSpecified.testBit(var.binding)) {
494 QRhiTexture *t = var.type == QShaderDescription::SamplerCube ? dummyCubeTexture : dummyTexture;
495 bindings.addTexture(var.binding, RENDERER_VISIBILITY_ALL, t, dummySampler);
884 QSSGRhiGraphicsPipelineState *ps,
885 const QVector2D *depthAdjust,
886 const QSSGRenderableObjectList &sortedOpaqueObjects,
887 QSSGRenderCamera &inCamera,
889 QSSGRenderTextureCubeFace cubeFace,
890 quint32 cascadeIndex)
892 QSSGShaderFeatures featureSet;
894 featureSet.set(QSSGShaderFeatures::Feature::OrthoShadowPass,
true);
896 featureSet.set(QSSGShaderFeatures::Feature::PerspectiveShadowPass,
true);
902 featureSet.set(QSSGShaderFeatures::Feature::DisableMultiView,
true);
904 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
905 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
906 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
908 for (
const auto &handle : sortedOpaqueObjects) {
909 QSSGRenderableObject *theObject = handle.obj;
910 QSSG_ASSERT(theObject->renderableFlags.castsShadows(),
continue);
912 QSSGShaderFeatures objectFeatureSet = featureSet;
913 const bool isOpaqueDepthPrePass = theObject->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
914 if (isOpaqueDepthPrePass)
915 objectFeatureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
917 QSSGRhiDrawCallData *dcd =
nullptr;
918 QMatrix4x4 modelViewProjection;
919 QSSGSubsetRenderable &renderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
920 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
921 const bool hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(renderable.shaderDescription) > 0;
922 modelViewProjection = hasSkinning ? pEntry->m_lightViewProjection[cascadeIndex]
923 : pEntry->m_lightViewProjection[cascadeIndex] * renderable.modelContext.globalTransform;
929 const quintptr entryIdx = cascadeIndex + cubeFaceIdx + (quintptr(renderable.subset.offset) << 3);
930 dcd = &rhiCtxD->drawCallData({ passKey, &renderable.modelContext.model,
nullptr, pEntry, entryIdx });
933 QSSGRhiShaderResourceBindingList bindings;
934 QSSGRhiShaderPipelinePtr shaderPipeline;
935 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
936 if (theObject->type == QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset) {
937 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
938 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
939 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
941 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, objectFeatureSet);
944 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
945 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
947 QSSGRenderCameraList cameras({ &inCamera });
948 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras, depthAdjust, &modelViewProjection);
950 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
951 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
953 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
954 }
else if (theObject->type == QSSGSubsetRenderable::Type::CustomMaterialMeshSubset) {
955 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
956 ps->cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
958 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
959 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), objectFeatureSet);
962 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
963 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
965 QSSGRenderCameraList cameras({ &inCamera });
966 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, material, subsetRenderable,
967 cameras, depthAdjust, &modelViewProjection);
968 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
971 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
973 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
974 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
975 ia = subsetRenderable.subset.rhi.ia;
976 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
977 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
978 InstanceLevels::ModelWide, *shaderPipeline);
979 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
982 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
985 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
987 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
989 if (isOpaqueDepthPrePass) {
990 addOpaqueDepthPrePassBindings(rhiCtx,
991 shaderPipeline.get(),
992 subsetRenderable.firstImage,
994 (theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
1001 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
1002 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
1003 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
1004 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
1005 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
1006 if (screenTextureBinding >= 0) {
1007 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1008 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
1009 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1010 bindings.addTexture(screenTextureBinding,
1011 QRhiShaderResourceBinding::FragmentStage,
1012 dummyTexture, sampler);
1014 if (screenTextureArrayBinding >= 0) {
1015 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1016 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates, QSize(64, 64), Qt::black, inData.layer.viewCount);
1017 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1018 bindings.addTexture(screenTextureArrayBinding,
1019 QRhiShaderResourceBinding::FragmentStage,
1020 dummyTexture, sampler);
1025 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
1026 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
1028 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1029 QRhiSampler::Nearest,
1031 QRhiSampler::ClampToEdge,
1032 QRhiSampler::ClampToEdge,
1035 bindings.addTexture(binding,
1036 QRhiShaderResourceBinding::VertexStage,
1043 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
1044 if (targetsTexture) {
1045 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
1047 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1048 QRhiSampler::Nearest,
1050 QRhiSampler::ClampToEdge,
1051 QRhiSampler::ClampToEdge,
1052 QRhiSampler::ClampToEdge
1054 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
1058 shaderPipeline->applyAndClearStorageBindings(RENDERER_VISIBILITY_ALL, bindings);
1059 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
1061 subsetRenderable.rhiRenderData.shadowPass.pipeline = rhiCtxD->pipeline(*ps, pEntry->m_rhiRenderPassDesc[cascadeIndex], srb);
1062 subsetRenderable.rhiRenderData.shadowPass.srb[cubeFaceIdx] = srb;
1094 QSSGPassKey passKey,
1097 QRhiRenderPassDescriptor *renderPassDescriptor,
1098 QSSGRhiGraphicsPipelineState *ps,
1099 QSSGShaderFeatures featureSet,
1103 QSSGRenderCamera *alteredCamera,
1104 QMatrix4x4 *alteredModelViewProjection,
1105 QSSGRenderTextureCubeFace cubeFace,
1109 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
1111 switch (inObject.type) {
1112 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
1114 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
1116 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
1117 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
1119 if ((cubeFace != QSSGRenderTextureCubeFaceNone)) {
1121 featureSet.disableTonemapping();
1124 if (subsetRenderable.renderableFlags.rendersWithLightmap())
1125 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
1127 const auto &shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
1128 if (shaderPipeline) {
1136 QSSGRhiShaderResourceBindingList bindings;
1137 const auto &modelNode = subsetRenderable.modelContext.model;
1138 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
1149 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
1150 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone)
1151 * (cubeFaceIdx + (quintptr(subsetRenderable.subset.offset) << 3));
1153 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
1154 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode, &subsetRenderable.material, entry,
1157 shaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
1158 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
1159 if (alteredCamera) {
1160 Q_ASSERT(alteredModelViewProjection);
1161 QSSGRenderCameraList cameras({ alteredCamera });
1162 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras,
nullptr, alteredModelViewProjection);
1164 Q_ASSERT(!alteredModelViewProjection);
1165 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
1169 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
1170 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
1173 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
1176 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
1177 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
1179 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1180 QRhiSampler::Nearest,
1182 QRhiSampler::ClampToEdge,
1183 QRhiSampler::ClampToEdge,
1186 bindings.addTexture(binding,
1187 QRhiShaderResourceBinding::VertexStage,
1193 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
1194 if (targetsTexture) {
1195 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
1197 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1198 QRhiSampler::Nearest,
1200 QRhiSampler::ClampToEdge,
1201 QRhiSampler::ClampToEdge,
1202 QRhiSampler::ClampToEdge
1204 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
1208 ps->samples = samples;
1209 ps->viewCount = viewCount;
1211 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
1212 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
1213 if (!oit || (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::None))
1214 fillTargetBlend(&ps->targetBlend[0], material.blendMode);
1216 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
1218 ia = subsetRenderable.subset.rhi.ia;
1219 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
1220 QVector3D cameraDirection = cameraDatas[0].direction;
1221 QVector3D cameraPosition = cameraDatas[0].position;
1222 if (alteredCamera) {
1223 const QMatrix4x4 camGlobalTranform = inData.getGlobalTransform(*alteredCamera);
1224 cameraDirection = QSSGRenderNode::getScalingCorrectDirection(camGlobalTranform);
1225 cameraPosition = QSSGRenderNode::getGlobalPos(camGlobalTranform);
1230 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, inData, cameraDirection, cameraPosition,
1231 alteredCamera ? InstanceLevels::ModelWide : InstanceLevels::PerInstance, *shaderPipeline);
1232 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
1234 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf, 0, shaderPipeline->ub0Size());
1236 if (shaderPipeline->isLightingEnabled()) {
1237 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1238 shaderPipeline->ub0LightDataOffset(),
1239 sizeof(QSSGShaderLightsUniformData));
1240 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1241 shaderPipeline->ub0DirectionalLightDataOffset(),
1242 sizeof(QSSGShaderDirectionalLightsUniformData));
1247 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
1248 while (renderableImage) {
1249 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
1250 const int samplerHint =
int(renderableImage->m_mapType);
1251 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
1252 if (samplerBinding >= 0) {
1253 QRhiTexture *texture = renderableImage->m_texture.m_texture;
1254 if (samplerBinding >= 0 && texture) {
1255 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
1256 QSSGRhiSamplerDescription samplerDesc = {
1257 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
1258 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
1259 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
1260 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
1261 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
1262 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
1264 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
1265 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
1266 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
1269 renderableImage = renderableImage->m_nextImage;
1272 if (shaderPipeline->isLightingEnabled()) {
1274 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
1275 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
1278 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1279 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
1280 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1283 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1284 QRhiSampler::Linear,
1286 QRhiSampler::ClampToEdge,
1287 QRhiSampler::ClampToEdge,
1288 QRhiSampler::Repeat });
1290 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1294 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
1295 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
1296 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
1298 QRhiTexture *texture = shadowMapBlueNoise;
1299 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1300 QRhiSampler::Linear,
1302 QRhiSampler::Repeat,
1303 QRhiSampler::Repeat,
1304 QRhiSampler::Repeat });
1305 Q_ASSERT(texture && sampler);
1306 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1309 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1310 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
1311 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1312 QRhiSampler::Linear,
1314 QRhiSampler::Repeat,
1315 QRhiSampler::Repeat,
1316 QRhiSampler::Repeat });
1317 Q_ASSERT(texture && sampler);
1318 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1319 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1326 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
1327 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
1328 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
1329 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
1330 ? QRhiSampler::Linear
1331 : QRhiSampler::None;
1332 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1333 QRhiSampler::Linear,
1335 QRhiSampler::ClampToEdge,
1336 QRhiSampler::ClampToEdge,
1337 QRhiSampler::Repeat });
1338 if (reflectionSampler >= 0) {
1341 if (!reflectionTexture) {
1342 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1343 reflectionTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
1344 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1346 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
1350 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
1352 auto texture = shaderPipeline->lightProbeTexture();
1354 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1355 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
1356 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1360 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
1361 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1362 QRhiSampler::Linear,
1363 QRhiSampler::Linear,
1364 QSSGRhiHelpers::toRhi(tiling.first),
1365 QSSGRhiHelpers::toRhi(tiling.second),
1366 QRhiSampler::Repeat });
1367 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1371 if (shaderPipeline->screenTexture()) {
1372 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
1373 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
1374 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
1375 QRhiTexture *screenTexture = shaderPipeline->screenTexture();
1377 if (screenMapPass) {
1378 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1380 const QRhiTexture::Flags flags = screenTexture->flags();
1382 screenTexture = rhiCtx->dummyTexture(flags, resourceUpdates, QSize(64, 64), Qt::black, screenTexture->arraySize());
1383 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1390 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
1391 ? QRhiSampler::Linear : QRhiSampler::None;
1392 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
1393 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
1394 if (screenTextureBinding >= 0) {
1395 bindings.addTexture(screenTextureBinding,
1396 QRhiShaderResourceBinding::FragmentStage,
1397 screenTexture, sampler);
1399 if (screenTextureArrayBinding >= 0) {
1400 bindings.addTexture(screenTextureArrayBinding,
1401 QRhiShaderResourceBinding::FragmentStage,
1402 screenTexture, sampler);
1407 if (shaderPipeline->lightmapTexture()) {
1408 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
1410 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1411 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
1412 bindings.addTexture(binding,
1413 QRhiShaderResourceBinding::FragmentStage,
1414 shaderPipeline->lightmapTexture(), sampler);
1420 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1423 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1425 if (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
1435 QRhiShaderResourceBindings *&srb = dcd.srb;
1436 bool srbChanged =
false;
1437 if (!srb || bindings != dcd.bindings) {
1438 srb = rhiCtxD->srb(bindings);
1439 rhiCtxD->releaseCachedSrb(dcd.bindings);
1440 dcd.bindings = bindings;
1444 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1445 subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx] = srb;
1447 subsetRenderable.rhiRenderData.mainPass.srb = srb;
1449 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
1452 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
1453 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
1456 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1457 subsetRenderable.rhiRenderData.reflectionPass.pipeline = dcd.pipeline;
1459 subsetRenderable.rhiRenderData.mainPass.pipeline = dcd.pipeline;
1461 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1462 subsetRenderable.rhiRenderData.reflectionPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1463 renderPassDescriptor,
1465 dcd.pipeline = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1467 subsetRenderable.rhiRenderData.mainPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1468 renderPassDescriptor,
1470 dcd.pipeline = subsetRenderable.rhiRenderData.mainPass.pipeline;
1472 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
1473 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;
1479 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1481 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
1482 const QSSGRenderCustomMaterial &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
1483 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1485 featureSet.set(QSSGShaderFeatures::Feature::LightProbe, inData.layer.lightProbe || material.m_iblProbe);
1487 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
1488 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
1490 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1492 featureSet.disableTonemapping();
1495 if (subsetRenderable.renderableFlags.rendersWithLightmap())
1496 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
1498 customMaterialSystem.rhiPrepareRenderable(ps, passKey, subsetRenderable, featureSet,
1499 material, inData, renderPassDescriptor, samples, viewCount, screenMapPass,
1500 alteredCamera, cubeFace, alteredModelViewProjection, entry, oit);
1503 case QSSGRenderableObject::Type::Particles:
1506 const auto &shaderPipeline = shadersForParticleMaterial(ps, particleRenderable, featureSet);
1507 if (shaderPipeline) {
1508 QSSGParticleRenderer::rhiPrepareRenderable(*shaderPipeline, passKey, rhiCtx, ps, particleRenderable, inData, renderPassDescriptor, samples, viewCount,
1509 alteredCamera, cubeFace, entry, oit);
1573 const QSSGRhiGraphicsPipelineState &state,
1575 bool *needsSetViewport,
1576 QSSGRenderTextureCubeFace cubeFace,
1577 qsizetype userPassIndex)
1579 switch (object.type) {
1580 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
1582 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1584 QSSG_ASSERT(QSSGUserRenderPassManager::maxUserPassSlots() == std::size(subsetRenderable.rhiRenderData.userPassData),
return);
1586 QRhiGraphicsPipeline *ps = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1587 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].pipeline : subsetRenderable.rhiRenderData.mainPass.pipeline;
1588 QRhiShaderResourceBindings *srb = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1589 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].srb : subsetRenderable.rhiRenderData.mainPass.srb;
1591 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1592 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
1593 ps = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1594 srb = subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx];
1600 QRhiBuffer *indexBuffer = subsetRenderable.subset.rhi.indexBuffer ? subsetRenderable.subset.rhi.indexBuffer->buffer() :
nullptr;
1602 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1604 cb->setGraphicsPipeline(ps);
1605 cb->setShaderResources(srb);
1607 if (*needsSetViewport) {
1608 cb->setViewport(state.viewport);
1609 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesScissor))
1610 cb->setScissor(state.scissor);
1611 *needsSetViewport =
false;
1614 QRhiCommandBuffer::VertexInput vertexBuffers[3];
1615 int vertexBufferCount = 0;
1616 quint32 instances = 1;
1620 const bool autoLod = subsetRenderable.usesAutoLod && indexBuffer && cubeFace == QSSGRenderTextureCubeFaceNone;
1621 if (autoLod && !RenderHelpers::anyAutoLodInstances(subsetRenderable))
1624 if (!setupDrawInputs(rhiCtx, ps, subsetRenderable,
1625 autoLod ? subsetRenderable.autoLodInstanceIndexBuffer : subsetRenderable.instanceIndexBuffer,
1626 vertexBuffers, vertexBufferCount, instances))
1628 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1629 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesStencilRef))
1630 cb->setStencilRef(state.stencilRef);
1633 }
else if (indexBuffer) {
1634 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, subsetRenderable.subset.rhi.indexBuffer->indexFormat());
1635 cb->drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances, subsetRenderable.subset.lodOffset(subsetRenderable.subsetLevelOfDetail));
1636 QSSGRHICTX_STAT(rhiCtx, drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances));
1638 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1639 rhiDrawNonIndexed(rhiCtx, cb, subsetRenderable, instances);
1641 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (subsetRenderable.subset.count | quint64(instances) << 32),
1642 QVector<
int>({subsetRenderable.modelContext.model.profilingId,
1643 subsetRenderable.material.profilingId}));
1646 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1648 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1649 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1650 customMaterialSystem.rhiRenderRenderable(rhiCtx, subsetRenderable, needsSetViewport, cubeFace, state, userPassIndex);
1653 case QSSGRenderableObject::Type::Particles:
1656 QSSGParticleRenderer::rhiRenderRenderable(rhiCtx, renderable, needsSetViewport, cubeFace, state);
1681 QSSGPassKey passKey,
1682 QSSGRhiGraphicsPipelineState &ps,
1683 QSSGRenderShadowMap &shadowMapManager,
1684 const QSSGRenderCamera &camera,
1685 QSSGRenderCamera *debugCamera,
1687 const QSSGRenderableObjectList &sortedOpaqueObjects,
1689 const QSSGBounds3 &castingObjectsBox,
1690 const QSSGBounds3 &receivingObjectsBox)
1693 QSSGDebugDrawSystem *debugDrawSystem = renderer.contextInterface()->debugDrawSystem().get();
1694 const bool drawDirectionalLightShadowBoxes = layerData.layer.drawDirectionalLightShadowBoxes;
1695 const bool drawPointLightShadowBoxes = layerData.layer.drawPointLightShadowBoxes;
1696 const bool drawShadowCastingBounds = layerData.layer.drawShadowCastingBounds;
1697 const bool drawShadowReceivingBounds = layerData.layer.drawShadowReceivingBounds;
1698 const bool drawCascades = layerData.layer.drawCascades;
1699 const bool drawSceneCascadeIntersection = layerData.layer.drawSceneCascadeIntersection;
1700 const bool disableShadowCameraUpdate = layerData.layer.disableShadowCameraUpdate;
1701 const bool drawCulledObjects = layerData.layer.drawCulledObjects;
1702 QVector<
bool> debugIsObjectCulled = drawCulledObjects ? QVector<
bool>(sortedOpaqueObjects.size(),
true) : QVector<
bool>();
1704 static const auto rhiRenderOneShadowMap = [](QSSGRhiContext *rhiCtx,
1705 QSSGRhiGraphicsPipelineState *ps,
1706 const QSSGRenderableObjectList &sortedOpaqueObjects,
1708 const QSSGBounds3 cameraBounds,
1709 QVector<
bool> &debugIsObjectCulled,
1710 bool drawCulledObjects) {
1711 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1712 bool needsSetViewport =
true;
1714 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
1719 if (theObject->globalBoundsInstancing.isFinite() && theObject->globalBounds.isFinite()) {
1720 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
1721 : theObject->globalBounds;
1722 if (!globalBounds.isEmpty() && !cameraBounds.intersects(globalBounds)) {
1727 if (Q_UNLIKELY(drawCulledObjects))
1728 debugIsObjectCulled[i] =
false;
1731 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
1732 QSSGSubsetRenderable *renderable(
static_cast<QSSGSubsetRenderable *>(theObject));
1734 QRhiBuffer *indexBuffer = renderable->subset.rhi.indexBuffer
1735 ? renderable->subset.rhi.indexBuffer->buffer()
1739 if (!renderable->rhiRenderData.shadowPass.pipeline)
1744 QRhiCommandBuffer::VertexInput vertexBuffers[3];
1745 int vertexBufferCount = 0;
1746 quint32 instances = 1;
1747 if (!setupDrawInputs(rhiCtx, renderable->rhiRenderData.shadowPass.pipeline, *renderable, renderable->instanceIndexBuffer,
1748 vertexBuffers, vertexBufferCount, instances))
1751 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1753 cb->setGraphicsPipeline(renderable->rhiRenderData.shadowPass.pipeline);
1755 QRhiShaderResourceBindings *srb = renderable->rhiRenderData.shadowPass.srb[cubeFace];
1756 cb->setShaderResources(srb);
1758 if (needsSetViewport) {
1759 cb->setViewport(ps->viewport);
1760 needsSetViewport =
false;
1764 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, renderable->subset.rhi.indexBuffer->indexFormat());
1765 cb->drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances, renderable->subset.lodOffset(renderable->subsetLevelOfDetail));
1766 QSSGRHICTX_STAT(rhiCtx, drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances));
1768 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1769 rhiDrawNonIndexed(rhiCtx, cb, *renderable, instances);
1771 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (renderable->subset.count | quint64(instances) << 32),
1772 QVector<
int>({renderable->modelContext.model.profilingId,
1773 renderable->material.profilingId}));
1778 static const auto rhiClearShadowMap = [](
QSSGRenderer &renderer, QSSGRenderShadowMap &shadowMapManager, QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiRenderPassDescriptor *renderPassDesc) {
1779 auto clearShadowMapShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiClearShadowMapShader();
1780 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, clearShadowMapShaderPipeline.get());
1783 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled,
false);
1784 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled,
false);
1785 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, shadowMapManager.shadowClearSrb(), renderPassDesc, {});
1787 ps->flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
1790 QRhi *rhi = rhiCtx->rhi();
1791 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1795 QVector2D depthAdjust;
1796 if (rhi->isClipDepthZeroToOne()) {
1798 depthAdjust[0] = 0.0f;
1799 depthAdjust[1] = 1.0f;
1802 depthAdjust[0] = 1.0f;
1803 depthAdjust[1] = 0.5f;
1806 if (drawShadowCastingBounds)
1808 if (drawShadowReceivingBounds)
1812 const QSize atlasTextureSize = shadowMapManager.shadowMapAtlasTexture()->pixelSize();
1814 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
1815 for (
int i = 0, ie = globalLights.size(); i != ie; ++i) {
1816 if (!globalLights[i].shadows || globalLights[i].light->m_fullyBaked)
1823 const auto &light = globalLights[i].light;
1825 if (!shadowMapManager.shadowMapAtlasTexture())
1828 if (light->type == QSSGRenderLight::Type::DirectionalLight || light->type == QSSGRenderLight::Type::SpotLight) {
1829 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1833 if (!disableShadowCameraUpdate && debugCamera) {
1834 debugCamera->clipPlanes = camera.clipPlanes;
1835 debugCamera->projection = camera.projection;
1839 debugCamera->localTransform = layerData.getGlobalTransform(camera);
1842 QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> cascades;
1843 if (light->type == QSSGRenderLight::Type::DirectionalLight) {
1844 const float pcfRadius = light->m_softShadowQuality == QSSGRenderLight::SoftShadowQuality::Hard ? 0.f : light->m_pcfFactor;
1845 const float clipNear = camera.clipPlanes.clipNear();
1846 const float clipFar = qMin(light->m_shadowMapFar, camera.clipPlanes.clipFar());
1847 const float clipRange = clipFar - clipNear;
1848 cascades = setupCascadingCamerasForShadowMap(layerData,
1849 disableShadowCameraUpdate ? *debugCamera : camera,
1856 receivingObjectsBox,
1857 light->m_lockShadowmapTexels,
1860 drawSceneCascadeIntersection);
1863 pEntry->m_csmSplits[0] = clipNear + clipRange * (light->m_csmNumSplits > 0 ? light->m_csmSplit1 : 1.0f);
1864 pEntry->m_csmSplits[1] = clipNear + clipRange * (light->m_csmNumSplits > 1 ? light->m_csmSplit2 : 1.0f);
1865 pEntry->m_csmSplits[2] = clipNear + clipRange * (light->m_csmNumSplits > 2 ? light->m_csmSplit3 : 1.0f);
1866 pEntry->m_csmSplits[3] = clipNear + clipRange * 1.0f;
1867 pEntry->m_shadowMapFar = clipFar;
1868 }
else if (light->type == QSSGRenderLight::Type::SpotLight) {
1869 auto spotlightCamera =
std::make_unique<QSSGRenderCamera>(QSSGRenderCamera::Type::PerspectiveCamera);
1870 spotlightCamera->fov = QSSGRenderCamera::FieldOfView::fromDegrees(light->m_coneAngle * 2.0f);
1871 spotlightCamera->clipPlanes = { 1.0f, light->m_shadowMapFar };
1872 const QMatrix4x4 lightGlobalTransform = layerData.getGlobalTransform(*light);
1873 const QVector3D lightDir = QSSGRenderNode::getDirection(lightGlobalTransform);
1874 const QVector3D lightPos = QSSGRenderNode::getGlobalPos(lightGlobalTransform) - lightDir * spotlightCamera->clipPlanes.clipNear();
1875 const QVector3D lightPivot = light->pivot;
1876 const QVector3D forward = lightDir.normalized();
1877 const QVector3D right = qFuzzyCompare(qAbs(forward.y()), 1.0f)
1878 ? QVector3D::crossProduct(forward, QVector3D(1, 0, 0)).normalized()
1879 : QVector3D::crossProduct(forward, QVector3D(0, 1, 0)).normalized();
1880 const QVector3D up = QVector3D::crossProduct(right, forward).normalized();
1881 spotlightCamera->localTransform = QSSGRenderNode::calculateTransformMatrix(lightPos,
1882 QSSGRenderNode::initScale,
1884 QQuaternion::fromDirection(forward, up));
1885 QRectF theViewport(0.0f, 0.0f, (
float)light->m_shadowMapRes, (
float)light->m_shadowMapRes);
1886 QSSGRenderCamera::calculateProjectionInternal(*spotlightCamera, theViewport);
1887 cascades.push_back(
std::move(spotlightCamera));
1888 pEntry->m_shadowMapFar = light->m_shadowMapFar;
1893 memset(pEntry->m_csmActive, 0,
sizeof(pEntry->m_csmActive));
1895 QMatrix4x4 cascadeCameraGlobalTransforms(Qt::Uninitialized);
1896 const QMatrix4x4 bias = { 0.5, 0.0, 0.0, 0.5,
1899 0.0, 0.0, 0.0, 1.0 };
1901 for (
int cascadeIndex = 0; cascadeIndex < cascades.length(); cascadeIndex++) {
1902 const auto &cascadeCamera = cascades[cascadeIndex];
1906 cascadeCameraGlobalTransforms = layerData.getGlobalTransform(*cascadeCamera);
1907 pEntry->m_csmActive[cascadeIndex] = 1.f;
1908 QMatrix4x4 &viewProjection = pEntry->m_lightViewProjection[cascadeIndex];
1909 cascadeCamera->calculateViewProjectionMatrix(cascadeCameraGlobalTransforms, viewProjection);
1910 pEntry->m_lightViewProjection[cascadeIndex] = viewProjection;
1911 pEntry->m_fixedScaleBiasMatrix[cascadeIndex] = bias * viewProjection;
1912 const QMatrix4x4 inverted = viewProjection.inverted();
1913 const float x = 0.5f / (inverted * QVector4D(1, 0, 0, 0)).length();
1914 const float y = 0.5f / (inverted * QVector4D(0, 1, 0, 0)).length();
1915 const float z = 0.5f / (inverted * QVector4D(0, 0, 1, 0)).length();
1916 const QSSGBoxPoints frustumPoints = computeFrustumBounds(viewProjection);
1917 const QSSGBounds3 bounds = QSSGBounds3(frustumPoints);
1918 pEntry->m_dimensionsInverted[cascadeIndex] = QVector4D(x, y, z, 0.0f);
1919 pEntry->m_lightView = cascadeCameraGlobalTransforms.inverted();
1920 const bool isOrtho = cascadeCamera->type == QSSGRenderGraphObject::Type::OrthographicCamera;
1921 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[cascadeIndex], rhi->isYUpInFramebuffer());
1922 rhiPrepareResourcesForShadowMap(rhiCtx, layerData, passKey, pEntry, &ps, &depthAdjust, sortedOpaqueObjects, *cascadeCamera, isOrtho, QSSGRenderTextureCubeFaceNone, cascadeIndex);
1925 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[cascadeIndex];
1926 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1927 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1928 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1929 rhiClearShadowMap(renderer, shadowMapManager, rhiCtx, &ps, rt->renderPassDescriptor());
1930 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, 0, bounds, debugIsObjectCulled, drawCulledObjects);
1932 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1934 if (drawDirectionalLightShadowBoxes) {
1938 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"shadow_map"));
1940 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1941 ps.viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
1943 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1944 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1945 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1946 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1947 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1948 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
1949 const float shadowMapFar = qMax<
float>(2.0f, light->m_shadowMapFar);
1950 setupCubeShadowCameras(layerData, light, shadowMapFar, theCameras);
1951 pEntry->m_lightView = QMatrix4x4();
1952 pEntry->m_shadowMapFar = shadowMapFar;
1954 const bool swapYFaces = !rhi->isYUpInFramebuffer();
1955 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
1956 for (
const auto face : QSSGRenderTextureCubeFaces) {
1957 cameraGlobalTransform = layerData.getGlobalTransform(theCameras[quint8(face)]);
1958 theCameras[quint8(face)].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_lightViewProjection[0]);
1959 pEntry->m_lightCubeView[quint8(face)] = cameraGlobalTransform.inverted();
1961 rhiPrepareResourcesForShadowMap(rhiCtx,
1967 sortedOpaqueObjects,
1968 theCameras[quint8(face)],
1975 const QVector3D center = QSSGRenderNode::getGlobalPos(layerData.getGlobalTransform(*light));
1976 const QSSGBounds3 bounds = QSSGBounds3(center - QVector3D(shadowMapFar, shadowMapFar, shadowMapFar),
1977 center + QVector3D(shadowMapFar, shadowMapFar, shadowMapFar));
1979 for (
const auto face : QSSGRenderTextureCubeFaces) {
1983 QSSGRenderTextureCubeFace outFace = face;
2001 if (outFace == QSSGRenderTextureCubeFace::PosY)
2002 outFace = QSSGRenderTextureCubeFace::NegY;
2003 else if (outFace == QSSGRenderTextureCubeFace::NegY)
2004 outFace = QSSGRenderTextureCubeFace::PosY;
2006 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargetCube[quint8(outFace)];
2007 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
2008 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
2009 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
2010 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, quint8(face), bounds, debugIsObjectCulled, drawCulledObjects);
2012 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2013 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_cube", 0, outFace));
2020 QRhiTextureRenderTarget *rtFront = pEntry->m_rhiRenderTargets[0];
2021 QRhiRenderPassDescriptor *frontDesc = pEntry->m_rhiRenderPassDesc[0];
2022 auto atlasShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiCubeMapToAtlasShader();
2023 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[0], rhi->isYUpInFramebuffer());
2024 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, atlasShaderPipeline.get());
2025 QRhiShaderResourceBindings *srb = pEntry->m_cubeToAtlasFrontSrb;
2026 cb->beginPass(rtFront, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
2027 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtFront));
2028 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
2030 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, frontDesc, QSSGRhiQuadRenderer::UvCoords);
2033 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2034 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 6, 0));
2037 QRhiTextureRenderTarget *rtBack = pEntry->m_rhiRenderTargets[1];
2038 QRhiRenderPassDescriptor *backDesc = pEntry->m_rhiRenderPassDesc[1];
2039 srb = pEntry->m_cubeToAtlasBackSrb;
2040 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[1], rhi->isYUpInFramebuffer());
2041 cb->beginPass(rtBack, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
2042 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtBack));
2043 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
2045 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, backDesc, QSSGRhiQuadRenderer::UvCoords);
2048 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2049 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 7, 0));
2053 ps = layerData.getPipelineState();
2054 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
2055 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::UsesScissor,
false);
2057 ps.slopeScaledDepthBias = 1.5f;
2059 if (drawPointLightShadowBoxes) {
2065 if (Q_UNLIKELY(drawCulledObjects)) {
2066 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
2068 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
2069 : theObject->globalBounds;
2070 const QColor color = debugIsObjectCulled[i] ? QColorConstants::Red : QColorConstants::Green;
2077 QSSGPassKey passKey,
2079 QSSGRhiGraphicsPipelineState *ps,
2080 QSSGRenderReflectionMap &reflectionMapManager,
2081 const std::vector<QSSGRenderReflectionProbe *> &reflectionProbes,
2082 const QSSGRenderableObjectList &reflectionPassObjects,
2085 QSSGRhiContext *rhiCtx = context.rhiContext().get();
2086 QRhi *rhi = rhiCtx->rhi();
2087 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
2089 const bool renderSkybox = (inData.layer.background == QSSGRenderLayer::Background::SkyBox
2090 || inData.layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap
2091 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial)
2092 && rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch);
2094 for (size_t i = 0, ie = reflectionProbes.size(); i != ie; ++i) {
2095 QSSGReflectionMapEntry *pEntry = reflectionMapManager.reflectionMapEntry(
int(i));
2099 if (!pEntry->m_needsRender)
2102 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame && pEntry->m_rendered)
2105 if (reflectionProbes[i]->texture)
2108 Q_ASSERT(pEntry->m_rhiDepthStencil);
2109 Q_ASSERT(pEntry->m_rhiCube);
2111 const QSize size = pEntry->m_rhiCube->pixelSize();
2112 ps->viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
2114 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
2115 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
2116 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
2117 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
2118 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
2119 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
2120 setupCubeReflectionCameras(inData, reflectionProbes[i], theCameras);
2121 const bool swapYFaces = !rhi->isYUpInFramebuffer();
2122 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
2123 for (
const auto face : QSSGRenderTextureCubeFaces) {
2124 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face);
2125 cameraGlobalTransform = inData.getGlobalTransform(theCameras[cubeFaceIdx]);
2126 theCameras[cubeFaceIdx].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_viewProjection);
2128 rhiPrepareResourcesForReflectionMap(context, passKey, inData, pEntry, ps, reflectionPassObjects, theCameras[cubeFaceIdx], renderer, face);
2130 QRhiRenderPassDescriptor *renderPassDesc =
nullptr;
2131 for (
auto face : QSSGRenderTextureCubeFaces) {
2132 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
2133 face = pEntry->m_timeSliceFace;
2135 QSSGRenderTextureCubeFace outFace = face;
2139 if (outFace == QSSGRenderTextureCubeFace::PosY)
2140 outFace = QSSGRenderTextureCubeFace::NegY;
2141 else if (outFace == QSSGRenderTextureCubeFace::NegY)
2142 outFace = QSSGRenderTextureCubeFace::PosY;
2144 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[quint8(outFace)];
2145 cb->beginPass(rt, reflectionProbes[i]->clearColor, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
2146 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
2147 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
2149 if (renderSkybox && pEntry->m_skyBoxSrbs[quint8(face)]) {
2150 const auto &shaderCache = renderer.contextInterface()->shaderCache();
2151 const bool useSkyBoxQuad = inData.layer.background == QSSGRenderLayer::Background::SkyBox
2152 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial;
2153 const auto &shaderPipeline = useSkyBoxQuad
2154 ? shaderCache->getBuiltInRhiShaders().getRhiSkyBoxShader(QSSGRenderLayer::TonemapMode::None,
2155 inData.layer.skyBoxIsRgbe8,
2157 : shaderCache->getBuiltInRhiShaders().getRhiSkyBoxCubeShader(QSSGRenderLayer::TonemapMode::None,
2158 !inData.layer.skyBoxIsSrgb,
2160 Q_ASSERT(shaderPipeline);
2161 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
2162 QRhiShaderResourceBindings *srb = pEntry->m_skyBoxSrbs[quint8(face)];
2163 if (!renderPassDesc)
2164 renderPassDesc = rt->newCompatibleRenderPassDescriptor();
2165 rt->setRenderPassDescriptor(renderPassDesc);
2166 useSkyBoxQuad ? renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, srb, renderPassDesc, {})
2167 : renderer.rhiCubeRenderer()->recordRenderCube(rhiCtx, ps, srb, renderPassDesc, {});
2170 bool needsSetViewport =
true;
2171 for (
const auto &handle : reflectionPassObjects)
2172 rhiRenderRenderable(rhiCtx, *ps, *handle.obj, &needsSetViewport, face);
2175 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2176 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"reflection_cube", 0, outFace));
2178 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
2182 renderPassDesc->deleteLater();
2184 pEntry->renderMips(rhiCtx);
2186 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
2187 pEntry->m_timeSliceFace = QSSGBaseTypeHelpers::next(pEntry->m_timeSliceFace);
2189 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame)
2190 pEntry->m_rendered =
true;
2192 reflectionProbes[i]->hasScheduledUpdate =
false;
2193 pEntry->m_needsRender =
false;
2247 QSSGPassKey passKey,
2249 QSSGRhiShaderPipeline &shaderPipeline,
2250 QSSGRhiGraphicsPipelineState &ps,
2251 const QSSGAmbientOcclusionSettings &ao,
2252 const QSSGRhiRenderableTexture &rhiAoTexture,
2253 const QSSGRhiRenderableTexture &rhiDepthTexture,
2254 const QSSGRenderCamera &camera)
2256 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2259 if (!rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch)) {
2260 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
2262 cb->beginPass(rhiAoTexture.rt, Qt::white, { 1.0f, 0 });
2263 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rhiAoTexture.rt));
2265 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2269 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, &shaderPipeline);
2271 const float R2 = ao.aoDistance * ao.aoDistance * 0.16f;
2272 const QSize textureSize = rhiAoTexture.texture->pixelSize();
2273 const float rw =
float(textureSize.width());
2274 const float rh =
float(textureSize.height());
2275 const float fov = camera.fov.asVerticalFov(rw / rh).radians();
2276 const float tanHalfFovY = tanf(0.5f * fov * (rh / rw));
2277 const float invFocalLenX = tanHalfFovY * (rw / rh);
2279 const QVector4D aoProps(ao.aoStrength * 0.01f, ao.aoDistance * 0.4f, ao.aoSoftness * 0.02f, ao.aoBias);
2280 const QVector4D aoProps2(
float(ao.aoSamplerate), (ao.aoDither) ? 1.0f : 0.0f, 0.0f, 0.0f);
2281 const QVector4D aoScreenConst(1.0f / R2, rh / (2.0f * tanHalfFovY), 1.0f / rw, 1.0f / rh);
2282 const QVector4D uvToEyeConst(2.0f * invFocalLenX, -2.0f * tanHalfFovY, -invFocalLenX, tanHalfFovY);
2283 const QVector2D cameraProps = camera.clipPlanes;
2292 const int UBUF_SIZE = 72;
2293 QSSGRhiDrawCallData &dcd(rhiCtxD->drawCallData({ passKey,
nullptr,
nullptr,
nullptr, 0 }));
2295 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SIZE);
2299 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2300 memcpy(ubufData, &aoProps, 16);
2301 memcpy(ubufData + 16, &aoProps2, 16);
2302 memcpy(ubufData + 32, &aoScreenConst, 16);
2303 memcpy(ubufData + 48, &uvToEyeConst, 16);
2304 memcpy(ubufData + 64, &cameraProps, 8);
2305 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2307 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2308 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
2309 QSSGRhiShaderResourceBindingList bindings;
2310 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2313 bindings.addTexture(1, QRhiShaderResourceBinding::FragmentStage, rhiDepthTexture.texture, sampler);
2314 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
2316 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2317 renderer.rhiQuadRenderer()->recordRenderQuadPass(rhiCtx, &ps, srb, rhiAoTexture.rt, {});
2472 QSSGPassKey passKey,
2473 QSSGRenderLayer &layer,
2474 QSSGRenderCameraList &cameras,
2478 uint tonemapMode = 0)
2480 QSSG_ASSERT(layer.renderData,
return);
2482 const auto *renderData = layer.renderData;
2484 auto rhiCtx = context.rhiContext().get();
2486 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2488 QSSGRenderImageTexture lightProbeTexture;
2489 switch (layer.background) {
2490 case QSSGRenderLayer::Background::SkyBox:
2491 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.lightProbe,
2492 QSSGBufferManager::MipModeBsdf);
2494 case QSSGRenderLayer::Background::SkyBoxCubeMap:
2495 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.skyBoxCubeMap,
2496 QSSGBufferManager::MipModeDisable);
2498 case QSSGRenderLayer::Background::SkyMaterial:
2499 lightProbeTexture = renderData->skyMaterialTexture;
2502 Q_UNREACHABLE_RETURN();
2505 if (lightProbeTexture.m_texture ==
nullptr) {
2508 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2511 for (QRhiShaderResourceBindings *&srb : entry->m_skyBoxSrbs)
2514 layer.skyBoxSrb =
nullptr;
2519 const bool cubeMapMode = layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap;
2521 if (cubeFace == QSSGRenderTextureCubeFaceNone)
2522 layer.skyBoxIsRgbe8 = lightProbeTexture.m_flags.isRgbe8();
2524 layer.skyBoxIsSrgb = !lightProbeTexture.m_flags.isLinear();
2526 QSSGRhiShaderResourceBindingList bindings;
2528 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
2529 QRhiSampler::Linear,
2530 cubeMapMode ? QRhiSampler::None : QRhiSampler::Linear,
2531 QRhiSampler::Repeat,
2532 QRhiSampler::ClampToEdge,
2533 QRhiSampler::Repeat });
2534 int samplerBinding = 1;
2535 bindings.addTexture(samplerBinding, QRhiShaderResourceBinding::FragmentStage, lightProbeTexture.m_texture, sampler);
2537 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2538 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * cubeFaceIdx;
2539 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey,
nullptr,
nullptr, entry, entryIdx });
2541 QRhi *rhi = rhiCtx->rhi();
2542 const quint32 ubufSize = cameras.count() >= 2 ? 416 : 240;
2544 dcd.ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
int(ubufSize));
2546 }
else if (dcd.ubuf->size() != ubufSize) {
2549 dcd.ubuf->setSize(ubufSize);
2553 float adjustY = rhi->isYUpInNDC() ? 1.0f : -1.0f;
2554 const float exposure = layer.lightProbeSettings.probeExposure;
2556 const QMatrix3x3 &rotationMatrix(layer.lightProbeSettings.probeOrientation);
2559 const float blurAmountOrSrgb = cubeMapMode ? layer.skyBoxIsSrgb : layer.skyboxBlurAmount;
2560 const float maxMipLevelOrTonemapMode = cubeMapMode ?
float(tonemapMode) :
float(lightProbeTexture.m_mipmapCount - 2);
2562 const QVector4D skyboxProperties = {
2566 maxMipLevelOrTonemapMode
2569 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2570 quint32 ubufOffset = 0;
2572 memcpy(ubufData + ubufOffset, &skyboxProperties, 16);
2575 memcpy(ubufData + ubufOffset, rotationMatrix.constData(), 12);
2577 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 12, 12);
2579 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 24, 12);
2582 for (qsizetype viewIdx = 0; viewIdx < cameras.count(); ++viewIdx) {
2583 const QMatrix4x4 &inverseProjection = cameras[viewIdx]->projection.inverted();
2584 const QMatrix4x4 &viewMatrix = renderData->getGlobalTransform(*cameras[viewIdx]);
2585 QMatrix4x4 viewProjection(Qt::Uninitialized);
2586 cameras[viewIdx]->calculateViewProjectionWithoutTranslation(viewMatrix, 0.1f, 5.0f, viewProjection);
2588 quint32 viewDataOffset = ubufOffset;
2589 memcpy(ubufData + viewDataOffset + viewIdx * 64, viewProjection.constData(), 64);
2590 viewDataOffset += cameras.count() * 64;
2591 memcpy(ubufData + viewDataOffset + viewIdx * 64, inverseProjection.constData(), 64);
2592 viewDataOffset += cameras.count() * 64;
2593 memcpy(ubufData + viewDataOffset + viewIdx * 48, viewMatrix.constData(), 48);
2595 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2597 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2599 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2600 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2601 entry->m_skyBoxSrbs[cubeFaceIdx] = rhiCtxD->srb(bindings);
2603 layer.skyBoxSrb = rhiCtxD->srb(bindings);
2607 renderer.rhiCubeRenderer()->prepareCube(rhiCtx,
nullptr);
2609 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2648 QSSGPassKey passKey,
2649 const QSSGRhiGraphicsPipelineState &basePipelineState,
2650 QRhiRenderPassDescriptor *rpDesc,
2652 const QSSGRenderableObjectList &sortedOpaqueObjects,
2653 const QSSGRenderableObjectList &sortedTransparentObjects,
2657 static const auto rhiPrepareDepthPassForObject = [](QSSGRhiContext *rhiCtx,
2658 QSSGPassKey passKey,
2661 QRhiRenderPassDescriptor *rpDesc,
2662 QSSGRhiGraphicsPipelineState *ps) {
2663 QSSGRhiShaderPipelinePtr shaderPipeline;
2664 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2666 const bool isOpaqueDepthPrePass = obj->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
2667 QSSGShaderFeatures featureSet;
2668 featureSet.set(QSSGShaderFeatures::Feature::DepthPass,
true);
2669 if (isOpaqueDepthPrePass)
2670 featureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
2672 QSSGRhiDrawCallData *dcd =
nullptr;
2673 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2674 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2675 const void *modelNode = &subsetRenderable.modelContext.model;
2676 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, 0 });
2679 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2680 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2681 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2682 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2684 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
2685 if (shaderPipeline) {
2686 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2687 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2688 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2689 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2693 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2694 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2696 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2698 ps->cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2700 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
2701 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2703 if (shaderPipeline) {
2704 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2705 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2706 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, customMaterial, subsetRenderable,
2707 inData.renderedCameras,
nullptr,
nullptr);
2708 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2715 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2716 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2717 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
2718 ia = subsetRenderable.subset.rhi.ia;
2720 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
2721 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
2722 InstanceLevels::PerInstance, *shaderPipeline);
2723 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
2725 QSSGRhiShaderResourceBindingList bindings;
2726 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
2729 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
2731 if (isOpaqueDepthPrePass) {
2732 addOpaqueDepthPrePassBindings(rhiCtx,
2733 shaderPipeline.get(),
2734 subsetRenderable.firstImage,
2736 (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
2742 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
2743 if (normalTextureBinding >= 0) {
2744 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2745 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
2746 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
2747 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
2748 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
2749 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
2753 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
2754 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
2756 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2757 QRhiSampler::Nearest,
2759 QRhiSampler::ClampToEdge,
2760 QRhiSampler::ClampToEdge,
2763 bindings.addTexture(binding,
2764 QRhiShaderResourceBinding::VertexStage,
2771 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
2772 if (targetsTexture) {
2773 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
2775 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2776 QRhiSampler::Nearest,
2778 QRhiSampler::ClampToEdge,
2779 QRhiSampler::ClampToEdge,
2780 QRhiSampler::ClampToEdge
2782 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
2785 shaderPipeline->applyAndClearStorageBindings(RENDERER_VISIBILITY_ALL, bindings);
2787 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
2789 subsetRenderable.rhiRenderData.depthPrePass.pipeline = rhiCtxD->pipeline(*ps,
2792 subsetRenderable.rhiRenderData.depthPrePass.srb = srb;
2803 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2808 ps.samples = samples;
2809 ps.viewCount = viewCount;
2810 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
2811 ps.targetBlend[0].colorWrite = {};
2813 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2814 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2818 for (
const QSSGRenderableObjectHandle &handle : sortedTransparentObjects) {
2819 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2950 QSSGPassKey passKey,
2951 const QSSGRhiGraphicsPipelineState &basePipelineState,
2952 QRhiRenderPassDescriptor *rpDesc,
2954 const QSSGRenderableObjectList &sortedOpaqueObjects)
2956 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2957 ps.depthFunc = QRhiGraphicsPipeline::LessOrEqual;
2958 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::BlendEnabled,
false);
2960 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2961 QSSGRenderableObject *obj = handle.obj;
2962 QSSGRhiShaderPipelinePtr shaderPipeline;
2963 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2965 QSSGShaderFeatures featureSet;
2966 featureSet.set(QSSGShaderFeatures::Feature::NormalPass,
true);
2968 QSSGRhiDrawCallData *dcd =
nullptr;
2969 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2970 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2971 const void *modelNode = &subsetRenderable.modelContext.model;
2972 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, 0 });
2975 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2976 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2977 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2978 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2980 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
2981 if (shaderPipeline) {
2982 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2983 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2984 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2985 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2987 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2988 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2990 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2992 ps.cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2994 const auto &customMaterialSystem = subsetRenderable.renderer->contextInterface()->customMaterialSystem();
2995 shaderPipeline = customMaterialSystem->shadersForCustomMaterial(&ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2997 if (shaderPipeline) {
2998 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2999 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3000 customMaterialSystem->updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, customMaterial, subsetRenderable,
3001 inData.renderedCameras,
nullptr,
nullptr);
3002 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3007 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3008 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3009 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3010 ia = subsetRenderable.subset.rhi.ia;
3012 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3013 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
3014 InstanceLevels::PerInstance, *shaderPipeline);
3015 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3017 QSSGRhiShaderResourceBindingList bindings;
3018 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3021 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3022 while (renderableImage) {
3023 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3024 const int samplerHint =
int(renderableImage->m_mapType);
3025 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3026 if (samplerBinding >= 0) {
3027 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3028 if (samplerBinding >= 0 && texture) {
3029 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3030 QSSGRhiSamplerDescription samplerDesc = {
3031 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3032 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3033 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3034 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3035 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3036 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3038 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3039 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3040 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3043 renderableImage = renderableImage->m_nextImage;
3046 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3049 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3050 if (normalTextureBinding >= 0) {
3051 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3052 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3053 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3054 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3055 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3056 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3062 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3063 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3065 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3066 QRhiSampler::Nearest,
3068 QRhiSampler::ClampToEdge,
3069 QRhiSampler::ClampToEdge,
3072 bindings.addTexture(binding,
3073 QRhiShaderResourceBinding::VertexStage,
3080 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3081 if (targetsTexture) {
3082 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3084 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3085 QRhiSampler::Nearest,
3087 QRhiSampler::ClampToEdge,
3088 QRhiSampler::ClampToEdge,
3089 QRhiSampler::ClampToEdge
3091 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3095 shaderPipeline->applyAndClearStorageBindings(RENDERER_VISIBILITY_ALL, bindings);
3096 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3098 subsetRenderable.rhiRenderData.normalPass.pipeline = rhiCtxD->pipeline(ps,
3101 subsetRenderable.rhiRenderData.normalPass.srb = srb;
3171 QSSGPassKey passKey,
3172 const QSSGRhiGraphicsPipelineState &basePipelineState,
3173 QRhiRenderPassDescriptor *rpDesc,
3174 QSSGRenderGraphObject *overrideMaterial,
3176 QSSGRenderableObjectList &inObjects,
3177 QSSGShaderFeatures featureSet)
3180 const qsizetype index = overrideMaterial ? inData.getUserRenderPassManager()->acquireUserPassSlot() : -1;
3185 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3186 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3188 const bool isCustomMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::CustomMaterial);
3189 const bool isDefaultMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
3190 overrideMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
3191 overrideMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial);
3193 if (!isCustomMaterial && !isDefaultMaterial) {
3194 qDebug() <<
"Override material must be a default or custom material.";
3198 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3199 QSSGRenderableObject *obj = handle.obj;
3201 if (obj->type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
3202 obj->type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
3205 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3206 const void *modelNode = &subsetRenderable.modelContext.model;
3208 QSSGRhiDrawCallData *dcd = &rhiCtxD->drawCallData({ passKey, modelNode, overrideMaterial,
nullptr,
3215 QSSGRenderableObjectFlags renderableFlags = subsetRenderable.renderableFlags;
3216 float opacity = subsetRenderable.opacity;
3219 const bool hasAnyLights = !subsetRenderable.lights.isEmpty();
3220 const bool anyLightHasShadows = std::any_of(subsetRenderable.lights.begin(),
3221 subsetRenderable.lights.end(),
3222 [](
const QSSGShaderLight &light) {
return light.shadows; });
3226 auto layerPrepFlags = inData.layerPrepResult.getFlags();
3228 if (isCustomMaterial) {
3229 auto &material =
static_cast<QSSGRenderCustomMaterial &>(*overrideMaterial);
3230 auto prepResult = inData.prepareCustomMaterialForRender(material, renderableFlags, opacity,
3231 false, hasAnyLights, anyLightHasShadows,
3233 subsetRenderable.shaderDescription = prepResult.materialKey;
3235 auto &material =
static_cast<QSSGRenderDefaultMaterial &>(*overrideMaterial);
3236 auto prepResult = inData.prepareDefaultMaterialForRender(material, renderableFlags, opacity,
3237 hasAnyLights, anyLightHasShadows,
3239 subsetRenderable.shaderDescription = prepResult.materialKey;
3243 QSSGRhiShaderPipelinePtr shaderPipeline;
3245 if (isCustomMaterial) {
3246 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(*overrideMaterial);
3247 if (!ps.userSetCullMode)
3248 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3250 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3251 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3252 inData.getDefaultMaterialPropertyTable(), featureSet);
3253 if (shaderPipeline) {
3254 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3255 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3256 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3257 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3258 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3261 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(*overrideMaterial);
3262 if (!ps.userSetCullMode)
3263 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3265 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3266 if (shaderPipeline) {
3267 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3268 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3269 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3270 inData.renderedCameras,
nullptr,
nullptr, overrideMaterial);
3271 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3275 if (!shaderPipeline)
3278 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3279 ia = subsetRenderable.subset.rhi.ia;
3281 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3282 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
3283 InstanceLevels::PerInstance, *shaderPipeline);
3284 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3286 QSSGRhiShaderResourceBindingList bindings;
3287 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3290 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3291 while (renderableImage) {
3292 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3293 const int samplerHint =
int(renderableImage->m_mapType);
3294 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3295 if (samplerBinding >= 0) {
3296 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3298 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3299 QSSGRhiSamplerDescription samplerDesc = {
3300 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3301 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3302 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3303 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3304 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3305 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3307 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3308 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3309 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3312 renderableImage = renderableImage->m_nextImage;
3315 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3318 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3319 if (normalTextureBinding >= 0) {
3320 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3321 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3322 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3323 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3324 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3325 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3329 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3330 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3332 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3333 QRhiSampler::Nearest,
3335 QRhiSampler::ClampToEdge,
3336 QRhiSampler::ClampToEdge,
3339 bindings.addTexture(binding,
3340 QRhiShaderResourceBinding::VertexStage,
3347 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3348 if (targetsTexture) {
3349 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3351 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3352 QRhiSampler::Nearest,
3354 QRhiSampler::ClampToEdge,
3355 QRhiSampler::ClampToEdge,
3356 QRhiSampler::ClampToEdge
3358 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3363 if (shaderPipeline->isLightingEnabled()) {
3364 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3365 shaderPipeline->ub0LightDataOffset(),
3366 sizeof(QSSGShaderLightsUniformData));
3367 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3368 shaderPipeline->ub0DirectionalLightDataOffset(),
3369 sizeof(QSSGShaderDirectionalLightsUniformData));
3372 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3373 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3376 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3377 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3378 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3381 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3382 QRhiSampler::Linear,
3384 QRhiSampler::ClampToEdge,
3385 QRhiSampler::ClampToEdge,
3386 QRhiSampler::Repeat });
3388 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3392 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3393 QRhiTexture *texture = shaderPipeline->shadowMapBlueNoiseTexture();
3396 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3397 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3398 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3401 QRhiSampler *sampler = rhiCtx->sampler(
3402 { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3404 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3408 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3410 auto texture = shaderPipeline->lightProbeTexture();
3412 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3413 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3414 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3418 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3419 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3420 QRhiSampler::Linear,
3421 QRhiSampler::Linear,
3422 QSSGRhiHelpers::toRhi(tiling.first),
3423 QSSGRhiHelpers::toRhi(tiling.second),
3424 QRhiSampler::Repeat });
3425 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3428 shaderPipeline->applyAndClearStorageBindings(QRhiShaderResourceBinding::FragmentStage | QRhiShaderResourceBinding::VertexStage, bindings);
3430 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3432 QSSG_ASSERT(srb,
continue);
3433 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3434 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3435 rhiPassData.srb = srb;
3442 QSSGPassKey passKey,
3443 const QSSGRhiGraphicsPipelineState &basePipelineState,
3444 QRhiRenderPassDescriptor *rpDesc,
3446 QSSGRenderableObjectList &inObjects,
3447 QSSGShaderFeatures featureSet)
3449 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3454 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3455 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3457 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3458 QSSGRenderableObject *obj = handle.obj;
3459 QSSGRhiShaderPipelinePtr shaderPipeline;
3461 QSSGRhiDrawCallData *dcd =
nullptr;
3462 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3463 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3464 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3465 const void *modelNode = &subsetRenderable.modelContext.model;
3467 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, quintptr(index) });
3470 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3471 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3472 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3473 if (!ps.userSetCullMode)
3474 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3476 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3477 if (shaderPipeline) {
3478 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3479 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3480 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3481 inData.renderedCameras,
nullptr,
nullptr);
3482 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3484 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3485 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3486 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3487 if (!ps.userSetCullMode)
3488 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3490 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3491 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3492 inData.getDefaultMaterialPropertyTable(), featureSet);
3493 if (shaderPipeline) {
3494 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3495 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3496 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3497 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3498 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3502 if (!shaderPipeline)
3506 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3507 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3508 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3509 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3510 ia = subsetRenderable.subset.rhi.ia;
3512 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3513 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
3514 InstanceLevels::PerInstance, *shaderPipeline);
3515 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3517 QSSGRhiShaderResourceBindingList bindings;
3518 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3521 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3522 while (renderableImage) {
3523 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3524 const int samplerHint =
int(renderableImage->m_mapType);
3525 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3526 if (samplerBinding >= 0) {
3527 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3528 if (samplerBinding >= 0 && texture) {
3529 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3530 QSSGRhiSamplerDescription samplerDesc = {
3531 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3532 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3533 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3534 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3535 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3536 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3538 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3539 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3540 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3543 renderableImage = renderableImage->m_nextImage;
3546 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3549 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3550 if (normalTextureBinding >= 0) {
3551 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3552 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3553 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3554 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3555 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3556 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3560 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3561 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3563 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3564 QRhiSampler::Nearest,
3566 QRhiSampler::ClampToEdge,
3567 QRhiSampler::ClampToEdge,
3570 bindings.addTexture(binding,
3571 QRhiShaderResourceBinding::VertexStage,
3578 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3579 if (targetsTexture) {
3580 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3582 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3583 QRhiSampler::Nearest,
3585 QRhiSampler::ClampToEdge,
3586 QRhiSampler::ClampToEdge,
3587 QRhiSampler::ClampToEdge
3589 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3594 if (shaderPipeline->isLightingEnabled()) {
3595 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3596 shaderPipeline->ub0LightDataOffset(),
3597 sizeof(QSSGShaderLightsUniformData));
3598 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3599 shaderPipeline->ub0DirectionalLightDataOffset(),
3600 sizeof(QSSGShaderDirectionalLightsUniformData));
3603 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3604 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3607 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3608 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3609 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3612 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3613 QRhiSampler::Linear,
3615 QRhiSampler::ClampToEdge,
3616 QRhiSampler::ClampToEdge,
3617 QRhiSampler::Repeat });
3619 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3623 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3624 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
3625 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
3627 QRhiTexture *texture = shadowMapBlueNoise;
3628 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3629 QRhiSampler::Linear,
3631 QRhiSampler::Repeat,
3632 QRhiSampler::Repeat,
3633 QRhiSampler::Repeat });
3634 Q_ASSERT(texture && sampler);
3635 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3638 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3639 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
3640 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3641 QRhiSampler::Linear,
3643 QRhiSampler::Repeat,
3644 QRhiSampler::Repeat,
3645 QRhiSampler::Repeat });
3646 Q_ASSERT(texture && sampler);
3647 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3648 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3653 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
3654 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
3655 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
3656 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
3657 ? QRhiSampler::Linear
3658 : QRhiSampler::None;
3659 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3660 QRhiSampler::Linear,
3662 QRhiSampler::ClampToEdge,
3663 QRhiSampler::ClampToEdge,
3664 QRhiSampler::Repeat });
3665 if (reflectionSampler >= 0) {
3668 if (!reflectionTexture) {
3669 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3670 reflectionTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3671 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3673 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
3677 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3679 auto texture = shaderPipeline->lightProbeTexture();
3681 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3682 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3683 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3687 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3688 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3689 QRhiSampler::Linear,
3690 QRhiSampler::Linear,
3691 QSSGRhiHelpers::toRhi(tiling.first),
3692 QSSGRhiHelpers::toRhi(tiling.second),
3693 QRhiSampler::Repeat });
3694 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3698 if (shaderPipeline->screenTexture()) {
3699 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
3700 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
3701 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
3702 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
3703 ? QRhiSampler::Linear : QRhiSampler::None;
3704 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
3705 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3706 if (screenTextureBinding >= 0) {
3707 bindings.addTexture(screenTextureBinding,
3708 QRhiShaderResourceBinding::FragmentStage,
3709 shaderPipeline->screenTexture(), sampler);
3711 if (screenTextureArrayBinding >= 0) {
3712 bindings.addTexture(screenTextureArrayBinding,
3713 QRhiShaderResourceBinding::FragmentStage,
3714 shaderPipeline->screenTexture(), sampler);
3719 if (shaderPipeline->lightmapTexture()) {
3720 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
3722 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
3723 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3724 bindings.addTexture(binding,
3725 QRhiShaderResourceBinding::FragmentStage,
3726 shaderPipeline->lightmapTexture(), sampler);
3733 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3734 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3736 int maxSamplerBinding = -1;
3737 QVector<QShaderDescription::InOutVariable> samplerVars =
3738 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3739 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3740 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3741 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3742 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3743 if (it == samplerVars.cend())
3744 samplerVars.append(var);
3747 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3748 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3750 if (maxSamplerBinding >= 0) {
3751 int extraTexCount = shaderPipeline->extraTextureCount();
3752 for (
int i = 0; i < extraTexCount; ++i) {
3753 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
3754 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
3755 if (samplerBinding >= 0) {
3756 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
3757 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
3758 bindings.addTexture(samplerBinding,
3759 QRhiShaderResourceBinding::FragmentStage,
3765 shaderPipeline->applyAndClearStorageBindings(QRhiShaderResourceBinding::FragmentStage | QRhiShaderResourceBinding::VertexStage, bindings);
3768 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3770 QSSG_ASSERT(srb,
continue);
3772 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3773 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3774 rhiPassData.srb = srb;
3783 QSSGPassKey passKey,
3784 const QSSGRhiGraphicsPipelineState &basePipelineState,
3785 QRhiRenderPassDescriptor *rpDesc,
3788 QSSGRenderableObjectList &inObjects,
3789 QSSGShaderFeatures featureSet)
3791 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3797 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3798 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3799 QSSGRenderableObject *obj = handle.obj;
3800 QSSGRhiShaderPipelinePtr shaderPipeline;
3801 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3802 const auto &bufferManager = inData.contextInterface()->bufferManager();
3804 featureSet.set(QSSGShaderFeatures::Feature::UserRenderPass,
true);
3806 QSSGRhiDrawCallData *dcd =
nullptr;
3807 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3808 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3809 const void *modelNode = &subsetRenderable.modelContext.model;
3811 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, quintptr(index) });
3814 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3815 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3816 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3817 if (!ps.userSetCullMode)
3818 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3820 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet, shaderAugmentation);
3821 if (shaderPipeline) {
3822 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3823 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3825 for (
const auto &u : shaderAugmentation.propertyUniforms)
3826 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3827 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3828 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3830 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3831 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3832 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3833 if (!ps.userSetCullMode)
3834 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3836 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3838 if (material.m_shadingMode == QSSGRenderCustomMaterial::ShadingMode::Unshaded)
3839 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
3841 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet, shaderAugmentation);
3843 if (shaderPipeline) {
3844 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3845 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3847 for (
const auto &u : shaderAugmentation.propertyUniforms)
3848 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3849 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3850 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3851 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3855 if (!shaderPipeline) {
3857 qDebug() <<
"Failed to prepare user augmented pass for object.";
3862 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3863 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3864 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3865 ia = subsetRenderable.subset.rhi.ia;
3867 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3868 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, inData, cameraDatas[0].direction, cameraDatas[0].position,
3869 InstanceLevels::PerInstance, *shaderPipeline);
3870 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3872 QSSGRhiShaderResourceBindingList bindings;
3873 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3880 QVector<QShaderDescription::InOutVariable> samplerVars =
3881 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3882 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3883 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3884 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3885 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3886 if (it == samplerVars.cend())
3887 samplerVars.append(var);
3890 int maxSamplerBinding = -1;
3891 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3892 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3894 QBitArray specifiedBindings(maxSamplerBinding + 1);
3895 const auto addTrackedTexture = [&](
int binding, QRhiShaderResourceBinding::StageFlags stage,
3896 QRhiTexture *texture, QRhiSampler *sampler) {
3897 specifiedBindings.setBit(binding);
3898 bindings.addTexture(binding, stage, texture, sampler);
3902 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3903 while (renderableImage) {
3904 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3905 const int samplerHint =
int(renderableImage->m_mapType);
3906 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3907 if (samplerBinding >= 0) {
3908 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3909 if (samplerBinding >= 0 && texture) {
3910 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3911 QSSGRhiSamplerDescription samplerDesc = {
3912 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3913 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3914 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3915 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3916 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3917 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3919 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3920 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3921 addTrackedTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3924 renderableImage = renderableImage->m_nextImage;
3927 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings, &specifiedBindings);
3930 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3931 if (normalTextureBinding >= 0) {
3932 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3933 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3934 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3935 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3936 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3937 addTrackedTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3943 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3944 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3946 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3947 QRhiSampler::Nearest,
3949 QRhiSampler::ClampToEdge,
3950 QRhiSampler::ClampToEdge,
3953 addTrackedTexture(binding,
3954 QRhiShaderResourceBinding::VertexStage,
3961 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3962 if (targetsTexture) {
3963 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3965 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3966 QRhiSampler::Nearest,
3968 QRhiSampler::ClampToEdge,
3969 QRhiSampler::ClampToEdge,
3970 QRhiSampler::ClampToEdge
3972 addTrackedTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3977 if (shaderPipeline->isLightingEnabled()) {
3978 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3979 shaderPipeline->ub0LightDataOffset(),
3980 sizeof(QSSGShaderLightsUniformData));
3981 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3982 shaderPipeline->ub0DirectionalLightDataOffset(),
3983 sizeof(QSSGShaderDirectionalLightsUniformData));
3986 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3987 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3990 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3991 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3992 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3995 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3996 QRhiSampler::Linear,
3998 QRhiSampler::ClampToEdge,
3999 QRhiSampler::ClampToEdge,
4000 QRhiSampler::Repeat });
4002 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
4006 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
4007 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
4008 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
4010 QRhiTexture *texture = shadowMapBlueNoise;
4011 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
4012 QRhiSampler::Linear,
4014 QRhiSampler::Repeat,
4015 QRhiSampler::Repeat,
4016 QRhiSampler::Repeat });
4017 Q_ASSERT(texture && sampler);
4018 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
4021 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
4022 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
4023 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
4024 QRhiSampler::Linear,
4026 QRhiSampler::Repeat,
4027 QRhiSampler::Repeat,
4028 QRhiSampler::Repeat });
4029 Q_ASSERT(texture && sampler);
4030 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
4031 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
4038 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
4039 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
4040 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
4041 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
4042 ? QRhiSampler::Linear
4043 : QRhiSampler::None;
4044 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
4045 QRhiSampler::Linear,
4047 QRhiSampler::ClampToEdge,
4048 QRhiSampler::ClampToEdge,
4049 QRhiSampler::Repeat });
4050 if (reflectionSampler >= 0) {
4051 if (!reflectionTexture) {
4052 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
4053 reflectionTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
4054 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
4056 addTrackedTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
4059 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
4061 auto texture = shaderPipeline->lightProbeTexture();
4063 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
4064 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
4065 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
4069 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
4070 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
4071 QRhiSampler::Linear,
4072 QRhiSampler::Linear,
4073 QSSGRhiHelpers::toRhi(tiling.first),
4074 QSSGRhiHelpers::toRhi(tiling.second),
4075 QRhiSampler::Repeat });
4076 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
4080 if (shaderPipeline->screenTexture()) {
4081 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
4082 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
4083 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
4088 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
4089 ? QRhiSampler::Linear : QRhiSampler::None;
4090 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
4091 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
4092 if (screenTextureBinding >= 0) {
4093 addTrackedTexture(screenTextureBinding,
4094 QRhiShaderResourceBinding::FragmentStage,
4095 shaderPipeline->screenTexture(), sampler);
4097 if (screenTextureArrayBinding >= 0) {
4098 addTrackedTexture(screenTextureArrayBinding,
4099 QRhiShaderResourceBinding::FragmentStage,
4100 shaderPipeline->screenTexture(), sampler);
4105 if (shaderPipeline->lightmapTexture()) {
4106 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
4108 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
4109 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
4110 addTrackedTexture(binding,
4111 QRhiShaderResourceBinding::FragmentStage,
4112 shaderPipeline->lightmapTexture(), sampler);
4119 if (maxSamplerBinding >= 0) {
4121 int extraTexCount = shaderPipeline->extraTextureCount();
4122 for (
int i = 0; i < extraTexCount; ++i) {
4123 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
4124 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
4125 if (samplerBinding >= 0) {
4126 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
4127 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
4128 addTrackedTexture(samplerBinding,
4129 QRhiShaderResourceBinding::FragmentStage,
4137 QRhiSampler *dummySampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
4138 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
4139 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
4140 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
4141 QRhiTexture *dummyTextureArray = rhiCtx->dummyTexture({}, resourceUpdates, QSize(64, 64), Qt::black, qMax(2u, inData.layer.viewCount));
4142 QRhiTexture *dummyTexture3D = rhiCtx->dummyTexture(QRhiTexture::ThreeDimensional, resourceUpdates);
4143 QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
4144 QRhiTexture *dummyCubeTextureArray = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates, QSize(64, 64), Qt::black, qMax(2u, inData.layer.viewCount));
4145 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
4147 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars)) {
4148 if (!specifiedBindings.testBit(var.binding)) {
4149 QRhiTexture *t =
nullptr;
4150 if (var.type == QShaderDescription::SamplerCube)
4151 t = dummyCubeTexture;
4152 else if (var.type == QShaderDescription::Sampler3D)
4154 else if (var.type == QShaderDescription::Sampler2DArray)
4155 t = dummyTextureArray;
4156 else if (var.type == QShaderDescription::SamplerCubeArray)
4157 t = dummyCubeTextureArray;
4160 bindings.addTexture(var.binding, RENDERER_VISIBILITY_ALL, t, dummySampler);
4163 shaderPipeline->applyAndClearStorageBindings(QRhiShaderResourceBinding::FragmentStage | QRhiShaderResourceBinding::VertexStage, bindings);
4166 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
4167 QSSG_ASSERT(srb,
return -1);
4168 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
4169 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
4170 rhiPassData.srb = srb;
4239 QSSGPassKey passKey,
4241 const QMatrix4x4 &viewProjection,
4243 QRhiRenderPassDescriptor *renderPassDescriptor,
4244 QSSGRhiGraphicsPipelineState *ps,
4245 QSSGRenderMotionVectorMap &motionVectorMapManager)
4247 if (inObject.type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
4248 inObject.type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
4251 QSSGSubsetRenderable &subsetRenderable =
static_cast<QSSGSubsetRenderable &>(inObject);
4252 auto &modelNode = subsetRenderable.modelContext.model;
4254 bool skin = modelNode.usesBoneTexture();
4255 bool instance = modelNode.usesInstanceBuffer();
4256 bool morph = modelNode.morphTargets.size() > 0;
4258 QSSGRhiShaderPipelinePtr shaderPipeline = inData.contextInterface()->shaderCache()->
4259 getBuiltInRhiShaders().getRhiMotionVectorShader(skin, instance, morph);
4261 if (!shaderPipeline)
4264 QSSGRhiShaderResourceBindingList bindings;
4266 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
4267 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode,
nullptr,
nullptr, 0 });
4270 QSSGRenderTextureData *boneTextureData =
nullptr;
4272 boneTextureData = modelNode.skin;
4273 else if (modelNode.skeleton)
4274 boneTextureData = &modelNode.skeleton->boneTexData;
4276 QMatrix4x4 modelViewProjection = subsetRenderable.modelContext.modelViewProjections[0];
4277 QMatrix4x4 instanceLocal;
4278 QMatrix4x4 instanceGlobal;
4281 instanceLocal = inData.getInstanceTransforms(modelNode).local;
4282 instanceGlobal = inData.getInstanceTransforms(modelNode).global;
4284 modelViewProjection = viewProjection;
4286 auto motionData = motionVectorMapManager.trackMotionData(&modelNode,
4287 modelViewProjection,
4291 instance ? modelNode.instanceTable :
nullptr,
4292 modelNode.morphWeights);
4293 float velocityAmount = subsetRenderable.modelContext.model.motionVectorScale;
4294 int morphTargetCount = modelNode.morphWeights.count();
4306 const int ubufSize = 416;
4309 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic,
4310 QRhiBuffer::UniformBuffer,
4315 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
4317 memcpy(ubufData, modelViewProjection.constData(), 64);
4319 memcpy(ubufData + ubufOffset, motionData.prevModelViewProjection.constData(), 64);
4321 memcpy(ubufData + ubufOffset, instanceLocal.constData(), 64);
4323 memcpy(ubufData + ubufOffset, instanceGlobal.constData(), 64);
4325 memcpy(ubufData + ubufOffset, motionData.prevInstanceLocal.constData(), 64);
4327 memcpy(ubufData + ubufOffset, motionData.prevInstanceGlobal.constData(), 64);
4329 memcpy(ubufData + ubufOffset, &inData.layer.currentAndLastJitter, 16);
4331 memcpy(ubufData + ubufOffset, &velocityAmount, 4);
4333 memcpy(ubufData + ubufOffset, &morphTargetCount, 4);
4334 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
4336 bindings.addUniformBuffer(0, QRhiShaderResourceBinding::VertexStage |
4337 QRhiShaderResourceBinding::FragmentStage, dcd.ubuf);
4339 QRhiSampler *nearestSampler =
nullptr;
4341 if (skin || instance) {
4342 nearestSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4343 QRhiSampler::Nearest,
4345 QRhiSampler::ClampToEdge,
4346 QRhiSampler::ClampToEdge,
4352 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
4355 bindings.addTexture(binding,
4356 QRhiShaderResourceBinding::VertexStage,
4357 inData.getBonemapTexture(subsetRenderable.modelContext),
4359 binding = shaderPipeline->bindingForTexture(
"lastBoneTexture");
4361 bindings.addTexture(binding,
4362 QRhiShaderResourceBinding::VertexStage,
4363 motionData.prevBoneTexture.m_texture,
4370 int binding = shaderPipeline->bindingForTexture(
"lastInstanceTexture");
4372 bindings.addTexture(binding,
4373 QRhiShaderResourceBinding::VertexStage,
4374 motionData.prevInstanceTexture.m_texture,
4379 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
4380 if (targetsTexture) {
4381 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
4383 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4384 QRhiSampler::Nearest,
4386 QRhiSampler::ClampToEdge,
4387 QRhiSampler::ClampToEdge,
4388 QRhiSampler::ClampToEdge
4390 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
4392 if ((binding = shaderPipeline->bindingForTexture(
"morphWeightTexture")) >= 0)
4393 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.currentMorphWeightTexture.m_texture, targetsSampler);
4395 if ((binding = shaderPipeline->bindingForTexture(
"lastMorphWeightTexture")) >= 0)
4396 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.prevMorphWeightTexture.m_texture, targetsSampler);
4400 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
4401 ia = subsetRenderable.subset.rhi.ia;
4402 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
4403 QVector3D cameraDirection = cameraDatas[0].direction;
4404 QVector3D cameraPosition = cameraDatas[0].position;
4408 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, inData, cameraDirection, cameraPosition,
4409 InstanceLevels::ModelWide, *shaderPipeline);
4410 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
4411 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
4413 QRhiShaderResourceBindings *&srb = dcd.srb;
4414 bool srbChanged =
false;
4415 if (!srb || bindings != dcd.bindings) {
4416 srb = rhiCtxD->srb(bindings);
4417 rhiCtxD->releaseCachedSrb(dcd.bindings);
4418 dcd.bindings = bindings;
4422 subsetRenderable.rhiRenderData.motionVectorPass.srb = srb;
4424 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
4427 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
4428 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
4431 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = dcd.pipeline;
4434 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = rhiCtxD->pipeline(pipelineKey,
4435 renderPassDescriptor,
4437 dcd.pipeline = subsetRenderable.rhiRenderData.motionVectorPass.pipeline;
4438 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
4439 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;