6#include "Containers/Array.h"
7#include "Containers/HashTable.h"
8#include "Containers/Map.h"
9#include "Containers/Set.h"
33#ifndef USE_MMAPPED_SHADERARCHIVE
35 #define USE_MMAPPED_SHADERARCHIVE 0
39#define UE_SCA_VISUALIZE_SHADER_USAGE (!WITH_EDITOR && UE_BUILD_DEVELOPMENT && PLATFORM_DESKTOP)
42#define UE_SCA_DEBUG_PRELOADING (0)
53 return Ar << Ref.ShaderIndicesOffset << Ref.NumShaders << Ref.FirstPreloadIndex << Ref.NumPreloadEntries;
59 return Ar <<
Ref.Offset <<
Ref.Size;
63#if USE_MMAPPED_SHADERARCHIVE
80 return Ar << Ref.Offset << Ref.Size << Ref.UncompressedSize << Ref.Frequency;
83#if USE_MMAPPED_SHADERARCHIVE
91#if USE_MMAPPED_SHADERARCHIVE
197#if USE_MMAPPED_SHADERARCHIVE
222#if !USE_MMAPPED_SHADERARCHIVE
234#if !USE_MMAPPED_SHADERARCHIVE
271#if !USE_MMAPPED_SHADERARCHIVE
302#if UE_SCA_VISUALIZE_SHADER_USAGE
385 return sizeof(*this) +
507 return Ar << Ref.ShaderIndicesOffset << Ref.NumShaders;
542 return Ar << Ref.Packed;
546static_assert(
sizeof(
FIoStoreShaderCodeEntry) ==
sizeof(
uint64),
"To reduce memory footprint, shader code entries should be as small as possible");
563 return Ar << Ref.ShaderIndicesOffset << Ref.NumShaders << Ref.UncompressedSize << Ref.CompressedSize;
632 return sizeof(*this) +
659 return sizeof(*this) +
660 Header.GetAllocatedSize() +
661 PreloadedShaderGroups.GetAllocatedSize();
673 return Header.ShaderIndices[
ShaderMapEntry.ShaderIndicesOffset + i];
689 return Header.ShaderHashes[
GetShaderIndex(ShaderMapIndex, ShaderIndex)];
694 return Header.ShaderMapHashes[ShaderMapIndex];
705 return Header.ShaderEntries[ShaderIndex].ShaderGroupIndex;
715 static constexpr uint32 CurrentVersion = 1;
717 struct FShaderGroupPreloadEntry
721 uint32 FramePreloadStarted = ~0u;
723 uint32 bNeverToBePreloaded : 1;
725#if UE_SCA_DEBUG_PRELOADING
729 FShaderGroupPreloadEntry()
731 , bNeverToBePreloaded(0)
735 ~FShaderGroupPreloadEntry()
752 void SetupPreloadEntryForLoading(
int32 ShaderGroupIndex, FShaderGroupPreloadEntry&
PreloadEntry);
755 void MarkPreloadEntrySkipped(
int32 ShaderGroupIndex
762 void ReleasePreloadEntry(
int32 ShaderGroupIndex
769 inline FShaderGroupPreloadEntry* FindOrAddPreloadEntry(
int32 ShaderGroupIndex)
771 FShaderGroupPreloadEntry*& Ptr = PreloadedShaderGroups.FindOrAdd(ShaderGroupIndex);
774 Ptr =
new FShaderGroupPreloadEntry;
780 inline FShaderGroupPreloadEntry* FindExistingPreloadEntry(
int32 ShaderGroupIndex)
782 FShaderGroupPreloadEntry**
PtrPtr = PreloadedShaderGroups.Find(ShaderGroupIndex);
783 checkf(
PtrPtr !=
nullptr,
TEXT(
"The preload entry for a shader group we assumed to exist does not exist!"));
788 bool GroupOnlyContainsRaytracingShaders(
int32 ShaderGroupIndex);
800 FRWLock PreloadedShaderGroupsLock;
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
bool LoadFromCompactBinary(FCbFieldView Field, FAssetDependency &Dependency)
Definition AssetRegistry.cpp:10420
#define LIKELY(x)
Definition CityHash.cpp:107
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
FArchive & operator<<(FArchive &Ar, FEnvQueryDebugProfileData::FStep &Data)
Definition EnvQueryTypes.cpp:489
EShaderFrequency
Definition RHIDefinitions.h:202
@ SF_NumFrequencies
Definition RHIDefinitions.h:216
@ SF_NumBits
Definition RHIDefinitions.h:224
int32 GShaderCodeLibraryVisualizeShaderUsage
Definition ShaderCodeArchive.cpp:55
#define UE_SCA_DEBUG_PRELOADING
Definition ShaderCodeArchive.h:42
TSet< FName > FShaderMapAssetPaths
Definition Shader.h:88
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
Definition Archive.h:1208
Definition CompactBinary.h:610
Definition CompactBinaryWriter.h:68
Definition HashTable.h:210
void Clear()
Definition HashTable.h:350
Definition IoChunkId.h:64
Definition IoDispatcher.h:350
Definition IoDispatcher.h:237
Definition ShaderCodeArchive.h:644
virtual bool IsPreloading(int32 ShaderIndex, FGraphEventArray &OutCompletionEvents) override
Definition ShaderCodeArchive.cpp:2172
virtual int32 GetNumShadersForShaderMap(int32 ShaderMapIndex) const override
Definition ShaderCodeArchive.h:668
static RENDERCORE_API FIoChunkId GetShaderCodeChunkId(const FSHAHash &ShaderHash)
Definition ShaderCodeArchive.cpp:1498
virtual TRefCountPtr< FRHIShader > CreateShader(int32 Index, bool bRequired=true) override
Definition ShaderCodeArchive.cpp:2388
virtual bool IsNativeLibrary() const override
Definition ShaderCodeArchive.h:655
virtual int32 GetShaderIndex(int32 ShaderMapIndex, int32 i) const override
Definition ShaderCodeArchive.h:670
virtual ~FIoStoreShaderCodeArchive()
Definition ShaderCodeArchive.cpp:2009
virtual FSHAHash GetShaderMapHash(int32 ShaderMapIndex) const override
Definition ShaderCodeArchive.h:692
virtual int32 GetNumShaderMaps() const override
Definition ShaderCodeArchive.h:667
virtual void Teardown() override
Definition ShaderCodeArchive.cpp:2015
static RENDERCORE_API void CreateIoStoreShaderCodeArchiveHeader(const FName &Format, const FSerializedShaderArchive &SerializedShaders, FIoStoreShaderCodeArchiveHeader &OutHeader)
Definition ShaderCodeArchive.cpp:1508
virtual int32 FindShaderIndex(const FSHAHash &Hash) override
Definition ShaderCodeArchive.cpp:2375
virtual void AddRefPreloadedShaderGroup(int32 ShaderGroupIndex) override
Definition ShaderCodeArchive.cpp:2160
virtual FSHAHash GetShaderHash(int32 ShaderMapIndex, int32 ShaderIndex) override
Definition ShaderCodeArchive.h:687
virtual void ReleasePreloadedShaderGroup(int32 ShaderGroupIndex) override
Definition ShaderCodeArchive.cpp:2167
virtual int32 FindShaderMapIndex(const FSHAHash &Hash) override
Definition ShaderCodeArchive.cpp:2362
virtual bool PreloadShaderMap(int32 ShaderMapIndex, FGraphEventArray &OutCompletionEvents) override
Definition ShaderCodeArchive.cpp:2217
virtual int32 GetGroupIndexForShader(int32 ShaderIndex) const override
Definition ShaderCodeArchive.h:703
static RENDERCORE_API void SaveIoStoreShaderCodeArchive(const FIoStoreShaderCodeArchiveHeader &Header, FArchive &OutLibraryAr)
Definition ShaderCodeArchive.cpp:1947
static RENDERCORE_API FIoChunkId GetShaderCodeArchiveChunkId(const FString &LibraryName, FName FormatName)
Definition ShaderCodeArchive.cpp:1490
virtual uint32 GetSizeBytes() const override
Definition ShaderCodeArchive.h:657
virtual uint32 GetShaderSizeBytes(int32 ShaderIndex) const override
Definition ShaderCodeArchive.cpp:2155
virtual void ReleasePreloadedShader(int32 ShaderIndex) override
Definition ShaderCodeArchive.cpp:2353
virtual void GetAllShaderIndices(int32 ShaderMapIndex, TArray< int32 > &ShaderIndices)
Definition ShaderCodeArchive.h:676
virtual int32 GetNumShaders() const override
Definition ShaderCodeArchive.h:666
Definition NameTypes.h:617
Definition RHIShaderLibrary.h:13
FString LibraryName
Definition RHIShaderLibrary.h:50
Definition SecureHash.h:226
Definition ScopeLock.h:141
Definition ShaderCodeArchive.h:89
TArrayType< FShaderCodeEntry > ShaderEntries
Definition ShaderCodeArchive.h:109
bool IsEmpty() const
Definition ShaderCodeArchive.h:256
TArrayType< uint32 > ShaderIndices
Definition ShaderCodeArchive.h:117
int32 GetNumShaderMaps() const
Definition ShaderCodeArchive.h:246
RENDERCORE_API int32 FindShader(const FSHAHash &Hash) const
Definition ShaderCodeArchive.cpp:177
const TArrayView< const FFileCachePreloadEntry > GetPreloadEntries() const
Definition ShaderCodeArchive.h:121
TArrayType< FFileCachePreloadEntry > PreloadEntries
Definition ShaderCodeArchive.h:112
const TArrayView< const FShaderCodeEntry > GetShaderEntries() const
Definition ShaderCodeArchive.h:122
RENDERCORE_API int32 FindShaderMap(const FSHAHash &Hash) const
Definition ShaderCodeArchive.cpp:133
FHashTable ShaderHashTable
Definition ShaderCodeArchive.h:128
const TArrayView< const FSHAHash > GetShaderMapHashes() const
Definition ShaderCodeArchive.h:125
RENDERCORE_API bool FindOrAddShader(const FSHAHash &Hash, int32 &OutIndex)
Definition ShaderCodeArchive.cpp:184
FSerializedShaderArchive()
Definition ShaderCodeArchive.h:191
const TArrayView< const FSHAHash > GetShaderHashes() const
Definition ShaderCodeArchive.h:124
void Empty()
Definition ShaderCodeArchive.h:219
const TArrayView< const uint32 > GetShaderIndices() const
Definition ShaderCodeArchive.h:120
void EmptyShaderMaps()
Definition ShaderCodeArchive.h:232
TArrayType< FShaderMapEntry > ShaderMapEntries
Definition ShaderCodeArchive.h:106
RENDERCORE_API void RemoveLastAddedShader()
Definition ShaderCodeArchive.cpp:204
RENDERCORE_API void Finalize()
Definition ShaderCodeArchive.cpp:535
int64 GetAllocatedSize() const
Definition ShaderCodeArchive.h:195
const TArrayView< const FShaderMapEntry > GetShaderMapEntries() const
Definition ShaderCodeArchive.h:123
RENDERCORE_API bool FindOrAddShaderMap(const FSHAHash &Hash, int32 &OutIndex, const FShaderMapAssetPaths *AssociatedAssets)
Definition ShaderCodeArchive.cpp:140
RENDERCORE_API void DecompressShader(int32 Index, const TArray< FSharedBuffer > &ShaderCode, TArray< uint8 > &OutDecompressedShader) const
Definition ShaderCodeArchive.cpp:520
FHashTable ShaderMapHashTable
Definition ShaderCodeArchive.h:127
TArrayType< FSHAHash > ShaderHashes
Definition ShaderCodeArchive.h:103
RENDERCORE_API int32 FindShaderMapWithKey(const FSHAHash &Hash, uint32 Key) const
Definition ShaderCodeArchive.cpp:121
TArrayType< FSHAHash > ShaderMapHashes
Definition ShaderCodeArchive.h:100
RENDERCORE_API int32 FindShaderWithKey(const FSHAHash &Hash, uint32 Key) const
Definition ShaderCodeArchive.cpp:165
int32 GetNumShaders() const
Definition ShaderCodeArchive.h:251
friend FArchive & operator<<(FArchive &Ar, FSerializedShaderArchive &Ref)
Definition ShaderCodeArchive.h:292
Definition ShaderCodeArchive.h:375
virtual int32 FindShaderMapIndex(const FSHAHash &Hash) override
Definition ShaderCodeArchive.h:409
virtual int32 GetNumShaderMaps() const override
Definition ShaderCodeArchive.h:391
FShaderUsageVisualizer DebugVisualizer
Definition ShaderCodeArchive.h:480
virtual bool IsNativeLibrary() const override
Definition ShaderCodeArchive.h:381
int64 LibraryCodeOffset
Definition ShaderCodeArchive.h:468
virtual ~FShaderCodeArchive()
Definition ShaderCodeArchive.cpp:1184
FString LibraryDir
Definition ShaderCodeArchive.h:465
FSerializedShaderArchive SerializedShaders
Definition ShaderCodeArchive.h:474
bool WaitForPreload(FShaderPreloadEntry &ShaderPreloadEntry)
Definition ShaderCodeArchive.cpp:1351
virtual bool PreloadShaderMap(int32 ShaderMapIndex, FGraphEventArray &OutCompletionEvents) override
Definition ShaderCodeArchive.cpp:1293
void OnShaderPreloadFinished(int32 ShaderIndex, const IMemoryReadStreamRef &PreloadData)
Definition ShaderCodeArchive.cpp:1217
virtual int32 FindShaderIndex(const FSHAHash &Hash) override
Definition ShaderCodeArchive.h:414
TArray< FShaderPreloadEntry > ShaderPreloads
Definition ShaderCodeArchive.h:476
virtual int32 GetNumShaders() const override
Definition ShaderCodeArchive.h:390
virtual int32 GetShaderIndex(int32 ShaderMapIndex, int32 i) const override
Definition ShaderCodeArchive.h:394
FORCENOINLINE void CheckShaderCreation(void *ShaderPtr, int32 Index)
Definition ShaderCodeArchive.h:443
virtual FSHAHash GetShaderMapHash(int32 ShaderMapIndex) const override
Definition ShaderCodeArchive.h:424
virtual FSHAHash GetShaderHash(int32 ShaderMapIndex, int32 ShaderIndex) override
Definition ShaderCodeArchive.h:419
virtual uint32 GetSizeBytes() const override
Definition ShaderCodeArchive.h:383
virtual void GetAllShaderIndices(int32 ShaderMapIndex, TArray< int32 > &ShaderIndices)
Definition ShaderCodeArchive.h:400
IFileCacheHandle * FileCacheHandle
Definition ShaderCodeArchive.h:471
virtual void ReleasePreloadedShader(int32 ShaderIndex) override
Definition ShaderCodeArchive.cpp:1374
FRWLock ShaderPreloadLock
Definition ShaderCodeArchive.h:477
virtual void Teardown() override
Definition ShaderCodeArchive.cpp:1190
virtual int32 GetNumShadersForShaderMap(int32 ShaderMapIndex) const override
Definition ShaderCodeArchive.h:392
virtual TRefCountPtr< FRHIShader > CreateShader(int32 Index, bool bRequired=true) override
Definition ShaderCodeArchive.cpp:1403
Definition FileCache.h:32
Definition ArrayView.h:139
UE_REWRITE bool IsEmpty() const
Definition Array.h:1133
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
UE_FORCEINLINE_HINT SizeType AddUnique(ElementType &&Item)
Definition Array.h:2993
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition RefCounting.h:454
ECompressor
Definition OodleDataCompression.h:49
ECompressionLevel
Definition OodleDataCompression.h:94
Definition FieldSystemNoiseAlgo.cpp:6
Definition ShaderCodeArchive.h:486
RENDERCORE_API void DecompressShaderWithOodle(uint8 *OutDecompressedShader, int64 UncompressedSize, const uint8 *CompressedShaderCode, int64 CompressedSize)
Definition ShaderCodeArchive.cpp:471
RENDERCORE_API bool CompressShaderWithOodle(uint8 *OutCompressedShader, int64 &OutCompressedSize, const uint8 *InUncompressedShaderCode, int64 InUncompressedSize, FOodleDataCompression::ECompressor InOodleCompressor, FOodleDataCompression::ECompressionLevel InOodleLevel)
Definition ShaderCodeArchive.cpp:504
@ Ref
Value is copied out after function call. Only valid on function param declaration.
Definition ObjectMacros.h:1074
U16 Index
Definition radfft.cpp:71
Definition FileCache.h:17
Definition ShaderCodeArchive.h:513
uint64 Frequency
Definition ShaderCodeArchive.h:520
FIoStoreShaderCodeEntry()
Definition ShaderCodeArchive.h:530
uint64 Packed
Definition ShaderCodeArchive.h:516
EShaderFrequency GetFrequency() const
Definition ShaderCodeArchive.h:535
uint64 UncompressedOffsetInGroup
Definition ShaderCodeArchive.h:526
uint64 ShaderGroupIndex
Definition ShaderCodeArchive.h:523
friend FArchive & operator<<(FArchive &Ar, FIoStoreShaderCodeEntry &Ref)
Definition ShaderCodeArchive.h:540
Definition ShaderCodeArchive.h:550
uint32 NumShaders
Definition ShaderCodeArchive.h:554
friend FArchive & operator<<(FArchive &Ar, FIoStoreShaderGroupEntry &Ref)
Definition ShaderCodeArchive.h:561
bool IsGroupCompressed() const
Definition ShaderCodeArchive.h:567
uint32 UncompressedSize
Definition ShaderCodeArchive.h:557
uint32 ShaderIndicesOffset
Definition ShaderCodeArchive.h:552
uint32 CompressedSize
Definition ShaderCodeArchive.h:559
Definition ShaderCodeArchive.h:499
uint32 NumShaders
Definition ShaderCodeArchive.h:503
uint32 ShaderIndicesOffset
Definition ShaderCodeArchive.h:501
friend FArchive & operator<<(FArchive &Ar, FIoStoreShaderMapEntry &Ref)
Definition ShaderCodeArchive.h:505
Definition ShaderCodeArchive.h:448
FShaderPreloadEntry()
Definition ShaderCodeArchive.h:455
FGraphEventRef PreloadEvent
Definition ShaderCodeArchive.h:449
uint32 FramePreloadStarted
Definition ShaderCodeArchive.h:451
void * Code
Definition ShaderCodeArchive.h:450
uint32 bNeverToBePreloaded
Definition ShaderCodeArchive.h:453
uint32 NumRefs
Definition ShaderCodeArchive.h:452
Definition ShaderCodeArchive.h:67
EShaderFrequency GetFrequency() const
Definition ShaderCodeArchive.h:73
uint32 UncompressedSize
Definition ShaderCodeArchive.h:70
uint64 Offset
Definition ShaderCodeArchive.h:68
friend FArchive & operator<<(FArchive &Ar, FShaderCodeEntry &Ref)
Definition ShaderCodeArchive.h:78
uint32 Size
Definition ShaderCodeArchive.h:69
uint8 Frequency
Definition ShaderCodeArchive.h:71
Definition ShaderCodeArchive.h:45
uint32 FirstPreloadIndex
Definition ShaderCodeArchive.h:48
uint32 ShaderIndicesOffset
Definition ShaderCodeArchive.h:46
uint32 NumPreloadEntries
Definition ShaderCodeArchive.h:49
uint32 NumShaders
Definition ShaderCodeArchive.h:47
friend FArchive & operator<<(FArchive &Ar, FShaderMapEntry &Ref)
Definition ShaderCodeArchive.h:51
Definition ShaderCodeArchive.h:301
void MarkDecompressedForVisualization(int32 ShaderIndex)
Definition ShaderCodeArchive.h:368
void MarkCreatedForVisualization(int32 ShaderIndex)
Definition ShaderCodeArchive.h:369
void SaveShaderUsageBitmap(const FString &Name, EShaderPlatform ShaderPlatform)
Definition ShaderCodeArchive.h:370
void MarkPreloadedForVisualization(int32 ShaderIndex)
Definition ShaderCodeArchive.h:366
void Initialize(const int32 InNumShaders)
Definition ShaderCodeArchive.h:365
void MarkExplicitlyPreloadedForVisualization(int32 ShaderIndex)
Definition ShaderCodeArchive.h:367