From c2b8c7086a7009362e6d512cc9d4f2ab05d616ed Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 12 Aug 2018 00:52:02 +0300 Subject: [PATCH] Fix race condition Values are accessed from main thread and cell preloader threads. --- components/terrain/material.cpp | 199 +++++++++++++++++++++----------- 1 file changed, 133 insertions(+), 66 deletions(-) diff --git a/components/terrain/material.cpp b/components/terrain/material.cpp index 722df95078b..a0f051524b5 100644 --- a/components/terrain/material.cpp +++ b/components/terrain/material.cpp @@ -12,66 +12,150 @@ #include +#include -namespace Terrain +namespace { + class BlendmapTexMat + { + public: + static const osg::ref_ptr& value(const int blendmapScale) + { + static BlendmapTexMat instance; + return instance.get(blendmapScale); + } - osg::ref_ptr getBlendmapTexMat(int blendmapScale) + const osg::ref_ptr& get(const int blendmapScale) + { + const std::lock_guard lock(mMutex); + auto texMat = mTexMatMap.find(blendmapScale); + if (texMat == mTexMatMap.end()) + { + osg::Matrixf matrix; + float scale = (blendmapScale/(static_cast(blendmapScale)+1.f)); + matrix.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f)); + matrix.preMultScale(osg::Vec3f(scale, scale, 1.f)); + matrix.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f)); + // We need to nudge the blendmap to look like vanilla. + // This causes visible seams unless the blendmap's resolution is doubled, but Vanilla also doubles the blendmap, apparently. + matrix.preMultTranslate(osg::Vec3f(1.0f/blendmapScale/4.0f, 1.0f/blendmapScale/4.0f, 0.f)); + + texMat = mTexMatMap.insert(std::make_pair(blendmapScale, new osg::TexMat(matrix))).first; + } + return texMat->second; + } + + private: + std::mutex mMutex; + std::map> mTexMatMap; + }; + + class LayerTexMat { - static std::map > texMatMap; - osg::ref_ptr texMat = texMatMap[blendmapScale]; - if (!texMat) + public: + static const osg::ref_ptr& value(const float layerTileSize) { - osg::Matrixf matrix; - float scale = (blendmapScale/(static_cast(blendmapScale)+1.f)); - matrix.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f)); - matrix.preMultScale(osg::Vec3f(scale, scale, 1.f)); - matrix.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f)); - // We need to nudge the blendmap to look like vanilla. - // This causes visible seams unless the blendmap's resolution is doubled, but Vanilla also doubles the blendmap, apparently. - matrix.preMultTranslate(osg::Vec3f(1.0f/blendmapScale/4.0f, 1.0f/blendmapScale/4.0f, 0.f)); - - texMat = new osg::TexMat(matrix); - - texMatMap[blendmapScale] = texMat; + static LayerTexMat instance; + return instance.get(layerTileSize); } - return texMat; - } - osg::ref_ptr getLayerTexMat(float layerTileSize) + const osg::ref_ptr& get(const float layerTileSize) + { + const std::lock_guard lock(mMutex); + auto texMat = mTexMatMap.find(layerTileSize); + if (texMat == mTexMatMap.end()) + { + texMat = mTexMatMap.insert(std::make_pair(layerTileSize, + new osg::TexMat(osg::Matrix::scale(osg::Vec3f(layerTileSize, layerTileSize, 1.f))))).first; + } + return texMat->second; + } + + private: + std::mutex mMutex; + std::map> mTexMatMap; + }; + + class EqualDepth { - static std::map > texMatMap; - osg::ref_ptr texMat = texMatMap[layerTileSize]; - if (!texMat) + public: + static const osg::ref_ptr& value() { - texMat = new osg::TexMat(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); + static EqualDepth instance; + return instance.mValue; + } + + private: + osg::ref_ptr mValue; - texMatMap[layerTileSize] = texMat; + EqualDepth() + : mValue(new osg::Depth) + { + mValue->setFunction(osg::Depth::EQUAL); } - return texMat; - } + }; - osg::ref_ptr getEqualDepth() + class LequalDepth { - static osg::ref_ptr depth; - if (!depth) + public: + static const osg::ref_ptr& value() { - depth = new osg::Depth; - depth->setFunction(osg::Depth::EQUAL); + static LequalDepth instance; + return instance.mValue; } - return depth; - } - osg::ref_ptr getLequalDepth() + + private: + osg::ref_ptr mValue; + + LequalDepth() + : mValue(new osg::Depth) + { + mValue->setFunction(osg::Depth::LEQUAL); + } + }; + + class BlendFunc { - static osg::ref_ptr depth; - if (!depth) + public: + static const osg::ref_ptr& value() { - depth = new osg::Depth; - depth->setFunction(osg::Depth::LEQUAL); + static BlendFunc instance; + return instance.mValue; } - return depth; - } + private: + osg::ref_ptr mValue; + + BlendFunc() + : mValue(new osg::BlendFunc) + { + mValue->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE); + } + }; + + class TexEnvCombine + { + public: + static const osg::ref_ptr& value() + { + static TexEnvCombine instance; + return instance.mValue; + } + + private: + osg::ref_ptr mValue; + + TexEnvCombine() + : mValue(new osg::TexEnvCombine) + { + mValue->setCombine_RGB(osg::TexEnvCombine::REPLACE); + mValue->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); + } + }; +} + +namespace Terrain +{ std::vector > createPasses(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector &layers, const std::vector > &blendmaps, int blendmapScale, float layerTileSize) { @@ -87,16 +171,9 @@ namespace Terrain if (!firstLayer) { - static osg::ref_ptr blendFunc; - if (!blendFunc) - { - blendFunc= new osg::BlendFunc(); - blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE); - } stateset->setMode(GL_BLEND, osg::StateAttribute::ON); - stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON); - - stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON); + stateset->setAttributeAndModes(BlendFunc::value(), osg::StateAttribute::ON); + stateset->setAttributeAndModes(EqualDepth::value(), osg::StateAttribute::ON); } // disable fog if we're the first layer of several - supposed to be completely black if (firstLayer && blendmaps.size() > 0) @@ -105,7 +182,7 @@ namespace Terrain fog->setStart(10000000); fog->setEnd(10000000); stateset->setAttributeAndModes(fog, osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); - stateset->setAttributeAndModes(getLequalDepth(), osg::StateAttribute::ON); + stateset->setAttributeAndModes(LequalDepth::value(), osg::StateAttribute::ON); } int texunit = 0; @@ -115,7 +192,7 @@ namespace Terrain stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); if (layerTileSize != 1.f) - stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); + stateset->setTextureAttributeAndModes(texunit, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); @@ -125,8 +202,7 @@ namespace Terrain osg::ref_ptr blendmap = blendmaps.at(blendmapIndex++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); - - stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); + stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale)); stateset->addUniform(new osg::Uniform("blendMap", texunit)); } @@ -165,17 +241,8 @@ namespace Terrain stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. - stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); - - static osg::ref_ptr texEnvCombine; - if (!texEnvCombine) - { - texEnvCombine = new osg::TexEnvCombine; - texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE); - texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); - } - - stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON); + stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale)); + stateset->setTextureAttributeAndModes(texunit, TexEnvCombine::value(), osg::StateAttribute::ON); ++texunit; } @@ -185,7 +252,7 @@ namespace Terrain stateset->setTextureAttributeAndModes(texunit, tex.get()); if (layerTileSize != 1.f) - stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); + stateset->setTextureAttributeAndModes(texunit, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON); } stateset->setRenderBinDetails(passIndex++, "RenderBin");