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 #1521

Merged
merged 83 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
5c4d926
Tests passing.
azrogers Aug 16, 2024
4a0eb74
Merge with main
azrogers Aug 16, 2024
0748638
Update cesium-native
azrogers Aug 16, 2024
e08581e
Update cesium-native
azrogers Aug 16, 2024
1d85d7b
Start of test for shared images.
azrogers Aug 20, 2024
fcb53ea
Almost fully asynced glTF loading
azrogers Aug 30, 2024
21f034f
Fixed UniquePtr issues
azrogers Sep 3, 2024
164379e
Attempting to resolve invalid pointer issue with material
azrogers Sep 3, 2024
dd14eb1
Fixed crash
azrogers Sep 4, 2024
d9bf27e
Tile debug overlay
azrogers Sep 4, 2024
5c486cf
Working! All tests passing
azrogers Sep 9, 2024
30eeee7
Fix test, update cesium-native
azrogers Sep 11, 2024
03065e9
Merge branch 'main' of github.com:CesiumGS/cesium-unreal into shared-…
azrogers Sep 11, 2024
cc1bd44
Re-add workaround from 396f78f
azrogers Sep 11, 2024
1b7b940
clang-format
azrogers Sep 11, 2024
38042e4
Fix clang-formatting by letting npm dependencies update.
kring Sep 12, 2024
4d68a89
Update cesium-native
azrogers Sep 13, 2024
3784f5d
Merge branch 'shared-assets' of github.com:CesiumGS/cesium-unreal int…
azrogers Sep 13, 2024
1ff1fd3
De-asyncify most of the gltf loading. Still seeing mysterious compila…
azrogers Sep 13, 2024
b0f37ec
Fix lifetime issues
azrogers Sep 18, 2024
6b43702
Log asset stats
azrogers Sep 18, 2024
6fcce71
Fix mipmap generation
azrogers Sep 19, 2024
ac6dbbb
Hopefully fix CI errors
azrogers Sep 19, 2024
1ef546f
Add Map header
azrogers Sep 19, 2024
4da4e83
Add automation test headers
azrogers Sep 19, 2024
0637307
Add WITH_EDITOR block
azrogers Sep 19, 2024
9530e72
Update based on review
azrogers Sep 25, 2024
9e9b409
Merge remote-tracking branch 'origin/main' into shared-assets
kring Sep 25, 2024
eefb113
WIP texture loading reorg.
kring Sep 26, 2024
efe384a
Merge remote-tracking branch 'origin/main' into shared-assets-kring
kring Sep 29, 2024
8b0bcdc
Sort of working again after reorg.
kring Sep 30, 2024
ceca504
Undo accidental rename.
kring Sep 30, 2024
3067284
Use correct sRGB setting when initially creating texture.
kring Sep 30, 2024
aa58bd9
Fix UE 5.3/5.4 compile error.
kring Sep 30, 2024
1246c2d
Fix raster overlays and water mask.
kring Sep 30, 2024
b75d274
GetOrCreate -> getOrCreate.
kring Sep 30, 2024
8141c5c
Fix another UE 5.3/5.4 compile error.
kring Sep 30, 2024
e4734e3
Fix test failures.
kring Sep 30, 2024
1777db0
Formatting.
kring Sep 30, 2024
28a28e0
Merge remote-tracking branch 'origin/main' into shared-assets
kring Sep 30, 2024
93a4265
Merge remote-tracking branch 'origin/shared-assets' into shared-asset…
kring Sep 30, 2024
b03d1f6
SharedImages test working on CI
azrogers Oct 1, 2024
0f341f0
Merge remote-tracking branch 'origin/main' into shared-assets
kring Oct 2, 2024
83a24d1
Merge remote-tracking branch 'origin/shared-assets' into shared-asset…
kring Oct 2, 2024
8020401
Const correctness, doc.
kring Oct 3, 2024
b1b0368
Cleanup, make some useful warnings visible.
kring Oct 3, 2024
d9dffcd
Fix metadata crash caused by failed move from const.
kring Oct 3, 2024
6451ef3
Use ion for shared images test
azrogers Oct 3, 2024
22786f6
Merge branch 'shared-assets-kring' of github.com:CesiumGS/cesium-unre…
azrogers Oct 3, 2024
d3fbccc
AttributeSemantics -> VertexAttributeSemantics
kring Oct 3, 2024
ddff9db
Adapt for ImageCesium only design.
kring Oct 4, 2024
edd8226
More updates for changes in Native.
kring Oct 4, 2024
0a14600
Update cesium-native to shared-assets branch
azrogers Oct 4, 2024
90ea7e4
Integrate deletion rework
azrogers Oct 4, 2024
9a63282
Add skeleton of a Snowdon test, properly report texture stats
azrogers Oct 8, 2024
fc60c1e
Update cesium-native.
kring Oct 10, 2024
1b59597
Rename ImageCesium
azrogers Oct 10, 2024
c042a39
Merge pull request #1531 from CesiumGS/shared-assets-tweaks
azrogers Oct 10, 2024
62bdd81
Merge pull request #1527 from CesiumGS/shared-assets-kring
azrogers Oct 10, 2024
9a675e2
Format, update cesium-native
azrogers Oct 10, 2024
85d6b39
Clean up CesiumTextureUtility
azrogers Oct 10, 2024
d8f27c6
Use shared-assets-wip branch of cesium-native.
kring Oct 28, 2024
4a96eea
Update cesium-native.
kring Oct 28, 2024
2d1ccd8
Update cesium-native.
kring Oct 29, 2024
d7cda44
Merge remote-tracking branch 'origin/main' into shared-assets
kring Oct 29, 2024
71b885b
Merge remote-tracking branch 'origin/shared-assets' into shared-asset…
kring Oct 29, 2024
463e027
Update cesium-native.
kring Oct 29, 2024
9bc94d3
Merge pull request #1536 from CesiumGS/shared-assets-wip
azrogers Oct 29, 2024
6b7f4af
Return to shared-assets branch of cesium-native
azrogers Oct 29, 2024
adec72e
pCesium -> pAsset.
kring Oct 29, 2024
f16dffb
Fix test failure.
kring Oct 30, 2024
c45f82f
Formatting.
kring Oct 30, 2024
04bac36
Don't use deprecated methods.
kring Oct 30, 2024
71d0297
Fix compiler error on macOS, hopefully.
kring Oct 30, 2024
d61b912
Remove unused code.
kring Oct 30, 2024
2ec8b60
LogAssetStats -> LogSharedAssetStats
kring Oct 30, 2024
da6f64c
Remove CesiumGltf usings to try to fix OSX build
azrogers Oct 30, 2024
935e0db
Fix memory leak and non-async texture creation.
kring Oct 31, 2024
26eff5e
More namespacing.
kring Oct 31, 2024
783bb4e
Fix tests
azrogers Oct 31, 2024
358d779
Update CHANGES, remove Snowdon test
azrogers Oct 31, 2024
a6d7b3b
Remove Class.h include
azrogers Oct 31, 2024
28333a1
Eliminate another round of `using namespace CesiumGltf`.
kring Oct 31, 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
13 changes: 7 additions & 6 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
##### Additions :tada:

