Fix exit crash when CTextureBump is destroyed after _NameToNF

feature/pre-code-move
kaetemi 5 years ago committed by Jan Boon
parent f0687a8157
commit 0244c348ee

@ -84,14 +84,6 @@ protected:
float *_NormalizationFactor; float *_NormalizationFactor;
bool _DisableSharing; bool _DisableSharing;
bool _ForceNormalize; bool _ForceNormalize;
// Map that give the normalization factor for each map from its sharename. This avoid to generate several time the maps to get the normalization factor if a bumpmap is shared by severals CTextureBump instances;
struct CNormalizationInfo
{
uint NumRefs;
float NormalizationFactor;
};
typedef std::map<std::string, CNormalizationInfo> TNameToNI; // sharename to the normalization factor
static TNameToNI _NameToNF; // name to normalization factor
private: private:
/// we don't allow for mipmap for bump so we redefine this to prevent the user from doing this on the base class Itexture /// we don't allow for mipmap for bump so we redefine this to prevent the user from doing this on the base class Itexture
virtual void setFilterMode(TMagFilter magf, TMinFilter minf); virtual void setFilterMode(TMagFilter magf, TMinFilter minf);

@ -26,9 +26,31 @@
namespace NL3D { namespace NL3D {
namespace /* anonymous */ {
CTextureBump::TNameToNI CTextureBump::_NameToNF; // Map that give the normalization factor for each map from its sharename. This avoid to generate several time the maps to get the normalization factor if a bumpmap is shared by severals CTextureBump instances;
struct CNormalizationInfo
{
uint NumRefs;
float NormalizationFactor;
};
typedef std::map<std::string, CNormalizationInfo> TNameToNI; // sharename to the normalization factor
class CNameToNFStatic {
public:
TNameToNI Map;
bool Initialized;
CNameToNFStatic() : Initialized(true)
{
}
~CNameToNFStatic()
{
Initialized = false;
}
};
CNameToNFStatic s_NameToNF;
} /* anonymous namespace */
#define GET_HGT(x, y) ((sint) ((src[(uint) (x) % width + ((uint) (y) % height) * width] & 0x00ff00) >> 8)) #define GET_HGT(x, y) ((sint) ((src[(uint) (x) % width + ((uint) (y) % height) * width] & 0x00ff00) >> 8))
/// create a DsDt texture from a height map (red component of a rgba bitmap) /// create a DsDt texture from a height map (red component of a rgba bitmap)
@ -242,14 +264,15 @@ void CTextureBump::doGenerate(bool async)
} }
// create entry in the map for the normalization factor // create entry in the map for the normalization factor
std::string shareName = getShareName(); std::string shareName = getShareName();
TNameToNI::iterator it = _NameToNF.find(shareName); nlassertverbose(s_NameToNF.Initialized);
if (it == _NameToNF.end()) TNameToNI::iterator it = s_NameToNF.Map.find(shareName);
if (it == s_NameToNF.Map.end())
{ {
// create a new entry // create a new entry
CNormalizationInfo ni; CNormalizationInfo ni;
ni.NumRefs = 1; ni.NumRefs = 1;
ni.NormalizationFactor = normalizationFactor; ni.NormalizationFactor = normalizationFactor;
std::pair<TNameToNI::iterator, bool> pb = _NameToNF.insert(TNameToNI::value_type(shareName, ni)); std::pair<TNameToNI::iterator, bool> pb = s_NameToNF.Map.insert(TNameToNI::value_type(shareName, ni));
_NormalizationFactor = &(pb.first->second.NormalizationFactor); _NormalizationFactor = &(pb.first->second.NormalizationFactor);
} }
else else
@ -303,8 +326,9 @@ float CTextureBump::getNormalizationFactor()
if (!_HeightMap) return 1.f; if (!_HeightMap) return 1.f;
// not computed yet, see if another map has computed it // not computed yet, see if another map has computed it
std::string shareName = getShareName(); std::string shareName = getShareName();
TNameToNI::iterator it = _NameToNF.find(shareName); nlassertverbose(s_NameToNF.Initialized);
if (it != _NameToNF.end()) TNameToNI::iterator it = s_NameToNF.Map.find(shareName);
if (it != s_NameToNF.Map.end())
{ {
_NormalizationFactor = &(it->second.NormalizationFactor); _NormalizationFactor = &(it->second.NormalizationFactor);
++(it->second.NumRefs); ++(it->second.NumRefs);
@ -320,16 +344,16 @@ float CTextureBump::getNormalizationFactor()
///============================================================================================== ///==============================================================================================
CTextureBump::~CTextureBump() CTextureBump::~CTextureBump()
{ {
if (_NormalizationFactor && !_NameToNF.empty()) if (s_NameToNF.Initialized && _NormalizationFactor && !s_NameToNF.Map.empty())
{ {
// find normalization factor from its name // find normalization factor from its name
TNameToNI::iterator it = _NameToNF.find(getShareName()); TNameToNI::iterator it = s_NameToNF.Map.find(getShareName());
// if found // if found
if (it != _NameToNF.end()) if (it != s_NameToNF.Map.end())
{ {
// we can delete it only if it's not used anymore // we can delete it only if it's not used anymore
if (--(it->second.NumRefs) == 0) _NameToNF.erase(it); if (--(it->second.NumRefs) == 0) s_NameToNF.Map.erase(it);
} }
} }
} }

Loading…
Cancel
Save