From d770b5578f84d875329a47836a01dd730018b03b Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Fri, 18 Aug 2023 10:13:28 -0400 Subject: [PATCH] Fix crash when reloading tileset --- .../include/cesium/omniverse/FabricResourceManager.h | 5 +++++ src/core/src/FabricGeometry.cpp | 3 +++ src/core/src/FabricMaterial.cpp | 11 ++++++++--- src/core/src/FabricResourceManager.cpp | 9 +++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/core/include/cesium/omniverse/FabricResourceManager.h b/src/core/include/cesium/omniverse/FabricResourceManager.h index 6808e7872..fbcdac4d4 100644 --- a/src/core/include/cesium/omniverse/FabricResourceManager.h +++ b/src/core/include/cesium/omniverse/FabricResourceManager.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -70,6 +71,8 @@ class FabricResourceManager { void setTexturePoolInitialCapacity(uint64_t texturePoolInitialCapacity); void setDebugRandomColors(bool debugRandomColors); + void retainPath(const omni::fabric::Path& path); + void clear(); protected: @@ -117,6 +120,8 @@ class FabricResourceManager { std::unique_ptr _defaultTexture; pxr::TfToken _defaultTextureAssetPathToken; + + std::vector _retainedPaths; }; } // namespace cesium::omniverse diff --git a/src/core/src/FabricGeometry.cpp b/src/core/src/FabricGeometry.cpp index f94f13656..09eb99d03 100644 --- a/src/core/src/FabricGeometry.cpp +++ b/src/core/src/FabricGeometry.cpp @@ -2,6 +2,7 @@ #include "cesium/omniverse/FabricAttributesBuilder.h" #include "cesium/omniverse/FabricMaterial.h" +#include "cesium/omniverse/FabricResourceManager.h" #include "cesium/omniverse/FabricUtil.h" #include "cesium/omniverse/GltfUtil.h" #include "cesium/omniverse/Tokens.h" @@ -46,6 +47,8 @@ FabricGeometry::FabricGeometry( return; } + FabricResourceManager::getInstance().retainPath(path); + initialize(); reset(); } diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index 79b6e95ca..f5ccf6cc6 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -2,6 +2,7 @@ #include "cesium/omniverse/Context.h" #include "cesium/omniverse/FabricAttributesBuilder.h" +#include "cesium/omniverse/FabricResourceManager.h" #include "cesium/omniverse/FabricUtil.h" #include "cesium/omniverse/Tokens.h" #include "cesium/omniverse/UsdUtil.h" @@ -67,15 +68,19 @@ void FabricMaterial::initialize() { const auto shaderPath = FabricUtil::joinPaths(materialPath, FabricTokens::Shader); const auto baseColorTexturePath = FabricUtil::joinPaths(materialPath, FabricTokens::baseColorTexture); - createMaterial(materialPath); _materialPath = materialPath; + _shaderPath = shaderPath; + _baseColorTexturePath = baseColorTexturePath; + + FabricResourceManager::getInstance().retainPath(materialPath); + FabricResourceManager::getInstance().retainPath(shaderPath); + FabricResourceManager::getInstance().retainPath(baseColorTexturePath); + createMaterial(materialPath); createShader(shaderPath, materialPath); - _shaderPath = shaderPath; if (hasBaseColorTexture) { createTexture(baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture); - _baseColorTexturePath = baseColorTexturePath; } } diff --git a/src/core/src/FabricResourceManager.cpp b/src/core/src/FabricResourceManager.cpp index 76f1f8ab4..59a1755d8 100644 --- a/src/core/src/FabricResourceManager.cpp +++ b/src/core/src/FabricResourceManager.cpp @@ -268,6 +268,15 @@ std::shared_ptr FabricResourceManager::createTexturePool() { std::make_shared(getNextPoolId(), _texturePoolInitialCapacity)); } +void FabricResourceManager::retainPath(const omni::fabric::Path& path) { + // This is a workaround for https://github.com/CesiumGS/cesium-omniverse/issues/425. + // By retaining a reference to the path we avoid a crash deep in the Sdf library + // that's triggered by loading a tileset, removing the tileset, and reloading the tileset. + // It's possible this will be fixed in a future Kit release at which point we can remove + // this workaround. + _retainedPaths.push_back(path); +} + int64_t FabricResourceManager::getNextGeometryId() { return _geometryId++; }