Autodesk: Prevent undesired materialx garbage collection #3051
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Change(s)
Summary: Add a member variable for HdMaterialNetwork to hold the generated MaterialX Shader to prevent unnecessary garbage collection.
With the current code base, every time we re-apply an existing material, the MaterialX::ShaderPtr is still considered as "the first instance"
OpenUSD/pxr/imaging/hdSt/materialXFilter.cpp
Line 1008 in 0b18ad3
even the underlying HdMaterialNetwork is cached on application side. The reason is that after every drawcall, there is a garbage collection process (https://github.com/PixarAnimationStudios/OpenUSD/blob/release/pxr/imaging/hdSt/resourceRegistry.cpp#L1098).
The previously unapplied material (MaterialX::ShaderPtr) is garbage collected because HdStorm considered that nobody needs it anymore even though, as I said, the HdMaterialNetwork is cached. This is not the case for PreviewSurace or any other custom glslfx because there is member variable called _surfaceGfx which holds a reference of the generated glslfx thus the reference count is at least 2 and the ResourceRegistry won't garbage collect it.
To address this issue, we can simply add a member variable in HdMaterialNetwork to hold the generated MaterialX glslfx shared_ptr just like the _surfaceGfx to prevent the unnecessary garbage collection for MaterialX shader as well.
Fixes Issue(s)