- Added `CircumscribedGroundHeight` property to `CesiumSunSky`. It defaults to 0, which is consistent with the previous behavior. It can be set to a larger value (like 40) to avoid dark splotchy artifacts when zoomed out far from the globe in certain tilesets where geometry extends very far beyond the ellipsoid in the low-detail representation, such as Google Photorealistic 3D Tiles.
- Added a shared assets system that ensures external images referenced by different glTFs will only be loaded once per image. Previously, these images would be loaded again for each glTF that referenced them. This significantly reduces memory usage for tilesets that reuse the same textures.

##### Fixes :wrench:

Expand Down Expand Up @@ -164,16 +165,16 @@ In addition to the above, this release updates [cesium-native](https://github.co

##### Additions :tada:

- Added support for multiple Cesium ion servers by creating `CesiumIonServer` data assets.
- Added support for multiple Cesium ion servers by creating `CesiumIonServer` data assets.

In addition to the above, this release updates [cesium-native](https://github.com/CesiumGS/cesium-native) from v0.30.0 to v0.31.0. See the [changelog](https://github.com/CesiumGS/cesium-native/blob/main/CHANGES.md) for a complete list of changes in cesium-native.

### v2.1.0 - 2023-12-01

##### Additions :tada:

- Added support for styling with property textures in `EXT_structural_metadata`.
- Significantly improved tile download performance by adding `HttpThreadActiveFrameTimeInSeconds=0.001` to `Engine.ini`. This results in a major performance improvement for all tilesets, particularly Google Photorealistic 3D Tiles.
- Added support for styling with property textures in `EXT_structural_metadata`.
- Significantly improved tile download performance by adding `HttpThreadActiveFrameTimeInSeconds=0.001` to `Engine.ini`. This results in a major performance improvement for all tilesets, particularly Google Photorealistic 3D Tiles.
- Added `HttpMaxConnectionsPerServer=40` to `Engine.ini`. By default, only 16 connections are allowed, which limits the performance when downloading tiles.

##### Fixes :wrench:
Expand All @@ -193,7 +194,7 @@ This release no longer supports Unreal Engine v5.0. Unreal Engine v5.1, v5.2, or
##### Breaking Changes :mega:

- Removed `FCesiumIntegerColor`, `FCesiumFloatColor`, `UCesiumFeatureTexturePropertyBlueprintLibrary::GetIntegerColorFromTextureCoordinates` and `UCesiumFeatureTexturePropertyBlueprintLibrary::GetFloatColorFromTextureCoordinates`. Check out the [upgrade guide](Documentation/upgrade-to-2.0-guide.md) for how retrieve metadata from property textures with the new API.
- Renamed `GetTextureCoordinateIndex` to `GetUnrealUVChannel` in both `UCesiumFeatureIdTextureBlueprintLibrary` and `UCesiumPropertyTexturePropertyBlueprintLibrary`. Contrary to what the documentation claimed, this function retrieved the index of the texture coordinate set in the *Unreal static mesh*, which is not necessarily equal to the texture coordinate set index in the *glTF primitive*. For the latter value, use `GetGltfTextureCoordinateSetIndex` instead.
- Renamed `GetTextureCoordinateIndex` to `GetUnrealUVChannel` in both `UCesiumFeatureIdTextureBlueprintLibrary` and `UCesiumPropertyTexturePropertyBlueprintLibrary`. Contrary to what the documentation claimed, this function retrieved the index of the texture coordinate set in the _Unreal static mesh_, which is not necessarily equal to the texture coordinate set index in the _glTF primitive_. For the latter value, use `GetGltfTextureCoordinateSetIndex` instead.
- Removed the old "exclusion zones" feature, which has been deprecated since v1.11.0. Use `CesiumCartographicPolygon` or `CesiumTileExcluder` instead.

##### Additions :tada:
Expand Down Expand Up @@ -342,7 +343,7 @@ In addition to the above, this release updates [cesium-native](https://github.co
##### Fixes :wrench:

- Added a workaround for an apparent bug in Unreal Engine 5.1 that prevented collisions from working with Cesium3DTilesets.
- Fixed a bug that could cause the `AGlobeAwareDefaultPawn` / `DynamicPawn` to suddenly move to a very high height for one render frame just as it arrives at its destination during a flight.
- Fixed a bug that could cause the `AGlobeAwareDefaultPawn` / `DynamicPawn` to suddenly move to a very high height for one render frame just as it arrives at its destination during a flight.

In addition to the above, this release updates [cesium-native](https://github.com/CesiumGS/cesium-native) from v0.25.0 to v0.25.1. See the [changelog](https://github.com/CesiumGS/cesium-native/blob/main/CHANGES.md) for a complete list of changes in cesium-native.

Expand Down Expand Up @@ -405,7 +406,7 @@ This will be the _last_ release that supports Unreal Engine v4.27. Future versio

##### Additions :tada:

- The `FlyToAltitudeProfileCurve`, `FlyToProgressCurve`, `FlyToMaximumAltitudeCurve`, `FlyToDuration`, and `FlyToGranularityDegrees` properties of `GlobeAwareDefaultPawn` / `DynamicPawn` may now be read and written from Blueprints.
- The `FlyToAltitudeProfileCurve`, `FlyToProgressCurve`, `FlyToMaximumAltitudeCurve`, `FlyToDuration`, and `FlyToGranularityDegrees` properties of `GlobeAwareDefaultPawn` / `DynamicPawn` may now be read and written from Blueprints.
- Added an option on `Cesium3DTileset` to ignore the `KHR_materials_unlit` extension entirely and use normal lighting and shadows.
- Added `CreateNavCollision` property to `Cesium3DTileset`. When enabled, `CreateNavCollision` is called on the static meshes created for tiles.

Expand Down
149 changes: 108 additions & 41 deletions Source/CesiumRuntime/Private/Cesium3DTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
#include "Cesium3DTilesSelection/Tile.h"
#include "Cesium3DTilesSelection/TilesetLoadFailureDetails.h"
#include "Cesium3DTilesSelection/TilesetOptions.h"
#include "Cesium3DTilesSelection/TilesetSharedAssetSystem.h"
#include "Cesium3DTilesetLoadFailureDetails.h"
#include "Cesium3DTilesetRoot.h"
#include "CesiumActors.h"
#include "CesiumAsync/SharedAssetDepot.h"
#include "CesiumBoundingVolumeComponent.h"
#include "CesiumCamera.h"
#include "CesiumCameraManager.h"
#include "CesiumCommon.h"
#include "CesiumCustomVersion.h"
#include "CesiumGeospatial/GlobeTransforms.h"
#include "CesiumGltf/ImageCesium.h"
#include "CesiumGltf/ImageAsset.h"
#include "CesiumGltf/Ktx2TranscodeTargets.h"
#include "CesiumGltfComponent.h"
#include "CesiumGltfPointsSceneProxyUpdater.h"
Expand All @@ -40,6 +42,7 @@
#include "Engine/TextureRenderTarget2D.h"
#include "Engine/World.h"
#include "EngineUtils.h"
#include "ExtensionImageAssetUnreal.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/GameplayStatics.h"
#include "LevelSequenceActor.h"
Expand Down Expand Up @@ -781,16 +784,14 @@ class UnrealResourcePreparer
Cesium3DTilesSelection::TileLoadResult&& tileLoadResult,
const glm::dmat4& transform,
const std::any& rendererOptions) override {
CesiumGltf::Model* pModel =
std::get_if<CesiumGltf::Model>(&tileLoadResult.contentKind);
if (!pModel)
CreateGltfOptions::CreateModelOptions options(std::move(tileLoadResult));
if (!options.pModel) {
return asyncSystem.createResolvedFuture(
Cesium3DTilesSelection::TileLoadResultAndRenderResources{
std::move(tileLoadResult),
std::move(options.tileLoadResult),
nullptr});
}

CreateGltfOptions::CreateModelOptions options;
options.pModel = pModel;
options.alwaysIncludeTangents = this->_pActor->GetAlwaysIncludeTangents();
options.createPhysicsMeshes = this->_pActor->GetCreatePhysicsMeshes();

Expand All @@ -807,16 +808,21 @@ class UnrealResourcePreparer

const CesiumGeospatial::Ellipsoid& ellipsoid = tileLoadResult.ellipsoid;

TUniquePtr<UCesiumGltfComponent::HalfConstructed> pHalf =
UCesiumGltfComponent::CreateOffGameThread(
CesiumAsync::Future<UCesiumGltfComponent::CreateOffGameThreadResult>
pHalfFuture = UCesiumGltfComponent::CreateOffGameThread(
asyncSystem,
transform,
options,
std::move(options),
ellipsoid);

return asyncSystem.createResolvedFuture(
Cesium3DTilesSelection::TileLoadResultAndRenderResources{
std::move(tileLoadResult),
pHalf.Release()});
return MoveTemp(pHalfFuture)
.thenImmediately(
[](UCesiumGltfComponent::CreateOffGameThreadResult&& result)
-> Cesium3DTilesSelection::TileLoadResultAndRenderResources {
return Cesium3DTilesSelection::TileLoadResultAndRenderResources{
std::move(result.TileLoadResult),
result.HalfConstructed.Release()};
});
}

virtual void* prepareInMainThread(
Expand Down Expand Up @@ -862,7 +868,7 @@ class UnrealResourcePreparer
}

virtual void* prepareRasterInLoadThread(
CesiumGltf::ImageCesium& image,
CesiumGltf::ImageAsset& image,
const std::any& rendererOptions) override {
auto ppOptions =
std::any_cast<FRasterOverlayRendererOptions*>(&rendererOptions);
Expand All @@ -875,7 +881,7 @@ class UnrealResourcePreparer

if (pOptions->useMipmaps) {
std::optional<std::string> errorMessage =
CesiumGltfReader::GltfReader::generateMipMaps(image);
CesiumGltfReader::ImageDecoder::generateMipMaps(image);
if (errorMessage) {
UE_LOG(
LogCesium,
Expand All @@ -885,17 +891,31 @@ class UnrealResourcePreparer
}
}

// TODO: sRGB should probably be configurable on the raster overlay.
bool sRGB = true;

const ExtensionImageAssetUnreal& extension =
ExtensionImageAssetUnreal::getOrCreate(
CesiumAsync::AsyncSystem(nullptr), // TODO
image,
sRGB,
pOptions->useMipmaps,
std::nullopt);

// Because raster overlay images are never shared (at least currently!), the
// future should already be resolved by the time we get here.
check(extension.getFuture().isReady());

auto texture = CesiumTextureUtility::loadTextureAnyThreadPart(
image,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
pOptions->filter,
pOptions->useMipmaps,
pOptions->group,
// TODO: sRGB should probably be configurable on the raster overlay.
true,
std::nullopt,
nullptr);
sRGB,
std::nullopt);

return texture.Release();
}

Expand Down Expand Up @@ -954,7 +974,7 @@ class UnrealResourcePreparer
const Cesium3DTilesSelection::TileContent& content = tile.getContent();
const Cesium3DTilesSelection::TileRenderContent* pRenderContent =
content.getRenderContent();
if (pRenderContent) {
if (pMainThreadRendererResources != nullptr && pRenderContent != nullptr) {
UCesiumGltfComponent* pGltfContent =
reinterpret_cast<UCesiumGltfComponent*>(
pRenderContent->getRenderResources());
Expand Down Expand Up @@ -984,7 +1004,7 @@ class UnrealResourcePreparer
UCesiumGltfComponent* pGltfContent =
reinterpret_cast<UCesiumGltfComponent*>(
pRenderContent->getRenderResources());
if (pGltfContent) {
if (pMainThreadRendererResources != nullptr && pGltfContent != nullptr) {
pGltfContent->DetachRasterTile(
tile,
rasterTile,
Expand Down Expand Up @@ -1941,7 +1961,38 @@ void ACesium3DTileset::updateLastViewUpdateResultState(
const Cesium3DTilesSelection::ViewUpdateResult& result) {
TRACE_CPUPROFILER_EVENT_SCOPE(Cesium::updateLastViewUpdateResultState)

if (!this->LogSelectionStats) {
if (this->DrawTileInfo) {
azrogers marked this conversation as resolved.
Show resolved Hide resolved
const UWorld* World = GetWorld();
check(World);

const TSoftObjectPtr<ACesiumGeoreference> Georeference =
ResolveGeoreference();
check(Georeference);
azrogers marked this conversation as resolved.
Show resolved Hide resolved

for (Cesium3DTilesSelection::Tile* tile : result.tilesToRenderThisFrame) {

CesiumGeometry::OrientedBoundingBox obb =
Cesium3DTilesSelection::getOrientedBoundingBoxFromBoundingVolume(
tile->getBoundingVolume(),
Georeference->GetEllipsoid()->GetNativeEllipsoid());

FVector unrealCenter =
Georeference->TransformEarthCenteredEarthFixedPositionToUnreal(
VecMath::createVector(obb.getCenter()));

FString text = FString::Printf(
TEXT("ID %s (%p)"),
UTF8_TO_TCHAR(
Cesium3DTilesSelection::TileIdUtilities::createTileIdString(
tile->getTileID())
.c_str()),
tile);

DrawDebugString(World, unrealCenter, text, nullptr, FColor::Red, 0, true);
}
}

if (!this->LogSelectionStats && !this->LogSharedAssetStats) {
return;
}

Expand Down Expand Up @@ -1972,24 +2023,40 @@ void ACesium3DTileset::updateLastViewUpdateResultState(
result.tilesWaitingForOcclusionResults;
this->_lastMaxDepthVisited = result.maxDepthVisited;

UE_LOG(
LogCesium,
Display,
TEXT(
"%s: %d ms, Visited %d, Culled Visited %d, Rendered %d, Culled %d, Occluded %d, Waiting For Occlusion Results %d, Max Depth Visited: %d, Loading-Worker %d, Loading-Main %d, Loaded tiles %g%%"),
*this->GetName(),
(std::chrono::high_resolution_clock::now() - this->_startTime).count() /
1000000,
result.tilesVisited,
result.culledTilesVisited,
result.tilesToRenderThisFrame.size(),
result.tilesCulled,
result.tilesOccluded,
result.tilesWaitingForOcclusionResults,
result.maxDepthVisited,
result.workerThreadTileLoadQueueLength,
result.mainThreadTileLoadQueueLength,
this->LoadProgress);
if (this->LogSelectionStats) {
UE_LOG(
LogCesium,
Display,
TEXT(
"%s: %d ms, Visited %d, Culled Visited %d, Rendered %d, Culled %d, Occluded %d, Waiting For Occlusion Results %d, Max Depth Visited: %d, Loading-Worker %d, Loading-Main %d, Loaded tiles %g%%"),
*this->GetName(),
(std::chrono::high_resolution_clock::now() - this->_startTime)
.count() /
1000000,
result.tilesVisited,
result.culledTilesVisited,
result.tilesToRenderThisFrame.size(),
result.tilesCulled,
result.tilesOccluded,
result.tilesWaitingForOcclusionResults,
result.maxDepthVisited,
result.workerThreadTileLoadQueueLength,
result.mainThreadTileLoadQueueLength,
this->LoadProgress);
}

if (this->LogSharedAssetStats && this->_pTileset) {
const Cesium3DTilesSelection::TilesetSharedAssetSystem::ImageDepot&
imageDepot = *this->_pTileset->getSharedAssetSystem().pImage;
UE_LOG(
LogCesium,
Display,
TEXT(
"Images shared asset depot: %d distinct assets, %d inactive assets pending deletion (%d bytes)"),
imageDepot.getAssetCount(),
imageDepot.getInactiveAssetCount(),
imageDepot.getInactiveAssetTotalSizeBytes());
}
}
}

Expand Down
Loading