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);
709 QSSGRhiGraphicsPipelineState *ps,
710 const QVector2D *depthAdjust,
711 const QSSGRenderableObjectList &sortedOpaqueObjects,
712 QSSGRenderCamera &inCamera,
714 QSSGRenderTextureCubeFace cubeFace,
715 quint32 cascadeIndex)
717 QSSGShaderFeatures featureSet;
719 featureSet.set(QSSGShaderFeatures::Feature::OrthoShadowPass,
true);
721 featureSet.set(QSSGShaderFeatures::Feature::PerspectiveShadowPass,
true);
727 featureSet.set(QSSGShaderFeatures::Feature::DisableMultiView,
true);
729 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
730 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
731 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
733 for (
const auto &handle : sortedOpaqueObjects) {
734 QSSGRenderableObject *theObject = handle.obj;
735 QSSG_ASSERT(theObject->renderableFlags.castsShadows(),
continue);
737 QSSGShaderFeatures objectFeatureSet = featureSet;
738 const bool isOpaqueDepthPrePass = theObject->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
739 if (isOpaqueDepthPrePass)
740 objectFeatureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
742 QSSGRhiDrawCallData *dcd =
nullptr;
743 QMatrix4x4 modelViewProjection;
744 QSSGSubsetRenderable &renderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
745 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
746 const bool hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(renderable.shaderDescription) > 0;
747 modelViewProjection = hasSkinning ? pEntry->m_lightViewProjection[cascadeIndex]
748 : pEntry->m_lightViewProjection[cascadeIndex] * renderable.modelContext.globalTransform;
752 const quintptr entryIdx = cascadeIndex + cubeFaceIdx + (quintptr(renderable.subset.offset) << 3);
753 dcd = &rhiCtxD->drawCallData({ passKey, &renderable.modelContext.model, pEntry, entryIdx });
756 QSSGRhiShaderResourceBindingList bindings;
757 QSSGRhiShaderPipelinePtr shaderPipeline;
758 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*theObject));
759 if (theObject->type == QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset) {
760 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
761 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
762 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
764 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, objectFeatureSet);
767 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
768 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
770 QSSGRenderCameraList cameras({ &inCamera });
771 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras, depthAdjust, &modelViewProjection);
773 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
774 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
776 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
777 }
else if (theObject->type == QSSGSubsetRenderable::Type::CustomMaterialMeshSubset) {
778 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
779 ps->cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
781 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
782 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), objectFeatureSet);
785 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
786 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
788 QSSGRenderCameraList cameras({ &inCamera });
789 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, material, subsetRenderable,
790 cameras, depthAdjust, &modelViewProjection);
791 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
794 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
796 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
797 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
798 ia = subsetRenderable.subset.rhi.ia;
799 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
800 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
801 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
804 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
807 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
809 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
811 if (isOpaqueDepthPrePass) {
812 addOpaqueDepthPrePassBindings(rhiCtx,
813 shaderPipeline.get(),
814 subsetRenderable.firstImage,
816 (theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
823 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
824 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
825 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
826 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
827 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
828 if (screenTextureBinding >= 0) {
829 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
830 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
831 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
832 bindings.addTexture(screenTextureBinding,
833 QRhiShaderResourceBinding::FragmentStage,
834 dummyTexture, sampler);
836 if (screenTextureArrayBinding >= 0) {
837 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
838 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates, QSize(64, 64), Qt::black, inData.layer.viewCount);
839 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
840 bindings.addTexture(screenTextureArrayBinding,
841 QRhiShaderResourceBinding::FragmentStage,
842 dummyTexture, sampler);
847 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
848 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
850 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
851 QRhiSampler::Nearest,
853 QRhiSampler::ClampToEdge,
854 QRhiSampler::ClampToEdge,
857 bindings.addTexture(binding,
858 QRhiShaderResourceBinding::VertexStage,
865 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
866 if (targetsTexture) {
867 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
869 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
870 QRhiSampler::Nearest,
872 QRhiSampler::ClampToEdge,
873 QRhiSampler::ClampToEdge,
874 QRhiSampler::ClampToEdge
876 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
880 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
881 subsetRenderable.rhiRenderData.shadowPass.pipeline = rhiCtxD->pipeline(*ps, pEntry->m_rhiRenderPassDesc[cascadeIndex], srb);
882 subsetRenderable.rhiRenderData.shadowPass.srb[cubeFaceIdx] = srb;
917 QRhiRenderPassDescriptor *renderPassDescriptor,
918 QSSGRhiGraphicsPipelineState *ps,
919 QSSGShaderFeatures featureSet,
923 QSSGRenderCamera *alteredCamera,
924 QMatrix4x4 *alteredModelViewProjection,
925 QSSGRenderTextureCubeFace cubeFace,
929 const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable();
931 switch (inObject.type) {
932 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
934 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
936 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
937 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
939 if ((cubeFace != QSSGRenderTextureCubeFaceNone)) {
941 featureSet.disableTonemapping();
944 if (subsetRenderable.renderableFlags.rendersWithLightmap())
945 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
947 const auto &shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
948 if (shaderPipeline) {
956 QSSGRhiShaderResourceBindingList bindings;
957 const auto &modelNode = subsetRenderable.modelContext.model;
958 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(subsetRenderable.shaderDescription);
965 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
966 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * (cubeFaceIdx + (quintptr(subsetRenderable.subset.offset) << 3));
968 const auto entryPartA =
reinterpret_cast<quintptr>(&subsetRenderable.material);
969 const auto entryPartB =
reinterpret_cast<quintptr>(entry);
970 const void *entryId =
reinterpret_cast<
const void *>(entryPartA ^ entryPartB);
972 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
973 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode, entryId, entryIdx });
975 shaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
976 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
978 Q_ASSERT(alteredModelViewProjection);
979 QSSGRenderCameraList cameras({ alteredCamera });
980 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras,
nullptr, alteredModelViewProjection);
982 Q_ASSERT(!alteredModelViewProjection);
983 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
987 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &subsetRenderable.modelContext.model, subsetRenderable.subset.offset);
988 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
991 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &subsetRenderable.modelContext.model);
994 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
995 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
997 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
998 QRhiSampler::Nearest,
1000 QRhiSampler::ClampToEdge,
1001 QRhiSampler::ClampToEdge,
1004 bindings.addTexture(binding,
1005 QRhiShaderResourceBinding::VertexStage,
1011 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
1012 if (targetsTexture) {
1013 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
1015 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
1016 QRhiSampler::Nearest,
1018 QRhiSampler::ClampToEdge,
1019 QRhiSampler::ClampToEdge,
1020 QRhiSampler::ClampToEdge
1022 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
1026 ps->samples = samples;
1027 ps->viewCount = viewCount;
1029 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
1030 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
1031 if (!oit || (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::None))
1032 fillTargetBlend(&ps->targetBlend[0], material.blendMode);
1034 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
1036 ia = subsetRenderable.subset.rhi.ia;
1037 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
1038 QVector3D cameraDirection = cameraDatas[0].direction;
1039 QVector3D cameraPosition = cameraDatas[0].position;
1040 if (alteredCamera) {
1041 const QMatrix4x4 camGlobalTranform = inData.getGlobalTransform(*alteredCamera);
1042 cameraDirection = QSSGRenderNode::getScalingCorrectDirection(camGlobalTranform);
1043 cameraPosition = QSSGRenderNode::getGlobalPos(camGlobalTranform);
1045 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDirection, cameraPosition);
1046 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
1048 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf, 0, shaderPipeline->ub0Size());
1050 if (shaderPipeline->isLightingEnabled()) {
1051 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1052 shaderPipeline->ub0LightDataOffset(),
1053 sizeof(QSSGShaderLightsUniformData));
1054 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd.ubuf,
1055 shaderPipeline->ub0DirectionalLightDataOffset(),
1056 sizeof(QSSGShaderDirectionalLightsUniformData));
1061 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
1062 while (renderableImage) {
1063 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
1064 const int samplerHint =
int(renderableImage->m_mapType);
1065 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
1066 if (samplerBinding >= 0) {
1067 QRhiTexture *texture = renderableImage->m_texture.m_texture;
1068 if (samplerBinding >= 0 && texture) {
1069 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
1070 QSSGRhiSamplerDescription samplerDesc = {
1071 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
1072 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
1073 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
1074 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
1075 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
1076 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
1078 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
1079 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
1080 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
1083 renderableImage = renderableImage->m_nextImage;
1086 if (shaderPipeline->isLightingEnabled()) {
1088 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
1089 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
1092 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1093 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
1094 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1097 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1098 QRhiSampler::Linear,
1100 QRhiSampler::ClampToEdge,
1101 QRhiSampler::ClampToEdge,
1102 QRhiSampler::Repeat });
1104 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1108 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
1109 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
1110 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
1112 QRhiTexture *texture = shadowMapBlueNoise;
1113 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1114 QRhiSampler::Linear,
1116 QRhiSampler::Repeat,
1117 QRhiSampler::Repeat,
1118 QRhiSampler::Repeat });
1119 Q_ASSERT(texture && sampler);
1120 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1123 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1124 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
1125 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1126 QRhiSampler::Linear,
1128 QRhiSampler::Repeat,
1129 QRhiSampler::Repeat,
1130 QRhiSampler::Repeat });
1131 Q_ASSERT(texture && sampler);
1132 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1133 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1140 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
1141 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
1142 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
1143 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
1144 ? QRhiSampler::Linear
1145 : QRhiSampler::None;
1146 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1147 QRhiSampler::Linear,
1149 QRhiSampler::ClampToEdge,
1150 QRhiSampler::ClampToEdge,
1151 QRhiSampler::Repeat });
1152 if (reflectionSampler >= 0 && reflectionTexture)
1153 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
1156 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
1158 auto texture = shaderPipeline->lightProbeTexture();
1160 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1161 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
1162 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1166 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
1167 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
1168 QRhiSampler::Linear,
1169 QRhiSampler::Linear,
1170 QSSGRhiHelpers::toRhi(tiling.first),
1171 QSSGRhiHelpers::toRhi(tiling.second),
1172 QRhiSampler::Repeat });
1173 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
1177 if (shaderPipeline->screenTexture()) {
1178 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
1179 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
1180 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
1181 QRhiTexture *screenTexture = shaderPipeline->screenTexture();
1183 if (screenMapPass) {
1184 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
1186 const QRhiTexture::Flags flags = screenTexture->flags();
1188 screenTexture = rhiCtx->dummyTexture(flags, resourceUpdates);
1189 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
1196 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
1197 ? QRhiSampler::Linear : QRhiSampler::None;
1198 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
1199 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
1200 if (screenTextureBinding >= 0) {
1201 bindings.addTexture(screenTextureBinding,
1202 QRhiShaderResourceBinding::FragmentStage,
1203 screenTexture, sampler);
1205 if (screenTextureArrayBinding >= 0) {
1206 bindings.addTexture(screenTextureArrayBinding,
1207 QRhiShaderResourceBinding::FragmentStage,
1208 screenTexture, sampler);
1213 if (shaderPipeline->lightmapTexture()) {
1214 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
1216 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
1217 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
1218 bindings.addTexture(binding,
1219 QRhiShaderResourceBinding::FragmentStage,
1220 shaderPipeline->lightmapTexture(), sampler);
1226 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1229 addNormalTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
1231 if (oit && inData.layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
1241 QRhiShaderResourceBindings *&srb = dcd.srb;
1242 bool srbChanged =
false;
1243 if (!srb || bindings != dcd.bindings) {
1244 srb = rhiCtxD->srb(bindings);
1245 rhiCtxD->releaseCachedSrb(dcd.bindings);
1246 dcd.bindings = bindings;
1250 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1251 subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx] = srb;
1253 subsetRenderable.rhiRenderData.mainPass.srb = srb;
1255 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
1258 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
1259 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
1262 if (cubeFace != QSSGRenderTextureCubeFaceNone)
1263 subsetRenderable.rhiRenderData.reflectionPass.pipeline = dcd.pipeline;
1265 subsetRenderable.rhiRenderData.mainPass.pipeline = dcd.pipeline;
1267 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1268 subsetRenderable.rhiRenderData.reflectionPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1269 renderPassDescriptor,
1271 dcd.pipeline = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1273 subsetRenderable.rhiRenderData.mainPass.pipeline = rhiCtxD->pipeline(pipelineKey,
1274 renderPassDescriptor,
1276 dcd.pipeline = subsetRenderable.rhiRenderData.mainPass.pipeline;
1278 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
1279 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;
1285 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1287 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(inObject));
1288 const QSSGRenderCustomMaterial &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
1289 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1291 featureSet.set(QSSGShaderFeatures::Feature::LightProbe, inData.layer.lightProbe || material.m_iblProbe);
1293 if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(QSSGRenderableObjectFlag::ReceivesReflections))
1294 featureSet.set(QSSGShaderFeatures::Feature::ReflectionProbe,
true);
1296 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1298 featureSet.disableTonemapping();
1301 if (subsetRenderable.renderableFlags.rendersWithLightmap())
1302 featureSet.set(QSSGShaderFeatures::Feature::Lightmap,
true);
1304 customMaterialSystem.rhiPrepareRenderable(ps, passKey, subsetRenderable, featureSet,
1305 material, inData, renderPassDescriptor, samples, viewCount, screenMapPass,
1306 alteredCamera, cubeFace, alteredModelViewProjection, entry, oit);
1309 case QSSGRenderableObject::Type::Particles:
1312 const auto &shaderPipeline = shadersForParticleMaterial(ps, particleRenderable, featureSet);
1313 if (shaderPipeline) {
1314 QSSGParticleRenderer::rhiPrepareRenderable(*shaderPipeline, passKey, rhiCtx, ps, particleRenderable, inData, renderPassDescriptor, samples, viewCount,
1315 alteredCamera, cubeFace, entry, oit);
1361 const QSSGRhiGraphicsPipelineState &state,
1363 bool *needsSetViewport,
1364 QSSGRenderTextureCubeFace cubeFace,
1365 qsizetype userPassIndex)
1367 switch (object.type) {
1368 case QSSGRenderableObject::Type::DefaultMaterialMeshSubset:
1370 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1372 QSSG_ASSERT(QSSGUserRenderPassManager::maxUserPassSlots() == std::size(subsetRenderable.rhiRenderData.userPassData),
return);
1374 QRhiGraphicsPipeline *ps = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1375 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].pipeline : subsetRenderable.rhiRenderData.mainPass.pipeline;
1376 QRhiShaderResourceBindings *srb = (userPassIndex >= 0 && size_t(userPassIndex) < QSSGUserRenderPassManager::maxUserPassSlots())
1377 ? subsetRenderable.rhiRenderData.userPassData[userPassIndex].srb : subsetRenderable.rhiRenderData.mainPass.srb;
1379 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
1380 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
1381 ps = subsetRenderable.rhiRenderData.reflectionPass.pipeline;
1382 srb = subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx];
1388 QRhiBuffer *vertexBuffer = subsetRenderable.subset.rhi.vertexBuffer->buffer();
1389 QRhiBuffer *indexBuffer = subsetRenderable.subset.rhi.indexBuffer ? subsetRenderable.subset.rhi.indexBuffer->buffer() :
nullptr;
1391 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1393 cb->setGraphicsPipeline(ps);
1394 cb->setShaderResources(srb);
1396 if (*needsSetViewport) {
1397 cb->setViewport(state.viewport);
1398 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesScissor))
1399 cb->setScissor(state.scissor);
1400 *needsSetViewport =
false;
1403 QRhiCommandBuffer::VertexInput vertexBuffers[2];
1404 int vertexBufferCount = 1;
1405 vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0);
1406 quint32 instances = 1;
1407 if ( subsetRenderable.modelContext.model.instancing()) {
1408 instances = subsetRenderable.modelContext.model.instanceCount();
1415 vertexBuffers[1] = QRhiCommandBuffer::VertexInput(subsetRenderable.instanceBuffer, 0);
1416 vertexBufferCount = 2;
1418 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1419 if (state.flags.testFlag(QSSGRhiGraphicsPipelineState::Flag::UsesStencilRef))
1420 cb->setStencilRef(state.stencilRef);
1422 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, subsetRenderable.subset.rhi.indexBuffer->indexFormat());
1423 cb->drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances, subsetRenderable.subset.lodOffset(subsetRenderable.subsetLevelOfDetail));
1424 QSSGRHICTX_STAT(rhiCtx, drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances));
1426 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1427 cb->draw(subsetRenderable.subset.count, instances, subsetRenderable.subset.offset);
1428 QSSGRHICTX_STAT(rhiCtx, draw(subsetRenderable.subset.count, instances));
1430 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (subsetRenderable.subset.count | quint64(instances) << 32),
1431 QVector<
int>({subsetRenderable.modelContext.model.profilingId,
1432 subsetRenderable.material.profilingId}));
1435 case QSSGRenderableObject::Type::CustomMaterialMeshSubset:
1437 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(object));
1438 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
1439 customMaterialSystem.rhiRenderRenderable(rhiCtx, subsetRenderable, needsSetViewport, cubeFace, state, userPassIndex);
1442 case QSSGRenderableObject::Type::Particles:
1445 QSSGParticleRenderer::rhiRenderRenderable(rhiCtx, renderable, needsSetViewport, cubeFace, state);
1470 QSSGPassKey passKey,
1471 QSSGRhiGraphicsPipelineState &ps,
1472 QSSGRenderShadowMap &shadowMapManager,
1473 const QSSGRenderCamera &camera,
1474 QSSGRenderCamera *debugCamera,
1476 const QSSGRenderableObjectList &sortedOpaqueObjects,
1478 const QSSGBounds3 &castingObjectsBox,
1479 const QSSGBounds3 &receivingObjectsBox)
1482 QSSGDebugDrawSystem *debugDrawSystem = renderer.contextInterface()->debugDrawSystem().get();
1483 const bool drawDirectionalLightShadowBoxes = layerData.layer.drawDirectionalLightShadowBoxes;
1484 const bool drawPointLightShadowBoxes = layerData.layer.drawPointLightShadowBoxes;
1485 const bool drawShadowCastingBounds = layerData.layer.drawShadowCastingBounds;
1486 const bool drawShadowReceivingBounds = layerData.layer.drawShadowReceivingBounds;
1487 const bool drawCascades = layerData.layer.drawCascades;
1488 const bool drawSceneCascadeIntersection = layerData.layer.drawSceneCascadeIntersection;
1489 const bool disableShadowCameraUpdate = layerData.layer.disableShadowCameraUpdate;
1490 const bool drawCulledObjects = layerData.layer.drawCulledObjects;
1491 QVector<
bool> debugIsObjectCulled = drawCulledObjects ? QVector<
bool>(sortedOpaqueObjects.size(),
true) : QVector<
bool>();
1493 static const auto rhiRenderOneShadowMap = [](QSSGRhiContext *rhiCtx,
1494 QSSGRhiGraphicsPipelineState *ps,
1495 const QSSGRenderableObjectList &sortedOpaqueObjects,
1497 const QSSGBounds3 cameraBounds,
1498 QVector<
bool> &debugIsObjectCulled,
1499 bool drawCulledObjects) {
1500 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1501 bool needsSetViewport =
true;
1503 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
1508 if (theObject->globalBoundsInstancing.isFinite() && theObject->globalBounds.isFinite()) {
1509 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
1510 : theObject->globalBounds;
1511 if (!globalBounds.isEmpty() && !cameraBounds.intersects(globalBounds)) {
1516 if (Q_UNLIKELY(drawCulledObjects))
1517 debugIsObjectCulled[i] =
false;
1520 if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
1521 QSSGSubsetRenderable *renderable(
static_cast<QSSGSubsetRenderable *>(theObject));
1523 QRhiBuffer *vertexBuffer = renderable->subset.rhi.vertexBuffer->buffer();
1524 QRhiBuffer *indexBuffer = renderable->subset.rhi.indexBuffer
1525 ? renderable->subset.rhi.indexBuffer->buffer()
1529 if (!renderable->rhiRenderData.shadowPass.pipeline)
1532 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall);
1534 cb->setGraphicsPipeline(renderable->rhiRenderData.shadowPass.pipeline);
1536 QRhiShaderResourceBindings *srb = renderable->rhiRenderData.shadowPass.srb[cubeFace];
1537 cb->setShaderResources(srb);
1539 if (needsSetViewport) {
1540 cb->setViewport(ps->viewport);
1541 needsSetViewport =
false;
1544 QRhiCommandBuffer::VertexInput vertexBuffers[2];
1545 int vertexBufferCount = 1;
1546 vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0);
1547 quint32 instances = 1;
1548 if (renderable->modelContext.model.instancing()) {
1549 instances = renderable->modelContext.model.instanceCount();
1550 vertexBuffers[1] = QRhiCommandBuffer::VertexInput(renderable->instanceBuffer, 0);
1551 vertexBufferCount = 2;
1554 cb->setVertexInput(0, vertexBufferCount, vertexBuffers, indexBuffer, 0, renderable->subset.rhi.indexBuffer->indexFormat());
1555 cb->drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances, renderable->subset.lodOffset(renderable->subsetLevelOfDetail));
1556 QSSGRHICTX_STAT(rhiCtx, drawIndexed(renderable->subset.lodCount(renderable->subsetLevelOfDetail), instances));
1558 cb->setVertexInput(0, vertexBufferCount, vertexBuffers);
1559 cb->draw(renderable->subset.count, instances, renderable->subset.offset);
1560 QSSGRHICTX_STAT(rhiCtx, draw(renderable->subset.count, instances));
1562 Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (renderable->subset.count | quint64(instances) << 32),
1563 QVector<
int>({renderable->modelContext.model.profilingId,
1564 renderable->material.profilingId}));
1569 static const auto rhiClearShadowMap = [](
QSSGRenderer &renderer, QSSGRenderShadowMap &shadowMapManager, QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiRenderPassDescriptor *renderPassDesc) {
1570 auto clearShadowMapShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiClearShadowMapShader();
1571 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, clearShadowMapShaderPipeline.get());
1574 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled,
false);
1575 ps->flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled,
false);
1576 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, shadowMapManager.shadowClearSrb(), renderPassDesc, {});
1578 ps->flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
1581 QRhi *rhi = rhiCtx->rhi();
1582 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1586 QVector2D depthAdjust;
1587 if (rhi->isClipDepthZeroToOne()) {
1589 depthAdjust[0] = 0.0f;
1590 depthAdjust[1] = 1.0f;
1593 depthAdjust[0] = 1.0f;
1594 depthAdjust[1] = 0.5f;
1597 if (drawShadowCastingBounds)
1599 if (drawShadowReceivingBounds)
1603 const QSize atlasTextureSize = shadowMapManager.shadowMapAtlasTexture()->pixelSize();
1605 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
1606 for (
int i = 0, ie = globalLights.size(); i != ie; ++i) {
1607 if (!globalLights[i].shadows || globalLights[i].light->m_fullyBaked)
1614 const auto &light = globalLights[i].light;
1616 if (!shadowMapManager.shadowMapAtlasTexture())
1619 if (light->type == QSSGRenderLight::Type::DirectionalLight || light->type == QSSGRenderLight::Type::SpotLight) {
1620 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1624 if (!disableShadowCameraUpdate && debugCamera) {
1625 debugCamera->clipPlanes = camera.clipPlanes;
1626 debugCamera->projection = camera.projection;
1630 debugCamera->localTransform = layerData.getGlobalTransform(camera);
1633 QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> cascades;
1634 if (light->type == QSSGRenderLight::Type::DirectionalLight) {
1635 const float pcfRadius = light->m_softShadowQuality == QSSGRenderLight::SoftShadowQuality::Hard ? 0.f : light->m_pcfFactor;
1636 const float clipNear = camera.clipPlanes.clipNear();
1637 const float clipFar = qMin(light->m_shadowMapFar, camera.clipPlanes.clipFar());
1638 const float clipRange = clipFar - clipNear;
1639 cascades = setupCascadingCamerasForShadowMap(layerData,
1640 disableShadowCameraUpdate ? *debugCamera : camera,
1647 receivingObjectsBox,
1648 light->m_lockShadowmapTexels,
1651 drawSceneCascadeIntersection);
1654 pEntry->m_csmSplits[0] = clipNear + clipRange * (light->m_csmNumSplits > 0 ? light->m_csmSplit1 : 1.0f);
1655 pEntry->m_csmSplits[1] = clipNear + clipRange * (light->m_csmNumSplits > 1 ? light->m_csmSplit2 : 1.0f);
1656 pEntry->m_csmSplits[2] = clipNear + clipRange * (light->m_csmNumSplits > 2 ? light->m_csmSplit3 : 1.0f);
1657 pEntry->m_csmSplits[3] = clipNear + clipRange * 1.0f;
1658 pEntry->m_shadowMapFar = clipFar;
1659 }
else if (light->type == QSSGRenderLight::Type::SpotLight) {
1660 auto spotlightCamera =
std::make_unique<QSSGRenderCamera>(QSSGRenderCamera::Type::PerspectiveCamera);
1661 spotlightCamera->fov = QSSGRenderCamera::FieldOfView::fromDegrees(light->m_coneAngle * 2.0f);
1662 spotlightCamera->clipPlanes = { 1.0f, light->m_shadowMapFar };
1663 const QMatrix4x4 lightGlobalTransform = layerData.getGlobalTransform(*light);
1664 const QVector3D lightDir = QSSGRenderNode::getDirection(lightGlobalTransform);
1665 const QVector3D lightPos = QSSGRenderNode::getGlobalPos(lightGlobalTransform) - lightDir * spotlightCamera->clipPlanes.clipNear();
1666 const QVector3D lightPivot = light->pivot;
1667 const QVector3D forward = lightDir.normalized();
1668 const QVector3D right = qFuzzyCompare(qAbs(forward.y()), 1.0f)
1669 ? QVector3D::crossProduct(forward, QVector3D(1, 0, 0)).normalized()
1670 : QVector3D::crossProduct(forward, QVector3D(0, 1, 0)).normalized();
1671 const QVector3D up = QVector3D::crossProduct(right, forward).normalized();
1672 spotlightCamera->localTransform = QSSGRenderNode::calculateTransformMatrix(lightPos,
1673 QSSGRenderNode::initScale,
1675 QQuaternion::fromDirection(forward, up));
1676 QRectF theViewport(0.0f, 0.0f, (
float)light->m_shadowMapRes, (
float)light->m_shadowMapRes);
1677 QSSGRenderCamera::calculateProjectionInternal(*spotlightCamera, theViewport);
1678 cascades.push_back(
std::move(spotlightCamera));
1679 pEntry->m_shadowMapFar = light->m_shadowMapFar;
1684 memset(pEntry->m_csmActive, 0,
sizeof(pEntry->m_csmActive));
1686 QMatrix4x4 cascadeCameraGlobalTransforms(Qt::Uninitialized);
1687 const QMatrix4x4 bias = { 0.5, 0.0, 0.0, 0.5,
1690 0.0, 0.0, 0.0, 1.0 };
1692 for (
int cascadeIndex = 0; cascadeIndex < cascades.length(); cascadeIndex++) {
1693 const auto &cascadeCamera = cascades[cascadeIndex];
1697 cascadeCameraGlobalTransforms = layerData.getGlobalTransform(*cascadeCamera);
1698 pEntry->m_csmActive[cascadeIndex] = 1.f;
1699 QMatrix4x4 &viewProjection = pEntry->m_lightViewProjection[cascadeIndex];
1700 cascadeCamera->calculateViewProjectionMatrix(cascadeCameraGlobalTransforms, viewProjection);
1701 pEntry->m_lightViewProjection[cascadeIndex] = viewProjection;
1702 pEntry->m_fixedScaleBiasMatrix[cascadeIndex] = bias * viewProjection;
1703 const QMatrix4x4 inverted = viewProjection.inverted();
1704 const float x = 0.5f / (inverted * QVector4D(1, 0, 0, 0)).length();
1705 const float y = 0.5f / (inverted * QVector4D(0, 1, 0, 0)).length();
1706 const float z = 0.5f / (inverted * QVector4D(0, 0, 1, 0)).length();
1707 const QSSGBoxPoints frustumPoints = computeFrustumBounds(viewProjection);
1708 const QSSGBounds3 bounds = QSSGBounds3(frustumPoints);
1709 pEntry->m_dimensionsInverted[cascadeIndex] = QVector4D(x, y, z, 0.0f);
1710 pEntry->m_lightView = cascadeCameraGlobalTransforms.inverted();
1711 const bool isOrtho = cascadeCamera->type == QSSGRenderGraphObject::Type::OrthographicCamera;
1712 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[cascadeIndex], rhi->isYUpInFramebuffer());
1713 rhiPrepareResourcesForShadowMap(rhiCtx, layerData, passKey, pEntry, &ps, &depthAdjust, sortedOpaqueObjects, *cascadeCamera, isOrtho, QSSGRenderTextureCubeFaceNone, cascadeIndex);
1716 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[cascadeIndex];
1717 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1718 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1719 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1720 rhiClearShadowMap(renderer, shadowMapManager, rhiCtx, &ps, rt->renderPassDescriptor());
1721 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, 0, bounds, debugIsObjectCulled, drawCulledObjects);
1723 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1725 if (drawDirectionalLightShadowBoxes) {
1729 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"shadow_map"));
1731 const QSize size = atlasTextureSize * pEntry->m_atlasInfo[0].uvScale;
1732 ps.viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
1734 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1735 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1736 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1737 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1738 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1739 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
1740 const float shadowMapFar = qMax<
float>(2.0f, light->m_shadowMapFar);
1741 setupCubeShadowCameras(layerData, light, shadowMapFar, theCameras);
1742 pEntry->m_lightView = QMatrix4x4();
1743 pEntry->m_shadowMapFar = shadowMapFar;
1745 const bool swapYFaces = !rhi->isYUpInFramebuffer();
1746 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
1747 for (
const auto face : QSSGRenderTextureCubeFaces) {
1748 cameraGlobalTransform = layerData.getGlobalTransform(theCameras[quint8(face)]);
1749 theCameras[quint8(face)].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_lightViewProjection[0]);
1750 pEntry->m_lightCubeView[quint8(face)] = cameraGlobalTransform.inverted();
1752 rhiPrepareResourcesForShadowMap(rhiCtx,
1758 sortedOpaqueObjects,
1759 theCameras[quint8(face)],
1766 const QVector3D center = QSSGRenderNode::getGlobalPos(layerData.getGlobalTransform(*light));
1767 const QSSGBounds3 bounds = QSSGBounds3(center - QVector3D(shadowMapFar, shadowMapFar, shadowMapFar),
1768 center + QVector3D(shadowMapFar, shadowMapFar, shadowMapFar));
1770 for (
const auto face : QSSGRenderTextureCubeFaces) {
1774 QSSGRenderTextureCubeFace outFace = face;
1792 if (outFace == QSSGRenderTextureCubeFace::PosY)
1793 outFace = QSSGRenderTextureCubeFace::NegY;
1794 else if (outFace == QSSGRenderTextureCubeFace::NegY)
1795 outFace = QSSGRenderTextureCubeFace::PosY;
1797 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargetCube[quint8(outFace)];
1798 cb->beginPass(rt, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1799 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1800 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1801 rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, quint8(face), bounds, debugIsObjectCulled, drawCulledObjects);
1803 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1804 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_cube", 0, outFace));
1811 QRhiTextureRenderTarget *rtFront = pEntry->m_rhiRenderTargets[0];
1812 QRhiRenderPassDescriptor *frontDesc = pEntry->m_rhiRenderPassDesc[0];
1813 auto atlasShaderPipeline = renderer.contextInterface()->shaderCache()->getBuiltInRhiShaders().getRhiCubeMapToAtlasShader();
1814 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[0], rhi->isYUpInFramebuffer());
1815 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, atlasShaderPipeline.get());
1816 QRhiShaderResourceBindings *srb = pEntry->m_cubeToAtlasFrontSrb;
1817 cb->beginPass(rtFront, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1818 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtFront));
1819 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1821 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, frontDesc, QSSGRhiQuadRenderer::UvCoords);
1824 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1825 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 6, 0));
1828 QRhiTextureRenderTarget *rtBack = pEntry->m_rhiRenderTargets[1];
1829 QRhiRenderPassDescriptor *backDesc = pEntry->m_rhiRenderPassDesc[1];
1830 srb = pEntry->m_cubeToAtlasBackSrb;
1831 ps.viewport = calculateAtlasViewport(atlasTextureSize, pEntry->m_atlasInfo[1], rhi->isYUpInFramebuffer());
1832 cb->beginPass(rtBack, Qt::white, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1833 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rtBack));
1834 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1836 renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, &ps, srb, backDesc, QSSGRhiQuadRenderer::UvCoords);
1839 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1840 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"shadow_atlas", 7, 0));
1844 ps = layerData.getPipelineState();
1845 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
1847 ps.slopeScaledDepthBias = 1.5f;
1849 if (drawPointLightShadowBoxes) {
1855 if (Q_UNLIKELY(drawCulledObjects)) {
1856 for (
int i = 0, n = sortedOpaqueObjects.size(); i < n; ++i) {
1858 const QSSGBounds3 &globalBounds = !theObject->globalBoundsInstancing.isEmpty() ? theObject->globalBoundsInstancing
1859 : theObject->globalBounds;
1860 const QColor color = debugIsObjectCulled[i] ? QColorConstants::Red : QColorConstants::Green;
1867 QSSGPassKey passKey,
1869 QSSGRhiGraphicsPipelineState *ps,
1870 QSSGRenderReflectionMap &reflectionMapManager,
1871 const std::vector<QSSGRenderReflectionProbe *> &reflectionProbes,
1872 const QSSGRenderableObjectList &reflectionPassObjects,
1875 QSSGRhiContext *rhiCtx = context.rhiContext().get();
1876 QRhi *rhi = rhiCtx->rhi();
1877 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
1879 const bool renderSkybox = (inData.layer.background == QSSGRenderLayer::Background::SkyBox
1880 || inData.layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap
1881 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial)
1882 && rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch);
1884 for (size_t i = 0, ie = reflectionProbes.size(); i != ie; ++i) {
1885 QSSGReflectionMapEntry *pEntry = reflectionMapManager.reflectionMapEntry(
int(i));
1889 if (!pEntry->m_needsRender)
1892 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame && pEntry->m_rendered)
1895 if (reflectionProbes[i]->texture)
1898 Q_ASSERT(pEntry->m_rhiDepthStencil);
1899 Q_ASSERT(pEntry->m_rhiCube);
1901 const QSize size = pEntry->m_rhiCube->pixelSize();
1902 ps->viewport = QRhiViewport(0, 0,
float(size.width()),
float(size.height()));
1904 QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1905 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1906 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1907 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1908 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera},
1909 QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} };
1910 setupCubeReflectionCameras(inData, reflectionProbes[i], theCameras);
1911 const bool swapYFaces = !rhi->isYUpInFramebuffer();
1912 QMatrix4x4 cameraGlobalTransform(Qt::Uninitialized);
1913 for (
const auto face : QSSGRenderTextureCubeFaces) {
1914 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face);
1915 cameraGlobalTransform = inData.getGlobalTransform(theCameras[cubeFaceIdx]);
1916 theCameras[cubeFaceIdx].calculateViewProjectionMatrix(cameraGlobalTransform, pEntry->m_viewProjection);
1918 rhiPrepareResourcesForReflectionMap(context, passKey, inData, pEntry, ps, reflectionPassObjects, theCameras[cubeFaceIdx], renderer, face);
1920 QRhiRenderPassDescriptor *renderPassDesc =
nullptr;
1921 for (
auto face : QSSGRenderTextureCubeFaces) {
1922 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
1923 face = pEntry->m_timeSliceFace;
1925 QSSGRenderTextureCubeFace outFace = face;
1929 if (outFace == QSSGRenderTextureCubeFace::PosY)
1930 outFace = QSSGRenderTextureCubeFace::NegY;
1931 else if (outFace == QSSGRenderTextureCubeFace::NegY)
1932 outFace = QSSGRenderTextureCubeFace::PosY;
1934 QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[quint8(outFace)];
1935 cb->beginPass(rt, reflectionProbes[i]->clearColor, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
1936 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt));
1937 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
1939 if (renderSkybox && pEntry->m_skyBoxSrbs[quint8(face)]) {
1940 const auto &shaderCache = renderer.contextInterface()->shaderCache();
1941 const bool useSkyBoxQuad = inData.layer.background == QSSGRenderLayer::Background::SkyBox
1942 || inData.layer.background == QSSGRenderLayer::Background::SkyMaterial;
1943 const auto &shaderPipeline = useSkyBoxQuad
1944 ? shaderCache->getBuiltInRhiShaders().getRhiSkyBoxShader(QSSGRenderLayer::TonemapMode::None,
1945 inData.layer.skyBoxIsRgbe8,
1947 : shaderCache->getBuiltInRhiShaders().getRhiSkyBoxCubeShader(QSSGRenderLayer::TonemapMode::None,
1948 !inData.layer.skyBoxIsSrgb,
1950 Q_ASSERT(shaderPipeline);
1951 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
1952 QRhiShaderResourceBindings *srb = pEntry->m_skyBoxSrbs[quint8(face)];
1953 if (!renderPassDesc)
1954 renderPassDesc = rt->newCompatibleRenderPassDescriptor();
1955 rt->setRenderPassDescriptor(renderPassDesc);
1956 useSkyBoxQuad ? renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, srb, renderPassDesc, {})
1957 : renderer.rhiCubeRenderer()->recordRenderCube(rhiCtx, ps, srb, renderPassDesc, {});
1960 bool needsSetViewport =
true;
1961 for (
const auto &handle : reflectionPassObjects)
1962 rhiRenderRenderable(rhiCtx, *ps, *handle.obj, &needsSetViewport, face);
1965 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
1966 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME(
"reflection_cube", 0, outFace));
1968 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
1972 renderPassDesc->deleteLater();
1974 pEntry->renderMips(rhiCtx);
1976 if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces)
1977 pEntry->m_timeSliceFace = QSSGBaseTypeHelpers::next(pEntry->m_timeSliceFace);
1979 if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame)
1980 pEntry->m_rendered =
true;
1982 reflectionProbes[i]->hasScheduledUpdate =
false;
1983 pEntry->m_needsRender =
false;
2034 QSSGPassKey passKey,
2036 QSSGRhiShaderPipeline &shaderPipeline,
2037 QSSGRhiGraphicsPipelineState &ps,
2038 const QSSGAmbientOcclusionSettings &ao,
2039 const QSSGRhiRenderableTexture &rhiAoTexture,
2040 const QSSGRhiRenderableTexture &rhiDepthTexture,
2041 const QSSGRenderCamera &camera)
2043 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2046 if (!rhiCtx->rhi()->isFeatureSupported(QRhi::TexelFetch)) {
2047 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
2049 cb->beginPass(rhiAoTexture.rt, Qt::white, { 1.0f, 0 });
2050 QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rhiAoTexture.rt));
2052 QSSGRHICTX_STAT(rhiCtx, endRenderPass());
2056 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, &shaderPipeline);
2058 const float R2 = ao.aoDistance * ao.aoDistance * 0.16f;
2059 const QSize textureSize = rhiAoTexture.texture->pixelSize();
2060 const float rw =
float(textureSize.width());
2061 const float rh =
float(textureSize.height());
2062 const float fov = camera.fov.asVerticalFov(rw / rh).radians();
2063 const float tanHalfFovY = tanf(0.5f * fov * (rh / rw));
2064 const float invFocalLenX = tanHalfFovY * (rw / rh);
2066 const QVector4D aoProps(ao.aoStrength * 0.01f, ao.aoDistance * 0.4f, ao.aoSoftness * 0.02f, ao.aoBias);
2067 const QVector4D aoProps2(
float(ao.aoSamplerate), (ao.aoDither) ? 1.0f : 0.0f, 0.0f, 0.0f);
2068 const QVector4D aoScreenConst(1.0f / R2, rh / (2.0f * tanHalfFovY), 1.0f / rw, 1.0f / rh);
2069 const QVector4D uvToEyeConst(2.0f * invFocalLenX, -2.0f * tanHalfFovY, -invFocalLenX, tanHalfFovY);
2070 const QVector2D cameraProps = camera.clipPlanes;
2079 const int UBUF_SIZE = 72;
2080 QSSGRhiDrawCallData &dcd(rhiCtxD->drawCallData({ passKey,
nullptr,
nullptr, 0 }));
2082 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SIZE);
2086 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2087 memcpy(ubufData, &aoProps, 16);
2088 memcpy(ubufData + 16, &aoProps2, 16);
2089 memcpy(ubufData + 32, &aoScreenConst, 16);
2090 memcpy(ubufData + 48, &uvToEyeConst, 16);
2091 memcpy(ubufData + 64, &cameraProps, 8);
2092 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2094 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2095 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
2096 QSSGRhiShaderResourceBindingList bindings;
2097 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2100 bindings.addTexture(1, QRhiShaderResourceBinding::FragmentStage, rhiDepthTexture.texture, sampler);
2101 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
2103 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2104 renderer.rhiQuadRenderer()->recordRenderQuadPass(rhiCtx, &ps, srb, rhiAoTexture.rt, {});
2252 QSSGPassKey passKey,
2253 QSSGRenderLayer &layer,
2254 QSSGRenderCameraList &cameras,
2258 uint tonemapMode = 0)
2260 QSSG_ASSERT(layer.renderData,
return);
2262 const auto *renderData = layer.renderData;
2264 auto rhiCtx = context.rhiContext().get();
2266 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2268 QSSGRenderImageTexture lightProbeTexture;
2269 switch (layer.background) {
2270 case QSSGRenderLayer::Background::SkyBox:
2271 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.lightProbe,
2272 QSSGBufferManager::MipModeBsdf);
2274 case QSSGRenderLayer::Background::SkyBoxCubeMap:
2275 lightProbeTexture = renderer.contextInterface()->bufferManager()->loadRenderImage(layer.skyBoxCubeMap,
2276 QSSGBufferManager::MipModeDisable);
2278 case QSSGRenderLayer::Background::SkyMaterial:
2279 lightProbeTexture = renderData->skyMaterialTexture;
2282 Q_UNREACHABLE_RETURN();
2285 if (lightProbeTexture.m_texture ==
nullptr) {
2288 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2291 for (QRhiShaderResourceBindings *&srb : entry->m_skyBoxSrbs)
2294 layer.skyBoxSrb =
nullptr;
2299 const bool cubeMapMode = layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap;
2301 if (cubeFace == QSSGRenderTextureCubeFaceNone)
2302 layer.skyBoxIsRgbe8 = lightProbeTexture.m_flags.isRgbe8();
2304 layer.skyBoxIsSrgb = !lightProbeTexture.m_flags.isLinear();
2306 QSSGRhiShaderResourceBindingList bindings;
2308 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
2309 QRhiSampler::Linear,
2310 cubeMapMode ? QRhiSampler::None : QRhiSampler::Linear,
2311 QRhiSampler::Repeat,
2312 QRhiSampler::ClampToEdge,
2313 QRhiSampler::Repeat });
2314 int samplerBinding = 1;
2315 bindings.addTexture(samplerBinding, QRhiShaderResourceBinding::FragmentStage, lightProbeTexture.m_texture, sampler);
2317 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2318 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * cubeFaceIdx;
2319 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey,
nullptr, entry, entryIdx });
2321 QRhi *rhi = rhiCtx->rhi();
2322 const quint32 ubufSize = cameras.count() >= 2 ? 416 : 240;
2323 if (!dcd.ubuf || dcd.ubuf->size() != ubufSize) {
2325 dcd.ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
int(ubufSize));
2329 float adjustY = rhi->isYUpInNDC() ? 1.0f : -1.0f;
2330 const float exposure = layer.lightProbeSettings.probeExposure;
2332 const QMatrix3x3 &rotationMatrix(layer.lightProbeSettings.probeOrientation);
2335 const float blurAmountOrSrgb = cubeMapMode ? layer.skyBoxIsSrgb : layer.skyboxBlurAmount;
2336 const float maxMipLevelOrTonemapMode = cubeMapMode ?
float(tonemapMode) :
float(lightProbeTexture.m_mipmapCount - 2);
2338 const QVector4D skyboxProperties = {
2342 maxMipLevelOrTonemapMode
2345 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2346 quint32 ubufOffset = 0;
2348 memcpy(ubufData + ubufOffset, &skyboxProperties, 16);
2351 memcpy(ubufData + ubufOffset, rotationMatrix.constData(), 12);
2353 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 12, 12);
2355 memcpy(ubufData + ubufOffset, (
char *)rotationMatrix.constData() + 24, 12);
2358 for (qsizetype viewIdx = 0; viewIdx < cameras.count(); ++viewIdx) {
2359 const QMatrix4x4 &inverseProjection = cameras[viewIdx]->projection.inverted();
2360 const QMatrix4x4 &viewMatrix = renderData->getGlobalTransform(*cameras[viewIdx]);
2361 QMatrix4x4 viewProjection(Qt::Uninitialized);
2362 cameras[viewIdx]->calculateViewProjectionWithoutTranslation(viewMatrix, 0.1f, 5.0f, viewProjection);
2364 quint32 viewDataOffset = ubufOffset;
2365 memcpy(ubufData + viewDataOffset + viewIdx * 64, viewProjection.constData(), 64);
2366 viewDataOffset += cameras.count() * 64;
2367 memcpy(ubufData + viewDataOffset + viewIdx * 64, inverseProjection.constData(), 64);
2368 viewDataOffset += cameras.count() * 64;
2369 memcpy(ubufData + viewDataOffset + viewIdx * 48, viewMatrix.constData(), 48);
2371 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2373 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd.ubuf);
2375 if (cubeFace != QSSGRenderTextureCubeFaceNone) {
2376 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
2377 entry->m_skyBoxSrbs[cubeFaceIdx] = rhiCtxD->srb(bindings);
2379 layer.skyBoxSrb = rhiCtxD->srb(bindings);
2383 renderer.rhiCubeRenderer()->prepareCube(rhiCtx,
nullptr);
2385 renderer.rhiQuadRenderer()->prepareQuad(rhiCtx,
nullptr);
2424 QSSGPassKey passKey,
2425 const QSSGRhiGraphicsPipelineState &basePipelineState,
2426 QRhiRenderPassDescriptor *rpDesc,
2428 const QSSGRenderableObjectList &sortedOpaqueObjects,
2429 const QSSGRenderableObjectList &sortedTransparentObjects,
2433 static const auto rhiPrepareDepthPassForObject = [](QSSGRhiContext *rhiCtx,
2434 QSSGPassKey passKey,
2437 QRhiRenderPassDescriptor *rpDesc,
2438 QSSGRhiGraphicsPipelineState *ps) {
2439 QSSGRhiShaderPipelinePtr shaderPipeline;
2440 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2442 const bool isOpaqueDepthPrePass = obj->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass;
2443 QSSGShaderFeatures featureSet;
2444 featureSet.set(QSSGShaderFeatures::Feature::DepthPass,
true);
2445 if (isOpaqueDepthPrePass)
2446 featureSet.set(QSSGShaderFeatures::Feature::OpaqueDepthPrePass,
true);
2448 QSSGRhiDrawCallData *dcd =
nullptr;
2449 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2450 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2451 const void *modelNode = &subsetRenderable.modelContext.model;
2452 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material, 0 });
2455 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2456 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2457 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2458 ps->cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2460 shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet);
2461 if (shaderPipeline) {
2462 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2463 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2464 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2465 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2469 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2470 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2472 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2474 ps->cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2476 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
2477 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2479 if (shaderPipeline) {
2480 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2481 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2482 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, ps, customMaterial, subsetRenderable,
2483 inData.renderedCameras,
nullptr,
nullptr);
2484 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2491 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2492 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2493 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
2494 ia = subsetRenderable.subset.rhi.ia;
2496 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
2497 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
2498 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
2500 QSSGRhiShaderResourceBindingList bindings;
2501 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
2504 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
2506 if (isOpaqueDepthPrePass) {
2507 addOpaqueDepthPrePassBindings(rhiCtx,
2508 shaderPipeline.get(),
2509 subsetRenderable.firstImage,
2511 (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset));
2517 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
2518 if (normalTextureBinding >= 0) {
2519 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2520 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
2521 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
2522 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
2523 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
2524 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
2528 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
2529 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
2531 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2532 QRhiSampler::Nearest,
2534 QRhiSampler::ClampToEdge,
2535 QRhiSampler::ClampToEdge,
2538 bindings.addTexture(binding,
2539 QRhiShaderResourceBinding::VertexStage,
2546 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
2547 if (targetsTexture) {
2548 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
2550 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2551 QRhiSampler::Nearest,
2553 QRhiSampler::ClampToEdge,
2554 QRhiSampler::ClampToEdge,
2555 QRhiSampler::ClampToEdge
2557 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
2561 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
2563 subsetRenderable.rhiRenderData.depthPrePass.pipeline = rhiCtxD->pipeline(*ps,
2566 subsetRenderable.rhiRenderData.depthPrePass.srb = srb;
2577 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2582 ps.samples = samples;
2583 ps.viewCount = viewCount;
2584 ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled };
2585 ps.targetBlend[0].colorWrite = {};
2587 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2588 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2592 for (
const QSSGRenderableObjectHandle &handle : sortedTransparentObjects) {
2593 if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps))
2720 QSSGPassKey passKey,
2721 const QSSGRhiGraphicsPipelineState &basePipelineState,
2722 QRhiRenderPassDescriptor *rpDesc,
2724 const QSSGRenderableObjectList &sortedOpaqueObjects)
2726 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2727 ps.depthFunc = QRhiGraphicsPipeline::LessOrEqual;
2728 ps.flags.setFlag(QSSGRhiGraphicsPipelineState::Flag::BlendEnabled,
false);
2730 for (
const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) {
2731 QSSGRenderableObject *obj = handle.obj;
2732 QSSGRhiShaderPipelinePtr shaderPipeline;
2733 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2735 QSSGShaderFeatures featureSet;
2736 featureSet.set(QSSGShaderFeatures::Feature::NormalPass,
true);
2738 QSSGRhiDrawCallData *dcd =
nullptr;
2739 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2740 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2741 const void *modelNode = &subsetRenderable.modelContext.model;
2742 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material, 0 });
2745 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
2746 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2747 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
2748 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
2750 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
2751 if (shaderPipeline) {
2752 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2753 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2754 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
2755 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2757 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2758 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2760 const auto &customMaterial =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
2762 ps.cullMode = QSSGRhiHelpers::toCullMode(customMaterial.m_cullMode);
2764 const auto &customMaterialSystem = subsetRenderable.renderer->contextInterface()->customMaterialSystem();
2765 shaderPipeline = customMaterialSystem->shadersForCustomMaterial(&ps, customMaterial, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
2767 if (shaderPipeline) {
2768 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
2769 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
2770 customMaterialSystem->updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, customMaterial, subsetRenderable,
2771 inData.renderedCameras,
nullptr,
nullptr);
2772 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
2777 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
2778 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
2779 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
2780 ia = subsetRenderable.subset.rhi.ia;
2782 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
2783 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
2784 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
2786 QSSGRhiShaderResourceBindingList bindings;
2787 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
2790 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
2791 while (renderableImage) {
2792 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
2793 const int samplerHint =
int(renderableImage->m_mapType);
2794 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
2795 if (samplerBinding >= 0) {
2796 QRhiTexture *texture = renderableImage->m_texture.m_texture;
2797 if (samplerBinding >= 0 && texture) {
2798 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
2799 QSSGRhiSamplerDescription samplerDesc = {
2800 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
2801 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
2802 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
2803 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
2804 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
2805 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
2807 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
2808 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
2809 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
2812 renderableImage = renderableImage->m_nextImage;
2815 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
2818 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
2819 if (normalTextureBinding >= 0) {
2820 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
2821 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
2822 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
2823 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
2824 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
2825 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
2831 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
2832 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
2834 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2835 QRhiSampler::Nearest,
2837 QRhiSampler::ClampToEdge,
2838 QRhiSampler::ClampToEdge,
2841 bindings.addTexture(binding,
2842 QRhiShaderResourceBinding::VertexStage,
2849 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
2850 if (targetsTexture) {
2851 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
2853 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
2854 QRhiSampler::Nearest,
2856 QRhiSampler::ClampToEdge,
2857 QRhiSampler::ClampToEdge,
2858 QRhiSampler::ClampToEdge
2860 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
2864 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
2866 subsetRenderable.rhiRenderData.normalPass.pipeline = rhiCtxD->pipeline(ps,
2869 subsetRenderable.rhiRenderData.normalPass.srb = srb;
2938 QSSGPassKey passKey,
2939 const QSSGRhiGraphicsPipelineState &basePipelineState,
2940 QRhiRenderPassDescriptor *rpDesc,
2941 QSSGRenderGraphObject *overrideMaterial,
2943 QSSGRenderableObjectList &inObjects,
2944 QSSGShaderFeatures featureSet)
2947 const qsizetype index = overrideMaterial ? inData.getUserRenderPassManager()->acquireUserPassSlot() : -1;
2952 QSSGRhiGraphicsPipelineState ps = basePipelineState;
2953 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
2955 const bool isCustomMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::CustomMaterial);
2956 const bool isDefaultMaterial = (overrideMaterial->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
2957 overrideMaterial->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
2958 overrideMaterial->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial);
2960 if (!isCustomMaterial && !isDefaultMaterial) {
2961 qDebug() <<
"Override material must be a default or custom material.";
2965 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
2966 QSSGRenderableObject *obj = handle.obj;
2968 if (obj->type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
2969 obj->type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
2972 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
2973 const void *modelNode = &subsetRenderable.modelContext.model;
2974 QSSGRhiDrawCallData *dcd = &rhiCtxD->drawCallData({ passKey, modelNode, overrideMaterial, 0 });
2980 QSSGRenderableObjectFlags renderableFlags = subsetRenderable.renderableFlags;
2981 float opacity = subsetRenderable.opacity;
2984 const bool hasAnyLights = !subsetRenderable.lights.isEmpty();
2985 const bool anyLightHasShadows = std::any_of(subsetRenderable.lights.begin(),
2986 subsetRenderable.lights.end(),
2987 [](
const QSSGShaderLight &light) {
return light.shadows; });
2991 auto layerPrepFlags = inData.layerPrepResult.getFlags();
2993 if (isCustomMaterial) {
2994 auto &material =
static_cast<QSSGRenderCustomMaterial &>(*overrideMaterial);
2995 auto prepResult = inData.prepareCustomMaterialForRender(material, renderableFlags, opacity,
2996 false, hasAnyLights, anyLightHasShadows,
2998 subsetRenderable.shaderDescription = prepResult.materialKey;
3000 auto &material =
static_cast<QSSGRenderDefaultMaterial &>(*overrideMaterial);
3001 auto prepResult = inData.prepareDefaultMaterialForRender(material, renderableFlags, opacity,
3002 hasAnyLights, anyLightHasShadows,
3004 subsetRenderable.shaderDescription = prepResult.materialKey;
3008 QSSGRhiShaderPipelinePtr shaderPipeline;
3010 if (isCustomMaterial) {
3011 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(*overrideMaterial);
3012 if (!ps.userSetCullMode)
3013 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3015 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3016 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3017 inData.getDefaultMaterialPropertyTable(), featureSet);
3018 if (shaderPipeline) {
3019 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3020 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3021 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3022 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3023 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3026 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(*overrideMaterial);
3027 if (!ps.userSetCullMode)
3028 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3030 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3031 if (shaderPipeline) {
3032 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3033 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3034 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3035 inData.renderedCameras,
nullptr,
nullptr, overrideMaterial);
3036 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3040 if (!shaderPipeline)
3043 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3044 ia = subsetRenderable.subset.rhi.ia;
3046 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3047 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3048 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3050 QSSGRhiShaderResourceBindingList bindings;
3051 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3054 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3055 while (renderableImage) {
3056 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3057 const int samplerHint =
int(renderableImage->m_mapType);
3058 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3059 if (samplerBinding >= 0) {
3060 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3062 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3063 QSSGRhiSamplerDescription samplerDesc = {
3064 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3065 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3066 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3067 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3068 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3069 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3071 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3072 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3073 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3076 renderableImage = renderableImage->m_nextImage;
3079 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3082 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3083 if (normalTextureBinding >= 0) {
3084 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3085 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3086 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3087 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3088 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3089 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3093 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3094 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3096 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3097 QRhiSampler::Nearest,
3099 QRhiSampler::ClampToEdge,
3100 QRhiSampler::ClampToEdge,
3103 bindings.addTexture(binding,
3104 QRhiShaderResourceBinding::VertexStage,
3111 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3112 if (targetsTexture) {
3113 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3115 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3116 QRhiSampler::Nearest,
3118 QRhiSampler::ClampToEdge,
3119 QRhiSampler::ClampToEdge,
3120 QRhiSampler::ClampToEdge
3122 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3127 if (shaderPipeline->isLightingEnabled()) {
3128 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3129 shaderPipeline->ub0LightDataOffset(),
3130 sizeof(QSSGShaderLightsUniformData));
3131 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3132 shaderPipeline->ub0DirectionalLightDataOffset(),
3133 sizeof(QSSGShaderDirectionalLightsUniformData));
3136 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3137 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3140 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3141 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3142 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3145 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3146 QRhiSampler::Linear,
3148 QRhiSampler::ClampToEdge,
3149 QRhiSampler::ClampToEdge,
3150 QRhiSampler::Repeat });
3152 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3156 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3157 QRhiTexture *texture = shaderPipeline->shadowMapBlueNoiseTexture();
3160 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3161 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3162 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3165 QRhiSampler *sampler = rhiCtx->sampler(
3166 { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3168 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3172 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3174 auto texture = shaderPipeline->lightProbeTexture();
3176 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3177 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3178 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3182 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3183 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3184 QRhiSampler::Linear,
3185 QRhiSampler::Linear,
3186 QSSGRhiHelpers::toRhi(tiling.first),
3187 QSSGRhiHelpers::toRhi(tiling.second),
3188 QRhiSampler::Repeat });
3189 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3193 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
3195 QSSG_ASSERT(srb,
continue);
3196 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3197 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3198 rhiPassData.srb = srb;
3205 QSSGPassKey passKey,
3206 const QSSGRhiGraphicsPipelineState &basePipelineState,
3207 QRhiRenderPassDescriptor *rpDesc,
3209 QSSGRenderableObjectList &inObjects,
3210 QSSGShaderFeatures featureSet)
3212 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3217 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3218 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3220 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3221 QSSGRenderableObject *obj = handle.obj;
3222 QSSGRhiShaderPipelinePtr shaderPipeline;
3224 QSSGRhiDrawCallData *dcd =
nullptr;
3225 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3226 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3227 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3228 const void *modelNode = &subsetRenderable.modelContext.model;
3229 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material, 0 });
3232 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3233 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3234 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3235 if (!ps.userSetCullMode)
3236 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3238 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet);
3239 if (shaderPipeline) {
3240 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3241 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3242 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable,
3243 inData.renderedCameras,
nullptr,
nullptr);
3244 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3246 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3247 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3248 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3249 if (!ps.userSetCullMode)
3250 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3252 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3253 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable,
3254 inData.getDefaultMaterialPropertyTable(), featureSet);
3255 if (shaderPipeline) {
3256 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3257 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3258 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3259 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3260 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3264 if (!shaderPipeline)
3268 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset ||
3269 obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3270 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3271 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3272 ia = subsetRenderable.subset.rhi.ia;
3274 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3275 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3276 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3278 QSSGRhiShaderResourceBindingList bindings;
3279 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3282 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3283 while (renderableImage) {
3284 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3285 const int samplerHint =
int(renderableImage->m_mapType);
3286 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3287 if (samplerBinding >= 0) {
3288 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3289 if (samplerBinding >= 0 && texture) {
3290 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3291 QSSGRhiSamplerDescription samplerDesc = {
3292 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3293 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3294 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3295 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3296 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3297 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3299 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3300 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3301 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3304 renderableImage = renderableImage->m_nextImage;
3307 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3310 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3311 if (normalTextureBinding >= 0) {
3312 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3313 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3314 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3315 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3316 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3317 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3321 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3322 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3324 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3325 QRhiSampler::Nearest,
3327 QRhiSampler::ClampToEdge,
3328 QRhiSampler::ClampToEdge,
3331 bindings.addTexture(binding,
3332 QRhiShaderResourceBinding::VertexStage,
3339 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3340 if (targetsTexture) {
3341 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3343 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3344 QRhiSampler::Nearest,
3346 QRhiSampler::ClampToEdge,
3347 QRhiSampler::ClampToEdge,
3348 QRhiSampler::ClampToEdge
3350 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3355 if (shaderPipeline->isLightingEnabled()) {
3356 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3357 shaderPipeline->ub0LightDataOffset(),
3358 sizeof(QSSGShaderLightsUniformData));
3359 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3360 shaderPipeline->ub0DirectionalLightDataOffset(),
3361 sizeof(QSSGShaderDirectionalLightsUniformData));
3364 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3365 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3368 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3369 texture = rhiCtx->dummyTexture({ }, resourceUpdates);
3370 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3373 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3374 QRhiSampler::Linear,
3376 QRhiSampler::ClampToEdge,
3377 QRhiSampler::ClampToEdge,
3378 QRhiSampler::Repeat });
3380 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3384 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3385 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
3386 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
3388 QRhiTexture *texture = shadowMapBlueNoise;
3389 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3390 QRhiSampler::Linear,
3392 QRhiSampler::Repeat,
3393 QRhiSampler::Repeat,
3394 QRhiSampler::Repeat });
3395 Q_ASSERT(texture && sampler);
3396 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3399 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3400 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
3401 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3402 QRhiSampler::Linear,
3404 QRhiSampler::Repeat,
3405 QRhiSampler::Repeat,
3406 QRhiSampler::Repeat });
3407 Q_ASSERT(texture && sampler);
3408 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3409 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3414 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
3415 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
3416 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
3417 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
3418 ? QRhiSampler::Linear
3419 : QRhiSampler::None;
3420 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3421 QRhiSampler::Linear,
3423 QRhiSampler::ClampToEdge,
3424 QRhiSampler::ClampToEdge,
3425 QRhiSampler::Repeat });
3426 if (reflectionSampler >= 0 && reflectionTexture)
3427 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
3430 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3432 auto texture = shaderPipeline->lightProbeTexture();
3434 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3435 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3436 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3440 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3441 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3442 QRhiSampler::Linear,
3443 QRhiSampler::Linear,
3444 QSSGRhiHelpers::toRhi(tiling.first),
3445 QSSGRhiHelpers::toRhi(tiling.second),
3446 QRhiSampler::Repeat });
3447 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3451 if (shaderPipeline->screenTexture()) {
3452 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
3453 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
3454 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
3455 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
3456 ? QRhiSampler::Linear : QRhiSampler::None;
3457 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
3458 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3459 if (screenTextureBinding >= 0) {
3460 bindings.addTexture(screenTextureBinding,
3461 QRhiShaderResourceBinding::FragmentStage,
3462 shaderPipeline->screenTexture(), sampler);
3464 if (screenTextureArrayBinding >= 0) {
3465 bindings.addTexture(screenTextureArrayBinding,
3466 QRhiShaderResourceBinding::FragmentStage,
3467 shaderPipeline->screenTexture(), sampler);
3472 if (shaderPipeline->lightmapTexture()) {
3473 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
3475 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
3476 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3477 bindings.addTexture(binding,
3478 QRhiShaderResourceBinding::FragmentStage,
3479 shaderPipeline->lightmapTexture(), sampler);
3486 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3487 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3489 int maxSamplerBinding = -1;
3490 QVector<QShaderDescription::InOutVariable> samplerVars =
3491 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3492 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3493 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3494 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3495 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3496 if (it == samplerVars.cend())
3497 samplerVars.append(var);
3500 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3501 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3503 if (maxSamplerBinding >= 0) {
3504 int extraTexCount = shaderPipeline->extraTextureCount();
3505 for (
int i = 0; i < extraTexCount; ++i) {
3506 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
3507 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
3508 if (samplerBinding >= 0) {
3509 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
3510 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
3511 bindings.addTexture(samplerBinding,
3512 QRhiShaderResourceBinding::FragmentStage,
3520 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
3522 QSSG_ASSERT(srb,
continue);
3524 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3525 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3526 rhiPassData.srb = srb;
3535 QSSGPassKey passKey,
3536 const QSSGRhiGraphicsPipelineState &basePipelineState,
3537 QRhiRenderPassDescriptor *rpDesc,
3540 QSSGRenderableObjectList &inObjects,
3541 QSSGShaderFeatures featureSet)
3543 const qsizetype index = inData.getUserRenderPassManager()->acquireUserPassSlot();
3549 QSSGRhiGraphicsPipelineState ps = basePipelineState;
3550 for (
const QSSGRenderableObjectHandle &handle : std::as_const(inObjects)) {
3551 QSSGRenderableObject *obj = handle.obj;
3552 QSSGRhiShaderPipelinePtr shaderPipeline;
3553 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3554 const auto &bufferManager = inData.contextInterface()->bufferManager();
3556 featureSet.set(QSSGShaderFeatures::Feature::UserRenderPass,
true);
3558 QSSGRhiDrawCallData *dcd =
nullptr;
3559 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3560 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3561 const void *modelNode = &subsetRenderable.modelContext.model;
3562 dcd = &rhiCtxD->drawCallData({ passKey, modelNode, &subsetRenderable.material, 0 });
3565 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) {
3566 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3567 const auto &material =
static_cast<
const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial());
3568 if (!ps.userSetCullMode)
3569 ps.cullMode = QSSGRhiHelpers::toCullMode(material.cullMode);
3571 shaderPipeline = shadersForDefaultMaterial(&ps, subsetRenderable, featureSet, shaderAugmentation);
3572 if (shaderPipeline) {
3573 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3574 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3576 for (
const auto &u : shaderAugmentation.propertyUniforms)
3577 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3578 updateUniformsForDefaultMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3579 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3581 }
else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3582 QSSGSubsetRenderable &subsetRenderable(*
static_cast<QSSGSubsetRenderable *>(obj));
3583 const auto &material =
static_cast<
const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial());
3584 if (!ps.userSetCullMode)
3585 ps.cullMode = QSSGRhiHelpers::toCullMode(material.m_cullMode);
3587 QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get());
3589 if (material.m_shadingMode == QSSGRenderCustomMaterial::ShadingMode::Unshaded)
3590 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet);
3592 shaderPipeline = customMaterialSystem.shadersForCustomMaterial(&ps, material, subsetRenderable, inData.getDefaultMaterialPropertyTable(), featureSet, shaderAugmentation);
3594 if (shaderPipeline) {
3595 shaderPipeline->ensureCombinedUniformBuffer(&dcd->ubuf);
3596 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
3598 for (
const auto &u : shaderAugmentation.propertyUniforms)
3599 shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
3600 customMaterialSystem.updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, inData, ubufData, &ps, material,
3601 subsetRenderable, inData.renderedCameras,
nullptr,
nullptr);
3602 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
3606 if (!shaderPipeline) {
3608 qDebug() <<
"Failed to prepare user augmented pass for object.";
3613 if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) {
3614 QSSGSubsetRenderable &subsetRenderable(
static_cast<QSSGSubsetRenderable &>(*obj));
3615 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps);
3616 ia = subsetRenderable.subset.rhi.ia;
3618 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
3619 int instanceBufferBinding = setupInstancing(&subsetRenderable, &ps, rhiCtx, cameraDatas[0].direction, cameraDatas[0].position);
3620 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
3622 QSSGRhiShaderResourceBindingList bindings;
3623 bindings.addUniformBuffer(0, RENDERER_VISIBILITY_ALL, dcd->ubuf);
3626 QSSGRenderableImage *renderableImage = subsetRenderable.firstImage;
3627 while (renderableImage) {
3628 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
3629 const int samplerHint =
int(renderableImage->m_mapType);
3630 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
3631 if (samplerBinding >= 0) {
3632 QRhiTexture *texture = renderableImage->m_texture.m_texture;
3633 if (samplerBinding >= 0 && texture) {
3634 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
3635 QSSGRhiSamplerDescription samplerDesc = {
3636 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
3637 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
3638 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
3639 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
3640 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
3641 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
3643 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
3644 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
3645 bindings.addTexture(samplerBinding, RENDERER_VISIBILITY_ALL, texture, sampler);
3648 renderableImage = renderableImage->m_nextImage;
3651 addDepthTextureBindings(rhiCtx, shaderPipeline.get(), bindings);
3654 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
3655 if (normalTextureBinding >= 0) {
3656 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
3657 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3658 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3659 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
3660 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3661 bindings.addTexture(normalTextureBinding, RENDERER_VISIBILITY_ALL, dummyTexture, sampler);
3667 if (QRhiTexture *boneTexture = inData.getBonemapTexture(subsetRenderable.modelContext)) {
3668 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
3670 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3671 QRhiSampler::Nearest,
3673 QRhiSampler::ClampToEdge,
3674 QRhiSampler::ClampToEdge,
3677 bindings.addTexture(binding,
3678 QRhiShaderResourceBinding::VertexStage,
3685 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
3686 if (targetsTexture) {
3687 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
3689 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
3690 QRhiSampler::Nearest,
3692 QRhiSampler::ClampToEdge,
3693 QRhiSampler::ClampToEdge,
3694 QRhiSampler::ClampToEdge
3696 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
3701 if (shaderPipeline->isLightingEnabled()) {
3702 bindings.addUniformBuffer(1, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3703 shaderPipeline->ub0LightDataOffset(),
3704 sizeof(QSSGShaderLightsUniformData));
3705 bindings.addUniformBuffer(2, RENDERER_VISIBILITY_ALL, dcd->ubuf,
3706 shaderPipeline->ub0DirectionalLightDataOffset(),
3707 sizeof(QSSGShaderDirectionalLightsUniformData));
3710 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture"); binding >= 0) {
3711 QRhiTexture *texture = shaderPipeline->shadowMapAtlasTexture();
3714 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3715 texture = rhiCtx->dummyTexture({ }, resourceUpdates, QSize(1, 1), Qt::black, 2);
3716 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3719 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3720 QRhiSampler::Linear,
3722 QRhiSampler::ClampToEdge,
3723 QRhiSampler::ClampToEdge,
3724 QRhiSampler::Repeat });
3726 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3730 if (
int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture"); binding >= 0) {
3731 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
3732 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
3734 QRhiTexture *texture = shadowMapBlueNoise;
3735 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3736 QRhiSampler::Linear,
3738 QRhiSampler::Repeat,
3739 QRhiSampler::Repeat,
3740 QRhiSampler::Repeat });
3741 Q_ASSERT(texture && sampler);
3742 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3745 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3746 QRhiTexture *texture = rhiCtx->dummyTexture({}, resourceUpdates);
3747 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3748 QRhiSampler::Linear,
3750 QRhiSampler::Repeat,
3751 QRhiSampler::Repeat,
3752 QRhiSampler::Repeat });
3753 Q_ASSERT(texture && sampler);
3754 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3755 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3762 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
3763 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
3764 QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
3765 const auto mipMapFilter = reflectionTexture && reflectionTexture->flags().testFlag(QRhiTexture::Flag::MipMapped)
3766 ? QRhiSampler::Linear
3767 : QRhiSampler::None;
3768 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3769 QRhiSampler::Linear,
3771 QRhiSampler::ClampToEdge,
3772 QRhiSampler::ClampToEdge,
3773 QRhiSampler::Repeat });
3774 if (reflectionSampler >= 0 && reflectionTexture)
3775 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
3777 if (
int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
3779 auto texture = shaderPipeline->lightProbeTexture();
3781 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3782 texture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
3783 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3787 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
3788 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear,
3789 QRhiSampler::Linear,
3790 QRhiSampler::Linear,
3791 QSSGRhiHelpers::toRhi(tiling.first),
3792 QSSGRhiHelpers::toRhi(tiling.second),
3793 QRhiSampler::Repeat });
3794 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
3798 if (shaderPipeline->screenTexture()) {
3799 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
3800 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
3801 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
3806 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
3807 ? QRhiSampler::Linear : QRhiSampler::None;
3808 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
3809 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
3810 if (screenTextureBinding >= 0) {
3811 bindings.addTexture(screenTextureBinding,
3812 QRhiShaderResourceBinding::FragmentStage,
3813 shaderPipeline->screenTexture(), sampler);
3815 if (screenTextureArrayBinding >= 0) {
3816 bindings.addTexture(screenTextureArrayBinding,
3817 QRhiShaderResourceBinding::FragmentStage,
3818 shaderPipeline->screenTexture(), sampler);
3823 if (shaderPipeline->lightmapTexture()) {
3824 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
3826 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
3827 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
3828 bindings.addTexture(binding,
3829 QRhiShaderResourceBinding::FragmentStage,
3830 shaderPipeline->lightmapTexture(), sampler);
3837 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
3838 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
3840 QVector<QShaderDescription::InOutVariable> samplerVars =
3841 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
3842 const auto combinedSamplers = shaderPipeline->vertexStage()->shader().description().combinedImageSamplers();
3843 for (
const QShaderDescription::InOutVariable &var : combinedSamplers) {
3844 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
3845 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
3846 if (it == samplerVars.cend())
3847 samplerVars.append(var);
3850 int maxSamplerBinding = -1;
3851 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
3852 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
3861 QBitArray samplerBindingsSpecified(maxSamplerBinding + 1);
3863 if (maxSamplerBinding >= 0) {
3865 int extraTexCount = shaderPipeline->extraTextureCount();
3866 for (
int i = 0; i < extraTexCount; ++i) {
3867 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
3868 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
3869 if (samplerBinding >= 0) {
3870 samplerBindingsSpecified.setBit(samplerBinding);
3871 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
3872 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
3873 bindings.addTexture(samplerBinding,
3874 QRhiShaderResourceBinding::FragmentStage,
3882 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
3883 QSSG_ASSERT(srb,
return -1);
3884 auto &rhiPassData = subsetRenderable.rhiRenderData.userPassData[index];
3885 rhiPassData.pipeline = rhiCtxD->pipeline(ps, rpDesc, srb);
3886 rhiPassData.srb = srb;
3950 QSSGPassKey passKey,
3952 const QMatrix4x4 &viewProjection,
3954 QRhiRenderPassDescriptor *renderPassDescriptor,
3955 QSSGRhiGraphicsPipelineState *ps,
3956 QSSGRenderMotionVectorMap &motionVectorMapManager)
3958 if (inObject.type != QSSGRenderableObject::Type::DefaultMaterialMeshSubset &&
3959 inObject.type != QSSGRenderableObject::Type::CustomMaterialMeshSubset)
3962 QSSGSubsetRenderable &subsetRenderable =
static_cast<QSSGSubsetRenderable &>(inObject);
3963 auto &modelNode = subsetRenderable.modelContext.model;
3965 bool skin = modelNode.usesBoneTexture();
3966 bool instance = modelNode.instanceCount() > 0;
3967 bool morph = modelNode.morphTargets.size() > 0;
3969 QSSGRhiShaderPipelinePtr shaderPipeline = inData.contextInterface()->shaderCache()->
3970 getBuiltInRhiShaders().getRhiMotionVectorShader(skin, instance, morph);
3972 if (!shaderPipeline)
3975 QSSGRhiShaderResourceBindingList bindings;
3977 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
3978 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ passKey, &modelNode,
nullptr, 0 });
3981 QSSGRenderTextureData *boneTextureData =
nullptr;
3983 boneTextureData = modelNode.skin;
3984 else if (modelNode.skeleton)
3985 boneTextureData = &modelNode.skeleton->boneTexData;
3987 QMatrix4x4 modelViewProjection = subsetRenderable.modelContext.modelViewProjections[0];
3988 QMatrix4x4 instanceLocal;
3989 QMatrix4x4 instanceGlobal;
3992 instanceLocal = inData.getInstanceTransforms(modelNode).local;
3993 instanceGlobal = inData.getInstanceTransforms(modelNode).global;
3995 modelViewProjection = viewProjection;
3997 auto motionData = motionVectorMapManager.trackMotionData(&modelNode,
3998 modelViewProjection,
4002 modelNode.instanceTable,
4003 modelNode.morphWeights);
4004 float velocityAmount = subsetRenderable.modelContext.model.motionVectorScale;
4005 int morphTargetCount = modelNode.morphWeights.count();
4017 const int ubufSize = 416;
4020 dcd.ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic,
4021 QRhiBuffer::UniformBuffer,
4026 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
4028 memcpy(ubufData, modelViewProjection.constData(), 64);
4030 memcpy(ubufData + ubufOffset, motionData.prevModelViewProjection.constData(), 64);
4032 memcpy(ubufData + ubufOffset, instanceLocal.constData(), 64);
4034 memcpy(ubufData + ubufOffset, instanceGlobal.constData(), 64);
4036 memcpy(ubufData + ubufOffset, motionData.prevInstanceLocal.constData(), 64);
4038 memcpy(ubufData + ubufOffset, motionData.prevInstanceGlobal.constData(), 64);
4040 memcpy(ubufData + ubufOffset, &inData.layer.currentAndLastJitter, 16);
4042 memcpy(ubufData + ubufOffset, &velocityAmount, 4);
4044 memcpy(ubufData + ubufOffset, &morphTargetCount, 4);
4045 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
4047 bindings.addUniformBuffer(0, QRhiShaderResourceBinding::VertexStage |
4048 QRhiShaderResourceBinding::FragmentStage, dcd.ubuf);
4050 QRhiSampler *nearestSampler =
nullptr;
4052 if (skin || instance) {
4053 nearestSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4054 QRhiSampler::Nearest,
4056 QRhiSampler::ClampToEdge,
4057 QRhiSampler::ClampToEdge,
4063 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
4066 bindings.addTexture(binding,
4067 QRhiShaderResourceBinding::VertexStage,
4068 inData.getBonemapTexture(subsetRenderable.modelContext),
4070 binding = shaderPipeline->bindingForTexture(
"lastBoneTexture");
4072 bindings.addTexture(binding,
4073 QRhiShaderResourceBinding::VertexStage,
4074 motionData.prevBoneTexture.m_texture,
4081 int binding = shaderPipeline->bindingForTexture(
"lastInstanceTexture");
4083 bindings.addTexture(binding,
4084 QRhiShaderResourceBinding::VertexStage,
4085 motionData.prevInstanceTexture.m_texture,
4090 auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture;
4091 if (targetsTexture) {
4092 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
4094 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
4095 QRhiSampler::Nearest,
4097 QRhiSampler::ClampToEdge,
4098 QRhiSampler::ClampToEdge,
4099 QRhiSampler::ClampToEdge
4101 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, subsetRenderable.subset.rhi.targetsTexture, targetsSampler);
4103 if ((binding = shaderPipeline->bindingForTexture(
"morphWeightTexture")) >= 0)
4104 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.currentMorphWeightTexture.m_texture, targetsSampler);
4106 if ((binding = shaderPipeline->bindingForTexture(
"lastMorphWeightTexture")) >= 0)
4107 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, motionData.prevMorphWeightTexture.m_texture, targetsSampler);
4111 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
4112 ia = subsetRenderable.subset.rhi.ia;
4113 const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData);
4114 QVector3D cameraDirection = cameraDatas[0].direction;
4115 QVector3D cameraPosition = cameraDatas[0].position;
4116 int instanceBufferBinding = setupInstancing(&subsetRenderable, ps, rhiCtx, cameraDirection, cameraPosition);
4117 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
4118 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(*ps, shaderPipeline.get());
4120 QRhiShaderResourceBindings *&srb = dcd.srb;
4121 bool srbChanged =
false;
4122 if (!srb || bindings != dcd.bindings) {
4123 srb = rhiCtxD->srb(bindings);
4124 rhiCtxD->releaseCachedSrb(dcd.bindings);
4125 dcd.bindings = bindings;
4129 subsetRenderable.rhiRenderData.motionVectorPass.srb = srb;
4131 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
4134 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
4135 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
4138 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = dcd.pipeline;
4141 subsetRenderable.rhiRenderData.motionVectorPass.pipeline = rhiCtxD->pipeline(pipelineKey,
4142 renderPassDescriptor,
4144 dcd.pipeline = subsetRenderable.rhiRenderData.motionVectorPass.pipeline;
4145 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
4146 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;