-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destroy material when a mesh is deleted #324
Conversation
Signed-off-by: ahcorde <[email protected]>
ogre2/src/Ogre2MeshFactory.cc
Outdated
@@ -88,6 +89,17 @@ void Ogre2MeshFactory::Clear() | |||
this->ogreMeshes.clear(); | |||
} | |||
|
|||
void Ogre2MeshFactory::ClearMaterialsCache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified that this fixes the memory leak. A different issue is created after removing these materials though. Here are the steps to reproduce:
- Insert a model like this one (I just dragged and dropped) https://app.ignitionrobotics.org/GoogleResearch/fuel/models/Weisshai_Great_White_Shark
- it needs to be a model that references a texture in the dae or obj file but does not have a sdf element
- Insert the same model again -> model appear grey
After debugging a little, I remembered that these materials were created as "templates". Later on in this line https://github.com/ignitionrobotics/ign-rendering/blob/6fd0c03ce32568f8bc91c1041fc169cc7622248f/ogre2/src/Ogre2MeshFactory.cc#L601 a "new instance" is created and assigned to the submesh. This is done by looking for the name of the template material stored in the submesh, getting the template material from the scene, then assigning a copy to the submesh. Now that this template material is removed, subsequent meshes requiring the same material will not be able to find it.
One possible solution is to remove the code for creating these template materials, i.e. the code here: https://github.com/ignitionrobotics/ign-rendering/blob/6fd0c03ce32568f8bc91c1041fc169cc7622248f/ogre2/src/Ogre2MeshFactory.cc#L455-L471
we just then need to always create a new material and assign it to the submesh, i.e. replace the logic here: https://github.com/ignitionrobotics/ign-rendering/blob/6fd0c03ce32568f8bc91c1041fc169cc7622248f/ogre2/src/Ogre2MeshFactory.cc#L590-L602
I ran out of time today and haven't found a way to do his cleanly yet..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iche033, I don't fully understand why this is happening, I would expect this three things
- when we load a new mesh then the "template" material is loaded and removed.
- When we remove the mesh we remove the material created fom the template
- At this point we should not have any material associated with the mesh in memory.
- Finally we load again the same mesh and I would expect to reload everything again (template and material created from the template).
am I missing something ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally we load again the same mesh and I would expect to reload everything again (template and material created from the template).
we have this check here to skip loading the mesh if it's already loaded in ogre: https://github.com/ignitionrobotics/ign-rendering/blob/6fd0c03ce32568f8bc91c1041fc169cc7622248f/ogre2/src/Ogre2MeshFactory.cc#L167
That was mainly done for performance reason, e.g. when launching a world with hundreds of the same model, esp if they have large complex meshes.
Signed-off-by: ahcorde <[email protected]>
With the last commit, I fixed the issue that @iche033 mentioned and also fixed the small leak |
Signed-off-by: ahcorde <[email protected]>
Codecov Report
@@ Coverage Diff @@
## main #324 +/- ##
==========================================
- Coverage 57.71% 57.68% -0.03%
==========================================
Files 160 160
Lines 15887 15992 +105
==========================================
+ Hits 9169 9225 +56
- Misses 6718 6767 +49
Continue to review full report at Codecov.
|
There is still an issue when you insert more than one model of the same type, from the second model they will not get the material |
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confirms that the latest changes fix the missing texture issue for subsequent models. Memory consumption for repeatedly adding and removing models also look to be stable.
ogre/src/OgreMesh.cc
Outdated
@@ -58,6 +58,8 @@ void OgreMesh::Destroy() | |||
|
|||
auto ogreScene = std::dynamic_pointer_cast<OgreScene>(this->Scene()); | |||
|
|||
Ogre::MeshManager::getSingleton().remove( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this removes the template mesh. Should we have a similar logic as what you did for textures and check reference count to the mesh first before removing it? Same for the remove
call in OgreSubMesh::Destroy()
ogre/src/OgreMaterial.cc
Outdated
if (this->textureName == res->getName() && | ||
res->getName().find("scene::RenderTexture") == std::string::npos) | ||
{ | ||
this->Scene()->ClearMaterialsCache(this->textureName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an idea to prevent breaking ABI. We can add a new OgreScene::MeshFactory()
accessor function to OgreScene
class (and also Ogre2Scene
) class to get the mesh factory then here we just need to do:
OgreScenePtr s = std::dynamic_pointer_cast<OgreScene>(this->Scene());
auto factory = s->MeshFactory();
factory->ClearMaterialCache(this->textureName);
This prevents adding and overriding a virtual function in Scene.
Actually that was testing with ogre 1.x. I just tried ogre 2.x, I think there is still some leak. |
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
@mjcarroll and @iche033 I open this PR for review |
Signed-off-by: ahcorde <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! the latest changes fix the ogre2 memory leak for me. I just have a few coding style and doc comments
Signed-off-by: ahcorde <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me. just one minor typo.
Signed-off-by: ahcorde <[email protected]>
* Destroy material when a mesh is deleted Signed-off-by: ahcorde <[email protected]> * Fixed issue Signed-off-by: ahcorde <[email protected]> * make linters happy Signed-off-by: ahcorde <[email protected]> * Improved implementation Signed-off-by: ahcorde <[email protected]> * make linters happy Signed-off-by: ahcorde <[email protected]> * Ogre2 texture destruction some progress Signed-off-by: ahcorde <[email protected]> * Remove meshes when there is no reference Signed-off-by: ahcorde <[email protected]> * cleanup code Signed-off-by: ahcorde <[email protected]> * Avoid breaking ABI Signed-off-by: ahcorde <[email protected]> * make linters happy Signed-off-by: ahcorde <[email protected]> * Fixed tests Signed-off-by: ahcorde <[email protected]> * Fix macos warnings Signed-off-by: ahcorde <[email protected]> * Added feedback Signed-off-by: ahcorde <[email protected]> * Doc fix Signed-off-by: ahcorde <[email protected]> Signed-off-by: William Lew <[email protected]>
🎉 Enhancement
Summary
This PR allows to remove materials when a mesh is removed.
There are two cases:
I had to break the ABI, I'm open to suggestions to be able to backport this PR
Checklist
codecheck
passed (See contributing)Note to maintainers: Remember to use Squash-Merge