mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-03 15:09:39 +00:00
Fix race condition
Values are accessed from main thread and cell preloader threads.
This commit is contained in:
parent
52d4e25314
commit
c2b8c7086a
1 changed files with 149 additions and 82 deletions
|
@ -12,66 +12,150 @@
|
||||||
|
|
||||||
#include <components/shader/shadermanager.hpp>
|
#include <components/shader/shadermanager.hpp>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
class BlendmapTexMat
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::TexMat>& value(const int blendmapScale)
|
||||||
|
{
|
||||||
|
static BlendmapTexMat instance;
|
||||||
|
return instance.get(blendmapScale);
|
||||||
|
}
|
||||||
|
|
||||||
|
const osg::ref_ptr<osg::TexMat>& get(const int blendmapScale)
|
||||||
|
{
|
||||||
|
const std::lock_guard<std::mutex> lock(mMutex);
|
||||||
|
auto texMat = mTexMatMap.find(blendmapScale);
|
||||||
|
if (texMat == mTexMatMap.end())
|
||||||
|
{
|
||||||
|
osg::Matrixf matrix;
|
||||||
|
float scale = (blendmapScale/(static_cast<float>(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<float, osg::ref_ptr<osg::TexMat>> mTexMatMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LayerTexMat
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::TexMat>& value(const float layerTileSize)
|
||||||
|
{
|
||||||
|
static LayerTexMat instance;
|
||||||
|
return instance.get(layerTileSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
const osg::ref_ptr<osg::TexMat>& get(const float layerTileSize)
|
||||||
|
{
|
||||||
|
const std::lock_guard<std::mutex> 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<float, osg::ref_ptr<osg::TexMat>> mTexMatMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EqualDepth
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::Depth>& value()
|
||||||
|
{
|
||||||
|
static EqualDepth instance;
|
||||||
|
return instance.mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
osg::ref_ptr<osg::Depth> mValue;
|
||||||
|
|
||||||
|
EqualDepth()
|
||||||
|
: mValue(new osg::Depth)
|
||||||
|
{
|
||||||
|
mValue->setFunction(osg::Depth::EQUAL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class LequalDepth
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::Depth>& value()
|
||||||
|
{
|
||||||
|
static LequalDepth instance;
|
||||||
|
return instance.mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
osg::ref_ptr<osg::Depth> mValue;
|
||||||
|
|
||||||
|
LequalDepth()
|
||||||
|
: mValue(new osg::Depth)
|
||||||
|
{
|
||||||
|
mValue->setFunction(osg::Depth::LEQUAL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BlendFunc
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::BlendFunc>& value()
|
||||||
|
{
|
||||||
|
static BlendFunc instance;
|
||||||
|
return instance.mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
osg::ref_ptr<osg::BlendFunc> mValue;
|
||||||
|
|
||||||
|
BlendFunc()
|
||||||
|
: mValue(new osg::BlendFunc)
|
||||||
|
{
|
||||||
|
mValue->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class TexEnvCombine
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const osg::ref_ptr<osg::TexEnvCombine>& value()
|
||||||
|
{
|
||||||
|
static TexEnvCombine instance;
|
||||||
|
return instance.mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
osg::ref_ptr<osg::TexEnvCombine> mValue;
|
||||||
|
|
||||||
|
TexEnvCombine()
|
||||||
|
: mValue(new osg::TexEnvCombine)
|
||||||
|
{
|
||||||
|
mValue->setCombine_RGB(osg::TexEnvCombine::REPLACE);
|
||||||
|
mValue->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
namespace Terrain
|
namespace Terrain
|
||||||
{
|
{
|
||||||
|
|
||||||
osg::ref_ptr<osg::TexMat> getBlendmapTexMat(int blendmapScale)
|
|
||||||
{
|
|
||||||
static std::map<int, osg::ref_ptr<osg::TexMat> > texMatMap;
|
|
||||||
osg::ref_ptr<osg::TexMat> texMat = texMatMap[blendmapScale];
|
|
||||||
if (!texMat)
|
|
||||||
{
|
|
||||||
osg::Matrixf matrix;
|
|
||||||
float scale = (blendmapScale/(static_cast<float>(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;
|
|
||||||
}
|
|
||||||
return texMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::TexMat> getLayerTexMat(float layerTileSize)
|
|
||||||
{
|
|
||||||
static std::map<float, osg::ref_ptr<osg::TexMat> > texMatMap;
|
|
||||||
osg::ref_ptr<osg::TexMat> texMat = texMatMap[layerTileSize];
|
|
||||||
if (!texMat)
|
|
||||||
{
|
|
||||||
texMat = new osg::TexMat(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f)));
|
|
||||||
|
|
||||||
texMatMap[layerTileSize] = texMat;
|
|
||||||
}
|
|
||||||
return texMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::Depth> getEqualDepth()
|
|
||||||
{
|
|
||||||
static osg::ref_ptr<osg::Depth> depth;
|
|
||||||
if (!depth)
|
|
||||||
{
|
|
||||||
depth = new osg::Depth;
|
|
||||||
depth->setFunction(osg::Depth::EQUAL);
|
|
||||||
}
|
|
||||||
return depth;
|
|
||||||
}
|
|
||||||
osg::ref_ptr<osg::Depth> getLequalDepth()
|
|
||||||
{
|
|
||||||
static osg::ref_ptr<osg::Depth> depth;
|
|
||||||
if (!depth)
|
|
||||||
{
|
|
||||||
depth = new osg::Depth;
|
|
||||||
depth->setFunction(osg::Depth::LEQUAL);
|
|
||||||
}
|
|
||||||
return depth;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector<TextureLayer> &layers,
|
std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector<TextureLayer> &layers,
|
||||||
const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps, int blendmapScale, float layerTileSize)
|
const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps, int blendmapScale, float layerTileSize)
|
||||||
{
|
{
|
||||||
|
@ -87,16 +171,9 @@ namespace Terrain
|
||||||
|
|
||||||
if (!firstLayer)
|
if (!firstLayer)
|
||||||
{
|
{
|
||||||
static osg::ref_ptr<osg::BlendFunc> blendFunc;
|
|
||||||
if (!blendFunc)
|
|
||||||
{
|
|
||||||
blendFunc= new osg::BlendFunc();
|
|
||||||
blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
|
|
||||||
}
|
|
||||||
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
|
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
|
||||||
stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON);
|
stateset->setAttributeAndModes(BlendFunc::value(), osg::StateAttribute::ON);
|
||||||
|
stateset->setAttributeAndModes(EqualDepth::value(), osg::StateAttribute::ON);
|
||||||
stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON);
|
|
||||||
}
|
}
|
||||||
// disable fog if we're the first layer of several - supposed to be completely black
|
// disable fog if we're the first layer of several - supposed to be completely black
|
||||||
if (firstLayer && blendmaps.size() > 0)
|
if (firstLayer && blendmaps.size() > 0)
|
||||||
|
@ -105,7 +182,7 @@ namespace Terrain
|
||||||
fog->setStart(10000000);
|
fog->setStart(10000000);
|
||||||
fog->setEnd(10000000);
|
fog->setEnd(10000000);
|
||||||
stateset->setAttributeAndModes(fog, osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
|
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;
|
int texunit = 0;
|
||||||
|
@ -115,7 +192,7 @@ namespace Terrain
|
||||||
stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap);
|
stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap);
|
||||||
|
|
||||||
if (layerTileSize != 1.f)
|
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));
|
stateset->addUniform(new osg::Uniform("diffuseMap", texunit));
|
||||||
|
|
||||||
|
@ -125,8 +202,7 @@ namespace Terrain
|
||||||
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++);
|
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++);
|
||||||
|
|
||||||
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
|
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
|
||||||
|
stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale));
|
||||||
stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale));
|
|
||||||
stateset->addUniform(new osg::Uniform("blendMap", texunit));
|
stateset->addUniform(new osg::Uniform("blendMap", texunit));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,17 +241,8 @@ namespace Terrain
|
||||||
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
|
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
|
||||||
|
|
||||||
// This is to map corner vertices directly to the center of a blendmap texel.
|
// This is to map corner vertices directly to the center of a blendmap texel.
|
||||||
stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale));
|
stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale));
|
||||||
|
stateset->setTextureAttributeAndModes(texunit, TexEnvCombine::value(), osg::StateAttribute::ON);
|
||||||
static osg::ref_ptr<osg::TexEnvCombine> 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);
|
|
||||||
|
|
||||||
++texunit;
|
++texunit;
|
||||||
}
|
}
|
||||||
|
@ -185,7 +252,7 @@ namespace Terrain
|
||||||
stateset->setTextureAttributeAndModes(texunit, tex.get());
|
stateset->setTextureAttributeAndModes(texunit, tex.get());
|
||||||
|
|
||||||
if (layerTileSize != 1.f)
|
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");
|
stateset->setRenderBinDetails(passIndex++, "RenderBin");
|
||||||
|
|
Loading…
Reference in a new issue