Skip to content

Commit

Permalink
Merge branch 'main' into tileset-materials
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Jul 13, 2023
2 parents ddb7dae + f060e6b commit 276b471
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ tests/testUtils.h

# Tracing files
cesium-trace-*.json

# Carb settings from Cesium Power Tools extensions
carb_settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions exts/cesium.powertools/cesium/powertools/utils/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/core/include/cesium/omniverse/FabricUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/FabricGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 1 addition & 9 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void FabricMaterial::reset() {
auto srw = UsdUtil::getFabricStageReaderWriter();

setMaterialValues(GltfUtil::getDefaultMaterialInfo());
setTilesetId(-1);
setTilesetId(NO_TILESET_ID);

if (_materialDefinition.hasBaseColorTexture()) {
clearBaseColorTexture();
Expand All @@ -235,10 +235,6 @@ void FabricMaterial::setBaseColorTexture(
const std::shared_ptr<FabricTexture>& texture,
const TextureInfo& textureInfo) {

if (!UsdUtil::hasStage()) {
return;
}

if (!_materialDefinition.hasBaseColorTexture()) {
return;
}
Expand All @@ -247,10 +243,6 @@ void FabricMaterial::setBaseColorTexture(
}

void FabricMaterial::clearBaseColorTexture() {
if (!UsdUtil::hasStage()) {
return;
}

setBaseColorTextureValues(_defaultTextureAssetPath, GltfUtil::getDefaultTextureInfo());
}

Expand Down
49 changes: 48 additions & 1 deletion src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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);
Expand All @@ -223,6 +228,14 @@ FabricPrepareRenderResources::prepareInLoadThread(
return asyncSystem
.runInMainThread(
[this, hasImagery, meshes = std::move(meshes), tileLoadResult = std::move(tileLoadResult)]() mutable {
if (!tilesetExists()) {
return IntermediateLoadThreadResult{
std::move(tileLoadResult),
{},
{},
};
}

const auto pModel = std::get_if<CesiumGltf::Model>(&tileLoadResult.contentKind);
auto fabricMeshes = acquireFabricMeshes(*pModel, meshes, hasImagery, _tileset);
return IntermediateLoadThreadResult{
Expand All @@ -231,8 +244,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<CesiumGltf::Model>(&tileLoadResult.contentKind);
Expand All @@ -254,9 +272,14 @@ void* FabricPrepareRenderResources::prepareInMainThread(Cesium3DTilesSelection::
return nullptr;
}

// Wrap in a unique_ptr so that pLoadThreadResult gets freed when this function returns
std::unique_ptr<TileLoadThreadResult> pTileLoadThreadResult{
reinterpret_cast<TileLoadThreadResult*>(pLoadThreadResult)};

if (!tilesetExists()) {
return nullptr;
}

const auto& meshes = pTileLoadThreadResult->meshes;
auto& fabricMeshes = pTileLoadThreadResult->fabricMeshes;
const auto hasImagery = pTileLoadThreadResult->hasImagery;
Expand Down Expand Up @@ -316,6 +339,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};
Expand All @@ -328,9 +356,14 @@ void* FabricPrepareRenderResources::prepareRasterInMainThread(
return nullptr;
}

// Wrap in a unique_ptr so that pLoadThreadResult gets freed when this function returns
std::unique_ptr<ImageryLoadThreadResult> pImageryLoadThreadResult{
reinterpret_cast<ImageryLoadThreadResult*>(pLoadThreadResult)};

if (!tilesetExists()) {
return nullptr;
}

auto texture = pImageryLoadThreadResult->texture;

return new ImageryRenderResources{texture};
Expand Down Expand Up @@ -369,6 +402,10 @@ void FabricPrepareRenderResources::attachRasterInMainThread(
return;
}

if (!tilesetExists()) {
return;
}

const auto texture = pImageryRenderResources->texture;

auto& content = tile.getContent();
Expand Down Expand Up @@ -418,6 +455,10 @@ void FabricPrepareRenderResources::detachRasterInMainThread(
return;
}

if (!tilesetExists()) {
return;
}

for (const auto& mesh : pTileRenderResources->fabricMeshes) {
auto& material = mesh.material;
const auto& baseColorTexture = mesh.baseColorTexture;
Expand All @@ -434,4 +475,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
18 changes: 16 additions & 2 deletions src/core/src/FabricUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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<uint64_t>(changeTrackingPath, FabricTokens::_deletedPrims) == nullptr) {
return;
}

const auto deletedPrimsSize = srw.getArrayAttributeSize(changeTrackingPath, FabricTokens::_deletedPrims);
srw.setArrayAttributeSize(changeTrackingPath, FabricTokens::_deletedPrims, deletedPrimsSize + 1);
auto deletedPrimsFabric = srw.getArrayAttributeWr<uint64_t>(changeTrackingPath, FabricTokens::_deletedPrims);

deletedPrimsFabric[deletedPrimsSize] = omni::fabric::PathC(path).path;
}

void setTilesetTransform(int64_t tilesetId, const glm::dmat4& ecefToUsdTransform) {
Expand Down

0 comments on commit 276b471

Please sign in to comment.