250 const QSSGRenderCamera &inCamera,
251 const QSSGRenderLight *inLight,
252 const int shadowMapResolution,
253 const float pcfRadius,
254 const float clipNear,
256 const QSSGBounds3 &castingObjectsBox,
257 const QSSGBounds3 &receivingObjectsBox,
258 bool lockShadowmapTexels,
259 QSSGDebugDrawSystem *debugDrawSystem,
261 bool drawSceneCascadeIntersection)
263 Q_ASSERT(inLight->type == QSSGRenderLight::Type::DirectionalLight);
264 QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> result;
266 if (clipNear >= clipFar || qFuzzyCompare(clipNear, clipFar))
269 const QMatrix4x4 lightGlobalTransform = data.getGlobalTransform(*inLight);
270 const QVector3D lightDir = inLight->getDirection(lightGlobalTransform);
271 const QVector3D lightPivot = inLight->pivot;
273 const QVector3D forward = lightDir.normalized();
274 const QVector3D right = qFuzzyCompare(qAbs(forward.y()), 1.0f)
275 ? QVector3D::crossProduct(forward, QVector3D(1, 0, 0)).normalized()
276 : QVector3D::crossProduct(forward, QVector3D(0, 1, 0)).normalized();
277 const QVector3D up = QVector3D::crossProduct(right, forward).normalized();
279 QMatrix4x4 lightMatrix;
280 lightMatrix.setRow(0, QVector4D(right, 0.0f));
281 lightMatrix.setRow(1, QVector4D(up, 0.0f));
282 lightMatrix.setRow(2, QVector4D(forward, 0.0f));
283 lightMatrix.setRow(3, QVector4D(0.0f, 0.0f, 0.0f, 1.0f));
284 QMatrix4x4 lightMatrixInverted = lightMatrix.inverted();
286 const float farScale = (clipFar - clipNear) / (inCamera.clipPlanes.clipFar() - inCamera.clipPlanes.clipNear());
288 const QMatrix4x4 cameraGlobalTransform = data.getGlobalTransform(inCamera);
289 QMatrix4x4 viewProjection(Qt::Uninitialized);
290 inCamera.calculateViewProjectionMatrix(cameraGlobalTransform, viewProjection);
291 const QSSGBoxPoints frustum = computeFrustumBounds(viewProjection);
292 const QSSGBoxPoints frustumUntransformed = lockShadowmapTexels ? computeFrustumBounds(inCamera.projection) : QSSGBoxPoints();
296 const auto calcFrustumRadius = [&](
float t0,
float t1) ->
float {
297 const QSSGBoxPoints pts = sliceFrustum(frustumUntransformed, t0 * farScale, t1 * farScale);
299 QVector3D center = QVector3D(0.f, 0.f, 0.f);
300 for (QVector3D point : pts) {
303 center = center * 0.125f;
305 float radiusSquared = 0;
306 for (QVector3D point : pts) {
307 radiusSquared = qMax(radiusSquared, (point - center).lengthSquared());
310 return std::sqrt(radiusSquared);
313 const auto computeSplitRanges = [inLight](
const QVarLengthArray<
float, 3> &splits) -> QVarLengthArray<QPair<
float,
float>, 4> {
314 QVarLengthArray<QPair<
float,
float>, 4> ranges;
315 const float csmBlendRatio = inLight->m_csmBlendRatio;
317 for (qsizetype i = 0; i < splits.length(); i++) {
318 const float tI = qBound(qMin(t0 + 0.01f, 1.0f), splits[i], 1.0f);
319 ranges.emplace_back(t0, qMin(1.0f, tI + csmBlendRatio));
322 ranges.emplace_back(t0, 1.0f);
326 const auto computeFrustums = [&](
const QVarLengthArray<
float, 3> &splits) {
327 for (
const auto &range : computeSplitRanges(splits)) {
328 const float frustumRadius = lockShadowmapTexels ? calcFrustumRadius(range.first, range.second) : 0.0f;
329 auto camera = computeShadowCameraFromFrustum(lightMatrix,
337 range.first * farScale,
338 range.second * farScale,
345 drawSceneCascadeIntersection);
346 result.emplace_back(std::move(camera));
350 switch (inLight->m_csmNumSplits) {
356 computeFrustums({ inLight->m_csmSplit1 });
360 computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2 });
364 computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2, inLight->m_csmSplit3 });
406 QSSGRhiShaderPipeline *shaderPipeline,
407 QSSGRenderableImage *renderableImage,
408 QSSGRhiShaderResourceBindingList &bindings,
409 bool isCustomMaterialMeshSubset =
false)
411 static const auto imageAffectsAlpha = [](QSSGRenderableImage::Type mapType) {
412 return mapType == QSSGRenderableImage::Type::BaseColor ||
413 mapType == QSSGRenderableImage::Type::Diffuse ||
414 mapType == QSSGRenderableImage::Type::Translucency ||
415 mapType == QSSGRenderableImage::Type::Opacity;
418 while (renderableImage) {
419 const auto mapType = renderableImage->m_mapType;
420 if (imageAffectsAlpha(mapType)) {
421 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(mapType);
422 const int samplerHint =
int(mapType);
423 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
424 if (samplerBinding >= 0) {
425 QRhiTexture *texture = renderableImage->m_texture.m_texture;
426 if (samplerBinding >= 0 && texture) {
427 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
428 QRhiSampler *sampler = rhiCtx->sampler({ QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
429 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
430 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
431 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
432 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
433 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
435 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
439 renderableImage = renderableImage->m_nextImage;
442 if (isCustomMaterialMeshSubset) {
443 QVector<QShaderDescription::InOutVariable> samplerVars =
444 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
445 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
446 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
447 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
448 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
449 if (it == samplerVars.cend())
450 samplerVars.append(var);
453 int maxSamplerBinding = -1;
454 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
455 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
464 QBitArray samplerBindingsSpecified(maxSamplerBinding + 1);
466 if (maxSamplerBinding >= 0) {
468 int customTexCount = shaderPipeline->extraTextureCount();
469 for (
int i = 0; i < customTexCount; ++i) {
470 const QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
471 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
472 if (samplerBinding >= 0) {
473 samplerBindingsSpecified.setBit(samplerBinding);
474 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
475 bindings.addTexture(samplerBinding,
476 RENDERER_VISIBILITY_ALL,
484 QRhiSampler *dummySampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
485 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
486 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
487 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
488 QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
489 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
491 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars)) {
492 if (!samplerBindingsSpecified.testBit(var.binding)) {
493 QRhiTexture *t = var.type == QShaderDescription::SamplerCube ? dummyCubeTexture : dummyTexture;
494 bindings.addTexture(var.binding, RENDERER_VISIBILITY_ALL, t, dummySampler);
739 QSSGRhiGraphicsPipelineState *ps,
740 const QVector2D *depthAdjust,
741 const QSSGRenderableObjectList &sortedOpaqueObjects,
742 QSSGRenderCamera &inCamera,
744 QSSGRenderTextureCubeFace cubeFace,
745 quint32 cascadeIndex)
747 QSSGShaderFeatures featureSet;
749 featureSet.set(QSSGShaderFeatures::Feature::OrthoShadowPass,
true);
751 featureSet.set(QSSGShaderFeatures::Feature::PerspectiveShadowPass,
true);
757 featureSet.set(QSSGShaderFeatures::Feature::DisableMultiView,
true);
759 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
760 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
761 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
763 for (
const auto &handle : sortedOpaqueObjects) {
764 QSSGRenderableObject *theObject = handle.obj;
765 QSSG_ASSERT(theObject->renderableFlags.castsShadows(),
continue);
767 QSSGShaderFeatures objectFeatureSet = featureSet;
768 const bool isOpaqueDepthPrePass = theObject->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
769 if (isOpaqueDepthPrePass)
770 objectFeatureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
772 QSSGRhiDrawCallData *dcd =
nullptr;
773 QMatrix4x4 modelViewProjection;
774 QSSGSubsetRenderable &renderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
775 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
776 const bool hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(renderable.shaderDescription) > 0;
777 modelViewProjection = hasSkinning ? pEntry->m_lightViewProjection[cascadeIndex]
778 : pEntry->m_lightViewProjection[cascadeIndex] * renderable.modelContext.globalTransform;
784 const quintptr entryIdx = cascadeIndex + cubeFaceIdx + (quintptr(renderable.subset.offset) << 3);
785 dcd = &rhiCtxD->drawCallData({ passKey, &renderable.modelContext.model,
nullptr, pEntry, entryIdx });
788 QSSGRhiShaderResourceBindingList bindings;
789 QSSGRhiShaderPipelinePtr shaderPipeline;
790 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
791 if (theObject->type == QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset) {
792 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
793 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
794 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
796 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, objectFeatureSet);
799 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
800 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
802 QSSGRenderCameraList cameras({ &inCamera });
803 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras, depthAdjust, &modelViewProjection);
805 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
806 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
808 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
809 }
else if (theObject->type == QSSGSubsetRenderable::Type::CustomMaterialMeshSubset) {
810 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
811 ps->cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
813 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
814 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), objectFeatureSet);
817 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
818 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
820 QSSGRenderCameraList cameras({ &inCamera });
821 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, material, subsetRenderable,
822 cameras, depthAdjust, &modelViewProjection);
823 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
826 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
828 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
829 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
830 ia = subsetRenderable.subset.rhi.ia;
831 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
832 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
833 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
836 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
839 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
841 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
843 if (isOpaqueDepthPrePass) {
844 addOpaqueDepthPrePassBindings(rhiCtx,
845 shaderPipeline.get(),
846 subsetRenderable.firstImage,
848 (theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
855 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
856 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
857 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
858 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
859 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
860 if (screenTextureBinding >= 0) {
861 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
862 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
863 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
864 bindings.addTexture(screenTextureBinding,
865 QRhiShaderResourceBinding::FragmentStage,
866 dummyTexture, sampler);
868 if (screenTextureArrayBinding >= 0) {
869 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
870 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates, QSize(64, 64), Qt::black, inData.layer.viewCount);
871 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
872 bindings.addTexture(screenTextureArrayBinding,
873 QRhiShaderResourceBinding::FragmentStage,
874 dummyTexture, sampler);
879 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
880 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
882 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
883 QRhiSampler::Nearest,
885 QRhiSampler::ClampToEdge,
886 QRhiSampler::ClampToEdge,
889 bindings.addTexture(binding,
890 QRhiShaderResourceBinding::VertexStage,
897 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
898 if (targetsTexture) {
899 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
901 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
902 QRhiSampler::Nearest,
904 QRhiSampler::ClampToEdge,
905 QRhiSampler::ClampToEdge,
906 QRhiSampler::ClampToEdge
908 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
912 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
913 subsetRenderable.rhiRenderData.shadowPass.pipeline = rhiCtxD->pipeline(*ps, pEntry->m_rhiRenderPassDesc[cascadeIndex], srb);
914 subsetRenderable.rhiRenderData.shadowPass.srb[cubeFaceIdx] = srb;
949 QRhiRenderPassDescriptor *renderPassDescriptor,
950 QSSGRhiGraphicsPipelineState *ps,
951 QSSGShaderFeatures featureSet,
955 QSSGRenderCamera *alteredCamera,
956 QMatrix4x4 *alteredModelViewProjection,
957 QSSGRenderTextureCubeFace cubeFace,
961 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
963 switch (inObject.type) {
964 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
966 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
968 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
969 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
971 if ((cubeFace != QSSGRenderTextureCubeFaceNone)) {
973 featureSet.disableTonemapping();
976 if (subsetRenderable.renderableFlags.rendersWithLightmap())
977 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
979 const auto &shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
980 if (shaderPipeline) {
988 QSSGRhiShaderResourceBindingList bindings;
989 const auto &modelNode = subsetRenderable.modelContext.model;
990 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
1001 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
1002 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone)
1003 * (cubeFaceIdx + (quintptr(subsetRenderable.subset.offset) << 3));
1005 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
1006 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode, &subsetRenderable.material, entry,
1009 shaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
1010 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
1011 if (alteredCamera) {
1012 Q_ASSERT(alteredModelViewProjection);
1013 QSSGRenderCameraList cameras({ alteredCamera });
1014 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras,
nullptr, alteredModelViewProjection);
1016 Q_ASSERT(!alteredModelViewProjection);
1017 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
1021 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
1022 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
1025 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
1028 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
1029 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
1031 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1032 QRhiSampler::Nearest,
1034 QRhiSampler::ClampToEdge,
1035 QRhiSampler::ClampToEdge,
1038 bindings.addTexture(binding,
1039 QRhiShaderResourceBinding::VertexStage,
1045 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
1046 if (targetsTexture) {
1047 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
1049 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1050 QRhiSampler::Nearest,
1052 QRhiSampler::ClampToEdge,
1053 QRhiSampler::ClampToEdge,
1054 QRhiSampler::ClampToEdge
1056 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
1060 ps->samples = samples;
1061 ps->viewCount = viewCount;
1063 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
1064 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
1065 if (!oit || (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::None))
1066 fillTargetBlend(&ps->targetBlend[0], material.blendMode);
1068 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
1070 ia = subsetRenderable.subset.rhi.ia;
1071 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
1072 QVector3D cameraDirection = cameraDatas[0].direction;
1073 QVector3D cameraPosition = cameraDatas[0].position;
1074 if (alteredCamera) {
1075 const QMatrix4x4 camGlobalTranform = inData.getGlobalTransform(*alteredCamera);
1076 cameraDirection = QSSGRenderNode::getScalingCorrectDirection(camGlobalTranform);
1077 cameraPosition = QSSGRenderNode::getGlobalPos(camGlobalTranform);
1079 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDirection, cameraPosition);
1080 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
1082 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf, 0, shaderPipeline->ub0Size());
1084 if (shaderPipeline->isLightingEnabled()) {
1085 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1086 shaderPipeline->ub0LightDataOffset(),
1087 sizeof(QSSGShaderLightsUniformData));
1088 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1089 shaderPipeline->ub0DirectionalLightDataOffset(),
1090 sizeof(QSSGShaderDirectionalLightsUniformData));
1095 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
1096 while (renderableImage) {
1097 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
1098 const int samplerHint =
int(renderableImage->m_mapType);
1099 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
1100 if (samplerBinding >= 0) {
1101 QRhiTexture *texture = renderableImage->m_texture.m_texture;
1102 if (samplerBinding >= 0 && texture) {
1103 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
1104 QSSGRhiSamplerDescription samplerDesc = {
1105 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
1106 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
1107 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
1108 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
1109 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
1110 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
1112 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
1113 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
1114 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
1117 renderableImage = renderableImage->m_nextImage;
1120 if (shaderPipeline->isLightingEnabled()) {
1122 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
1123 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
1126 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1127 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
1128 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1131 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1132 QRhiSampler::Linear,
1134 QRhiSampler::ClampToEdge,
1135 QRhiSampler::ClampToEdge,
1136 QRhiSampler::Repeat });
1138 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1142 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
1143 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
1144 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
1146 QRhiTexture *texture = shadowMapBlueNoise;
1147 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1148 QRhiSampler::Linear,
1150 QRhiSampler::Repeat,
1151 QRhiSampler::Repeat,
1152 QRhiSampler::Repeat });
1153 Q_ASSERT(texture && sampler);
1154 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1157 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1158 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
1159 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1160 QRhiSampler::Linear,
1162 QRhiSampler::Repeat,
1163 QRhiSampler::Repeat,
1164 QRhiSampler::Repeat });
1165 Q_ASSERT(texture && sampler);
1166 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1167 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1174 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
1175 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
1176 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
1177 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
1178 ? QRhiSampler::Linear
1179 : QRhiSampler::None;
1180 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1181 QRhiSampler::Linear,
1183 QRhiSampler::ClampToEdge,
1184 QRhiSampler::ClampToEdge,
1185 QRhiSampler::Repeat });
1186 if (reflectionSampler >= 0 && reflectionTexture)
1187 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
1190 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
1192 auto texture = shaderPipeline->lightProbeTexture();
1194 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1195 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
1196 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1200 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
1201 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1202 QRhiSampler::Linear,
1203 QRhiSampler::Linear,
1204 QSSGRhiHelpers::toRhi(tiling.first),
1205 QSSGRhiHelpers::toRhi(tiling.second),
1206 QRhiSampler::Repeat });
1207 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1211 if (shaderPipeline->screenTexture()) {
1212 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
1213 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
1214 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
1215 QRhiTexture *screenTexture = shaderPipeline->screenTexture();
1217 if (screenMapPass) {
1218 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1220 const QRhiTexture::Flags flags = screenTexture->flags();
1222 screenTexture = rhiCtx->dummyTexture(flags, resourceUpdates);
1223 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1230 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
1231 ? QRhiSampler::Linear : QRhiSampler::None;
1232 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
1233 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
1234 if (screenTextureBinding >= 0) {
1235 bindings.addTexture(screenTextureBinding,
1236 QRhiShaderResourceBinding::FragmentStage,
1237 screenTexture, sampler);
1239 if (screenTextureArrayBinding >= 0) {
1240 bindings.addTexture(screenTextureArrayBinding,
1241 QRhiShaderResourceBinding::FragmentStage,
1242 screenTexture, sampler);
1247 if (shaderPipeline->lightmapTexture()) {
1248 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
1250 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1251 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
1252 bindings.addTexture(binding,
1253 QRhiShaderResourceBinding::FragmentStage,
1254 shaderPipeline->lightmapTexture(), sampler);
1260 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1263 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1265 if (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
1275 QRhiShaderResourceBindings *&srb = dcd.srb;
1276 bool srbChanged =
false;
1277 if (!srb || bindings != dcd.bindings) {
1278 srb = rhiCtxD->srb(bindings);
1279 rhiCtxD->releaseCachedSrb(dcd.bindings);
1280 dcd.bindings = bindings;
1284 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1285 subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx] = srb;
1287 subsetRenderable.rhiRenderData.mainPass.srb = srb;
1289 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
1292 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
1293 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
1296 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1297 subsetRenderable.rhiRenderData.reflectionPass.pipeline = dcd.pipeline;
1299 subsetRenderable.rhiRenderData.mainPass.pipeline = dcd.pipeline;
1301 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1302 subsetRenderable.rhiRenderData.reflectionPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1303 renderPassDescriptor,
1305 dcd.pipeline = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1307 subsetRenderable.rhiRenderData.mainPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1308 renderPassDescriptor,
1310 dcd.pipeline = subsetRenderable.rhiRenderData.mainPass.pipeline;
1312 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
1313 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;
1319 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1321 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
1322 const QSSGRenderCustomMaterial &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
1323 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1325 featureSet.set(QSSGShaderFeatures::Feature::LightProbe, inData.layer.lightProbe || material.m_iblProbe);
1327 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
1328 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
1330 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1332 featureSet.disableTonemapping();
1335 if (subsetRenderable.renderableFlags.rendersWithLightmap())
1336 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
1338 customMaterialSystem.rhiPrepareRenderable(ps, passKey, subsetRenderable, featureSet,
1339 material, inData, renderPassDescriptor, samples, viewCount, screenMapPass,
1340 alteredCamera, cubeFace, alteredModelViewProjection, entry, oit);
1343 case QSSGRenderableObject::Type::Particles:
1346 const auto &shaderPipeline = shadersForParticleMaterial(ps, particleRenderable, featureSet);
1347 if (shaderPipeline) {
1348 QSSGParticleRenderer::rhiPrepareRenderable(*shaderPipeline, passKey, rhiCtx, ps, particleRenderable, inData, renderPassDescriptor, samples, viewCount,
1349 alteredCamera, cubeFace, entry, oit);
1395 const QSSGRhiGraphicsPipelineState &state,
1397 bool *needsSetViewport,
1398 QSSGRenderTextureCubeFace cubeFace,
1399 qsizetype userPassIndex)
1401 switch (object.type) {
1402 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
1404 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1406 QSSG_ASSERT(QSSGUserRenderPassManager::maxUserPassSlots() == std::size(subsetRenderable.rhiRenderData.userPassData),
return);
1408 QRhiGraphicsPipeline *ps = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1409 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].pipeline : subsetRenderable.rhiRenderData.mainPass.pipeline;
1410 QRhiShaderResourceBindings *srb = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1411 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].srb : subsetRenderable.rhiRenderData.mainPass.srb;
1413 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1414 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
1415 ps = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1416 srb = subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx];
1422 QRhiBuffer *vertexBuffer = subsetRenderable.subset.rhi.vertexBuffer->buffer();
1423 QRhiBuffer *indexBuffer = subsetRenderable.subset.rhi.indexBuffer ? subsetRenderable.subset.rhi.indexBuffer->buffer() :
nullptr;
1425 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1427 cb->setGraphicsPipeline(ps);
1428 cb->setShaderResources(srb);
1430 if (*needsSetViewport) {
1431 cb->setViewport(state.viewport);
1432 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesScissor))
1433 cb->setScissor(state.scissor);
1434 *needsSetViewport =
false;
1437 QRhiCommandBuffer::VertexInput vertexBuffers[2];
1438 int vertexBufferCount = 1;
1439 vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0);
1440 quint32 instances = 1;
1441 if ( subsetRenderable.modelContext.model.instancing()) {
1442 instances = subsetRenderable.modelContext.model.instanceCount();
1449 vertexBuffers[1] = QRhiCommandBuffer::VertexInput(subsetRenderable.instanceBuffer, 0);
1450 vertexBufferCount = 2;
1452 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1453 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesStencilRef))
1454 cb->setStencilRef(state.stencilRef);
1456 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, subsetRenderable.subset.rhi.indexBuffer->indexFormat());
1457 cb->drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances, subsetRenderable.subset.lodOffset(subsetRenderable.subsetLevelOfDetail));
1458 QSSGRHICTX_STAT(rhiCtx, drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances));
1460 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1461 cb->draw(subsetRenderable.subset.count, instances, subsetRenderable.subset.offset);
1462 QSSGRHICTX_STAT(rhiCtx, draw(subsetRenderable.subset.count, instances));
1464 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (subsetRenderable.subset.count | quint64(instances) << 32),
1465 QVector<
int>({subsetRenderable.modelContext.model.profilingId,
1466 subsetRenderable.material.profilingId}));
1469 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1471 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1472 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1473 customMaterialSystem.rhiRenderRenderable(rhiCtx, subsetRenderable, needsSetViewport, cubeFace, state, userPassIndex);
1476 case QSSGRenderableObject::Type::Particles:
1479 QSSGParticleRenderer::rhiRenderRenderable(rhiCtx, renderable, needsSetViewport, cubeFace, state);
1504 QSSGPassKey passKey,
1505 QSSGRhiGraphicsPipelineState &ps,
1506 QSSGRenderShadowMap &shadowMapManager,
1507 const QSSGRenderCamera &camera,
1508 QSSGRenderCamera *debugCamera,
1510 const QSSGRenderableObjectList &sortedOpaqueObjects,
1512 const QSSGBounds3 &castingObjectsBox,
1513 const QSSGBounds3 &receivingObjectsBox)
1516 QSSGDebugDrawSystem *debugDrawSystem = renderer.contextInterface()->debugDrawSystem().get();
1517 const bool drawDirectionalLightShadowBoxes = layerData.layer.drawDirectionalLightShadowBoxes;
1518 const bool drawPointLightShadowBoxes = layerData.layer.drawPointLightShadowBoxes;
1519 const bool drawShadowCastingBounds = layerData.layer.drawShadowCastingBounds;
1520 const bool drawShadowReceivingBounds = layerData.layer.drawShadowReceivingBounds;
1521 const bool drawCascades = layerData.layer.drawCascades;
1522 const bool drawSceneCascadeIntersection = layerData.layer.drawSceneCascadeIntersection;
1523 const bool disableShadowCameraUpdate = layerData.layer.disableShadowCameraUpdate;
1524 const bool drawCulledObjects = layerData.layer.drawCulledObjects;
1525 QVector<
bool> debugIsObjectCulled = drawCulledObjects ? QVector<
bool>(sortedOpaqueObjects.size(),
true) : QVector<
bool>();
1527 static const auto rhiRenderOneShadowMap = [](QSSGRhiContext *rhiCtx,
1528 QSSGRhiGraphicsPipelineState *ps,
1529 const QSSGRenderableObjectList &sortedOpaqueObjects,
1531 const QSSGBounds3 cameraBounds,
1532 QVector<
bool> &debugIsObjectCulled,
1533 bool drawCulledObjects) {
1534 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1535 bool needsSetViewport =
true;
1537 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
1542 if (theObject->globalBoundsInstancing.isFinite() && theObject->globalBounds.isFinite()) {
1543 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
1544 : theObject->globalBounds;
1545 if (!globalBounds.isEmpty() && !cameraBounds.intersects(globalBounds)) {
1550 if (Q_UNLIKELY(drawCulledObjects))
1551 debugIsObjectCulled[i] =
false;
1554 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
1555 QSSGSubsetRenderable *renderable(
static_cast<QSSGSubsetRenderable *>(theObject));
1557 QRhiBuffer *vertexBuffer = renderable->subset.rhi.vertexBuffer->buffer();
1558 QRhiBuffer *indexBuffer = renderable->subset.rhi.indexBuffer
1559 ? renderable->subset.rhi.indexBuffer->buffer()
1563 if (!renderable->rhiRenderData.shadowPass.pipeline)
1566 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1568 cb->setGraphicsPipeline(renderable->rhiRenderData.shadowPass.pipeline);
1570 QRhiShaderResourceBindings *srb = renderable->rhiRenderData.shadowPass.srb[cubeFace];
1571 cb->setShaderResources(srb);
1573 if (needsSetViewport) {
1574 cb->setViewport(ps->viewport);
1575 needsSetViewport =
false;
1578 QRhiCommandBuffer::VertexInput vertexBuffers[2];
1579 int vertexBufferCount = 1;
1580 vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0);
1581 quint32 instances = 1;
1582 if (renderable->modelContext.model.instancing()) {
1583 instances = renderable->modelContext.model.instanceCount();
1584 vertexBuffers[1] = QRhiCommandBuffer::VertexInput(renderable->instanceBuffer, 0);
1585 vertexBufferCount = 2;
1588 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, renderable->subset.rhi.indexBuffer->indexFormat());
1589 cb->drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances, renderable->subset.lodOffset(renderable->subsetLevelOfDetail));
1590 QSSGRHICTX_STAT(rhiCtx, drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances));
1592 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1593 cb->draw(renderable->subset.count, instances, renderable->subset.offset);
1594 QSSGRHICTX_STAT(rhiCtx, draw(renderable->subset.count, instances));
1596 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (renderable->subset.count | quint64(instances) << 32),
1597 QVector<
int>({renderable->modelContext.model.profilingId,
1598 renderable->material.profilingId}));
1603 static const auto rhiClearShadowMap = [](
QSSGRenderer &renderer, QSSGRenderShadowMap &shadowMapManager, QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiRenderPassDescriptor *renderPassDesc) {
1604 auto clearShadowMapShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiClearShadowMapShader();
1605 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, clearShadowMapShaderPipeline.get());
1608 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled,
false);
1609 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled,
false);
1610 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, shadowMapManager.shadowClearSrb(), renderPassDesc, {});
1612 ps->flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
1615 QRhi *rhi = rhiCtx->rhi();
1616 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1620 QVector2D depthAdjust;
1621 if (rhi->isClipDepthZeroToOne()) {
1623 depthAdjust[0] = 0.0f;
1624 depthAdjust[1] = 1.0f;
1627 depthAdjust[0] = 1.0f;
1628 depthAdjust[1] = 0.5f;
1631 if (drawShadowCastingBounds)
1633 if (drawShadowReceivingBounds)
1637 const QSize atlasTextureSize = shadowMapManager.shadowMapAtlasTexture()->pixelSize();
1639 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
1640 for (
int i = 0, ie = globalLights.size(); i != ie; ++i) {
1641 if (!globalLights[i].shadows || globalLights[i].light->m_fullyBaked)
1648 const auto &light = globalLights[i].light;
1650 if (!shadowMapManager.shadowMapAtlasTexture())
1653 if (light->type == QSSGRenderLight::Type::DirectionalLight || light->type == QSSGRenderLight::Type::SpotLight) {
1654 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1658 if (!disableShadowCameraUpdate && debugCamera) {
1659 debugCamera->clipPlanes = camera.clipPlanes;
1660 debugCamera->projection = camera.projection;
1664 debugCamera->localTransform = layerData.getGlobalTransform(camera);
1667 QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> cascades;
1668 if (light->type == QSSGRenderLight::Type::DirectionalLight) {
1669 const float pcfRadius = light->m_softShadowQuality == QSSGRenderLight::SoftShadowQuality::Hard ? 0.f : light->m_pcfFactor;
1670 const float clipNear = camera.clipPlanes.clipNear();
1671 const float clipFar = qMin(light->m_shadowMapFar, camera.clipPlanes.clipFar());
1672 const float clipRange = clipFar - clipNear;
1673 cascades = setupCascadingCamerasForShadowMap(layerData,
1674 disableShadowCameraUpdate ? *debugCamera : camera,
1681 receivingObjectsBox,
1682 light->m_lockShadowmapTexels,
1685 drawSceneCascadeIntersection);
1688 pEntry->m_csmSplits[0] = clipNear + clipRange * (light->m_csmNumSplits > 0 ? light->m_csmSplit1 : 1.0f);
1689 pEntry->m_csmSplits[1] = clipNear + clipRange * (light->m_csmNumSplits > 1 ? light->m_csmSplit2 : 1.0f);
1690 pEntry->m_csmSplits[2] = clipNear + clipRange * (light->m_csmNumSplits > 2 ? light->m_csmSplit3 : 1.0f);
1691 pEntry->m_csmSplits[3] = clipNear + clipRange * 1.0f;
1692 pEntry->m_shadowMapFar = clipFar;
1693 }
else if (light->type == QSSGRenderLight::Type::SpotLight) {
1694 auto spotlightCamera =
std::make_unique<QSSGRenderCamera>(QSSGRenderCamera::Type::PerspectiveCamera);
1695 spotlightCamera->fov = QSSGRenderCamera::FieldOfView::fromDegrees(light->m_coneAngle * 2.0f);
1696 spotlightCamera->clipPlanes = { 1.0f, light->m_shadowMapFar };
1697 const QMatrix4x4 lightGlobalTransform = layerData.getGlobalTransform(*light);
1698 const QVector3D lightDir = QSSGRenderNode::getDirection(lightGlobalTransform);
1699 const QVector3D lightPos = QSSGRenderNode::getGlobalPos(lightGlobalTransform) - lightDir * spotlightCamera->clipPlanes.clipNear();
1700 const QVector3D lightPivot = light->pivot;
1701 const QVector3D forward = lightDir.normalized();
1702 const QVector3D right = qFuzzyCompare(qAbs(forward.y()), 1.0f)
1703 ? QVector3D::crossProduct(forward, QVector3D(1, 0, 0)).normalized()
1704 : QVector3D::crossProduct(forward, QVector3D(0, 1, 0)).normalized();
1705 const QVector3D up = QVector3D::crossProduct(right, forward).normalized();
1706 spotlightCamera->localTransform = QSSGRenderNode::calculateTransformMatrix(lightPos,
1707 QSSGRenderNode::initScale,
1709 QQuaternion::fromDirection(forward, up));
1710 QRectF theViewport(0.0f, 0.0f, (
float)light->m_shadowMapRes, (
float)light->m_shadowMapRes);
1711 QSSGRenderCamera::calculateProjectionInternal(*spotlightCamera, theViewport);
1712 cascades.push_back(
std::move(spotlightCamera));
1713 pEntry->m_shadowMapFar = light->m_shadowMapFar;
1718 memset(pEntry->m_csmActive, 0,
sizeof(pEntry->m_csmActive));
1720 QMatrix4x4 cascadeCameraGlobalTransforms(Qt::Uninitialized);
1721 const QMatrix4x4 bias = { 0.5, 0.0, 0.0, 0.5,
1724 0.0, 0.0, 0.0, 1.0 };
1726 for (
int cascadeIndex = 0; cascadeIndex < cascades.length(); cascadeIndex++) {
1727 const auto &cascadeCamera = cascades[cascadeIndex];
1731 cascadeCameraGlobalTransforms = layerData.getGlobalTransform(*cascadeCamera);
1732 pEntry->m_csmActive[cascadeIndex] = 1.f;
1733 QMatrix4x4 &viewProjection = pEntry->m_lightViewProjection[cascadeIndex];
1734 cascadeCamera->calculateViewProjectionMatrix(cascadeCameraGlobalTransforms, viewProjection);
1735 pEntry->m_lightViewProjection[cascadeIndex] = viewProjection;
1736 pEntry->m_fixedScaleBiasMatrix[cascadeIndex] = bias * viewProjection;
1737 const QMatrix4x4 inverted = viewProjection.inverted();
1738 const float x = 0.5f / (inverted * QVector4D(1, 0, 0, 0)).length();
1739 const float y = 0.5f / (inverted * QVector4D(0, 1, 0, 0)).length();
1740 const float z = 0.5f / (inverted * QVector4D(0, 0, 1, 0)).length();
1741 const QSSGBoxPoints frustumPoints = computeFrustumBounds(viewProjection);
1742 const QSSGBounds3 bounds = QSSGBounds3(frustumPoints);
1743 pEntry->m_dimensionsInverted[cascadeIndex] = QVector4D(x, y, z, 0.0f);
1744 pEntry->m_lightView = cascadeCameraGlobalTransforms.inverted();
1745 const bool isOrtho = cascadeCamera->type == QSSGRenderGraphObject::Type::OrthographicCamera;
1746 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[cascadeIndex], rhi->isYUpInFramebuffer());
1747 rhiPrepareResourcesForShadowMap(rhiCtx, layerData, passKey, pEntry, &ps, &depthAdjust, sortedOpaqueObjects, *cascadeCamera, isOrtho, QSSGRenderTextureCubeFaceNone, cascadeIndex);
1750 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[cascadeIndex];
1751 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1752 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1753 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1754 rhiClearShadowMap(renderer, shadowMapManager, rhiCtx, &ps, rt->renderPassDescriptor());
1755 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, 0, bounds, debugIsObjectCulled, drawCulledObjects);
1757 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1759 if (drawDirectionalLightShadowBoxes) {
1763 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"shadow_map"));
1765 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1766 ps.viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
1768 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1769 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1770 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1771 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1772 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1773 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
1774 const float shadowMapFar = qMax<
float>(2.0f, light->m_shadowMapFar);
1775 setupCubeShadowCameras(layerData, light, shadowMapFar, theCameras);
1776 pEntry->m_lightView = QMatrix4x4();
1777 pEntry->m_shadowMapFar = shadowMapFar;
1779 const bool swapYFaces = !rhi->isYUpInFramebuffer();
1780 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
1781 for (
const auto face : QSSGRenderTextureCubeFaces) {
1782 cameraGlobalTransform = layerData.getGlobalTransform(theCameras[quint8(face)]);
1783 theCameras[quint8(face)].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_lightViewProjection[0]);
1784 pEntry->m_lightCubeView[quint8(face)] = cameraGlobalTransform.inverted();
1786 rhiPrepareResourcesForShadowMap(rhiCtx,
1792 sortedOpaqueObjects,
1793 theCameras[quint8(face)],
1800 const QVector3D center = QSSGRenderNode::getGlobalPos(layerData.getGlobalTransform(*light));
1801 const QSSGBounds3 bounds = QSSGBounds3(center - QVector3D(shadowMapFar, shadowMapFar, shadowMapFar),
1802 center + QVector3D(shadowMapFar, shadowMapFar, shadowMapFar));
1804 for (
const auto face : QSSGRenderTextureCubeFaces) {
1808 QSSGRenderTextureCubeFace outFace = face;
1826 if (outFace == QSSGRenderTextureCubeFace::PosY)
1827 outFace = QSSGRenderTextureCubeFace::NegY;
1828 else if (outFace == QSSGRenderTextureCubeFace::NegY)
1829 outFace = QSSGRenderTextureCubeFace::PosY;
1831 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargetCube[quint8(outFace)];
1832 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1833 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1834 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1835 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, quint8(face), bounds, debugIsObjectCulled, drawCulledObjects);
1837 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1838 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_cube", 0, outFace));
1845 QRhiTextureRenderTarget *rtFront = pEntry->m_rhiRenderTargets[0];
1846 QRhiRenderPassDescriptor *frontDesc = pEntry->m_rhiRenderPassDesc[0];
1847 auto atlasShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiCubeMapToAtlasShader();
1848 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[0], rhi->isYUpInFramebuffer());
1849 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, atlasShaderPipeline.get());
1850 QRhiShaderResourceBindings *srb = pEntry->m_cubeToAtlasFrontSrb;
1851 cb->beginPass(rtFront, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1852 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtFront));
1853 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1855 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, frontDesc, QSSGRhiQuadRenderer::UvCoords);
1858 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1859 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 6, 0));
1862 QRhiTextureRenderTarget *rtBack = pEntry->m_rhiRenderTargets[1];
1863 QRhiRenderPassDescriptor *backDesc = pEntry->m_rhiRenderPassDesc[1];
1864 srb = pEntry->m_cubeToAtlasBackSrb;
1865 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[1], rhi->isYUpInFramebuffer());
1866 cb->beginPass(rtBack, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1867 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtBack));
1868 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1870 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, backDesc, QSSGRhiQuadRenderer::UvCoords);
1873 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1874 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 7, 0));
1878 ps = layerData.getPipelineState();
1879 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
1881 ps.slopeScaledDepthBias = 1.5f;
1883 if (drawPointLightShadowBoxes) {
1889 if (Q_UNLIKELY(drawCulledObjects)) {
1890 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
1892 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
1893 : theObject->globalBounds;
1894 const QColor color = debugIsObjectCulled[i] ? QColorConstants::Red : QColorConstants::Green;
1901 QSSGPassKey passKey,
1903 QSSGRhiGraphicsPipelineState *ps,
1904 QSSGRenderReflectionMap &reflectionMapManager,
1905 const std::vector<QSSGRenderReflectionProbe *> &reflectionProbes,
1906 const QSSGRenderableObjectList &reflectionPassObjects,
1909 QSSGRhiContext *rhiCtx = context.rhiContext().get();
1910 QRhi *rhi = rhiCtx->rhi();
1911 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1913 const bool renderSkybox = (inData.layer.background == QSSGRenderLayer::Background::SkyBox
1914 || inData.layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap
1915 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial)
1916 && rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch);
1918 for (size_t i = 0, ie = reflectionProbes.size(); i != ie; ++i) {
1919 QSSGReflectionMapEntry *pEntry = reflectionMapManager.reflectionMapEntry(
int(i));
1923 if (!pEntry->m_needsRender)
1926 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame && pEntry->m_rendered)
1929 if (reflectionProbes[i]->texture)
1932 Q_ASSERT(pEntry->m_rhiDepthStencil);
1933 Q_ASSERT(pEntry->m_rhiCube);
1935 const QSize size = pEntry->m_rhiCube->pixelSize();
1936 ps->viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
1938 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1939 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1940 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1941 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1942 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1943 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
1944 setupCubeReflectionCameras(inData, reflectionProbes[i], theCameras);
1945 const bool swapYFaces = !rhi->isYUpInFramebuffer();
1946 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
1947 for (
const auto face : QSSGRenderTextureCubeFaces) {
1948 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face);
1949 cameraGlobalTransform = inData.getGlobalTransform(theCameras[cubeFaceIdx]);
1950 theCameras[cubeFaceIdx].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_viewProjection);
1952 rhiPrepareResourcesForReflectionMap(context, passKey, inData, pEntry, ps, reflectionPassObjects, theCameras[cubeFaceIdx], renderer, face);
1954 QRhiRenderPassDescriptor *renderPassDesc =
nullptr;
1955 for (
auto face : QSSGRenderTextureCubeFaces) {
1956 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
1957 face = pEntry->m_timeSliceFace;
1959 QSSGRenderTextureCubeFace outFace = face;
1963 if (outFace == QSSGRenderTextureCubeFace::PosY)
1964 outFace = QSSGRenderTextureCubeFace::NegY;
1965 else if (outFace == QSSGRenderTextureCubeFace::NegY)
1966 outFace = QSSGRenderTextureCubeFace::PosY;
1968 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[quint8(outFace)];
1969 cb->beginPass(rt, reflectionProbes[i]->clearColor, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1970 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1971 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1973 if (renderSkybox && pEntry->m_skyBoxSrbs[quint8(face)]) {
1974 const auto &shaderCache = renderer.contextInterface()->shaderCache();
1975 const bool useSkyBoxQuad = inData.layer.background == QSSGRenderLayer::Background::SkyBox
1976 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial;
1977 const auto &shaderPipeline = useSkyBoxQuad
1978 ? shaderCache->getBuiltInRhiShaders().getRhiSkyBoxShader(QSSGRenderLayer::TonemapMode::None,
1979 inData.layer.skyBoxIsRgbe8,
1981 : shaderCache->getBuiltInRhiShaders().getRhiSkyBoxCubeShader(QSSGRenderLayer::TonemapMode::None,
1982 !inData.layer.skyBoxIsSrgb,
1984 Q_ASSERT(shaderPipeline);
1985 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
1986 QRhiShaderResourceBindings *srb = pEntry->m_skyBoxSrbs[quint8(face)];
1987 if (!renderPassDesc)
1988 renderPassDesc = rt->newCompatibleRenderPassDescriptor();
1989 rt->setRenderPassDescriptor(renderPassDesc);
1990 useSkyBoxQuad ? renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, srb, renderPassDesc, {})
1991 : renderer.rhiCubeRenderer()->recordRenderCube(rhiCtx, ps, srb, renderPassDesc, {});
1994 bool needsSetViewport =
true;
1995 for (
const auto &handle : reflectionPassObjects)
1996 rhiRenderRenderable(rhiCtx, *ps, *handle.obj, &needsSetViewport, face);
1999 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2000 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"reflection_cube", 0, outFace));
2002 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
2006 renderPassDesc->deleteLater();
2008 pEntry->renderMips(rhiCtx);
2010 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
2011 pEntry->m_timeSliceFace = QSSGBaseTypeHelpers::next(pEntry->m_timeSliceFace);
2013 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame)
2014 pEntry->m_rendered =
true;
2016 reflectionProbes[i]->hasScheduledUpdate =
false;
2017 pEntry->m_needsRender =
false;
2068 QSSGPassKey passKey,
2070 QSSGRhiShaderPipeline &shaderPipeline,
2071 QSSGRhiGraphicsPipelineState &ps,
2072 const QSSGAmbientOcclusionSettings &ao,
2073 const QSSGRhiRenderableTexture &rhiAoTexture,
2074 const QSSGRhiRenderableTexture &rhiDepthTexture,
2075 const QSSGRenderCamera &camera)
2077 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2080 if (!rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch)) {
2081 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
2083 cb->beginPass(rhiAoTexture.rt, Qt::white, { 1.0f, 0 });
2084 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rhiAoTexture.rt));
2086 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2090 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, &shaderPipeline);
2092 const float R2 = ao.aoDistance * ao.aoDistance * 0.16f;
2093 const QSize textureSize = rhiAoTexture.texture->pixelSize();
2094 const float rw =
float(textureSize.width());
2095 const float rh =
float(textureSize.height());
2096 const float fov = camera.fov.asVerticalFov(rw / rh).radians();
2097 const float tanHalfFovY = tanf(0.5f * fov * (rh / rw));
2098 const float invFocalLenX = tanHalfFovY * (rw / rh);
2100 const QVector4D aoProps(ao.aoStrength * 0.01f, ao.aoDistance * 0.4f, ao.aoSoftness * 0.02f, ao.aoBias);
2101 const QVector4D aoProps2(
float(ao.aoSamplerate), (ao.aoDither) ? 1.0f : 0.0f, 0.0f, 0.0f);
2102 const QVector4D aoScreenConst(1.0f / R2, rh / (2.0f * tanHalfFovY), 1.0f / rw, 1.0f / rh);
2103 const QVector4D uvToEyeConst(2.0f * invFocalLenX, -2.0f * tanHalfFovY, -invFocalLenX, tanHalfFovY);
2104 const QVector2D cameraProps = camera.clipPlanes;
2113 const int UBUF_SIZE = 72;
2114 QSSGRhiDrawCallData &dcd(rhiCtxD->drawCallData({ passKey,
nullptr,
nullptr,
nullptr, 0 }));
2116 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SIZE);
2120 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2121 memcpy(ubufData, &aoProps, 16);
2122 memcpy(ubufData + 16, &aoProps2, 16);
2123 memcpy(ubufData + 32, &aoScreenConst, 16);
2124 memcpy(ubufData + 48, &uvToEyeConst, 16);
2125 memcpy(ubufData + 64, &cameraProps, 8);
2126 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2128 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2129 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
2130 QSSGRhiShaderResourceBindingList bindings;
2131 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2134 bindings.addTexture(1, QRhiShaderResourceBinding::FragmentStage, rhiDepthTexture.texture, sampler);
2135 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
2137 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2138 renderer.rhiQuadRenderer()->recordRenderQuadPass(rhiCtx, &ps, srb, rhiAoTexture.rt, {});
2286 QSSGPassKey passKey,
2287 QSSGRenderLayer &layer,
2288 QSSGRenderCameraList &cameras,
2292 uint tonemapMode = 0)
2294 QSSG_ASSERT(layer.renderData,
return);
2296 const auto *renderData = layer.renderData;
2298 auto rhiCtx = context.rhiContext().get();
2300 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2302 QSSGRenderImageTexture lightProbeTexture;
2303 switch (layer.background) {
2304 case QSSGRenderLayer::Background::SkyBox:
2305 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.lightProbe,
2306 QSSGBufferManager::MipModeBsdf);
2308 case QSSGRenderLayer::Background::SkyBoxCubeMap:
2309 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.skyBoxCubeMap,
2310 QSSGBufferManager::MipModeDisable);
2312 case QSSGRenderLayer::Background::SkyMaterial:
2313 lightProbeTexture = renderData->skyMaterialTexture;
2316 Q_UNREACHABLE_RETURN();
2319 if (lightProbeTexture.m_texture ==
nullptr) {
2322 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2325 for (QRhiShaderResourceBindings *&srb : entry->m_skyBoxSrbs)
2328 layer.skyBoxSrb =
nullptr;
2333 const bool cubeMapMode = layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap;
2335 if (cubeFace == QSSGRenderTextureCubeFaceNone)
2336 layer.skyBoxIsRgbe8 = lightProbeTexture.m_flags.isRgbe8();
2338 layer.skyBoxIsSrgb = !lightProbeTexture.m_flags.isLinear();
2340 QSSGRhiShaderResourceBindingList bindings;
2342 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
2343 QRhiSampler::Linear,
2344 cubeMapMode ? QRhiSampler::None : QRhiSampler::Linear,
2345 QRhiSampler::Repeat,
2346 QRhiSampler::ClampToEdge,
2347 QRhiSampler::Repeat });
2348 int samplerBinding = 1;
2349 bindings.addTexture(samplerBinding, QRhiShaderResourceBinding::FragmentStage, lightProbeTexture.m_texture, sampler);
2351 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2352 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * cubeFaceIdx;
2353 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey,
nullptr,
nullptr, entry, entryIdx });
2355 QRhi *rhi = rhiCtx->rhi();
2356 const quint32 ubufSize = cameras.count() >= 2 ? 416 : 240;
2357 if (!dcd.ubuf || dcd.ubuf->size() != ubufSize) {
2359 dcd.ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
int(ubufSize));
2363 float adjustY = rhi->isYUpInNDC() ? 1.0f : -1.0f;
2364 const float exposure = layer.lightProbeSettings.probeExposure;
2366 const QMatrix3x3 &rotationMatrix(layer.lightProbeSettings.probeOrientation);
2369 const float blurAmountOrSrgb = cubeMapMode ? layer.skyBoxIsSrgb : layer.skyboxBlurAmount;
2370 const float maxMipLevelOrTonemapMode = cubeMapMode ?
float(tonemapMode) :
float(lightProbeTexture.m_mipmapCount - 2);
2372 const QVector4D skyboxProperties = {
2376 maxMipLevelOrTonemapMode
2379 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2380 quint32 ubufOffset = 0;
2382 memcpy(ubufData + ubufOffset, &skyboxProperties, 16);
2385 memcpy(ubufData + ubufOffset, rotationMatrix.constData(), 12);
2387 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 12, 12);
2389 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 24, 12);
2392 for (qsizetype viewIdx = 0; viewIdx < cameras.count(); ++viewIdx) {
2393 const QMatrix4x4 &inverseProjection = cameras[viewIdx]->projection.inverted();
2394 const QMatrix4x4 &viewMatrix = renderData->getGlobalTransform(*cameras[viewIdx]);
2395 QMatrix4x4 viewProjection(Qt::Uninitialized);
2396 cameras[viewIdx]->calculateViewProjectionWithoutTranslation(viewMatrix, 0.1f, 5.0f, viewProjection);
2398 quint32 viewDataOffset = ubufOffset;
2399 memcpy(ubufData + viewDataOffset + viewIdx * 64, viewProjection.constData(), 64);
2400 viewDataOffset += cameras.count() * 64;
2401 memcpy(ubufData + viewDataOffset + viewIdx * 64, inverseProjection.constData(), 64);
2402 viewDataOffset += cameras.count() * 64;
2403 memcpy(ubufData + viewDataOffset + viewIdx * 48, viewMatrix.constData(), 48);
2405 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2407 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2409 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2410 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2411 entry->m_skyBoxSrbs[cubeFaceIdx] = rhiCtxD->srb(bindings);
2413 layer.skyBoxSrb = rhiCtxD->srb(bindings);
2417 renderer.rhiCubeRenderer()->prepareCube(rhiCtx,
nullptr);
2419 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2458 QSSGPassKey passKey,
2459 const QSSGRhiGraphicsPipelineState &basePipelineState,
2460 QRhiRenderPassDescriptor *rpDesc,
2462 const QSSGRenderableObjectList &sortedOpaqueObjects,
2463 const QSSGRenderableObjectList &sortedTransparentObjects,
2467 static const auto rhiPrepareDepthPassForObject = [](QSSGRhiContext *rhiCtx,
2468 QSSGPassKey passKey,
2471 QRhiRenderPassDescriptor *rpDesc,
2472 QSSGRhiGraphicsPipelineState *ps) {
2473 QSSGRhiShaderPipelinePtr shaderPipeline;
2474 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2476 const bool isOpaqueDepthPrePass = obj->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
2477 QSSGShaderFeatures featureSet;
2478 featureSet.set(QSSGShaderFeatures::Feature::DepthPass,
true);
2479 if (isOpaqueDepthPrePass)
2480 featureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
2482 QSSGRhiDrawCallData *dcd =
nullptr;
2483 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2484 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2485 const void *modelNode = &subsetRenderable.modelContext.model;
2486 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, 0 });
2489 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2490 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2491 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2492 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2494 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
2495 if (shaderPipeline) {
2496 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2497 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2498 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2499 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2503 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2504 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2506 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2508 ps->cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2510 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
2511 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2513 if (shaderPipeline) {
2514 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2515 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2516 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, customMaterial, subsetRenderable,
2517 inData.renderedCameras,
nullptr,
nullptr);
2518 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2525 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2526 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2527 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
2528 ia = subsetRenderable.subset.rhi.ia;
2530 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
2531 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
2532 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
2534 QSSGRhiShaderResourceBindingList bindings;
2535 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
2538 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
2540 if (isOpaqueDepthPrePass) {
2541 addOpaqueDepthPrePassBindings(rhiCtx,
2542 shaderPipeline.get(),
2543 subsetRenderable.firstImage,
2545 (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
2551 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
2552 if (normalTextureBinding >= 0) {
2553 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2554 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
2555 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
2556 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
2557 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
2558 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
2562 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
2563 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
2565 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2566 QRhiSampler::Nearest,
2568 QRhiSampler::ClampToEdge,
2569 QRhiSampler::ClampToEdge,
2572 bindings.addTexture(binding,
2573 QRhiShaderResourceBinding::VertexStage,
2580 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
2581 if (targetsTexture) {
2582 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
2584 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2585 QRhiSampler::Nearest,
2587 QRhiSampler::ClampToEdge,
2588 QRhiSampler::ClampToEdge,
2589 QRhiSampler::ClampToEdge
2591 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
2595 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
2597 subsetRenderable.rhiRenderData.depthPrePass.pipeline = rhiCtxD->pipeline(*ps,
2600 subsetRenderable.rhiRenderData.depthPrePass.srb = srb;
2611 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2616 ps.samples = samples;
2617 ps.viewCount = viewCount;
2618 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
2619 ps.targetBlend[0].colorWrite = {};
2621 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2622 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2626 for (
const QSSGRenderableObjectHandle &handle : sortedTransparentObjects) {
2627 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2754 QSSGPassKey passKey,
2755 const QSSGRhiGraphicsPipelineState &basePipelineState,
2756 QRhiRenderPassDescriptor *rpDesc,
2758 const QSSGRenderableObjectList &sortedOpaqueObjects)
2760 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2761 ps.depthFunc = QRhiGraphicsPipeline::LessOrEqual;
2762 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::BlendEnabled,
false);
2764 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2765 QSSGRenderableObject *obj = handle.obj;
2766 QSSGRhiShaderPipelinePtr shaderPipeline;
2767 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2769 QSSGShaderFeatures featureSet;
2770 featureSet.set(QSSGShaderFeatures::Feature::NormalPass,
true);
2772 QSSGRhiDrawCallData *dcd =
nullptr;
2773 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2774 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2775 const void *modelNode = &subsetRenderable.modelContext.model;
2776 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, 0 });
2779 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2780 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2781 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2782 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2784 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
2785 if (shaderPipeline) {
2786 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2787 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2788 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2789 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2791 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2792 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2794 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2796 ps.cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2798 const auto &customMaterialSystem = subsetRenderable.renderer->contextInterface()->customMaterialSystem();
2799 shaderPipeline = customMaterialSystem->shadersForCustomMaterial(&ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2801 if (shaderPipeline) {
2802 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2803 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2804 customMaterialSystem->updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, customMaterial, subsetRenderable,
2805 inData.renderedCameras,
nullptr,
nullptr);
2806 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2811 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2812 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2813 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
2814 ia = subsetRenderable.subset.rhi.ia;
2816 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
2817 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
2818 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
2820 QSSGRhiShaderResourceBindingList bindings;
2821 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
2824 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
2825 while (renderableImage) {
2826 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
2827 const int samplerHint =
int(renderableImage->m_mapType);
2828 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
2829 if (samplerBinding >= 0) {
2830 QRhiTexture *texture = renderableImage->m_texture.m_texture;
2831 if (samplerBinding >= 0 && texture) {
2832 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
2833 QSSGRhiSamplerDescription samplerDesc = {
2834 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
2835 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
2836 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
2837 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
2838 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
2839 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
2841 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
2842 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
2843 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
2846 renderableImage = renderableImage->m_nextImage;
2849 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
2852 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
2853 if (normalTextureBinding >= 0) {
2854 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2855 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
2856 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
2857 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
2858 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
2859 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
2865 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
2866 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
2868 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2869 QRhiSampler::Nearest,
2871 QRhiSampler::ClampToEdge,
2872 QRhiSampler::ClampToEdge,
2875 bindings.addTexture(binding,
2876 QRhiShaderResourceBinding::VertexStage,
2883 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
2884 if (targetsTexture) {
2885 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
2887 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2888 QRhiSampler::Nearest,
2890 QRhiSampler::ClampToEdge,
2891 QRhiSampler::ClampToEdge,
2892 QRhiSampler::ClampToEdge
2894 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
2898 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
2900 subsetRenderable.rhiRenderData.normalPass.pipeline = rhiCtxD->pipeline(ps,
2903 subsetRenderable.rhiRenderData.normalPass.srb = srb;
2972 QSSGPassKey passKey,
2973 const QSSGRhiGraphicsPipelineState &basePipelineState,
2974 QRhiRenderPassDescriptor *rpDesc,
2975 QSSGRenderGraphObject *overrideMaterial,
2977 QSSGRenderableObjectList &inObjects,
2978 QSSGShaderFeatures featureSet)
2981 const qsizetype index = overrideMaterial ? inData.getUserRenderPassManager()->acquireUserPassSlot() : -1;
2986 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2987 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2989 const bool isCustomMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::CustomMaterial);
2990 const bool isDefaultMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
2991 overrideMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
2992 overrideMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial);
2994 if (!isCustomMaterial && !isDefaultMaterial) {
2995 qDebug() <<
"Override material must be a default or custom material.";
2999 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3000 QSSGRenderableObject *obj = handle.obj;
3002 if (obj->type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
3003 obj->type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
3006 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3007 const void *modelNode = &subsetRenderable.modelContext.model;
3009 QSSGRhiDrawCallData *dcd = &rhiCtxD->drawCallData({ passKey, modelNode, overrideMaterial,
nullptr,
3016 QSSGRenderableObjectFlags renderableFlags = subsetRenderable.renderableFlags;
3017 float opacity = subsetRenderable.opacity;
3020 const bool hasAnyLights = !subsetRenderable.lights.isEmpty();
3021 const bool anyLightHasShadows = std::any_of(subsetRenderable.lights.begin(),
3022 subsetRenderable.lights.end(),
3023 [](
const QSSGShaderLight &light) {
return light.shadows; });
3027 auto layerPrepFlags = inData.layerPrepResult.getFlags();
3029 if (isCustomMaterial) {
3030 auto &material =
static_cast<QSSGRenderCustomMaterial &>(*overrideMaterial);
3031 auto prepResult = inData.prepareCustomMaterialForRender(material, renderableFlags, opacity,
3032 false, hasAnyLights, anyLightHasShadows,
3034 subsetRenderable.shaderDescription = prepResult.materialKey;
3036 auto &material =
static_cast<QSSGRenderDefaultMaterial &>(*overrideMaterial);
3037 auto prepResult = inData.prepareDefaultMaterialForRender(material, renderableFlags, opacity,
3038 hasAnyLights, anyLightHasShadows,
3040 subsetRenderable.shaderDescription = prepResult.materialKey;
3044 QSSGRhiShaderPipelinePtr shaderPipeline;
3046 if (isCustomMaterial) {
3047 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(*overrideMaterial);
3048 if (!ps.userSetCullMode)
3049 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3051 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3052 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3053 inData.getDefaultMaterialPropertyTable(), featureSet);
3054 if (shaderPipeline) {
3055 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3056 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3057 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3058 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3059 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3062 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(*overrideMaterial);
3063 if (!ps.userSetCullMode)
3064 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3066 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3067 if (shaderPipeline) {
3068 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3069 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3070 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3071 inData.renderedCameras,
nullptr,
nullptr, overrideMaterial);
3072 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3076 if (!shaderPipeline)
3079 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3080 ia = subsetRenderable.subset.rhi.ia;
3082 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3083 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3084 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3086 QSSGRhiShaderResourceBindingList bindings;
3087 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3090 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3091 while (renderableImage) {
3092 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3093 const int samplerHint =
int(renderableImage->m_mapType);
3094 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3095 if (samplerBinding >= 0) {
3096 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3098 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3099 QSSGRhiSamplerDescription samplerDesc = {
3100 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3101 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3102 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3103 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3104 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3105 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3107 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3108 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3109 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3112 renderableImage = renderableImage->m_nextImage;
3115 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3118 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3119 if (normalTextureBinding >= 0) {
3120 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3121 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3122 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3123 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3124 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3125 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3129 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3130 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3132 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3133 QRhiSampler::Nearest,
3135 QRhiSampler::ClampToEdge,
3136 QRhiSampler::ClampToEdge,
3139 bindings.addTexture(binding,
3140 QRhiShaderResourceBinding::VertexStage,
3147 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3148 if (targetsTexture) {
3149 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3151 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3152 QRhiSampler::Nearest,
3154 QRhiSampler::ClampToEdge,
3155 QRhiSampler::ClampToEdge,
3156 QRhiSampler::ClampToEdge
3158 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3163 if (shaderPipeline->isLightingEnabled()) {
3164 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3165 shaderPipeline->ub0LightDataOffset(),
3166 sizeof(QSSGShaderLightsUniformData));
3167 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3168 shaderPipeline->ub0DirectionalLightDataOffset(),
3169 sizeof(QSSGShaderDirectionalLightsUniformData));
3172 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3173 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3176 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3177 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3178 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3181 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3182 QRhiSampler::Linear,
3184 QRhiSampler::ClampToEdge,
3185 QRhiSampler::ClampToEdge,
3186 QRhiSampler::Repeat });
3188 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3192 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3193 QRhiTexture *texture = shaderPipeline->shadowMapBlueNoiseTexture();
3196 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3197 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3198 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3201 QRhiSampler *sampler = rhiCtx->sampler(
3202 { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3204 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3208 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3210 auto texture = shaderPipeline->lightProbeTexture();
3212 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3213 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3214 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3218 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3219 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3220 QRhiSampler::Linear,
3221 QRhiSampler::Linear,
3222 QSSGRhiHelpers::toRhi(tiling.first),
3223 QSSGRhiHelpers::toRhi(tiling.second),
3224 QRhiSampler::Repeat });
3225 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3229 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3231 QSSG_ASSERT(srb,
continue);
3232 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3233 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3234 rhiPassData.srb = srb;
3241 QSSGPassKey passKey,
3242 const QSSGRhiGraphicsPipelineState &basePipelineState,
3243 QRhiRenderPassDescriptor *rpDesc,
3245 QSSGRenderableObjectList &inObjects,
3246 QSSGShaderFeatures featureSet)
3248 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3253 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3254 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3256 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3257 QSSGRenderableObject *obj = handle.obj;
3258 QSSGRhiShaderPipelinePtr shaderPipeline;
3260 QSSGRhiDrawCallData *dcd =
nullptr;
3261 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3262 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3263 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3264 const void *modelNode = &subsetRenderable.modelContext.model;
3266 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, quintptr(index) });
3269 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3270 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3271 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3272 if (!ps.userSetCullMode)
3273 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3275 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3276 if (shaderPipeline) {
3277 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3278 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3279 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3280 inData.renderedCameras,
nullptr,
nullptr);
3281 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3283 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3284 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3285 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3286 if (!ps.userSetCullMode)
3287 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3289 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3290 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3291 inData.getDefaultMaterialPropertyTable(), featureSet);
3292 if (shaderPipeline) {
3293 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3294 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3295 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3296 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3297 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3301 if (!shaderPipeline)
3305 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3306 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3307 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3308 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3309 ia = subsetRenderable.subset.rhi.ia;
3311 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3312 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3313 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3315 QSSGRhiShaderResourceBindingList bindings;
3316 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3319 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3320 while (renderableImage) {
3321 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3322 const int samplerHint =
int(renderableImage->m_mapType);
3323 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3324 if (samplerBinding >= 0) {
3325 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3326 if (samplerBinding >= 0 && texture) {
3327 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3328 QSSGRhiSamplerDescription samplerDesc = {
3329 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3330 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3331 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3332 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3333 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3334 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3336 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3337 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3338 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3341 renderableImage = renderableImage->m_nextImage;
3344 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3347 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3348 if (normalTextureBinding >= 0) {
3349 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3350 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3351 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3352 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3353 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3354 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3358 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3359 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3361 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3362 QRhiSampler::Nearest,
3364 QRhiSampler::ClampToEdge,
3365 QRhiSampler::ClampToEdge,
3368 bindings.addTexture(binding,
3369 QRhiShaderResourceBinding::VertexStage,
3376 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3377 if (targetsTexture) {
3378 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3380 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3381 QRhiSampler::Nearest,
3383 QRhiSampler::ClampToEdge,
3384 QRhiSampler::ClampToEdge,
3385 QRhiSampler::ClampToEdge
3387 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3392 if (shaderPipeline->isLightingEnabled()) {
3393 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3394 shaderPipeline->ub0LightDataOffset(),
3395 sizeof(QSSGShaderLightsUniformData));
3396 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3397 shaderPipeline->ub0DirectionalLightDataOffset(),
3398 sizeof(QSSGShaderDirectionalLightsUniformData));
3401 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3402 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3405 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3406 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3407 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3410 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3411 QRhiSampler::Linear,
3413 QRhiSampler::ClampToEdge,
3414 QRhiSampler::ClampToEdge,
3415 QRhiSampler::Repeat });
3417 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3421 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3422 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
3423 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
3425 QRhiTexture *texture = shadowMapBlueNoise;
3426 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3427 QRhiSampler::Linear,
3429 QRhiSampler::Repeat,
3430 QRhiSampler::Repeat,
3431 QRhiSampler::Repeat });
3432 Q_ASSERT(texture && sampler);
3433 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3436 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3437 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
3438 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3439 QRhiSampler::Linear,
3441 QRhiSampler::Repeat,
3442 QRhiSampler::Repeat,
3443 QRhiSampler::Repeat });
3444 Q_ASSERT(texture && sampler);
3445 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3446 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3451 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
3452 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
3453 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
3454 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
3455 ? QRhiSampler::Linear
3456 : QRhiSampler::None;
3457 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3458 QRhiSampler::Linear,
3460 QRhiSampler::ClampToEdge,
3461 QRhiSampler::ClampToEdge,
3462 QRhiSampler::Repeat });
3463 if (reflectionSampler >= 0 && reflectionTexture)
3464 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
3467 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3469 auto texture = shaderPipeline->lightProbeTexture();
3471 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3472 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3473 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3477 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3478 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3479 QRhiSampler::Linear,
3480 QRhiSampler::Linear,
3481 QSSGRhiHelpers::toRhi(tiling.first),
3482 QSSGRhiHelpers::toRhi(tiling.second),
3483 QRhiSampler::Repeat });
3484 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3488 if (shaderPipeline->screenTexture()) {
3489 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
3490 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
3491 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
3492 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
3493 ? QRhiSampler::Linear : QRhiSampler::None;
3494 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
3495 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3496 if (screenTextureBinding >= 0) {
3497 bindings.addTexture(screenTextureBinding,
3498 QRhiShaderResourceBinding::FragmentStage,
3499 shaderPipeline->screenTexture(), sampler);
3501 if (screenTextureArrayBinding >= 0) {
3502 bindings.addTexture(screenTextureArrayBinding,
3503 QRhiShaderResourceBinding::FragmentStage,
3504 shaderPipeline->screenTexture(), sampler);
3509 if (shaderPipeline->lightmapTexture()) {
3510 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
3512 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
3513 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3514 bindings.addTexture(binding,
3515 QRhiShaderResourceBinding::FragmentStage,
3516 shaderPipeline->lightmapTexture(), sampler);
3523 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3524 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3526 int maxSamplerBinding = -1;
3527 QVector<QShaderDescription::InOutVariable> samplerVars =
3528 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3529 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3530 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3531 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3532 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3533 if (it == samplerVars.cend())
3534 samplerVars.append(var);
3537 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3538 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3540 if (maxSamplerBinding >= 0) {
3541 int extraTexCount = shaderPipeline->extraTextureCount();
3542 for (
int i = 0; i < extraTexCount; ++i) {
3543 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
3544 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
3545 if (samplerBinding >= 0) {
3546 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
3547 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
3548 bindings.addTexture(samplerBinding,
3549 QRhiShaderResourceBinding::FragmentStage,
3557 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3559 QSSG_ASSERT(srb,
continue);
3561 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3562 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3563 rhiPassData.srb = srb;
3572 QSSGPassKey passKey,
3573 const QSSGRhiGraphicsPipelineState &basePipelineState,
3574 QRhiRenderPassDescriptor *rpDesc,
3577 QSSGRenderableObjectList &inObjects,
3578 QSSGShaderFeatures featureSet)
3580 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3586 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3587 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3588 QSSGRenderableObject *obj = handle.obj;
3589 QSSGRhiShaderPipelinePtr shaderPipeline;
3590 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3591 const auto &bufferManager = inData.contextInterface()->bufferManager();
3593 featureSet.set(QSSGShaderFeatures::Feature::UserRenderPass,
true);
3595 QSSGRhiDrawCallData *dcd =
nullptr;
3596 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3597 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3598 const void *modelNode = &subsetRenderable.modelContext.model;
3600 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material,
nullptr, quintptr(index) });
3603 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3604 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3605 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3606 if (!ps.userSetCullMode)
3607 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3609 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet, shaderAugmentation);
3610 if (shaderPipeline) {
3611 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3612 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3614 for (
const auto &u : shaderAugmentation.propertyUniforms)
3615 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3616 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3617 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3619 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3620 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3621 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3622 if (!ps.userSetCullMode)
3623 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3625 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3627 if (material.m_shadingMode == QSSGRenderCustomMaterial::ShadingMode::Unshaded)
3628 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
3630 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet, shaderAugmentation);
3632 if (shaderPipeline) {
3633 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3634 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3636 for (
const auto &u : shaderAugmentation.propertyUniforms)
3637 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3638 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3639 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3640 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3644 if (!shaderPipeline) {
3646 qDebug() <<
"Failed to prepare user augmented pass for object.";
3651 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3652 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3653 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3654 ia = subsetRenderable.subset.rhi.ia;
3656 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3657 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3658 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3660 QSSGRhiShaderResourceBindingList bindings;
3661 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3668 QVector<QShaderDescription::InOutVariable> samplerVars =
3669 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3670 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3671 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3672 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3673 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3674 if (it == samplerVars.cend())
3675 samplerVars.append(var);
3678 int maxSamplerBinding = -1;
3679 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3680 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3682 QBitArray specifiedBindings(maxSamplerBinding + 1);
3683 const auto addTrackedTexture = [&](
int binding, QRhiShaderResourceBinding::StageFlags stage,
3684 QRhiTexture *texture, QRhiSampler *sampler) {
3685 specifiedBindings.setBit(binding);
3686 bindings.addTexture(binding, stage, texture, sampler);
3690 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3691 while (renderableImage) {
3692 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3693 const int samplerHint =
int(renderableImage->m_mapType);
3694 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3695 if (samplerBinding >= 0) {
3696 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3697 if (samplerBinding >= 0 && texture) {
3698 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3699 QSSGRhiSamplerDescription samplerDesc = {
3700 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3701 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3702 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3703 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3704 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3705 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3707 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3708 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3709 addTrackedTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3712 renderableImage = renderableImage->m_nextImage;
3715 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings, &specifiedBindings);
3718 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3719 if (normalTextureBinding >= 0) {
3720 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3721 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3722 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3723 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3724 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3725 addTrackedTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3731 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3732 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3734 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3735 QRhiSampler::Nearest,
3737 QRhiSampler::ClampToEdge,
3738 QRhiSampler::ClampToEdge,
3741 addTrackedTexture(binding,
3742 QRhiShaderResourceBinding::VertexStage,
3749 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3750 if (targetsTexture) {
3751 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3753 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3754 QRhiSampler::Nearest,
3756 QRhiSampler::ClampToEdge,
3757 QRhiSampler::ClampToEdge,
3758 QRhiSampler::ClampToEdge
3760 addTrackedTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3765 if (shaderPipeline->isLightingEnabled()) {
3766 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3767 shaderPipeline->ub0LightDataOffset(),
3768 sizeof(QSSGShaderLightsUniformData));
3769 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3770 shaderPipeline->ub0DirectionalLightDataOffset(),
3771 sizeof(QSSGShaderDirectionalLightsUniformData));
3774 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3775 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3778 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3779 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3780 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3783 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3784 QRhiSampler::Linear,
3786 QRhiSampler::ClampToEdge,
3787 QRhiSampler::ClampToEdge,
3788 QRhiSampler::Repeat });
3790 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3794 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3795 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
3796 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
3798 QRhiTexture *texture = shadowMapBlueNoise;
3799 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3800 QRhiSampler::Linear,
3802 QRhiSampler::Repeat,
3803 QRhiSampler::Repeat,
3804 QRhiSampler::Repeat });
3805 Q_ASSERT(texture && sampler);
3806 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3809 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3810 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
3811 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3812 QRhiSampler::Linear,
3814 QRhiSampler::Repeat,
3815 QRhiSampler::Repeat,
3816 QRhiSampler::Repeat });
3817 Q_ASSERT(texture && sampler);
3818 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3819 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3826 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
3827 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
3828 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
3829 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
3830 ? QRhiSampler::Linear
3831 : QRhiSampler::None;
3832 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3833 QRhiSampler::Linear,
3835 QRhiSampler::ClampToEdge,
3836 QRhiSampler::ClampToEdge,
3837 QRhiSampler::Repeat });
3838 if (reflectionSampler >= 0 && reflectionTexture)
3839 addTrackedTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
3841 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3843 auto texture = shaderPipeline->lightProbeTexture();
3845 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3846 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3847 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3851 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3852 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3853 QRhiSampler::Linear,
3854 QRhiSampler::Linear,
3855 QSSGRhiHelpers::toRhi(tiling.first),
3856 QSSGRhiHelpers::toRhi(tiling.second),
3857 QRhiSampler::Repeat });
3858 addTrackedTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3862 if (shaderPipeline->screenTexture()) {
3863 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
3864 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
3865 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
3870 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
3871 ? QRhiSampler::Linear : QRhiSampler::None;
3872 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
3873 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3874 if (screenTextureBinding >= 0) {
3875 addTrackedTexture(screenTextureBinding,
3876 QRhiShaderResourceBinding::FragmentStage,
3877 shaderPipeline->screenTexture(), sampler);
3879 if (screenTextureArrayBinding >= 0) {
3880 addTrackedTexture(screenTextureArrayBinding,
3881 QRhiShaderResourceBinding::FragmentStage,
3882 shaderPipeline->screenTexture(), sampler);
3887 if (shaderPipeline->lightmapTexture()) {
3888 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
3890 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
3891 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3892 addTrackedTexture(binding,
3893 QRhiShaderResourceBinding::FragmentStage,
3894 shaderPipeline->lightmapTexture(), sampler);
3901 if (maxSamplerBinding >= 0) {
3903 int extraTexCount = shaderPipeline->extraTextureCount();
3904 for (
int i = 0; i < extraTexCount; ++i) {
3905 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
3906 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
3907 if (samplerBinding >= 0) {
3908 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
3909 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
3910 addTrackedTexture(samplerBinding,
3911 QRhiShaderResourceBinding::FragmentStage,
3919 QRhiSampler *dummySampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3920 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3921 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3922 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3923 QRhiTexture *dummyTextureArray = rhiCtx->dummyTexture({}, resourceUpdates, QSize(64, 64), Qt::black, qMax(2u, inData.layer.viewCount));
3924 QRhiTexture *dummyTexture3D = rhiCtx->dummyTexture(QRhiTexture::ThreeDimensional, resourceUpdates);
3925 QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3926 QRhiTexture *dummyCubeTextureArray = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates, QSize(64, 64), Qt::black, qMax(2u, inData.layer.viewCount));
3927 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3929 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars)) {
3930 if (!specifiedBindings.testBit(var.binding)) {
3931 QRhiTexture *t =
nullptr;
3932 if (var.type == QShaderDescription::SamplerCube)
3933 t = dummyCubeTexture;
3934 else if (var.type == QShaderDescription::Sampler3D)
3936 else if (var.type == QShaderDescription::Sampler2DArray)
3937 t = dummyTextureArray;
3938 else if (var.type == QShaderDescription::SamplerCubeArray)
3939 t = dummyCubeTextureArray;
3942 bindings.addTexture(var.binding, RENDERER_VISIBILITY_ALL, t, dummySampler);
3947 QRhiShaderResourceBindings *srb = drawCallDataSrb(rhiCtxD, *dcd, bindings);
3948 QSSG_ASSERT(srb,
return -1);
3949 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3950 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3951 rhiPassData.srb = srb;
4015 QSSGPassKey passKey,
4017 const QMatrix4x4 &viewProjection,
4019 QRhiRenderPassDescriptor *renderPassDescriptor,
4020 QSSGRhiGraphicsPipelineState *ps,
4021 QSSGRenderMotionVectorMap &motionVectorMapManager)
4023 if (inObject.type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
4024 inObject.type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
4027 QSSGSubsetRenderable &subsetRenderable =
static_cast<QSSGSubsetRenderable &>(inObject);
4028 auto &modelNode = subsetRenderable.modelContext.model;
4030 bool skin = modelNode.usesBoneTexture();
4031 bool instance = modelNode.instanceCount() > 0;
4032 bool morph = modelNode.morphTargets.size() > 0;
4034 QSSGRhiShaderPipelinePtr shaderPipeline = inData.contextInterface()->shaderCache()->
4035 getBuiltInRhiShaders().getRhiMotionVectorShader(skin, instance, morph);
4037 if (!shaderPipeline)
4040 QSSGRhiShaderResourceBindingList bindings;
4042 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
4043 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode,
nullptr,
nullptr, 0 });
4046 QSSGRenderTextureData *boneTextureData =
nullptr;
4048 boneTextureData = modelNode.skin;
4049 else if (modelNode.skeleton)
4050 boneTextureData = &modelNode.skeleton->boneTexData;
4052 QMatrix4x4 modelViewProjection = subsetRenderable.modelContext.modelViewProjections[0];
4053 QMatrix4x4 instanceLocal;
4054 QMatrix4x4 instanceGlobal;
4057 instanceLocal = inData.getInstanceTransforms(modelNode).local;
4058 instanceGlobal = inData.getInstanceTransforms(modelNode).global;
4060 modelViewProjection = viewProjection;
4062 auto motionData = motionVectorMapManager.trackMotionData(&modelNode,
4063 modelViewProjection,
4067 modelNode.instanceTable,
4068 modelNode.morphWeights);
4069 float velocityAmount = subsetRenderable.modelContext.model.motionVectorScale;
4070 int morphTargetCount = modelNode.morphWeights.count();
4082 const int ubufSize = 416;
4085 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic,
4086 QRhiBuffer::UniformBuffer,
4091 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
4093 memcpy(ubufData, modelViewProjection.constData(), 64);
4095 memcpy(ubufData + ubufOffset, motionData.prevModelViewProjection.constData(), 64);
4097 memcpy(ubufData + ubufOffset, instanceLocal.constData(), 64);
4099 memcpy(ubufData + ubufOffset, instanceGlobal.constData(), 64);
4101 memcpy(ubufData + ubufOffset, motionData.prevInstanceLocal.constData(), 64);
4103 memcpy(ubufData + ubufOffset, motionData.prevInstanceGlobal.constData(), 64);
4105 memcpy(ubufData + ubufOffset, &inData.layer.currentAndLastJitter, 16);
4107 memcpy(ubufData + ubufOffset, &velocityAmount, 4);
4109 memcpy(ubufData + ubufOffset, &morphTargetCount, 4);
4110 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
4112 bindings.addUniformBuffer(0, QRhiShaderResourceBinding::VertexStage |
4113 QRhiShaderResourceBinding::FragmentStage, dcd.ubuf);
4115 QRhiSampler *nearestSampler =
nullptr;
4117 if (skin || instance) {
4118 nearestSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4119 QRhiSampler::Nearest,
4121 QRhiSampler::ClampToEdge,
4122 QRhiSampler::ClampToEdge,
4128 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
4131 bindings.addTexture(binding,
4132 QRhiShaderResourceBinding::VertexStage,
4133 inData.getBonemapTexture(subsetRenderable.modelContext),
4135 binding = shaderPipeline->bindingForTexture(
"lastBoneTexture");
4137 bindings.addTexture(binding,
4138 QRhiShaderResourceBinding::VertexStage,
4139 motionData.prevBoneTexture.m_texture,
4146 int binding = shaderPipeline->bindingForTexture(
"lastInstanceTexture");
4148 bindings.addTexture(binding,
4149 QRhiShaderResourceBinding::VertexStage,
4150 motionData.prevInstanceTexture.m_texture,
4155 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
4156 if (targetsTexture) {
4157 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
4159 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4160 QRhiSampler::Nearest,
4162 QRhiSampler::ClampToEdge,
4163 QRhiSampler::ClampToEdge,
4164 QRhiSampler::ClampToEdge
4166 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
4168 if ((binding = shaderPipeline->bindingForTexture(
"morphWeightTexture")) >= 0)
4169 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.currentMorphWeightTexture.m_texture, targetsSampler);
4171 if ((binding = shaderPipeline->bindingForTexture(
"lastMorphWeightTexture")) >= 0)
4172 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.prevMorphWeightTexture.m_texture, targetsSampler);
4176 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
4177 ia = subsetRenderable.subset.rhi.ia;
4178 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
4179 QVector3D cameraDirection = cameraDatas[0].direction;
4180 QVector3D cameraPosition = cameraDatas[0].position;
4181 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDirection, cameraPosition);
4182 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
4183 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
4185 QRhiShaderResourceBindings *&srb = dcd.srb;
4186 bool srbChanged =
false;
4187 if (!srb || bindings != dcd.bindings) {
4188 srb = rhiCtxD->srb(bindings);
4189 rhiCtxD->releaseCachedSrb(dcd.bindings);
4190 dcd.bindings = bindings;
4194 subsetRenderable.rhiRenderData.motionVectorPass.srb = srb;
4196 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
4199 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
4200 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
4203 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = dcd.pipeline;
4206 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = rhiCtxD->pipeline(pipelineKey,
4207 renderPassDescriptor,
4209 dcd.pipeline = subsetRenderable.rhiRenderData.motionVectorPass.pipeline;
4210 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
4211 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;