From 481e2a723040929ee54da0f0d66ca4e1c38cd22c Mon Sep 17 00:00:00 2001 From: Lukas Aldershaab Date: Mon, 4 Jan 2021 20:06:17 +0100 Subject: [PATCH] Improve terrain rendering, handle bug with no detail --- Engine/source/gfx/gfxTextureArray.cpp | 2 + .../source/terrain/glsl/terrFeatureGLSL.cpp | 12 ++-- .../source/terrain/hlsl/terrFeatureHLSL.cpp | 10 ++- Engine/source/terrain/terrCellMaterial.cpp | 68 +++++++++++++++---- Engine/source/terrain/terrCellMaterial.h | 6 +- 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/Engine/source/gfx/gfxTextureArray.cpp b/Engine/source/gfx/gfxTextureArray.cpp index c648386c1..af0873fee 100644 --- a/Engine/source/gfx/gfxTextureArray.cpp +++ b/Engine/source/gfx/gfxTextureArray.cpp @@ -49,6 +49,7 @@ void GFXTextureArray::set(U32 width, U32 height, U32 size, GFXFormat format, U32 bool GFXTextureArray::fromTextureArray(const Vector& textureArray, U32 capacity) { + PROFILE_SCOPE(GFXTextureArray_fromTextureArray) bool success = true; // Not initialized, infer it from the given array of textures @@ -114,6 +115,7 @@ bool GFXTextureArray::fromTextureArray(const Vector& textureArray, void GFXTextureArray::setTexture(const GFXTexHandle& texture, U32 slot) { + PROFILE_SCOPE(GFXTextureArray_setTexture) GFXTexHandle handle = texture; if (texture->getPath().isNotEmpty()) { diff --git a/Engine/source/terrain/glsl/terrFeatureGLSL.cpp b/Engine/source/terrain/glsl/terrFeatureGLSL.cpp index bf9cb9c46..14f321cce 100644 --- a/Engine/source/terrain/glsl/terrFeatureGLSL.cpp +++ b/Engine/source/terrain/glsl/terrFeatureGLSL.cpp @@ -1289,10 +1289,14 @@ void TerrainHeightMapBlendGLSL::processVert( return; MultiLine* meta = new MultiLine; - - // Make sure the world to tangent transform - // is created and available for the pixel shader. - getOutViewToTangent(componentList, meta, fd); + + // Handle an edge-case when there are no detail-maps available + if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0) + { + // Make sure the world to tangent transform + // is created and available for the pixel shader. + getOutViewToTangent(componentList, meta, fd); + } output = meta; } diff --git a/Engine/source/terrain/hlsl/terrFeatureHLSL.cpp b/Engine/source/terrain/hlsl/terrFeatureHLSL.cpp index ba381887f..e136c82eb 100644 --- a/Engine/source/terrain/hlsl/terrFeatureHLSL.cpp +++ b/Engine/source/terrain/hlsl/terrFeatureHLSL.cpp @@ -1370,9 +1370,13 @@ void TerrainHeightMapBlendHLSL::processVert(Vector& componentL MultiLine* meta = new MultiLine; - // Make sure the world to tangent transform - // is created and available for the pixel shader. - getOutViewToTangent(componentList, meta, fd); + // Handle an edge-case when there are no detail-maps available + if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0) + { + // Make sure the world to tangent transform + // is created and available for the pixel shader. + getOutViewToTangent(componentList, meta, fd); + } output = meta; } diff --git a/Engine/source/terrain/terrCellMaterial.cpp b/Engine/source/terrain/terrCellMaterial.cpp index 0f3b64e14..41ad84f0d 100644 --- a/Engine/source/terrain/terrCellMaterial.cpp +++ b/Engine/source/terrain/terrCellMaterial.cpp @@ -675,16 +675,53 @@ bool TerrainCellMaterial::_initShader(bool deferredMat, void TerrainCellMaterial::_updateMaterialConsts( ) { PROFILE_SCOPE( TerrainCellMaterial_UpdateMaterialConsts ); - if (mMaterialInfos.empty()) + + int detailMatCount = 0; + for (MaterialInfo* materialInfo : mMaterialInfos) + { + if (materialInfo == NULL) + continue; + + TerrainMaterial* mat = materialInfo->mat; + + if (mat == NULL) + continue; + + // We only include materials that + // have more than a base texture. + if (mat->getDetailSize() <= 0 || + mat->getDetailDistance() <= 0 || + mat->getDetailMap().isEmpty()) + continue; + + detailMatCount++; + } + + if (detailMatCount == 0) { return; } - AlignedArray detailInfoArray(mMaterialInfos.size(), sizeof(Point4F)); - AlignedArray detailScaleAndFadeArray(mMaterialInfos.size(), sizeof(Point4F)); - for ( U32 j=0; j < mMaterialInfos.size(); j++ ) + AlignedArray detailInfoArray(detailMatCount, sizeof(Point4F)); + AlignedArray detailScaleAndFadeArray(detailMatCount, sizeof(Point4F)); + + int detailIndex = 0; + for (MaterialInfo* matInfo : mMaterialInfos) { - MaterialInfo *matInfo = mMaterialInfos[j]; + if (matInfo == NULL) + continue; + + TerrainMaterial* mat = matInfo->mat; + + if (mat == NULL) + continue; + + // We only include materials that + // have more than a base texture. + if (mat->getDetailSize() <= 0 || + mat->getDetailDistance() <= 0 || + mat->getDetailMap().isEmpty()) + continue; F32 detailSize = matInfo->mat->getDetailSize(); F32 detailScale = 1.0f; @@ -716,11 +753,19 @@ void TerrainCellMaterial::_updateMaterialConsts( ) matInfo->mat->getDetailStrength(), matInfo->mat->getParallaxScale(), 0 ); - detailScaleAndFadeArray[j] = detailScaleAndFade; - detailInfoArray[j] = detailIdStrengthParallax; + detailScaleAndFadeArray[detailIndex] = detailScaleAndFade; + detailInfoArray[detailIndex] = detailIdStrengthParallax; - mConsts->setSafe(matInfo->mBlendDepthConst, matInfo->mat->getBlendDepth()); - mConsts->setSafe(matInfo->mBlendContrastConst, matInfo->mat->getBlendContrast()); + if (matInfo->mBlendDepthConst != NULL) + { + mConsts->setSafe(matInfo->mBlendDepthConst, matInfo->mat->getBlendDepth()); + } + + if (matInfo->mBlendContrastConst != NULL) + { + mConsts->setSafe(matInfo->mBlendContrastConst, matInfo->mat->getBlendContrast()); + } + detailIndex++; } mConsts->setSafe(mDetailInfoVArrayConst, detailScaleAndFadeArray); @@ -739,11 +784,6 @@ bool TerrainCellMaterial::setupPass( const SceneRenderState *state, return false; } - if (mMaterialInfos.size() > 4) - { - int a = 2 + 2; - } - mCurrPass++; _updateMaterialConsts(); diff --git a/Engine/source/terrain/terrCellMaterial.h b/Engine/source/terrain/terrCellMaterial.h index c23f19e29..46facf70b 100644 --- a/Engine/source/terrain/terrCellMaterial.h +++ b/Engine/source/terrain/terrCellMaterial.h @@ -59,7 +59,7 @@ protected: public: MaterialInfo() - :mat(NULL), layerId(0) + :mat(NULL), layerId(0), mBlendDepthConst(NULL), mBlendContrastConst(NULL) { } @@ -69,8 +69,8 @@ protected: TerrainMaterial *mat; U32 layerId; - GFXShaderConstHandle* mBlendDepthConst; - GFXShaderConstHandle* mBlendContrastConst; + GFXShaderConstHandle *mBlendDepthConst; + GFXShaderConstHandle *mBlendContrastConst; }; ///