2021-05-03 14:05:29 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "CoreMinimal.h"
|
|
|
|
|
#include "Interfaces/ITextureFormatManagerModule.h"
|
|
|
|
|
#include "Interfaces/ITextureFormat.h"
|
|
|
|
|
#include "Interfaces/ITextureFormatModule.h"
|
|
|
|
|
#include "Modules/ModuleManager.h"
|
|
|
|
|
#include "TextureFormatManager.h"
|
|
|
|
|
|
|
|
|
|
DEFINE_LOG_CATEGORY_STATIC(LogTextureFormatManager, Log, All);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module for the target platform manager
|
|
|
|
|
*/
|
|
|
|
|
class FTextureFormatManagerModule
|
|
|
|
|
: public ITextureFormatManagerModule
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2022-04-08 16:06:54 -04:00
|
|
|
enum class EInitPhase
|
|
|
|
|
{
|
|
|
|
|
JustConstructedNotInit = 0,
|
|
|
|
|
Invalidated = 1,
|
|
|
|
|
GetTextureFormatsInProgressDontTouch = 2,
|
|
|
|
|
GetTextureFormatsPartialOkayToRead = 3, // values >= here are okay to make queries
|
|
|
|
|
GetTextureFormatsDone = 4
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-03 14:05:29 -04:00
|
|
|
/** Default constructor. */
|
|
|
|
|
FTextureFormatManagerModule()
|
|
|
|
|
: ModuleName(TEXT("TextureFormat"))
|
|
|
|
|
, bForceCacheUpdate(true)
|
2022-04-08 16:06:54 -04:00
|
|
|
, bModuleChangeCallbackEnabled(false)
|
|
|
|
|
, TextureFormatsInitPhase(EInitPhase::JustConstructedNotInit)
|
2021-05-03 14:05:29 -04:00
|
|
|
{
|
|
|
|
|
FModuleManager::Get().OnModulesChanged().AddRaw(this, &FTextureFormatManagerModule::ModulesChangesCallback);
|
2022-04-08 16:06:54 -04:00
|
|
|
|
|
|
|
|
// not usable until first Invalidate is called
|
2021-05-03 14:05:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Destructor. */
|
2021-05-13 16:15:45 -04:00
|
|
|
virtual ~FTextureFormatManagerModule() = default;
|
|
|
|
|
|
|
|
|
|
virtual void ShutdownModule()
|
2021-05-03 14:05:29 -04:00
|
|
|
{
|
|
|
|
|
FModuleManager::Get().OnModulesChanged().RemoveAll(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual const TArray<const ITextureFormat*>& GetTextureFormats() override
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
// should not be called recursively while I am building the list :
|
|
|
|
|
check( TextureFormatsInitPhase!= EInitPhase::GetTextureFormatsInProgressDontTouch );
|
2021-05-03 14:05:29 -04:00
|
|
|
|
2022-04-08 16:06:54 -04:00
|
|
|
// bForceCacheUpdate should be true on first call, so we don't need a separate static init flag
|
|
|
|
|
if ( bForceCacheUpdate )
|
2021-05-03 14:05:29 -04:00
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
// turn off flag immediately so that repeated calls to GetTextureFormats will not come in here again
|
|
|
|
|
bForceCacheUpdate = false;
|
|
|
|
|
bModuleChangeCallbackEnabled = false; // don't re-call me from my own module loads
|
|
|
|
|
TextureFormatsInitPhase = EInitPhase::GetTextureFormatsInProgressDontTouch;
|
|
|
|
|
|
|
|
|
|
// note the first time this is done is from FTargetPlatformManagerModule::FTargetPlatformManagerModule()
|
|
|
|
|
// so calls to it are dangerous
|
|
|
|
|
|
2021-05-13 16:15:45 -04:00
|
|
|
TextureFormats.Empty(TextureFormats.Num());
|
|
|
|
|
TextureFormatMetadata.Empty(TextureFormatMetadata.Num());
|
2021-05-03 14:05:29 -04:00
|
|
|
|
|
|
|
|
TArray<FName> Modules;
|
|
|
|
|
|
|
|
|
|
FModuleManager::Get().FindModules(TEXT("*TextureFormat*"), Modules);
|
|
|
|
|
|
|
|
|
|
if (!Modules.Num())
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogTextureFormatManager, Error, TEXT("No texture formats found!"));
|
2022-04-08 16:06:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TArray<FTextureFormatMetadata> BaseModules;
|
|
|
|
|
TArray<FTextureFormatMetadata> ChildModules;
|
2021-05-03 14:05:29 -04:00
|
|
|
|
|
|
|
|
for (int32 Index = 0; Index < Modules.Num(); Index++)
|
|
|
|
|
{
|
|
|
|
|
if (Modules[Index] != ModuleName) // Avoid our own module when going through this list that was gathered by name
|
|
|
|
|
{
|
|
|
|
|
ITextureFormatModule* Module = FModuleManager::LoadModulePtr<ITextureFormatModule>(Modules[Index]);
|
|
|
|
|
if (Module)
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
FTextureFormatMetadata ModuleMeta;
|
|
|
|
|
ModuleMeta.Module = Module;
|
|
|
|
|
ModuleMeta.ModuleName = Modules[Index];
|
|
|
|
|
if ( Module->CanCallGetTextureFormats() )
|
2021-05-03 14:05:29 -04:00
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
ChildModules.Add(ModuleMeta);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BaseModules.Add(ModuleMeta);
|
2021-05-03 14:05:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-08 16:06:54 -04:00
|
|
|
|
|
|
|
|
// first populate TextureFormats[] with all Base Modules
|
|
|
|
|
//
|
|
|
|
|
for (int32 Index = 0; Index < BaseModules.Num(); Index++)
|
|
|
|
|
{
|
|
|
|
|
ITextureFormatModule* Module = BaseModules[Index].Module;
|
|
|
|
|
|
|
|
|
|
ITextureFormat* Format = Module->GetTextureFormat();
|
|
|
|
|
if (Format != nullptr)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogTextureFormatManager, Display, TEXT("Loaded Base TextureFormat: %s"),*BaseModules[Index].ModuleName.ToString());
|
|
|
|
|
|
|
|
|
|
TextureFormats.Add(Format);
|
|
|
|
|
TextureFormatMetadata.Add(BaseModules[Index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init phase 3 means you are now allowd to call GetTextureFormats() and you will get only the Base formats
|
|
|
|
|
TextureFormatsInitPhase = EInitPhase::GetTextureFormatsPartialOkayToRead;
|
|
|
|
|
|
|
|
|
|
// run through the Child formats and call GetTextureFormat() on them
|
|
|
|
|
// this will call back to me and do GetTextureFormats() which will get only the base formats
|
|
|
|
|
for (int32 Index = 0; Index < ChildModules.Num(); Index++)
|
|
|
|
|
{
|
|
|
|
|
ITextureFormatModule* Module = ChildModules[Index].Module;
|
|
|
|
|
|
|
|
|
|
ITextureFormat* Format = Module->GetTextureFormat();
|
|
|
|
|
if (Format != nullptr)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogTextureFormatManager, Display, TEXT("Loaded Child TextureFormat: %s"),*ChildModules[Index].ModuleName.ToString());
|
|
|
|
|
|
|
|
|
|
// do not add me to TextureFormats yet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// back up phase to 2, no calls to GetTextureFormats() allowed now
|
|
|
|
|
TextureFormatsInitPhase = EInitPhase::GetTextureFormatsInProgressDontTouch;
|
|
|
|
|
|
|
|
|
|
for (int32 Index = 0; Index < ChildModules.Num(); Index++)
|
|
|
|
|
{
|
|
|
|
|
ITextureFormatModule* Module = ChildModules[Index].Module;
|
|
|
|
|
|
|
|
|
|
// GetTextureFormat was already done so this should just return a stored pointer, no more init
|
|
|
|
|
ITextureFormat* Format = Module->GetTextureFormat();
|
|
|
|
|
if (Format != nullptr)
|
|
|
|
|
{
|
|
|
|
|
// now add to the list :
|
|
|
|
|
TextureFormats.Add(Format);
|
|
|
|
|
TextureFormatMetadata.Add(ChildModules[Index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// all done :
|
|
|
|
|
TextureFormatsInitPhase = EInitPhase::GetTextureFormatsDone;
|
|
|
|
|
bModuleChangeCallbackEnabled = true;
|
2021-05-03 14:05:29 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 16:06:54 -04:00
|
|
|
check( (int)TextureFormatsInitPhase >= (int)EInitPhase::GetTextureFormatsPartialOkayToRead );
|
|
|
|
|
|
2021-05-13 16:15:45 -04:00
|
|
|
return TextureFormats;
|
2021-05-03 14:05:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual const ITextureFormat* FindTextureFormat(FName Name) override
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
check( (int)TextureFormatsInitPhase >= (int)EInitPhase::GetTextureFormatsPartialOkayToRead );
|
|
|
|
|
|
2021-05-13 16:15:45 -04:00
|
|
|
FName ModuleNameUnused;
|
|
|
|
|
ITextureFormatModule* ModuleUnused;
|
|
|
|
|
return FindTextureFormatAndModule(Name, ModuleNameUnused, ModuleUnused);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual const class ITextureFormat* FindTextureFormatAndModule(FName Name, FName& OutModuleName, ITextureFormatModule*& OutModule) override
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
check( (int)TextureFormatsInitPhase >= (int)EInitPhase::GetTextureFormatsPartialOkayToRead );
|
|
|
|
|
|
2021-05-13 16:15:45 -04:00
|
|
|
// Called to ensure the arrays are populated
|
2022-04-08 16:06:54 -04:00
|
|
|
// dangerous and not necessary, removed :
|
|
|
|
|
check( ! bForceCacheUpdate );
|
|
|
|
|
//GetTextureFormats();
|
2021-05-03 14:05:29 -04:00
|
|
|
|
|
|
|
|
for (int32 Index = 0; Index < TextureFormats.Num(); Index++)
|
|
|
|
|
{
|
|
|
|
|
TArray<FName> Formats;
|
|
|
|
|
|
|
|
|
|
TextureFormats[Index]->GetSupportedFormats(Formats);
|
|
|
|
|
|
|
|
|
|
for (int32 FormatIndex = 0; FormatIndex < Formats.Num(); FormatIndex++)
|
|
|
|
|
{
|
|
|
|
|
if (Formats[FormatIndex] == Name)
|
|
|
|
|
{
|
2021-05-13 16:15:45 -04:00
|
|
|
const FTextureFormatMetadata& FoundMeta = TextureFormatMetadata[Index];
|
|
|
|
|
OutModuleName = FoundMeta.ModuleName;
|
|
|
|
|
OutModule = FoundMeta.Module;
|
2021-05-03 14:05:29 -04:00
|
|
|
return TextureFormats[Index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Invalidate() override
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
// this is called right after the constructor
|
|
|
|
|
// we are not usable until the first Invalidate is called
|
|
|
|
|
TextureFormatsInitPhase = EInitPhase::Invalidated;
|
2021-05-03 14:05:29 -04:00
|
|
|
bForceCacheUpdate = true;
|
|
|
|
|
GetTextureFormats();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
void ModulesChangesCallback(FName InModuleName, EModuleChangeReason ReasonForChange)
|
|
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
if (bModuleChangeCallbackEnabled && (InModuleName != ModuleName) && InModuleName.ToString().Contains(TEXT("TextureFormat")))
|
2021-05-03 14:05:29 -04:00
|
|
|
{
|
2022-04-08 16:06:54 -04:00
|
|
|
// when a "TextureFormat" module is loaded, rebuild my list
|
2021-05-03 14:05:29 -04:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FName ModuleName;
|
|
|
|
|
|
2021-05-13 16:15:45 -04:00
|
|
|
TArray<const ITextureFormat*> TextureFormats;
|
|
|
|
|
|
|
|
|
|
struct FTextureFormatMetadata
|
|
|
|
|
{
|
|
|
|
|
FName ModuleName;
|
|
|
|
|
ITextureFormatModule* Module;
|
|
|
|
|
};
|
|
|
|
|
TArray<FTextureFormatMetadata> TextureFormatMetadata;
|
|
|
|
|
|
2021-05-03 14:05:29 -04:00
|
|
|
// Flag to force reinitialization of all cached data. This is needed to have up-to-date caches
|
|
|
|
|
// in case of a module reload of a TextureFormat-Module.
|
|
|
|
|
bool bForceCacheUpdate;
|
|
|
|
|
|
|
|
|
|
// Flag to avoid redunant reloads
|
2022-04-08 16:06:54 -04:00
|
|
|
bool bModuleChangeCallbackEnabled;
|
|
|
|
|
|
|
|
|
|
// Track tricky initialization progress
|
|
|
|
|
EInitPhase TextureFormatsInitPhase;
|
|
|
|
|
|
|
|
|
|
// @@!! Mutex and ScopeLock everything in case this is called from multiple threads ?
|
2021-05-03 14:05:29 -04:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_MODULE(FTextureFormatManagerModule, TextureFormat);
|