From 9a6328278ae7a93389ad7986524ed7b0a2a64a28 Mon Sep 17 00:00:00 2001 From: Ashley Rogers Date: Tue, 8 Oct 2024 15:28:18 -0400 Subject: [PATCH] Add skeleton of a Snowdon test, properly report texture stats --- .../Private/CesiumTextureResource.cpp | 97 ++++++++++++------- .../Private/CesiumTextureResource.h | 4 +- .../Private/CesiumTextureUtility.h | 4 + .../Private/Tests/Cesium3DTileset.spec.cpp | 45 +++++++++ extern/cesium-native | 2 +- 5 files changed, 117 insertions(+), 35 deletions(-) diff --git a/Source/CesiumRuntime/Private/CesiumTextureResource.cpp b/Source/CesiumRuntime/Private/CesiumTextureResource.cpp index 7fa9cd680..ae9438bfd 100644 --- a/Source/CesiumRuntime/Private/CesiumTextureResource.cpp +++ b/Source/CesiumRuntime/Private/CesiumTextureResource.cpp @@ -31,7 +31,8 @@ class FCesiumUseExistingTextureResource : public FCesiumTextureResource { TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData); + uint32 extData, + bool isPrimary); FCesiumUseExistingTextureResource( const TSharedPtr& pExistingTexture, @@ -44,7 +45,8 @@ class FCesiumUseExistingTextureResource : public FCesiumTextureResource { TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData); + uint32 extData, + bool isPrimary); protected: virtual FTextureRHIRef InitializeTextureRHI() override; @@ -182,6 +184,7 @@ FTexture2DRHIRef createAsyncTextureAndWait( ETextureCreateFlags Flags, void** InitialMipData, uint32 NumInitialMips) { + #if ENGINE_VERSION_5_4_OR_HIGHER FGraphEventRef CompletionEvent; @@ -352,6 +355,8 @@ void FCesiumTextureResourceDeleter::operator()(FCesiumTextureResource* p) { FTexture2DRHIRef textureReference = CreateRHITexture2D_Async(imageCesium, *maybePixelFormat, sRGB); + textureReference->SetName( + FName(UTF8_TO_TCHAR(imageCesium.getUniqueAssetId().c_str()))); auto pResult = TUniquePtr< FCesiumUseExistingTextureResource, FCesiumTextureResourceDeleter>(new FCesiumUseExistingTextureResource( @@ -365,7 +370,8 @@ void FCesiumTextureResourceDeleter::operator()(FCesiumTextureResource* p) { addressY, sRGB, needsMipMaps, - 0)); + 0, + true)); // Clear the now-unnecessary copy of the pixel data. // Calling clear() isn't good enough because it @@ -421,7 +427,8 @@ FCesiumTextureResourceUniquePtr FCesiumTextureResource::CreateWrapped( addressY, sRGB, useMipMapsIfAvailable, - 0)); + 0, + false)); } /*static*/ void FCesiumTextureResource::Destroy(FCesiumTextureResource* p) { @@ -445,7 +452,8 @@ FCesiumTextureResource::FCesiumTextureResource( TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData) + uint32 extData, + bool isPrimary) : _textureGroup(textureGroup), _width(width), _height(height), @@ -454,7 +462,8 @@ FCesiumTextureResource::FCesiumTextureResource( _addressX(convertAddressMode(addressX)), _addressY(convertAddressMode(addressY)), _useMipsIfAvailable(useMipsIfAvailable), - _platformExtData(extData) { + _platformExtData(extData), + _isPrimary(isPrimary) { this->bGreyScaleFormat = (_format == PF_G8) || (_format == PF_BC4); this->bSRGB = sRGB; STAT(this->_lodGroupStatName = TextureGroupStatFNames[this->_textureGroup]); @@ -500,32 +509,42 @@ void FCesiumTextureResource::InitRHI() { RHIUpdateTextureReference(TextureReferenceRHI, this->TextureRHI); #if STATS - ETextureCreateFlags textureFlags = TexCreate_ShaderResource; - if (this->bSRGB) { - textureFlags |= TexCreate_SRGB; - } - - const FIntPoint MipExtents = - CalcMipMapExtent(this->_width, this->_height, this->_format, 0); - uint32 alignment; - this->_textureSize = RHICalcTexture2DPlatformSize( - MipExtents.X, - MipExtents.Y, - this->_format, - this->GetCurrentMipCount(), - 1, - textureFlags, - FRHIResourceCreateInfo(this->_platformExtData), - alignment); + if (this->_isPrimary) { + ETextureCreateFlags textureFlags = TexCreate_ShaderResource; + if (this->bSRGB) { + textureFlags |= TexCreate_SRGB; + } - INC_DWORD_STAT_BY(STAT_TextureMemory, this->_textureSize); - INC_DWORD_STAT_FNAME_BY(this->_lodGroupStatName, this->_textureSize); + const FIntPoint MipExtents = + CalcMipMapExtent(this->_width, this->_height, this->_format, 0); + const FRHIResourceCreateInfo CreateInfo(this->_platformExtData); + + FDynamicRHI::FRHICalcTextureSizeResult result = RHICalcTexturePlatformSize( + FRHITextureDesc::Create2D( + MipExtents, + this->_format, + CreateInfo.ClearValueBinding, + textureFlags, + this->GetCurrentMipCount(), + 1, + CreateInfo.ExtData), + 0); + + this->_textureSize = result.Size; + + INC_DWORD_STAT_BY(STAT_TextureMemory, this->_textureSize); + INC_DWORD_STAT_FNAME_BY(this->_lodGroupStatName, this->_textureSize); + } #endif } void FCesiumTextureResource::ReleaseRHI() { - DEC_DWORD_STAT_BY(STAT_TextureMemory, this->_textureSize); - DEC_DWORD_STAT_FNAME_BY(this->_lodGroupStatName, this->_textureSize); +#if STATS + if (this->_isPrimary) { + DEC_DWORD_STAT_BY(STAT_TextureMemory, this->_textureSize); + DEC_DWORD_STAT_FNAME_BY(this->_lodGroupStatName, this->_textureSize); + } +#endif RHIUpdateTextureReference(TextureReferenceRHI, nullptr); @@ -570,7 +589,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource( TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData) + uint32 extData, + bool isPrimary) : FCesiumTextureResource( textureGroup, width, @@ -581,7 +601,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource( addressY, sRGB, useMipsIfAvailable, - extData), + extData, + isPrimary), _pExistingTexture(nullptr) { this->TextureRHI = std::move(existingTexture); } @@ -597,7 +618,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource( TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData) + uint32 extData, + bool isPrimary) : FCesiumTextureResource( textureGroup, width, @@ -608,7 +630,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource( addressY, sRGB, useMipsIfAvailable, - extData), + extData, + isPrimary), _pExistingTexture(pExistingTexture) {} FTextureRHIRef FCesiumUseExistingTextureResource::InitializeTextureRHI() { @@ -641,11 +664,19 @@ FCesiumCreateNewTextureResource::FCesiumCreateNewTextureResource( addressY, sRGB, useMipsIfAvailable, - extData), + extData, + true), _image(std::move(image)) {} FTextureRHIRef FCesiumCreateNewTextureResource::InitializeTextureRHI() { - FRHIResourceCreateInfo createInfo{TEXT("CesiumTextureUtility")}; + // Use the asset ID as the name of the texture so it will be visible in the + // Render Resource Viewer. + FString debugName = TEXT("CesiumTextureUtility"); + if (!this->_image.getUniqueAssetId().empty()) { + debugName = UTF8_TO_TCHAR(this->_image.getUniqueAssetId().c_str()); + } + + FRHIResourceCreateInfo createInfo{*debugName}; createInfo.BulkData = nullptr; createInfo.ExtData = _platformExtData; diff --git a/Source/CesiumRuntime/Private/CesiumTextureResource.h b/Source/CesiumRuntime/Private/CesiumTextureResource.h index cba96bc6b..4cbd8521a 100644 --- a/Source/CesiumRuntime/Private/CesiumTextureResource.h +++ b/Source/CesiumRuntime/Private/CesiumTextureResource.h @@ -90,7 +90,8 @@ class FCesiumTextureResource : public FTextureResource { TextureAddress addressY, bool sRGB, bool useMipsIfAvailable, - uint32 extData); + uint32 extData, + bool isPrimary); uint32 GetSizeX() const override { return this->_width; } uint32 GetSizeY() const override { return this->_height; } @@ -120,4 +121,5 @@ class FCesiumTextureResource : public FTextureResource { uint32 _platformExtData; FName _lodGroupStatName; uint64 _textureSize; + bool _isPrimary; }; diff --git a/Source/CesiumRuntime/Private/CesiumTextureUtility.h b/Source/CesiumRuntime/Private/CesiumTextureUtility.h index 97ee85e43..401643d41 100644 --- a/Source/CesiumRuntime/Private/CesiumTextureUtility.h +++ b/Source/CesiumRuntime/Private/CesiumTextureUtility.h @@ -4,6 +4,7 @@ #include "CesiumGltf/Model.h" #include "CesiumGltf/SharedAssetDepot.h" +#include "CesiumGltf/Texture.h" #include "CesiumMetadataValueType.h" #include "CesiumTextureResource.h" #include "Engine/Texture.h" @@ -201,4 +202,7 @@ std::optional getPixelFormatForImageCesium( const CesiumGltf::ImageCesium& imageCesium, const std::optional overridePixelFormat); +std::optional +getUnrealTextureFromGltfTexture(const CesiumGltf::Texture& texture); + } // namespace CesiumTextureUtility diff --git a/Source/CesiumRuntime/Private/Tests/Cesium3DTileset.spec.cpp b/Source/CesiumRuntime/Private/Tests/Cesium3DTileset.spec.cpp index bfa843276..23de71875 100644 --- a/Source/CesiumRuntime/Private/Tests/Cesium3DTileset.spec.cpp +++ b/Source/CesiumRuntime/Private/Tests/Cesium3DTileset.spec.cpp @@ -4,6 +4,7 @@ #include "Cesium3DTileset.h" #include "CesiumGlobeAnchorComponent.h" +#include "CesiumGltfComponent.h" #include "CesiumLoadTestCore.h" #include "CesiumSceneGeneration.h" #include "CesiumSunSky.h" @@ -88,6 +89,50 @@ bool FCesium3DTilesetSharedImages::RunTest(const FString& Parameters) { TEST_SCREEN_HEIGHT); } +IMPLEMENT_SIMPLE_AUTOMATION_TEST( + FCesium3DTilesetSnowdonBenchmark, + "Cesium.Performance.3DTileset.SnowdonBenchmark", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::PerfFilter); + +static void setupForSnowdon(SceneGenerationContext& context) { + context.setCommonProperties( + FVector(-79.8867314431, 40.0223377722, 197.1008007424), + FVector(-293.823058, 6736.144397, 2730.501500), + FRotator(-13.400000, -87.799997, 0.000000), + 60.0f); + + context.sunSky->TimeZone = 5.0f; + context.sunSky->UpdateSun(); + + ACesium3DTileset* tileset = context.world->SpawnActor(); + tileset->SetTilesetSource(ETilesetSource::FromCesiumIon); + tileset->SetIonAssetID(2758251); + + tileset->SetActorLabel(TEXT("Snowdon")); + tileset->SuspendUpdate = false; + tileset->LogSelectionStats = true; + context.tilesets.push_back(tileset); + + ADirectionalLight* Light = context.world->SpawnActor(); + Light->SetActorRotation(FQuat::MakeFromEuler(FVector(0, 0, 270))); +} + +void snowdonPass( + SceneGenerationContext& context, + TestPass::TestingParameter parameter) {} + +bool FCesium3DTilesetSnowdonBenchmark::RunTest(const FString& Parameters) { + std::vector testPasses; + testPasses.push_back(TestPass{"Refresh Pass", snowdonPass, nullptr}); + + return RunLoadTest( + GetBeautifiedTestName(), + setupForSnowdon, + testPasses, + TEST_SCREEN_WIDTH, + TEST_SCREEN_HEIGHT); +} + } // namespace Cesium #endif diff --git a/extern/cesium-native b/extern/cesium-native index aa81cbd67..924b000e0 160000 --- a/extern/cesium-native +++ b/extern/cesium-native @@ -1 +1 @@ -Subproject commit aa81cbd679db78d166fd2bb0fbfab91f61ed683d +Subproject commit 924b000e00fc9054d60591bae494c71532a8ec61