Skip to content

Commit

Permalink
Add skeleton of a Snowdon test, properly report texture stats
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Oct 8, 2024
1 parent 90ea7e4 commit 9a63282
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 35 deletions.
97 changes: 64 additions & 33 deletions Source/CesiumRuntime/Private/CesiumTextureResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class FCesiumUseExistingTextureResource : public FCesiumTextureResource {
TextureAddress addressY,
bool sRGB,
bool useMipsIfAvailable,
uint32 extData);
uint32 extData,
bool isPrimary);

FCesiumUseExistingTextureResource(
const TSharedPtr<FTextureResource>& pExistingTexture,
Expand All @@ -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;
Expand Down Expand Up @@ -182,6 +184,7 @@ FTexture2DRHIRef createAsyncTextureAndWait(
ETextureCreateFlags Flags,
void** InitialMipData,
uint32 NumInitialMips) {

#if ENGINE_VERSION_5_4_OR_HIGHER
FGraphEventRef CompletionEvent;

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -421,7 +427,8 @@ FCesiumTextureResourceUniquePtr FCesiumTextureResource::CreateWrapped(
addressY,
sRGB,
useMipMapsIfAvailable,
0));
0,
false));
}

/*static*/ void FCesiumTextureResource::Destroy(FCesiumTextureResource* p) {
Expand All @@ -445,7 +452,8 @@ FCesiumTextureResource::FCesiumTextureResource(
TextureAddress addressY,
bool sRGB,
bool useMipsIfAvailable,
uint32 extData)
uint32 extData,
bool isPrimary)
: _textureGroup(textureGroup),
_width(width),
_height(height),
Expand All @@ -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]);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -570,7 +589,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource(
TextureAddress addressY,
bool sRGB,
bool useMipsIfAvailable,
uint32 extData)
uint32 extData,
bool isPrimary)
: FCesiumTextureResource(
textureGroup,
width,
Expand All @@ -581,7 +601,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource(
addressY,
sRGB,
useMipsIfAvailable,
extData),
extData,
isPrimary),
_pExistingTexture(nullptr) {
this->TextureRHI = std::move(existingTexture);
}
Expand All @@ -597,7 +618,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource(
TextureAddress addressY,
bool sRGB,
bool useMipsIfAvailable,
uint32 extData)
uint32 extData,
bool isPrimary)
: FCesiumTextureResource(
textureGroup,
width,
Expand All @@ -608,7 +630,8 @@ FCesiumUseExistingTextureResource::FCesiumUseExistingTextureResource(
addressY,
sRGB,
useMipsIfAvailable,
extData),
extData,
isPrimary),
_pExistingTexture(pExistingTexture) {}

FTextureRHIRef FCesiumUseExistingTextureResource::InitializeTextureRHI() {
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion Source/CesiumRuntime/Private/CesiumTextureResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -120,4 +121,5 @@ class FCesiumTextureResource : public FTextureResource {
uint32 _platformExtData;
FName _lodGroupStatName;
uint64 _textureSize;
bool _isPrimary;
};
4 changes: 4 additions & 0 deletions Source/CesiumRuntime/Private/CesiumTextureUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -201,4 +202,7 @@ std::optional<EPixelFormat> getPixelFormatForImageCesium(
const CesiumGltf::ImageCesium& imageCesium,
const std::optional<EPixelFormat> overridePixelFormat);

std::optional<ReferenceCountedUnrealTexture>
getUnrealTextureFromGltfTexture(const CesiumGltf::Texture& texture);

} // namespace CesiumTextureUtility
45 changes: 45 additions & 0 deletions Source/CesiumRuntime/Private/Tests/Cesium3DTileset.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Cesium3DTileset.h"
#include "CesiumGlobeAnchorComponent.h"
#include "CesiumGltfComponent.h"
#include "CesiumLoadTestCore.h"
#include "CesiumSceneGeneration.h"
#include "CesiumSunSky.h"
Expand Down Expand Up @@ -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<ACesium3DTileset>();
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<ADirectionalLight>();
Light->SetActorRotation(FQuat::MakeFromEuler(FVector(0, 0, 270)));
}

void snowdonPass(
SceneGenerationContext& context,
TestPass::TestingParameter parameter) {}

bool FCesium3DTilesetSnowdonBenchmark::RunTest(const FString& Parameters) {
std::vector<TestPass> testPasses;
testPasses.push_back(TestPass{"Refresh Pass", snowdonPass, nullptr});

return RunLoadTest(
GetBeautifiedTestName(),
setupForSnowdon,
testPasses,
TEST_SCREEN_WIDTH,
TEST_SCREEN_HEIGHT);
}

} // namespace Cesium

#endif

0 comments on commit 9a63282

Please sign in to comment.