Skip to content
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

Fix crash on extension reload #673

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fixed zooming to tileset extents when tileset prims have non identity transformation.
* Fixed crash when updating tilesets shader inputs.
* Fixed crash when setting certain `/Cesium` debug options at runtime.
* Fixed crash when disabling and re-enabling the extension.

### v0.17.0 - 2024-02-01

Expand Down
4 changes: 4 additions & 0 deletions src/core/include/cesium/omniverse/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Context {

[[nodiscard]] RenderStatistics getRenderStatistics() const;

[[nodiscard]] int64_t getContextId() const;

private:
std::filesystem::path _cesiumExtensionLocation;
std::filesystem::path _certificatePath;
Expand All @@ -86,6 +88,8 @@ class Context {
std::unique_ptr<CesiumIonServerManager> _pCesiumIonServerManager;
std::unique_ptr<UsdNotificationHandler> _pUsdNotificationHandler;

int64_t _contextId;

pxr::UsdStageWeakPtr _pUsdStage;
std::unique_ptr<omni::fabric::StageReaderWriter> _pFabricStage;
int64_t _usdStageId{0};
Expand Down
18 changes: 15 additions & 3 deletions src/core/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usdUtils/stageCache.h>

#if CESIUM_TRACING_ENABLED
#include <chrono>
#endif

namespace cesium::omniverse {

namespace {
uint64_t getSecondsSinceEpoch() {
const auto timePoint = std::chrono::system_clock::now();
return std::chrono::duration_cast<std::chrono::seconds>(timePoint.time_since_epoch()).count();
}
} // namespace

Context::Context(const std::filesystem::path& cesiumExtensionLocation)
: _cesiumExtensionLocation(cesiumExtensionLocation.lexically_normal())
, _certificatePath(_cesiumExtensionLocation / "certs" / "cacert.pem")
Expand All @@ -45,7 +50,8 @@ Context::Context(const std::filesystem::path& cesiumExtensionLocation)
, _pAssetRegistry(std::make_unique<AssetRegistry>(this))
, _pFabricResourceManager(std::make_unique<FabricResourceManager>(this))
, _pCesiumIonServerManager(std::make_unique<CesiumIonServerManager>(this))
, _pUsdNotificationHandler(std::make_unique<UsdNotificationHandler>(this)) {
, _pUsdNotificationHandler(std::make_unique<UsdNotificationHandler>(this))
, _contextId(static_cast<int64_t>(getSecondsSinceEpoch())) {

Cesium3DTilesContent::registerAllTileContentTypes();

Expand Down Expand Up @@ -236,4 +242,10 @@ RenderStatistics Context::getRenderStatistics() const {
return renderStatistics;
}

int64_t Context::getContextId() const {
// Creating a Fabric prim with the same path as a previously destroyed prim causes a crash.
// The contextId is randomly generated and ensures that Fabric prim paths are unique even across extension reloads.
return _contextId;
}

} // namespace cesium::omniverse
4 changes: 3 additions & 1 deletion src/core/src/FabricGeometryPool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cesium/omniverse/FabricGeometryPool.h"

#include "cesium/omniverse/Context.h"
#include "cesium/omniverse/FabricVertexAttributeDescriptor.h"
#include "cesium/omniverse/GltfUtil.h"

Expand Down Expand Up @@ -28,7 +29,8 @@ int64_t FabricGeometryPool::getPoolId() const {
}

std::shared_ptr<FabricGeometry> FabricGeometryPool::createObject(uint64_t objectId) const {
const auto pathStr = fmt::format("/cesium_geometry_pool_{}_object_{}", _poolId, objectId);
const auto contextId = _pContext->getContextId();
const auto pathStr = fmt::format("/cesium_geometry_pool_{}_object_{}_context_{}", _poolId, objectId, contextId);
const auto path = omni::fabric::Path(pathStr.c_str());
return std::make_shared<FabricGeometry>(_pContext, path, _geometryDescriptor, _poolId);
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/FabricMaterialPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void FabricMaterialPool::updateShaderInput(const pxr::SdfPath& shaderPath, const
}

std::shared_ptr<FabricMaterial> FabricMaterialPool::createObject(uint64_t objectId) const {
const auto pathStr = fmt::format("/cesium_material_pool_{}_object_{}", _poolId, objectId);
const auto contextId = _pContext->getContextId();
const auto pathStr = fmt::format("/cesium_material_pool_{}_object_{}_context_{}", _poolId, objectId, contextId);
const auto path = omni::fabric::Path(pathStr.c_str());
return std::make_shared<FabricMaterial>(
_pContext,
Expand Down
13 changes: 8 additions & 5 deletions src/core/src/FabricResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace cesium::omniverse {

namespace {

const std::string_view DEFAULT_WHITE_TEXTURE_NAME = "fabric_default_white_texture";
const std::string_view DEFAULT_TRANSPARENT_TEXTURE_NAME = "fabric_default_transparent_texture";
const std::string_view DEFAULT_WHITE_TEXTURE_NAME = "cesium_default_white_texture";
const std::string_view DEFAULT_TRANSPARENT_TEXTURE_NAME = "cesium_default_transparent_texture";

std::unique_ptr<omni::ui::DynamicTextureProvider>
createSinglePixelTexture(const std::string_view& name, const std::array<uint8_t, 4>& bytes) {
Expand Down Expand Up @@ -86,7 +86,8 @@ std::shared_ptr<FabricGeometry> FabricResourceManager::acquireGeometry(
FabricGeometryDescriptor geometryDescriptor(model, primitive, featuresInfo, smoothNormals);

if (_disableGeometryPool) {
const auto pathStr = fmt::format("/cesium_geometry_{}", getNextGeometryId());
const auto contextId = _pContext->getContextId();
const auto pathStr = fmt::format("/cesium_geometry_{}_context_{}", getNextGeometryId(), contextId);
const auto path = omni::fabric::Path(pathStr.c_str());
return std::make_shared<FabricGeometry>(_pContext, path, geometryDescriptor, -1);
}
Expand Down Expand Up @@ -122,7 +123,8 @@ std::shared_ptr<FabricMaterial> FabricResourceManager::acquireMaterial(

std::shared_ptr<FabricTexture> FabricResourceManager::acquireTexture() {
if (_disableTexturePool) {
const auto name = fmt::format("/cesium_texture_{}", getNextTextureId());
const auto contextId = _pContext->getContextId();
const auto name = fmt::format("/cesium_texture_{}_context_{}", getNextTextureId(), contextId);
return std::make_shared<FabricTexture>(_pContext, name, -1);
}

Expand Down Expand Up @@ -241,7 +243,8 @@ void FabricResourceManager::clear() {

std::shared_ptr<FabricMaterial>
FabricResourceManager::createMaterial(const FabricMaterialDescriptor& materialDescriptor) {
const auto pathStr = fmt::format("/cesium_material_{}", getNextMaterialId());
const auto contextId = _pContext->getContextId();
const auto pathStr = fmt::format("/cesium_material_{}_context_{}", getNextMaterialId(), contextId);
const auto path = omni::fabric::Path(pathStr.c_str());
return std::make_shared<FabricMaterial>(
_pContext,
Expand Down
5 changes: 4 additions & 1 deletion src/core/src/FabricTexturePool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cesium/omniverse/FabricTexturePool.h"

#include "cesium/omniverse/Context.h"

#include <spdlog/fmt/fmt.h>

namespace cesium::omniverse {
Expand All @@ -16,7 +18,8 @@ int64_t FabricTexturePool::getPoolId() const {
}

std::shared_ptr<FabricTexture> FabricTexturePool::createObject(uint64_t objectId) const {
const auto name = fmt::format("/cesium_texture_pool_{}_object_{}", _poolId, objectId);
const auto contextId = _pContext->getContextId();
const auto name = fmt::format("/cesium_texture_pool_{}_object_{}_context_{}", _poolId, objectId, contextId);
return std::make_shared<FabricTexture>(_pContext, name, _poolId);
}

Expand Down
Loading