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

Cache images across glTFs to avoid duplication #926

Merged
merged 100 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
40806a2
Add ability for images to be shared between glTFs
azrogers Jul 10, 2024
7e5270a
Merge branch 'main' of github.com:CesiumGS/cesium-native into shared-…
azrogers Jul 10, 2024
a0954e3
Add test for shared images
azrogers Jul 23, 2024
dd8e3d0
Merge branch 'main' of github.com:CesiumGS/cesium-native into shared-…
azrogers Jul 23, 2024
8d2c5e1
clang-format
azrogers Jul 23, 2024
9bf3ac4
Fix unnecessary reference
azrogers Jul 23, 2024
2f622d7
Unused param
azrogers Jul 23, 2024
4e5bff4
Changes for Unreal implementation
azrogers Aug 16, 2024
e79b313
Merge with main
azrogers Aug 16, 2024
34ec18d
Revert adding extern folder back
azrogers Aug 16, 2024
8ae2150
Narrowing in on the correct set of extern files
azrogers Aug 16, 2024
0018493
Fix GltfReader I broke in merge
azrogers Aug 16, 2024
7e36d6c
Refactor to use IntrusivePointer for AssetContainer
azrogers Aug 23, 2024
2f852d0
Re-add thread safety that I accidentally reverted
azrogers Aug 23, 2024
a5829ef
Fix missing std::move
azrogers Sep 3, 2024
96cd924
Update based on review
azrogers Sep 11, 2024
8d38927
Merge with main
azrogers Sep 11, 2024
0e44d92
clang-format
azrogers Sep 11, 2024
8e664a1
Fix warnings breaking Unreal build
azrogers Sep 13, 2024
d612b32
Use template for factory so it can be moved into the future
azrogers Sep 17, 2024
5ae5539
Clean up SharedAssetDepot
azrogers Sep 18, 2024
0ce17c8
Fix wonky namespaces
azrogers Sep 18, 2024
4851ed2
Fix test
azrogers Sep 18, 2024
31d0048
Add timer for deleting assets
azrogers Sep 25, 2024
45365b2
Merge remote-tracking branch 'origin/main' into shared-assets
kring Sep 25, 2024
97909b8
Add AttributeSemantics.
kring Sep 26, 2024
e781882
Merge remote-tracking branch 'origin/all-with-void-futures' into shar…
kring Sep 26, 2024
6de6b38
Merge remote-tracking branch 'origin/main' into shared-assets-kring
kring Sep 29, 2024
86995ff
Allow constructor parameters to pass to ExtensibleObject::addExtension.
kring Sep 30, 2024
9fe3b4e
WIP error reporting for raster overlays.
kring Sep 2, 2024
95840fe
Fix compile errors in tests.
kring Sep 5, 2024
8ec646c
Propagate raster overlay load errors to the user.
kring Sep 5, 2024
03b60ef
Don't let failing overlays stop rendering completely.
kring Sep 6, 2024
f601680
errors -> errorList.
kring Sep 6, 2024
e83222c
Update CHANGES.md.
kring Sep 6, 2024
407be44
Fix clang error.
kring Sep 6, 2024
05e1af1
Show tileset if overlay tile provider can't be created.
kring Sep 17, 2024
d7bef24
Merge remote-tracking branch 'origin/main' into shared-assets
kring Sep 30, 2024
be51458
Merge remote-tracking branch 'origin/shared-assets' into shared-asset…
kring Sep 30, 2024
91f5b5f
Merge remote-tracking branch 'origin/main' into shared-assets
kring Oct 2, 2024
82fb7e8
Merge remote-tracking branch 'origin/shared-assets' into shared-asset…
kring Oct 2, 2024
e781a9c
AttributeSemantics -> VertexAttributeSemantics
kring Oct 3, 2024
cee6f21
Make SharedAsset a base class an eliminate AssetContainer.
kring Oct 4, 2024
ab21441
Fix water mask bug, add assets to depot.
kring Oct 4, 2024
275bac0
Better naming.
kring Oct 4, 2024
a88d83f
Move constructor for SharedAsset.
kring Oct 4, 2024
80502ad
Merge pull request #959 from CesiumGS/shared-assets-kring
azrogers Oct 4, 2024
e3e5665
Rework deletion
azrogers Oct 4, 2024
d6c7785
Merge branch 'shared-assets-image-cesium-only' of github.com:CesiumGS…
azrogers Oct 4, 2024
9574924
Format
azrogers Oct 4, 2024
aa81cbd
Merge pull request #960 from CesiumGS/shared-assets-image-cesium-only
azrogers Oct 4, 2024
924b000
Add accessors for deletion stats
azrogers Oct 8, 2024
21ecab9
Fix asset lifetime, avoid circular reference counting.
kring Oct 9, 2024
ee4b391
Remove TODO.
kring Oct 9, 2024
1b0934e
Don't use an iterator after it's erased.
kring Oct 9, 2024
8595163
getSizeBytes can use sizeInBytes or pixelData.size().
kring Oct 10, 2024
c8d9612
Remove unnecessary SharedAssetSystem::deletionTick.
kring Oct 10, 2024
6459729
Raster overlays store heap-allocated ImageCesiums.
kring Oct 10, 2024
36783ad
SharedAsset improvements...
kring Oct 10, 2024
e397591
Even independent assets can be shared.
kring Oct 10, 2024
a9516a0
Move SharedAsset[Depot] to CesiumAsync.
kring Oct 10, 2024
7f79837
GltfSharedAssetSystem
kring Oct 10, 2024
526e660
default -> getDefault
kring Oct 10, 2024
df1f887
Doc comments, rename ImageCesium
azrogers Oct 10, 2024
883e36f
Rename ImageAssetMipPosition
azrogers Oct 10, 2024
a230812
Merge pull request #965 from CesiumGS/shared-assets-tweaks
azrogers Oct 10, 2024
3e5eb71
Merge pull request #964 from CesiumGS/shared-assets-lifetime
azrogers Oct 10, 2024
38b4811
Pass factory to asset depot constructor
azrogers Oct 11, 2024
b220031
Doubly linked list for deletion candidates
azrogers Oct 11, 2024
083cb92
WIP refactoring.
kring Oct 25, 2024
727a68d
Introduce AssetEntry.
kring Oct 28, 2024
c75831f
Fix some problems that only showed up in Unreal.
kring Oct 28, 2024
c5533b7
Cleanup.
kring Oct 28, 2024
d16e3ef
Add doc.
kring Oct 28, 2024
49f1d63
Introduce TilesetSharedAssetSystem.
kring Oct 28, 2024
d43edce
Add ResultPointer.
kring Oct 29, 2024
7df4dc4
Keep depot alive if any live assets it manages are alive.
kring Oct 29, 2024
feaf448
Flesh out utility functions slightly.
kring Oct 29, 2024
c73f5b5
staleAssetSizeLimit -> inactiveAssetSizeLimitBytes
kring Oct 29, 2024
ff87332
Formatting.
kring Oct 29, 2024
fd601bc
Fix clang errors.
kring Oct 29, 2024
e9fac59
Fix incorrect comparison.
kring Oct 29, 2024
0961978
Fix a clang warning.
kring Oct 29, 2024
ce212af
Use brace initialization for base class.
kring Oct 29, 2024
172ce88
Merge remote-tracking branch 'origin/main' into shared-assets
kring Oct 29, 2024
117b65f
Merge branch 'shared-assets' into shared-assets-wip
kring Oct 29, 2024
1eee18f
Fix recursive mutex locking bug.
kring Oct 29, 2024
323de96
Fix clang warning.
kring Oct 29, 2024
a9d6c6a
Fix another clang warning.
kring Oct 29, 2024
c654653
Fix small typo
azrogers Oct 29, 2024
f9459d1
Merge pull request #971 from CesiumGS/shared-assets-wip
azrogers Oct 29, 2024
33d4948
Image::pCesium -> pAsset
kring Oct 29, 2024
8b60ebd
Minor tweaks.
kring Oct 30, 2024
e216b54
More small tweaks.
kring Oct 30, 2024
e3c3daf
Move SharedAsset to CesiumUtility.
kring Oct 30, 2024
9dfe894
ImageDecoder cleanup.
kring Oct 30, 2024
e8fbd40
Doc and cleanup.
kring Oct 30, 2024
3435ad6
Pass a TilesetSharedAssetSystem into TilesetExternals.
kring Oct 30, 2024
ba4f6f5
Update CHANGES.md.
kring Oct 30, 2024
750d5f9
Fix clang warning.
kring Oct 30, 2024
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 @@ -177,6 +177,14 @@ class CESIUM3DTILESSELECTION_API Tileset final {
/** @copydoc Tileset::getOverlays() */
const RasterOverlayCollection& getOverlays() const noexcept;

/**
* @brief Returns the {@link SharedAssetDepot} of this tileset.
*/
CesiumGltf::SharedAssetDepot& getSharedAssetDepot() noexcept;

/** @copydoc Tileset::getSharedAssetDepot() */
const CesiumGltf::SharedAssetDepot& getSharedAssetDepot() const noexcept;

/**
* @brief Updates this view but waits for all tiles that meet sse to finish
* loading and ready to be rendered before returning the function. This method
Expand Down
2 changes: 1 addition & 1 deletion Cesium3DTilesSelection/src/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int64_t Tile::computeByteSize() const noexcept {

// sizeBytes is set in TilesetContentManager::ContentKindSetter, if not
// sooner (e.g., by the renderer implementation).
bytes += image.cesium.sizeBytes;
bytes += image.cesium->sizeBytes;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Cesium3DTilesSelection/src/TileContentLoadInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ TileContentLoadInfo::TileContentLoadInfo(
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources_,
const std::shared_ptr<spdlog::logger>& pLogger_,
const std::optional<std::shared_ptr<CesiumGltf::SharedAssetDepot>>
maybeAssetDepot_,
const TilesetContentOptions& contentOptions_,
const Tile& tile)
: asyncSystem(asyncSystem_),
Expand All @@ -18,6 +20,7 @@ TileContentLoadInfo::TileContentLoadInfo(
tileID(tile.getTileID()),
tileBoundingVolume(tile.getBoundingVolume()),
tileContentBoundingVolume(tile.getContentBoundingVolume()),
maybeAssetDepot{maybeAssetDepot_},
tileRefine(tile.getRefine()),
tileGeometricError(tile.getGeometricError()),
tileTransform(tile.getTransform()),
Expand Down
3 changes: 3 additions & 0 deletions Cesium3DTilesSelection/src/TileContentLoadInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct TileContentLoadInfo {
const std::shared_ptr<IPrepareRendererResources>&
pPrepareRendererResources,
const std::shared_ptr<spdlog::logger>& pLogger,
const std::optional<std::shared_ptr<CesiumGltf::SharedAssetDepot>>
maybeAssetDepot,
const TilesetContentOptions& contentOptions,
const Tile& tile);

Expand All @@ -40,6 +42,7 @@ struct TileContentLoadInfo {
BoundingVolume tileBoundingVolume;

std::optional<BoundingVolume> tileContentBoundingVolume;
std::optional<std::shared_ptr<CesiumGltf::SharedAssetDepot>> maybeAssetDepot;

TileRefine tileRefine;

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

CesiumGltf::SharedAssetDepot& Tileset::getSharedAssetDepot() noexcept {
return *this->_pTilesetContentManager->getSharedAssetDepot();
}

const CesiumGltf::SharedAssetDepot&
Tileset::getSharedAssetDepot() const noexcept {
return *this->_pTilesetContentManager->getSharedAssetDepot();
}

static bool
operator<(const FogDensityAtHeight& fogDensity, double height) noexcept {
return fogDensity.cameraHeight < height;
Expand Down
21 changes: 19 additions & 2 deletions Cesium3DTilesSelection/src/TilesetContentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ struct ContentKindSetter {
// size now. We'll be adding this number to our total memory usage soon,
// and remove it when the tile is later unloaded, and we must use
// the same size in each case.
if (image.cesium.sizeBytes < 0) {
image.cesium.sizeBytes = int64_t(image.cesium.pixelData.size());

// TODO: this number will be wrong once we deduplicate images! it'll log
// the image's size for each time it's used, instead of only once as it
// should. if you are reading this comment in a code review, i forgot to
// fix this and you should remind me.
if (image.cesium->sizeBytes < 0) {
image.cesium->sizeBytes = int64_t(image.cesium->pixelData.size());
}
}

Expand Down Expand Up @@ -562,6 +567,9 @@ postProcessContentInWorkerThread(
tileLoadInfo.contentOptions.ktx2TranscodeTargets;
gltfOptions.applyTextureTransform =
tileLoadInfo.contentOptions.applyTextureTransform;
if (tileLoadInfo.maybeAssetDepot.has_value()) {
gltfOptions.sharedAssets = *tileLoadInfo.maybeAssetDepot;
}

auto asyncSystem = tileLoadInfo.asyncSystem;
auto pAssetAccessor = tileLoadInfo.pAssetAccessor;
Expand Down Expand Up @@ -655,6 +663,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_assetDepot(std::make_shared<CesiumGltf::SharedAssetDepot>()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -684,6 +693,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_assetDepot(std::make_shared<CesiumGltf::SharedAssetDepot>()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -835,6 +845,7 @@ TilesetContentManager::TilesetContentManager(
_tileLoadsInProgress{0},
_loadedTilesCount{0},
_tilesDataUsed{0},
_assetDepot(std::make_shared<CesiumGltf::SharedAssetDepot>()),
_destructionCompletePromise{externals.asyncSystem.createPromise<void>()},
_destructionCompleteFuture{
this->_destructionCompletePromise.getFuture().share()},
Expand Down Expand Up @@ -980,6 +991,7 @@ void TilesetContentManager::loadTileContent(
this->_externals.pAssetAccessor,
this->_externals.pPrepareRendererResources,
this->_externals.pLogger,
std::optional(this->_assetDepot),
tilesetOptions.contentOptions,
tile};

Expand Down Expand Up @@ -1209,6 +1221,11 @@ TilesetContentManager::getTilesetCredits() const noexcept {
return this->_tilesetCredits;
}

const std::shared_ptr<CesiumGltf::SharedAssetDepot>&
TilesetContentManager::getSharedAssetDepot() const noexcept {
return this->_assetDepot;
}

int32_t TilesetContentManager::getNumberOfTilesLoading() const noexcept {
return this->_tileLoadsInProgress;
}
Expand Down
7 changes: 7 additions & 0 deletions Cesium3DTilesSelection/src/TilesetContentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <Cesium3DTilesSelection/TilesetLoadFailureDetails.h>
#include <Cesium3DTilesSelection/TilesetOptions.h>
#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumGltf/SharedAssetDepot.h>
#include <CesiumUtility/CreditSystem.h>
#include <CesiumUtility/ReferenceCounted.h>

Expand Down Expand Up @@ -94,6 +95,9 @@ class TilesetContentManager

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

const std::shared_ptr<CesiumGltf::SharedAssetDepot>&
getSharedAssetDepot() const noexcept;

int32_t getNumberOfTilesLoading() const noexcept;

int32_t getNumberOfTilesLoaded() const noexcept;
Expand Down Expand Up @@ -146,6 +150,9 @@ class TilesetContentManager
int32_t _loadedTilesCount;
int64_t _tilesDataUsed;

// Stores assets that might be shared between tiles.
std::shared_ptr<CesiumGltf::SharedAssetDepot> _assetDepot;

CesiumAsync::Promise<void> _destructionCompletePromise;
CesiumAsync::SharedFuture<void> _destructionCompleteFuture;

Expand Down
1 change: 1 addition & 0 deletions Cesium3DTilesSelection/src/TilesetJsonLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,5 @@ void TilesetJsonLoader::addChildLoader(
std::unique_ptr<TilesetContentLoader> pLoader) {
this->_children.emplace_back(std::move(pLoader));
}

} // namespace Cesium3DTilesSelection
2 changes: 2 additions & 0 deletions Cesium3DTilesSelection/src/TilesetJsonLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Cesium3DTilesSelection/TilesetExternals.h>
#include <CesiumAsync/Future.h>
#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumGltf/SharedAssetDepot.h>

#include <rapidjson/fwd.h>

Expand Down Expand Up @@ -55,6 +56,7 @@ class TilesetJsonLoader : public TilesetContentLoader {
private:
std::string _baseUrl;
CesiumGeospatial::Ellipsoid _ellipsoid;
std::optional<std::shared_ptr<CesiumGltf::SharedAssetDepot>> _maybeAssetDepot;

/**
* @brief The axis that was declared as the "up-axis" for glTF content.
Expand Down
63 changes: 63 additions & 0 deletions Cesium3DTilesSelection/test/TestTilesetContentManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "SimplePrepareRendererResource.h"
#include "TestTilesetJsonLoader.h"
#include "TilesetContentManager.h"
#include "TilesetJsonLoader.h"

#include <Cesium3DTilesContent/registerAllTileContentTypes.h>
#include <Cesium3DTilesSelection/RasterOverlayCollection.h>
Expand Down Expand Up @@ -1626,4 +1628,65 @@ TEST_CASE("Test the tileset content manager's post processing for gltf") {

pManager->unloadTileContent(tile);
}

SECTION("Resolve external images, with deduplication") {
std::filesystem::path dirPath(testDataPath / "SharedImages");

// mock the requests for all files
for (const auto& entry : std::filesystem::directory_iterator(dirPath)) {
pMockedAssetAccessor->mockCompletedRequests.insert(
{entry.path().filename().string(), createMockRequest(entry.path())});
}

std::filesystem::path tilesetPath(dirPath / "tileset.json");
auto pExternals = createMockJsonTilesetExternals(
tilesetPath.string(),
pMockedAssetAccessor);

auto pJsonLoaderFuture =
TilesetJsonLoader::createLoader(pExternals, tilesetPath.string(), {});

externals.asyncSystem.dispatchMainThreadTasks();

auto loaderResult = pJsonLoaderFuture.wait();

REQUIRE(loaderResult.pRootTile);
REQUIRE(loaderResult.pRootTile->getChildren().size() == 1);

auto& rootTile = *loaderResult.pRootTile;
auto& containerTile = rootTile.getChildren()[0];

REQUIRE(containerTile.getChildren().size() == 100);

// create manager
Tile::LoadedLinkedList loadedTiles;
IntrusivePointer<TilesetContentManager> pManager =
new TilesetContentManager{
externals,
{},
RasterOverlayCollection{loadedTiles, externals},
{},
std::move(loaderResult.pLoader),
std::move(loaderResult.pRootTile)};

for (auto& child : containerTile.getChildren()) {
pManager->loadTileContent(child, {});
externals.asyncSystem.dispatchMainThreadTasks();
pManager->waitUntilIdle();

CHECK(child.getState() == TileLoadState::ContentLoaded);
CHECK(child.isRenderContent());

const auto& renderContent = child.getContent().getRenderContent();
const auto& images = renderContent->getModel().images;
CHECK(images.size() == 1);
}

CHECK(pManager->getSharedAssetDepot()->getImagesCount() == 2);

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