From b76f78c6daf4e92e3d4f204975c47cc9e97fc016 Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Sat, 26 Jan 2019 17:47:47 +0100 Subject: [PATCH] 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. --- simgear/scene/material/TextureBuilder.cxx | 32 +++++++++++++++++++++++ simgear/scene/material/mat.cxx | 4 +++ simgear/scene/model/BVHPageNodeOSG.cxx | 12 +++++++++ simgear/scene/model/SGReaderWriterXML.cxx | 4 +++ simgear/scene/model/model.cxx | 17 ++++++++++-- simgear/scene/model/modellib.cxx | 4 +++ simgear/scene/tgdb/ReaderWriterSPT.cxx | 16 ++++++++++++ simgear/scene/tgdb/ReaderWriterSTG.cxx | 8 ++++++ 8 files changed, 95 insertions(+), 2 deletions(-) diff --git a/simgear/scene/material/TextureBuilder.cxx b/simgear/scene/material/TextureBuilder.cxx index 0d2a60c7..43343169 100644 --- a/simgear/scene/material/TextureBuilder.cxx +++ b/simgear/scene/material/TextureBuilder.cxx @@ -275,7 +275,11 @@ bool setAttrs(const TexTuple& attrs, Texture* tex, options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED); else 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); +#endif options->setLoadOriginHint(origLOH); osg::ref_ptr image; if (result.success()) @@ -607,32 +611,56 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* SGReaderWriterOptions* wOpts = (SGReaderWriterOptions*)options; SGReaderWriterOptions::LoadOriginHint origLOH = wOpts->getLoadOriginHint(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); 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); +#endif if(result.success()) { osg::Image* image = result.getImage(); cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image); @@ -657,7 +685,11 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* return cubeTexture.release(); osgDB::ReaderWriter::ReadResult result; +#if OSG_VERSION_LESS_THAN(3,4,0) + result = osgDB::readImageFile(texname, options); +#else result = osgDB::readRefImageFile(texname, options); +#endif if(result.success()) { osg::Image* image = result.getImage(); image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index ba9d63f7..0eaa37a6 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -227,7 +227,11 @@ SGMaterial::read_properties(const SGReaderWriterOptions* options, } else { +#if OSG_VERSION_LESS_THAN(3,4,0) + osg::Image* image = osgDB::readImageFile(fullMaskPath, options); +#else osg::Image* image = osgDB::readRefImageFile(fullMaskPath, options); +#endif if (image && image->valid()) { Texture2DRef object_mask = new osg::Texture2D; diff --git a/simgear/scene/model/BVHPageNodeOSG.cxx b/simgear/scene/model/BVHPageNodeOSG.cxx index e8051847..a2f15bcb 100644 --- a/simgear/scene/model/BVHPageNodeOSG.cxx +++ b/simgear/scene/model/BVHPageNodeOSG.cxx @@ -215,7 +215,11 @@ public: if (pagedLOD.getMinRange(i) <= 0) { osg::ref_ptr options; 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()); +#endif } if (!node.valid()) node = new osg::Group; @@ -256,7 +260,11 @@ public: osg::ref_ptr options; options = getOptions(proxyNode.getDatabaseOptions(), proxyNode.getDatabasePath()); osg::ref_ptr 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()); +#endif if (!node.valid()) node = new osg::Group; if (i < proxyNode.getNumChildren()) @@ -353,7 +361,11 @@ SGSharedPtr BVHPageNodeOSG::load(const std::string& name, const osg::ref_ptr& options) { osg::ref_ptr node; +#if OSG_VERSION_LESS_THAN(3,4,0) + node = osgDB::readNodeFile(name, dynamic_cast(options.get())); +#else node = osgDB::readRefNodeFile(name, dynamic_cast(options.get())); +#endif if (!node.valid()) return SGSharedPtr(); diff --git a/simgear/scene/model/SGReaderWriterXML.cxx b/simgear/scene/model/SGReaderWriterXML.cxx index 51a9817e..64ad2754 100644 --- a/simgear/scene/model/SGReaderWriterXML.cxx +++ b/simgear/scene/model/SGReaderWriterXML.cxx @@ -338,7 +338,11 @@ sgLoad3DModel_internal(const SGPath& path, options->setDatabasePath(texturepath.local8BitStr()); 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()); +#endif if (!modelResult.validNode()) throw sg_io_exception("Failed to load 3D model:" + modelResult.message(), modelpath); diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index 0b86f701..737c7597 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -43,9 +43,18 @@ SGLoadTexture2D(bool staticTexture, const std::string& path, { osg::Image* image; 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 - 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 texture = new osg::Texture2D; texture->setImage(image); 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 (fullLiveryFile.empty() || fullLiveryFile == *fullFilePath) return 0; +#if OSG_VERSION_LESS_THAN(3,4,0) + Image* newImage = readImageFile(fullLiveryFile); +#else Image* newImage = readRefImageFile(fullLiveryFile); +#endif if (!newImage) return 0; CopyOp copyOp(CopyOp::DEEP_COPY_ALL & ~CopyOp::DEEP_COPY_IMAGES); diff --git a/simgear/scene/model/modellib.cxx b/simgear/scene/model/modellib.cxx index 7ce382d3..d11e0d21 100644 --- a/simgear/scene/model/modellib.cxx +++ b/simgear/scene/model/modellib.cxx @@ -101,7 +101,11 @@ osg::Node* loadFile(const string& path, SGReaderWriterOptions* options) options->setInstantiateEffects(true); } +#if OSG_VERSION_LESS_THAN(3,4,0) + ref_ptr model = readNodeFile(path, options); +#else ref_ptr model = readRefNodeFile(path, options); +#endif if (!model) return 0; else diff --git a/simgear/scene/tgdb/ReaderWriterSPT.cxx b/simgear/scene/tgdb/ReaderWriterSPT.cxx index 39facca1..66fad24b 100644 --- a/simgear/scene/tgdb/ReaderWriterSPT.cxx +++ b/simgear/scene/tgdb/ReaderWriterSPT.cxx @@ -208,7 +208,11 @@ ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* o imageFileName = osgDB::concatPaths(imageFileName, "Globe"); 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)) { +#endif osg::Texture2D* texture = new osg::Texture2D; texture->setImage(image); texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT); @@ -256,7 +260,11 @@ ReaderWriterSPT::createTree(const BucketBox& bucketBox, const LocalOptions& opti if (bucketBox.getIsBucketSize()) { std::string fileName; 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); +#endif } else if (!topLevel && options.isPageLevel(bucketBox.getStartLevel())) { return createPagedLOD(bucketBox, options); } else { @@ -314,7 +322,11 @@ ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const LocalOptions& std::string fileName = osgDB::findDataFile(lodPath + extensions[i], options._options); if (fileName.empty()) continue; +#if OSG_VERSION_LESS_THAN(3,4,0) + osg::ref_ptr node = osgDB::readNodeFile(fileName, options._options); +#else osg::ref_ptr node = osgDB::readRefNodeFile(fileName, options._options); +#endif if (!node.valid()) continue; pagedLOD->addChild(node.get(), range, std::numeric_limits::max()); @@ -412,7 +424,11 @@ ReaderWriterSPT::getLowLODStateSet(const LocalOptions& options) const localOptions = static_cast(options._options->clone(osg::CopyOp())); localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL); +#if OSG_VERSION_LESS_THAN(3,4,0) + osg::ref_ptr object = osgDB::readObjectFile("state.spt", localOptions.get()); +#else osg::ref_ptr object = osgDB::readRefObjectFile("state.spt", localOptions.get()); +#endif if (!dynamic_cast(object.get())) return 0; diff --git a/simgear/scene/tgdb/ReaderWriterSTG.cxx b/simgear/scene/tgdb/ReaderWriterSTG.cxx index 4a79e8ec..4a3887a4 100644 --- a/simgear/scene/tgdb/ReaderWriterSTG.cxx +++ b/simgear/scene/tgdb/ReaderWriterSTG.cxx @@ -156,7 +156,11 @@ struct ReaderWriterSTG::_ModelBin { proxy->setCenterMode(osg::ProxyNode::UNION_OF_BOUNDING_SPHERE_AND_USER_DEFINED); node = proxy; } 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()); +#endif if (!node.valid()) { SG_LOG(SG_TERRAIN, SG_ALERT, o._errorLocation << ": Failed to load " << o._token << " '" << o._name << "'"); @@ -553,7 +557,11 @@ struct ReaderWriterSTG::_ModelBin { if (_foundBase) { for (auto stgObject : _objectList) { osg::ref_ptr 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()); +#endif if (!node.valid()) { SG_LOG(SG_TERRAIN, SG_ALERT, stgObject._errorLocation << ": Failed to load " << stgObject._token << " '" << stgObject._name << "'");