Skip to content

Commit

Permalink
Safeguards against destroyed tileset
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Jul 12, 2023
1 parent 3c92860 commit 6695270
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
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
8 changes: 0 additions & 8 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 49 additions & 2 deletions src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<CesiumGltf::Model>(&tileLoadResult.contentKind);
auto fabricMeshes = acquireFabricMeshes(*pModel, meshes, hasImagery);
return IntermediateLoadThreadResult{
Expand All @@ -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<CesiumGltf::Model>(&tileLoadResult.contentKind);
Expand All @@ -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<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 @@ -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};
Expand All @@ -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<ImageryLoadThreadResult> pImageryLoadThreadResult{
reinterpret_cast<ImageryLoadThreadResult*>(pLoadThreadResult)};

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

auto texture = pImageryLoadThreadResult->texture;

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

if (!tilesetExists()) {
return;
}

const auto texture = pImageryRenderResources->texture;

auto& content = tile.getContent();
Expand Down Expand Up @@ -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;
Expand All @@ -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

0 comments on commit 6695270

Please sign in to comment.