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

Implement derived content caching #990

Draft
wants to merge 9 commits into
base: ue4-main
Choose a base branch
from
Draft
4 changes: 3 additions & 1 deletion Source/CesiumRuntime/CesiumRuntime.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ public CesiumRuntime(ReadOnlyTargetRules Target) : base(Target)
"CesiumGeometry",
"CesiumGeospatial",
"CesiumGltfReader",
"CesiumGltfWriter",
"CesiumGltf",
"CesiumJsonReader",
"CesiumJsonWriter",
"CesiumUtility",
"draco",
"ktx_read",
Expand Down Expand Up @@ -133,7 +135,7 @@ public CesiumRuntime(ReadOnlyTargetRules Target) : base(Target)
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"Core"
// ... add other public dependencies that you statically link with here ...
}
);
Expand Down
61 changes: 46 additions & 15 deletions Source/CesiumRuntime/Private/Cesium3DTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "CesiumGltf/Ktx2TranscodeTargets.h"
#include "CesiumGltfComponent.h"
#include "CesiumGltfPrimitiveComponent.h"
#include "CesiumGltfReader/GltfReader.h"
#include "CesiumGltfWriter/GltfWriter.h"
#include "CesiumLifetime.h"
#include "CesiumRasterOverlay.h"
#include "CesiumRuntime.h"
Expand Down Expand Up @@ -611,6 +613,8 @@ void ACesium3DTileset::NotifyHit(
// std::cout << "Hit face index 2: " << detailedHit.FaceIndex << std::endl;
}

namespace {} // namespace

class UnrealResourcePreparer
: public Cesium3DTilesSelection::IPrepareRendererResources {
public:
Expand All @@ -624,24 +628,49 @@ class UnrealResourcePreparer
#endif
{
}

virtual CesiumAsync::Future<
Cesium3DTilesSelection::TileLoadResultAndRenderResources>
virtual CesiumAsync::Future<Cesium3DTilesSelection::ClientTileLoadResult>
prepareInLoadThread(
const CesiumAsync::AsyncSystem& asyncSystem,
Cesium3DTilesSelection::TileLoadResult&& tileLoadResult,
const glm::dmat4& transform,
const std::any& rendererOptions) override {
CesiumGltf::Model* pModel =
// TODO: serialize model options into DDC so we can check cache hits
// to see if they used the same options. If the cached derived content
// used different options, we may need to invalidate or update the cache.
CreateGltfOptions::CreateModelOptions options;
options.pModel =
std::get_if<CesiumGltf::Model>(&tileLoadResult.contentKind);
if (!pModel)
return asyncSystem.createResolvedFuture(
Cesium3DTilesSelection::TileLoadResultAndRenderResources{
std::move(tileLoadResult),
nullptr});

CreateGltfOptions::CreateModelOptions options;
options.pModel = pModel;
if (!options.pModel) {
if (std::get_if<Cesium3DTilesSelection::TileCachedRenderContent>(
&tileLoadResult.contentKind)) {
// Custom serialized data was found in the cache.
const CesiumAsync::IAssetResponse* pResponse =
tileLoadResult.pCompletedRequest->response();
if (pResponse && !pResponse->clientData().empty()) {
// We have cached derived data
options.derivedDataCache = pResponse->clientData();

// Add empty model in the tile content, the gltf loader may populate
// it when deserializing the DDC.
tileLoadResult.contentKind = CesiumGltf::Model();
options.pModel =
std::get_if<CesiumGltf::Model>(&tileLoadResult.contentKind);
}
}

if (!options.pModel) {
// We have no model and no derived content, just write
// back the original response data.
return asyncSystem.createResolvedFuture(
Cesium3DTilesSelection::ClientTileLoadResult{
std::move(tileLoadResult),
nullptr,
true,
{}});
}
}

options.alwaysIncludeTangents = this->_pActor->GetAlwaysIncludeTangents();

#if PHYSICS_INTERFACE_PHYSX
Expand All @@ -651,12 +680,14 @@ class UnrealResourcePreparer
options.pEncodedMetadataDescription =
&this->_pActor->_encodedMetadataDescription;

TUniquePtr<UCesiumGltfComponent::HalfConstructed> pHalf =
UCesiumGltfComponent::CreateOffGameThread(transform, options);
UCesiumGltfComponent::HalfConstructed* pHalf =
UCesiumGltfComponent::CreateOffGameThread(transform, options).Release();
return asyncSystem.createResolvedFuture(
Cesium3DTilesSelection::TileLoadResultAndRenderResources{
Cesium3DTilesSelection::ClientTileLoadResult{
std::move(tileLoadResult),
pHalf.Release()});
pHalf,
false,
std::move(pHalf->derivedDataToCache)});
}

virtual void* prepareInMainThread(
Expand Down
Loading