OSG < 3.4.x fixes

This will use the inherently unsafe versions of the load methods which can result in deleting an object (from the cache) that has just been loaded in the database thread.

Symptom OSG WARN deleting still referenced object.
This commit is contained in:
Richard Harrison
2019-01-26 17:48:31 +01:00
parent 30482a3599
commit b76f78c6da
8 changed files with 95 additions and 2 deletions
+32
View File
@@ -275,7 +275,11 @@ bool setAttrs(const TexTuple& attrs, Texture* tex,
options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED); options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED);
else else
options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS);
#if OSG_VERSION_LESS_THAN(3,4,2)
result = osgDB::readImageFile(imageName, options);
#else
result = osgDB::readRefImageFile(imageName, options); result = osgDB::readRefImageFile(imageName, options);
#endif
options->setLoadOriginHint(origLOH); options->setLoadOriginHint(origLOH);
osg::ref_ptr<osg::Image> image; osg::ref_ptr<osg::Image> image;
if (result.success()) if (result.success())
@@ -607,32 +611,56 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*
SGReaderWriterOptions* wOpts = (SGReaderWriterOptions*)options; SGReaderWriterOptions* wOpts = (SGReaderWriterOptions*)options;
SGReaderWriterOptions::LoadOriginHint origLOH = wOpts->getLoadOriginHint(); SGReaderWriterOptions::LoadOriginHint origLOH = wOpts->getLoadOriginHint();
wOpts->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); wOpts->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS);
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<0>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<0>(), options); result = osgDB::readRefImageFile(_tuple.get<0>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image); cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<1>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<1>(), options); result = osgDB::readRefImageFile(_tuple.get<1>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image); cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<2>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<2>(), options); result = osgDB::readRefImageFile(_tuple.get<2>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image); cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<3>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<3>(), options); result = osgDB::readRefImageFile(_tuple.get<3>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image); cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<4>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<4>(), options); result = osgDB::readRefImageFile(_tuple.get<4>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image); cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(_tuple.get<5>(), options);
#else
result = osgDB::readRefImageFile(_tuple.get<5>(), options); result = osgDB::readRefImageFile(_tuple.get<5>(), options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image); cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
@@ -657,7 +685,11 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*
return cubeTexture.release(); return cubeTexture.release();
osgDB::ReaderWriter::ReadResult result; osgDB::ReaderWriter::ReadResult result;
#if OSG_VERSION_LESS_THAN(3,4,0)
result = osgDB::readImageFile(texname, options);
#else
result = osgDB::readRefImageFile(texname, options); result = osgDB::readRefImageFile(texname, options);
#endif
if(result.success()) { if(result.success()) {
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones
+4
View File
@@ -227,7 +227,11 @@ SGMaterial::read_properties(const SGReaderWriterOptions* options,
} }
else else
{ {
#if OSG_VERSION_LESS_THAN(3,4,0)
osg::Image* image = osgDB::readImageFile(fullMaskPath, options);
#else
osg::Image* image = osgDB::readRefImageFile(fullMaskPath, options); osg::Image* image = osgDB::readRefImageFile(fullMaskPath, options);
#endif
if (image && image->valid()) if (image && image->valid())
{ {
Texture2DRef object_mask = new osg::Texture2D; Texture2DRef object_mask = new osg::Texture2D;
+12
View File
@@ -215,7 +215,11 @@ public:
if (pagedLOD.getMinRange(i) <= 0) { if (pagedLOD.getMinRange(i) <= 0) {
osg::ref_ptr<const osgDB::Options> options; osg::ref_ptr<const osgDB::Options> options;
options = getOptions(pagedLOD.getDatabaseOptions(), pagedLOD.getDatabasePath()); options = getOptions(pagedLOD.getDatabaseOptions(), pagedLOD.getDatabasePath());
#if OSG_VERSION_LESS_THAN(3,4,0)
node = osgDB::readNodeFile(pagedLOD.getFileName(i), options.get());
#else
node = osgDB::readRefNodeFile(pagedLOD.getFileName(i), options.get()); node = osgDB::readRefNodeFile(pagedLOD.getFileName(i), options.get());
#endif
} }
if (!node.valid()) if (!node.valid())
node = new osg::Group; node = new osg::Group;
@@ -256,7 +260,11 @@ public:
osg::ref_ptr<const osgDB::Options> options; osg::ref_ptr<const osgDB::Options> options;
options = getOptions(proxyNode.getDatabaseOptions(), proxyNode.getDatabasePath()); options = getOptions(proxyNode.getDatabaseOptions(), proxyNode.getDatabasePath());
osg::ref_ptr<osg::Node> node; osg::ref_ptr<osg::Node> node;
#if OSG_VERSION_LESS_THAN(3,4,0)
node = osgDB::readNodeFile(proxyNode.getFileName(i), options.get());
#else
node = osgDB::readRefNodeFile(proxyNode.getFileName(i), options.get()); node = osgDB::readRefNodeFile(proxyNode.getFileName(i), options.get());
#endif
if (!node.valid()) if (!node.valid())
node = new osg::Group; node = new osg::Group;
if (i < proxyNode.getNumChildren()) if (i < proxyNode.getNumChildren())
@@ -353,7 +361,11 @@ SGSharedPtr<BVHNode>
BVHPageNodeOSG::load(const std::string& name, const osg::ref_ptr<const osg::Referenced>& options) BVHPageNodeOSG::load(const std::string& name, const osg::ref_ptr<const osg::Referenced>& options)
{ {
osg::ref_ptr<osg::Node> node; osg::ref_ptr<osg::Node> node;
#if OSG_VERSION_LESS_THAN(3,4,0)
node = osgDB::readNodeFile(name, dynamic_cast<const osgDB::Options*>(options.get()));
#else
node = osgDB::readRefNodeFile(name, dynamic_cast<const osgDB::Options*>(options.get())); node = osgDB::readRefNodeFile(name, dynamic_cast<const osgDB::Options*>(options.get()));
#endif
if (!node.valid()) if (!node.valid())
return SGSharedPtr<BVHNode>(); return SGSharedPtr<BVHNode>();
@@ -338,7 +338,11 @@ sgLoad3DModel_internal(const SGPath& path,
options->setDatabasePath(texturepath.local8BitStr()); options->setDatabasePath(texturepath.local8BitStr());
osgDB::ReaderWriter::ReadResult modelResult; osgDB::ReaderWriter::ReadResult modelResult;
#if OSG_VERSION_LESS_THAN(3,4,0)
modelResult = osgDB::readNodeFile(modelpath.local8BitStr(), options.get());
#else
modelResult = osgDB::readRefNodeFile(modelpath.local8BitStr(), options.get()); modelResult = osgDB::readRefNodeFile(modelpath.local8BitStr(), options.get());
#endif
if (!modelResult.validNode()) if (!modelResult.validNode())
throw sg_io_exception("Failed to load 3D model:" + modelResult.message(), throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
modelpath); modelpath);
+15 -2
View File
@@ -43,9 +43,18 @@ SGLoadTexture2D(bool staticTexture, const std::string& path,
{ {
osg::Image* image; osg::Image* image;
if (options) if (options)
image = osgDB::readRefImageFile(path, options); #if OSG_VERSION_LESS_THAN(3,4,0)
image = osgDB::readImageFile(path, options);
#else
image = osgDB::readRefImageFile(path, options);
#endif
else else
image = osgDB::readRefImageFile(path); #if OSG_VERSION_LESS_THAN(3,4,0)
image = osgDB::readImageFile(path);
#else
image = osgDB::readRefImageFile(path);
#endif
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(image); texture->setImage(image);
if (staticTexture) if (staticTexture)
@@ -141,7 +150,11 @@ Texture2D* TextureUpdateVisitor::textureReplace(int unit, const StateAttribute*
// If it is empty or they are identical then there is nothing to do // If it is empty or they are identical then there is nothing to do
if (fullLiveryFile.empty() || fullLiveryFile == *fullFilePath) if (fullLiveryFile.empty() || fullLiveryFile == *fullFilePath)
return 0; return 0;
#if OSG_VERSION_LESS_THAN(3,4,0)
Image* newImage = readImageFile(fullLiveryFile);
#else
Image* newImage = readRefImageFile(fullLiveryFile); Image* newImage = readRefImageFile(fullLiveryFile);
#endif
if (!newImage) if (!newImage)
return 0; return 0;
CopyOp copyOp(CopyOp::DEEP_COPY_ALL & ~CopyOp::DEEP_COPY_IMAGES); CopyOp copyOp(CopyOp::DEEP_COPY_ALL & ~CopyOp::DEEP_COPY_IMAGES);
+4
View File
@@ -101,7 +101,11 @@ osg::Node* loadFile(const string& path, SGReaderWriterOptions* options)
options->setInstantiateEffects(true); options->setInstantiateEffects(true);
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
ref_ptr<Node> model = readNodeFile(path, options);
#else
ref_ptr<Node> model = readRefNodeFile(path, options); ref_ptr<Node> model = readRefNodeFile(path, options);
#endif
if (!model) if (!model)
return 0; return 0;
else else
+16
View File
@@ -208,7 +208,11 @@ ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* o
imageFileName = osgDB::concatPaths(imageFileName, "Globe"); imageFileName = osgDB::concatPaths(imageFileName, "Globe");
imageFileName = osgDB::concatPaths(imageFileName, "world.topo.bathy.200407.3x4096x2048.png"); imageFileName = osgDB::concatPaths(imageFileName, "world.topo.bathy.200407.3x4096x2048.png");
} }
#if OSG_VERSION_LESS_THAN(3,4,0)
if (osg::Image* image = osgDB::readImageFile(imageFileName, options)) {
#else
if (osg::Image* image = osgDB::readRefImageFile(imageFileName, options)) { if (osg::Image* image = osgDB::readRefImageFile(imageFileName, options)) {
#endif
osg::Texture2D* texture = new osg::Texture2D; osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image); texture->setImage(image);
texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT); texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
@@ -256,7 +260,11 @@ ReaderWriterSPT::createTree(const BucketBox& bucketBox, const LocalOptions& opti
if (bucketBox.getIsBucketSize()) { if (bucketBox.getIsBucketSize()) {
std::string fileName; std::string fileName;
fileName = bucketBox.getBucket().gen_index_str() + std::string(".stg"); fileName = bucketBox.getBucket().gen_index_str() + std::string(".stg");
#if OSG_VERSION_LESS_THAN(3,4,0)
return osgDB::readNodeFile(fileName, options._options);
#else
return osgDB::readRefNodeFile(fileName, options._options); return osgDB::readRefNodeFile(fileName, options._options);
#endif
} else if (!topLevel && options.isPageLevel(bucketBox.getStartLevel())) { } else if (!topLevel && options.isPageLevel(bucketBox.getStartLevel())) {
return createPagedLOD(bucketBox, options); return createPagedLOD(bucketBox, options);
} else { } else {
@@ -314,7 +322,11 @@ ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const LocalOptions&
std::string fileName = osgDB::findDataFile(lodPath + extensions[i], options._options); std::string fileName = osgDB::findDataFile(lodPath + extensions[i], options._options);
if (fileName.empty()) if (fileName.empty())
continue; continue;
#if OSG_VERSION_LESS_THAN(3,4,0)
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(fileName, options._options);
#else
osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFile(fileName, options._options); osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFile(fileName, options._options);
#endif
if (!node.valid()) if (!node.valid())
continue; continue;
pagedLOD->addChild(node.get(), range, std::numeric_limits<float>::max()); pagedLOD->addChild(node.get(), range, std::numeric_limits<float>::max());
@@ -412,7 +424,11 @@ ReaderWriterSPT::getLowLODStateSet(const LocalOptions& options) const
localOptions = static_cast<osgDB::Options*>(options._options->clone(osg::CopyOp())); localOptions = static_cast<osgDB::Options*>(options._options->clone(osg::CopyOp()));
localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL); localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
#if OSG_VERSION_LESS_THAN(3,4,0)
osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get());
#else
osg::ref_ptr<osg::Object> object = osgDB::readRefObjectFile("state.spt", localOptions.get()); osg::ref_ptr<osg::Object> object = osgDB::readRefObjectFile("state.spt", localOptions.get());
#endif
if (!dynamic_cast<osg::StateSet*>(object.get())) if (!dynamic_cast<osg::StateSet*>(object.get()))
return 0; return 0;
+8
View File
@@ -156,7 +156,11 @@ struct ReaderWriterSTG::_ModelBin {
proxy->setCenterMode(osg::ProxyNode::UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED); proxy->setCenterMode(osg::ProxyNode::UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED);
node = proxy; node = proxy;
} else { } else {
#if OSG_VERSION_LESS_THAN(3,4,0)
node = osgDB::readNodeFile(o._name, o._options.get());
#else
node = osgDB::readRefNodeFile(o._name, o._options.get()); node = osgDB::readRefNodeFile(o._name, o._options.get());
#endif
if (!node.valid()) { if (!node.valid()) {
SG_LOG(SG_TERRAIN, SG_ALERT, o._errorLocation << ": Failed to load " SG_LOG(SG_TERRAIN, SG_ALERT, o._errorLocation << ": Failed to load "
<< o._token << " '" << o._name << "'"); << o._token << " '" << o._name << "'");
@@ -553,7 +557,11 @@ struct ReaderWriterSTG::_ModelBin {
if (_foundBase) { if (_foundBase) {
for (auto stgObject : _objectList) { for (auto stgObject : _objectList) {
osg::ref_ptr<osg::Node> node; osg::ref_ptr<osg::Node> node;
#if OSG_VERSION_LESS_THAN(3,4,0)
node = osgDB::readNodeFile(stgObject._name, stgObject._options.get());
#else
node = osgDB::readRefNodeFile(stgObject._name, stgObject._options.get()); node = osgDB::readRefNodeFile(stgObject._name, stgObject._options.get());
#endif
if (!node.valid()) { if (!node.valid()) {
SG_LOG(SG_TERRAIN, SG_ALERT, stgObject._errorLocation << ": Failed to load " SG_LOG(SG_TERRAIN, SG_ALERT, stgObject._errorLocation << ": Failed to load "
<< stgObject._token << " '" << stgObject._name << "'"); << stgObject._token << " '" << stgObject._name << "'");