From 255a5f68abeef3d13a96e6c5cacb85c7f00c6032 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 11 Jul 2023 15:53:52 -0400 Subject: [PATCH 1/4] Add Save Carb Settings action to CesiumPowerTools --- .gitignore | 3 +++ .../cesium/powertools/powertools_window.py | 7 ++++++- exts/cesium.powertools/cesium/powertools/utils/utils.py | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6e5647983..5e7615434 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,6 @@ _testoutput # Tracing files cesium-trace-*.json + +# Carb settings from Cesium Power Tools extensions +carb_settings.txt diff --git a/exts/cesium.powertools/cesium/powertools/powertools_window.py b/exts/cesium.powertools/cesium/powertools/powertools_window.py index 2202af0aa..a2712f149 100644 --- a/exts/cesium.powertools/cesium/powertools/powertools_window.py +++ b/exts/cesium.powertools/cesium/powertools/powertools_window.py @@ -3,7 +3,11 @@ from typing import Callable, Optional, List from cesium.omniverse.ui import CesiumOmniverseDebugWindow from .georefhelper.georef_helper_window import CesiumGeorefHelperWindow -from .utils import extend_far_plane +from .utils import extend_far_plane, save_carb_settings +import os +from functools import partial + +powertools_extension_location = os.path.join(os.path.dirname(__file__), "../../") class PowertoolsAction: @@ -39,6 +43,7 @@ def __init__(self, **kwargs): PowertoolsAction("Open Cesium Debugging Window", CesiumOmniverseDebugWindow.show_window), PowertoolsAction("Open Cesium Georeference Helper Window", CesiumGeorefHelperWindow.create_window), PowertoolsAction("Extend Far Plane", extend_far_plane), + PowertoolsAction("Save Carb Settings", partial(save_carb_settings, powertools_extension_location)), ] self.frame.set_build_fn(self._build_fn) diff --git a/exts/cesium.powertools/cesium/powertools/utils/utils.py b/exts/cesium.powertools/cesium/powertools/utils/utils.py index 9efe1dac2..cf6cbfc72 100644 --- a/exts/cesium.powertools/cesium/powertools/utils/utils.py +++ b/exts/cesium.powertools/cesium/powertools/utils/utils.py @@ -1,6 +1,9 @@ import omni.usd from omni.kit.viewport.utility import get_active_viewport from pxr import Gf, UsdGeom +import json +import carb.settings +import os # Modified version of ScopedEdit in _build_viewport_cameras in omni.kit.widget.viewport @@ -38,3 +41,9 @@ def extend_far_plane(): scoped_edit = ScopedEdit(stage) # noqa: F841 camera.GetClippingRangeAttr().Set(Gf.Vec2f(1.0, 10000000000.0)) + + +def save_carb_settings(powertools_extension_location: str): + carb_settings_path = os.path.join(powertools_extension_location, "carb_settings.txt") + with open(carb_settings_path, "w") as fh: + fh.write(json.dumps(carb.settings.get_settings().get("/"), indent=2)) From 6695270c0bc131f8d802271c9aff664379d1539c Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 12 Jul 2023 14:03:16 -0400 Subject: [PATCH 2/4] Safeguards against destroyed tileset --- .../omniverse/FabricPrepareRenderResources.h | 2 + src/core/src/FabricMaterial.cpp | 8 --- src/core/src/FabricPrepareRenderResources.cpp | 51 ++++++++++++++++++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/core/include/cesium/omniverse/FabricPrepareRenderResources.h b/src/core/include/cesium/omniverse/FabricPrepareRenderResources.h index 3c7275f3f..f939e1a5c 100644 --- a/src/core/include/cesium/omniverse/FabricPrepareRenderResources.h +++ b/src/core/include/cesium/omniverse/FabricPrepareRenderResources.h @@ -69,6 +69,8 @@ class FabricPrepareRenderResources final : public Cesium3DTilesSelection::IPrepa void* pMainThreadRendererResources) noexcept override; private: + [[nodiscard]] bool tilesetExists() const; + const OmniTileset& _tileset; }; } // namespace cesium::omniverse diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index 045432ed9..9ab33fc81 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -235,10 +235,6 @@ void FabricMaterial::setBaseColorTexture( const std::shared_ptr& texture, const TextureInfo& textureInfo) { - if (!UsdUtil::hasStage()) { - return; - } - if (!_materialDefinition.hasBaseColorTexture()) { return; } @@ -247,10 +243,6 @@ void FabricMaterial::setBaseColorTexture( } void FabricMaterial::clearBaseColorTexture() { - if (!UsdUtil::hasStage()) { - return; - } - setBaseColorTextureValues(_defaultTextureAssetPath, GltfUtil::getDefaultTextureInfo()); } diff --git a/src/core/src/FabricPrepareRenderResources.cpp b/src/core/src/FabricPrepareRenderResources.cpp index 6987a9a8d..1fcecb5ad 100644 --- a/src/core/src/FabricPrepareRenderResources.cpp +++ b/src/core/src/FabricPrepareRenderResources.cpp @@ -202,6 +202,11 @@ FabricPrepareRenderResources::prepareInLoadThread( Cesium3DTilesSelection::TileLoadResultAndRenderResources{std::move(tileLoadResult), nullptr}); } + if (!tilesetExists()) { + return asyncSystem.createResolvedFuture( + Cesium3DTilesSelection::TileLoadResultAndRenderResources{std::move(tileLoadResult), nullptr}); + } + const auto hasImagery = tileLoadResult.rasterOverlayDetails.has_value(); auto meshes = gatherMeshes(_tileset, transform, *pModel); @@ -214,7 +219,15 @@ FabricPrepareRenderResources::prepareInLoadThread( return asyncSystem .runInMainThread( - [hasImagery, meshes = std::move(meshes), tileLoadResult = std::move(tileLoadResult)]() mutable { + [this, hasImagery, meshes = std::move(meshes), tileLoadResult = std::move(tileLoadResult)]() mutable { + if (!tilesetExists()) { + return IntermediateLoadThreadResult{ + std::move(tileLoadResult), + {}, + {}, + }; + } + const auto pModel = std::get_if(&tileLoadResult.contentKind); auto fabricMeshes = acquireFabricMeshes(*pModel, meshes, hasImagery); return IntermediateLoadThreadResult{ @@ -223,8 +236,13 @@ FabricPrepareRenderResources::prepareInLoadThread( std::move(fabricMeshes), }; }) - .thenInWorkerThread([transform, hasImagery](IntermediateLoadThreadResult&& workerResult) mutable { + .thenInWorkerThread([this, transform, hasImagery](IntermediateLoadThreadResult&& workerResult) mutable { auto tileLoadResult = std::move(workerResult.tileLoadResult); + + if (!tilesetExists()) { + return Cesium3DTilesSelection::TileLoadResultAndRenderResources{std::move(tileLoadResult), nullptr}; + } + auto meshes = std::move(workerResult.meshes); auto fabricMeshes = std::move(workerResult.fabricMeshes); const auto pModel = std::get_if(&tileLoadResult.contentKind); @@ -246,9 +264,14 @@ void* FabricPrepareRenderResources::prepareInMainThread(Cesium3DTilesSelection:: return nullptr; } + // Wrap in a unique_ptr so that pLoadThreadResult gets freed when this function returns std::unique_ptr pTileLoadThreadResult{ reinterpret_cast(pLoadThreadResult)}; + if (!tilesetExists()) { + return nullptr; + } + const auto& meshes = pTileLoadThreadResult->meshes; auto& fabricMeshes = pTileLoadThreadResult->fabricMeshes; const auto hasImagery = pTileLoadThreadResult->hasImagery; @@ -308,6 +331,11 @@ void FabricPrepareRenderResources::free( void* FabricPrepareRenderResources::prepareRasterInLoadThread( CesiumGltf::ImageCesium& image, [[maybe_unused]] const std::any& rendererOptions) { + + if (!tilesetExists()) { + return nullptr; + } + auto texture = FabricResourceManager::getInstance().acquireTexture(); texture->setImage(image); return new ImageryLoadThreadResult{texture}; @@ -320,9 +348,14 @@ void* FabricPrepareRenderResources::prepareRasterInMainThread( return nullptr; } + // Wrap in a unique_ptr so that pLoadThreadResult gets freed when this function returns std::unique_ptr pImageryLoadThreadResult{ reinterpret_cast(pLoadThreadResult)}; + if (!tilesetExists()) { + return nullptr; + } + auto texture = pImageryLoadThreadResult->texture; return new ImageryRenderResources{texture}; @@ -361,6 +394,10 @@ void FabricPrepareRenderResources::attachRasterInMainThread( return; } + if (!tilesetExists()) { + return; + } + const auto texture = pImageryRenderResources->texture; auto& content = tile.getContent(); @@ -410,6 +447,10 @@ void FabricPrepareRenderResources::detachRasterInMainThread( return; } + if (!tilesetExists()) { + return; + } + for (const auto& mesh : pTileRenderResources->fabricMeshes) { auto& material = mesh.material; const auto& baseColorTexture = mesh.baseColorTexture; @@ -426,4 +467,10 @@ void FabricPrepareRenderResources::detachRasterInMainThread( } } +bool FabricPrepareRenderResources::tilesetExists() const { + // When a tileset is deleted there's a short period between the prim being deleted and TfNotice notifying us about the change. + // This function helps us know whether we should proceed with loading render resources. + return UsdUtil::hasStage() && UsdUtil::primExists(_tileset.getPath()); +} + } // namespace cesium::omniverse From e43b51210e34f7ab0df54d2dea89053d786129e4 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 12 Jul 2023 14:37:26 -0400 Subject: [PATCH 3/4] Add back TempChangeTracking so that fabric prims get deleted --- src/core/src/FabricUtil.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/core/src/FabricUtil.cpp b/src/core/src/FabricUtil.cpp index 4e44d8ed0..fb1d279a4 100644 --- a/src/core/src/FabricUtil.cpp +++ b/src/core/src/FabricUtil.cpp @@ -590,6 +590,20 @@ void destroyPrim(const omni::fabric::Path& path) { auto srw = UsdUtil::getFabricStageReaderWriter(); srw.destroyPrim(path); + + // Prims removed from Fabric need special handling for their removal to be reflected in the Hydra render index + // This workaround may not be needed in future Kit versions, but is needed as of Kit 105.0 + const omni::fabric::Path changeTrackingPath("/TempChangeTracking"); + + if (srw.getAttribute(changeTrackingPath, FabricTokens::_deletedPrims) == nullptr) { + return; + } + + const auto deletedPrimsSize = srw.getArrayAttributeSize(changeTrackingPath, FabricTokens::_deletedPrims); + srw.setArrayAttributeSize(changeTrackingPath, FabricTokens::_deletedPrims, deletedPrimsSize + 1); + auto deletedPrimsFabric = srw.getArrayAttributeWr(changeTrackingPath, FabricTokens::_deletedPrims); + + deletedPrimsFabric[deletedPrimsSize] = omni::fabric::PathC(path).path; } void setTilesetTransform(int64_t tilesetId, const glm::dmat4& ecefToUsdTransform) { From 9a1346d59b174ac45a062c4891ae4bf677cbae64 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 12 Jul 2023 18:01:53 -0400 Subject: [PATCH 4/4] Add constant for -1 tilesetId --- src/core/include/cesium/omniverse/FabricUtil.h | 3 +++ src/core/src/FabricGeometry.cpp | 2 +- src/core/src/FabricMaterial.cpp | 2 +- src/core/src/FabricUtil.cpp | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/core/include/cesium/omniverse/FabricUtil.h b/src/core/include/cesium/omniverse/FabricUtil.h index 161870e88..82fd32e8e 100644 --- a/src/core/include/cesium/omniverse/FabricUtil.h +++ b/src/core/include/cesium/omniverse/FabricUtil.h @@ -20,6 +20,9 @@ struct FabricStatistics { uint64_t trianglesRendered{0}; }; +// -1 means the prim is not yet associated with a tileset +const auto NO_TILESET_ID = int64_t(-1); + } // namespace cesium::omniverse namespace cesium::omniverse::FabricUtil { diff --git a/src/core/src/FabricGeometry.cpp b/src/core/src/FabricGeometry.cpp index 39a864eea..fe05f47a4 100644 --- a/src/core/src/FabricGeometry.cpp +++ b/src/core/src/FabricGeometry.cpp @@ -213,7 +213,7 @@ void FabricGeometry::reset() { displayColorFabric[0] = DEFAULT_VERTEX_COLOR; displayOpacityFabric[0] = DEFAULT_VERTEX_OPACITY; - FabricUtil::setTilesetId(_pathFabric, -1); + FabricUtil::setTilesetId(_pathFabric, NO_TILESET_ID); srw.setArrayAttributeSize(_pathFabric, FabricTokens::material_binding, 0); srw.setArrayAttributeSize(_pathFabric, FabricTokens::faceVertexCounts, 0); diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index 9ab33fc81..e85dbf5a2 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -217,7 +217,7 @@ void FabricMaterial::reset() { auto srw = UsdUtil::getFabricStageReaderWriter(); setMaterialValues(GltfUtil::getDefaultMaterialInfo()); - setTilesetId(-1); + setTilesetId(NO_TILESET_ID); if (_materialDefinition.hasBaseColorTexture()) { clearBaseColorTexture(); diff --git a/src/core/src/FabricUtil.cpp b/src/core/src/FabricUtil.cpp index fb1d279a4..05e79df3d 100644 --- a/src/core/src/FabricUtil.cpp +++ b/src/core/src/FabricUtil.cpp @@ -545,7 +545,7 @@ FabricStatistics getStatistics() { statistics.geometriesCapacity += paths.size(); for (size_t i = 0; i < paths.size(); i++) { - if (tilesetIdFabric[i] == -1) { + if (tilesetIdFabric[i] == NO_TILESET_ID) { continue; } @@ -571,7 +571,7 @@ FabricStatistics getStatistics() { statistics.materialsCapacity += paths.size(); for (size_t i = 0; i < paths.size(); i++) { - if (tilesetIdFabric[i] == -1) { + if (tilesetIdFabric[i] == NO_TILESET_ID) { continue; }