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

Some improvements to the SharedAsset system #971

Merged
merged 20 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include <vector>

namespace Cesium3DTilesSelection {

class TilesetContentManager;
class TilesetMetadata;
class TilesetHeightQuery;
class TilesetHeightRequest;
class TilesetSharedAssetSystem;

/**
* @brief A <a
Expand Down Expand Up @@ -184,11 +186,10 @@ class CESIUM3DTILESSELECTION_API Tileset final {
/**
* @brief Returns the {@link SharedAssetDepot} of this tileset.
*/
CesiumGltfReader::GltfSharedAssetSystem& getSharedAssetSystem() noexcept;
TilesetSharedAssetSystem& getSharedAssetSystem() noexcept;

/** @copydoc Tileset::getSharedAssetSystem() */
const CesiumGltfReader::GltfSharedAssetSystem&
getSharedAssetSystem() const noexcept;
const TilesetSharedAssetSystem& getSharedAssetSystem() const noexcept;

/**
* @brief Updates this view but waits for all tiles that meet sse to finish
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <CesiumGltfReader/GltfSharedAssetSystem.h>

namespace Cesium3DTilesSelection {

/**
* @brief Contains assets that are potentially shared across multiple Tilesets.
*/
class TilesetSharedAssetSystem
: public CesiumGltfReader::GltfSharedAssetSystem {
public:
static CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> getDefault();

virtual ~TilesetSharedAssetSystem() = default;
};

} // namespace Cesium3DTilesSelection
6 changes: 3 additions & 3 deletions Cesium3DTilesSelection/src/TileContentLoadInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ TileContentLoadInfo::TileContentLoadInfo(
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources_,
const std::shared_ptr<spdlog::logger>& pLogger_,
const CesiumUtility::IntrusivePointer<
CesiumGltfReader::GltfSharedAssetSystem> pAssetDepot_,
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
pSharedAssetSystem_,
const TilesetContentOptions& contentOptions_,
const Tile& tile)
: asyncSystem(asyncSystem_),
Expand All @@ -20,7 +20,7 @@ TileContentLoadInfo::TileContentLoadInfo(
tileID(tile.getTileID()),
tileBoundingVolume(tile.getBoundingVolume()),
tileContentBoundingVolume(tile.getContentBoundingVolume()),
pAssetDepot{pAssetDepot_},
pSharedAssetSystem{pSharedAssetSystem_},
tileRefine(tile.getRefine()),
tileGeometricError(tile.getGeometricError()),
tileTransform(tile.getTransform()),
Expand Down
9 changes: 4 additions & 5 deletions Cesium3DTilesSelection/src/TileContentLoadInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <Cesium3DTilesSelection/TileID.h>
#include <Cesium3DTilesSelection/TileRefine.h>
#include <Cesium3DTilesSelection/TilesetOptions.h>
#include <Cesium3DTilesSelection/TilesetSharedAssetSystem.h>
#include <CesiumAsync/AsyncSystem.h>
#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumGeometry/Axis.h>
#include <CesiumGltfReader/GltfSharedAssetSystem.h>

#include <gsl/span>
#include <spdlog/fwd.h>
Expand All @@ -25,8 +25,8 @@ struct TileContentLoadInfo {
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources,
const std::shared_ptr<spdlog::logger>& pLogger,
const CesiumUtility::IntrusivePointer<
CesiumGltfReader::GltfSharedAssetSystem> maybeAssetDepot,
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
pSharedAssetSystem,
const TilesetContentOptions& contentOptions,
const Tile& tile);

Expand All @@ -43,8 +43,7 @@ struct TileContentLoadInfo {
BoundingVolume tileBoundingVolume;

std::optional<BoundingVolume> tileContentBoundingVolume;
CesiumUtility::IntrusivePointer<CesiumGltfReader::GltfSharedAssetSystem>
pAssetDepot;
CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> pSharedAssetSystem;

TileRefine tileRefine;

Expand Down
6 changes: 2 additions & 4 deletions Cesium3DTilesSelection/src/Tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,11 @@ const RasterOverlayCollection& Tileset::getOverlays() const noexcept {
return this->_pTilesetContentManager->getRasterOverlayCollection();
}

CesiumGltfReader::GltfSharedAssetSystem&
Tileset::getSharedAssetSystem() noexcept {
TilesetSharedAssetSystem& Tileset::getSharedAssetSystem() noexcept {
return *this->_pTilesetContentManager->getSharedAssetSystem();
}

const CesiumGltfReader::GltfSharedAssetSystem&
Tileset::getSharedAssetSystem() const noexcept {
const TilesetSharedAssetSystem& Tileset::getSharedAssetSystem() const noexcept {
return *this->_pTilesetContentManager->getSharedAssetSystem();
}

Expand Down
18 changes: 6 additions & 12 deletions Cesium3DTilesSelection/src/TilesetContentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ postProcessContentInWorkerThread(
tileLoadInfo.contentOptions.ktx2TranscodeTargets;
gltfOptions.applyTextureTransform =
tileLoadInfo.contentOptions.applyTextureTransform;
if (tileLoadInfo.pAssetDepot) {
gltfOptions.pSharedAssets = tileLoadInfo.pAssetDepot;
if (tileLoadInfo.pSharedAssetSystem) {
gltfOptions.pSharedAssets = tileLoadInfo.pSharedAssetSystem;
}

auto asyncSystem = tileLoadInfo.asyncSystem;
Expand Down Expand Up @@ -666,9 +666,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_pSharedAssets(TilesetSharedAssetSystem::getDefault()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -698,9 +696,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_pSharedAssets(TilesetSharedAssetSystem::getDefault()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -852,9 +848,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_pSharedAssets(CesiumGltfReader::GltfSharedAssetSystem::getDefault(
CesiumGltfReader::AssetSystemOptions{
tilesetOptions.contentOptions.ktx2TranscodeTargets})),
_pSharedAssets(TilesetSharedAssetSystem::getDefault()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -1243,7 +1237,7 @@ TilesetContentManager::getTilesetCredits() const noexcept {
return this->_tilesetCredits;
}

const CesiumUtility::IntrusivePointer<CesiumGltfReader::GltfSharedAssetSystem>&
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
TilesetContentManager::getSharedAssetSystem() const noexcept {
return this->_pSharedAssets;
}
Expand Down
8 changes: 4 additions & 4 deletions Cesium3DTilesSelection/src/TilesetContentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Cesium3DTilesSelection {

class TilesetSharedAssetSystem;

class TilesetContentManager
: public CesiumUtility::ReferenceCountedNonThreadSafe<
TilesetContentManager> {
Expand Down Expand Up @@ -115,8 +117,7 @@ class TilesetContentManager

const std::vector<CesiumUtility::Credit>& getTilesetCredits() const noexcept;

const CesiumUtility::IntrusivePointer<
CesiumGltfReader::GltfSharedAssetSystem>&
const CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>&
getSharedAssetSystem() const noexcept;

int32_t getNumberOfTilesLoading() const noexcept;
Expand Down Expand Up @@ -172,8 +173,7 @@ class TilesetContentManager
int64_t _tilesDataUsed;

// Stores assets that might be shared between tiles.
CesiumUtility::IntrusivePointer<CesiumGltfReader::GltfSharedAssetSystem>
_pSharedAssets;
CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> _pSharedAssets;

CesiumAsync::Promise<void> _destructionCompletePromise;
CesiumAsync::SharedFuture<void> _destructionCompleteFuture;
Expand Down
5 changes: 2 additions & 3 deletions Cesium3DTilesSelection/src/TilesetJsonLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include <Cesium3DTilesSelection/TilesetContentLoader.h>
#include <Cesium3DTilesSelection/TilesetExternals.h>
#include <Cesium3DTilesSelection/TilesetSharedAssetSystem.h>
#include <CesiumAsync/Future.h>
#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumGltfReader/GltfSharedAssetSystem.h>

#include <rapidjson/fwd.h>

Expand Down Expand Up @@ -56,8 +56,7 @@ class TilesetJsonLoader : public TilesetContentLoader {
private:
std::string _baseUrl;
CesiumGeospatial::Ellipsoid _ellipsoid;
CesiumUtility::IntrusivePointer<CesiumGltfReader::GltfSharedAssetSystem>
_pSharedAssets;
CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> _pSharedAssets;

/**
* @brief The axis that was declared as the "up-axis" for glTF content.
Expand Down
34 changes: 34 additions & 0 deletions Cesium3DTilesSelection/src/TilesetSharedAssetSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Cesium3DTilesSelection/TilesetSharedAssetSystem.h>

using namespace Cesium3DTilesSelection;
using namespace CesiumAsync;
using namespace CesiumGltf;
using namespace CesiumGltfReader;
using namespace CesiumUtility;

namespace {

CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> createDefault() {
CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> p =
new TilesetSharedAssetSystem();

CesiumUtility::IntrusivePointer<GltfSharedAssetSystem> pGltf =
GltfSharedAssetSystem::getDefault();

p->pImage = pGltf->pImage;

return p;
}

} // namespace

namespace Cesium3DTilesSelection {

/*static*/ CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem>
TilesetSharedAssetSystem::getDefault() {
static CesiumUtility::IntrusivePointer<TilesetSharedAssetSystem> pDefault =
createDefault();
return pDefault;
}

} // namespace Cesium3DTilesSelection
19 changes: 18 additions & 1 deletion Cesium3DTilesSelection/test/TestTilesetContentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,11 +1682,28 @@ TEST_CASE("Test the tileset content manager's post processing for gltf") {
CHECK(images.size() == 1);
}

CHECK(pManager->getSharedAssetSystem()->pImage->getDistinctCount() == 2);
CHECK(
pManager->getSharedAssetSystem()
->pImage->getInactiveAssetTotalSizeBytes() == 0);
CHECK(pManager->getSharedAssetSystem()->pImage->getAssetCount() == 2);
CHECK(pManager->getSharedAssetSystem()->pImage->getActiveAssetCount() == 2);
CHECK(
pManager->getSharedAssetSystem()->pImage->getInactiveAssetCount() == 0);

// unload the tile content
for (auto& child : containerTile.getChildren()) {
pManager->unloadTileContent(child);
}

// Both of the assets will become inactive, and one of them will be
// destroyed, in order to bring the total under the limit.
CHECK(
pManager->getSharedAssetSystem()
->pImage->getInactiveAssetTotalSizeBytes() <=
pManager->getSharedAssetSystem()->pImage->inactiveAssetSizeLimitBytes);
CHECK(pManager->getSharedAssetSystem()->pImage->getAssetCount() == 1);
CHECK(pManager->getSharedAssetSystem()->pImage->getActiveAssetCount() == 0);
CHECK(
pManager->getSharedAssetSystem()->pImage->getInactiveAssetCount() == 1);
}
}
3 changes: 2 additions & 1 deletion CesiumAsync/include/CesiumAsync/IAssetAccessor.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "AsyncSystem.h"
#include "Future.h"
#include "IAssetRequest.h"
#include "Library.h"

Expand All @@ -12,6 +12,7 @@
#include <vector>

namespace CesiumAsync {

class AsyncSystem;

/**
Expand Down
43 changes: 43 additions & 0 deletions CesiumAsync/include/CesiumAsync/IDepotOwningAsset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

namespace CesiumAsync {

/**
* @brief An interface representing the depot that owns a {@link SharedAsset}.
* This interface is an implementation detail of the shared asset system and
* should not be used directly.
*
* {@link SharedAsset} has a pointer to the asset depot that owns it using this
* interface, rather than a complete {@link SharedAssetDepot}, in order to
* "erase" the type of the asset key. This allows SharedAsset to be templatized
* only on the asset type, not on the asset key type.
*/
template <typename TAssetType> class IDepotOwningAsset {
public:
virtual ~IDepotOwningAsset() {}

/**
* @brief Marks the given asset as a candidate for deletion.
* Should only be called by {@link SharedAsset}. May be called from any thread.
*
* @param asset The asset to mark for deletion.
* @param threadOwnsDepotLock True if the calling thread already owns the
* depot lock; otherwise, false.
*/
virtual void
markDeletionCandidate(const TAssetType& asset, bool threadOwnsDepotLock) = 0;

/**
* @brief Unmarks the given asset as a candidate for deletion.
* Should only be called by {@link SharedAsset}. May be called from any thread.
*
* @param asset The asset to unmark for deletion.
* @param threadOwnsDepotLock True if the calling thread already owns the
* depot lock; otherwise, false.
*/
virtual void unmarkDeletionCandidate(
const TAssetType& asset,
bool threadOwnsDepotLock) = 0;
};

} // namespace CesiumAsync
Loading
Loading