UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ShaderSerialization.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10#include "UObject/NameTypes.h"
11
13class FMemoryWriter64;
15
16#if WITH_EDITOR
17namespace UE::DerivedData
18{
19 class FCacheRecord;
20 struct FCacheKey;
21}
22#endif
23
24/* Context object used for storing state and serialization parameterization related to shader/shadermap serialization. */
26{
27 /* Default constructor, if called it's expected the archive pointer will be set by derived implementations. */
29
30 /* Constructor which accepts an FArchive reference, used for simple serialization cases */
32
33 virtual ~FShaderSerializeContext() = default;
34
35 /* If this is overridden to return true, SerializeCode will be called to serialize shader code separately from the rest of the serialized object. */
36 virtual bool EnableCustomCodeSerialize() { return false; }
37
38 /* Optional function which must be implemented if EnableCustomCodeSerialize returns true; use to serialize shader code separately from the main object */
39 virtual void SerializeCode(FShaderCodeResource& Resource, int32 Index) {};
40
41 /* Optional function that can be used to reserve space for the given number of code objects by derived classes. */
42 virtual void ReserveCode(int32 Count) {};
43
44 /* Archive pointer which should be used for serializing the object, possibly excluding shader code if the EnableCustomCodeSerialize returns true */
45 FArchive* Ar = nullptr;
46
47 /* Flag indicating whether this serialization is a cooked load; used to change serialization behaviour for cooked data vs cached data */
48 bool bLoadingCooked = false;
49
50 /* FName of the asset which triggered the serialization, this is used only for diagnostic messages */
52
53 /* Helper function to retrieve an FArchive reference; exists for convenience and validation purposes. */
55 {
56 check(Ar);
57 return *Ar;
58 }
59};
60
61
62/* FShaderSerializeContext implementation used for saving to or loading from caches (either DDC or in-memory job cache)
63 * Note that this is just a base class of the below and should not be used directly.
64 */
66{
68 {
71
72 checkf(ShaderCode.Num() == ShaderSymbols.Num(), TEXT("It is required to serialize a (possibly empty, but non-null) symbols buffer for every code buffer."));
73 }
74
75 virtual ~FShaderCacheSerializeContext() = default;
76
77 /* Buffer which stores the main object data for a cache entry - i.e. a shadermap or job structure */
79
80 /* View on array of buffers which store the bytecode objects for the object, one per shader/stage.
81 * Note that this may or may not point to the OwnedShaderCode array below, depending on usage.
82 */
84
85 /* View on array of compressed buffers which store the symbols for the object, one per shader/stage.
86 * Note that this may or may not point to the OwnedShaderSymbols array below, depending on usage.
87 */
89
90 /* Array of code buffers actually owned by this context object; it is valid for this to be empty in cases
91 * where the array of ShaderCode buffers is stored externally.
92 */
94
95 /* Array of symbol buffers actually owned by this context object; it is valid for this to be empty in cases
96 * where the array of ShaderSymbols buffers is stored externally.
97 */
99
100 /* Subclasses of this type must implement custom code serialization function */
101 virtual bool EnableCustomCodeSerialize() override { return true; }
102
103 /* Get the total serialized size of data for this context; note that this will return 0 if called prior to the FSharedBuffers
104 * being set (this is done in the derived implementations, see below).
105 */
107 {
108 int64 Size = 0u;
110 {
112
114 {
115 Size += CodeBuf.GetSize();
116 }
117 }
118 return Size;
119 }
120
121 /* Populates the given code array (transfering ownership) and resets the internal view to point to the new owning array's data */
123 {
128
129 checkf(ShaderCode.Num() == ShaderSymbols.Num(), TEXT("It is required to serialize a (possibly empty, but non-null) symbols buffer for every code buffer."));
130 }
131
132 /* Returns true if there is valid serialized data referenced by this context. */
133 bool HasData() const { return ShaderObjectData && !ShaderCode.IsEmpty(); }
134};
135
136/* Implementation of FShaderCacheSerializeContext used for saving data to caches. */
138{
139 /* Default constructor; sets up base class FArchive pointing to the owned memory writer. */
141
142 virtual ~FShaderCacheSaveContext() = default;
143
144 /* Converts the raw serialized object data into the ShaderObjectData FSharedBuffer.
145 * Note that this is called by BuildCacheRecord as well, calls subsequent to the first will have no effect.
146 */
148
149 /* Overridden code serialize implementation. */
150 RENDERCORE_API virtual void SerializeCode(FShaderCodeResource& Resource, int32 Index) override;
151
152 /* Overridden code reserve implementation. */
153 RENDERCORE_API virtual void ReserveCode(int32 Count) override;
154
155#if WITH_EDITOR
156 /* Helper function to generate a DDC record from the data serialized using this context. Serialization must
157 * be executed prior to calling this function.
158 */
159 RENDERCORE_API UE::DerivedData::FCacheRecord BuildCacheRecord(const UE::DerivedData::FCacheKey& Key);
160#endif
161
162 /* Call to reset internal state, this allows re-using internal allocations to store other objects (as an optimization). */
163 RENDERCORE_API void Reset();
164
165 /* Data that will be written to when serializing */
167
168 /* FArchive that is passed to the base class, which writes to ShaderObjectRawData */
170};
171
172/* Implementation of FShaderCacheSerializeContext used for loading data from caches. */
174{
175 /* Default constructor, use when array of code buffers will be allocated via ReadFromRecord */
177
178 /* Constructor which references buffers (and an array of code buffers) owned elsewhere, does not allocate the OwnedShaderCode array */
180
181 virtual ~FShaderCacheLoadContext() = default;
182
183 /* Resets internal state to point to the given buffers and recreates the owned memoryreader */
185
186 /* Call to reset reader to start position so the same load context can be used to populate multiple objects. */
187 RENDERCORE_API void Reuse();
188
189 /* Overridden code serialize implementation. */
190 RENDERCORE_API virtual void SerializeCode(FShaderCodeResource& Resource, int32 Index) override;
191
192#if WITH_EDITOR
193 /* Helper function to populate the internal state (FSharedBuffers defined on FShaderCacheSerializeContext) from a DDC record. */
194 RENDERCORE_API void ReadFromRecord(const UE::DerivedData::FCacheRecord& CacheRecord, bool bIsPersistent = false);
195#endif
196
197 /* FArchive passed to the base class; this will be used to read data from the FShaderCacheSerializeContext::ShaderObjectData shared buffer */
199};
200
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32 Size
Definition VulkanMemory.cpp:4034
Definition Archive.h:1208
Definition CompositeBuffer.h:27
Definition MemoryReader.h:75
Definition MemoryWriter.h:109
Definition NameTypes.h:617
Definition ShaderCore.h:1134
Definition SharedBuffer.h:341
uint64 GetSize() const
Definition SharedBuffer.h:388
Definition ArrayView.h:139
UE_FORCEINLINE_HINT constexpr SizeType Num() const
Definition ArrayView.h:380
constexpr bool IsEmpty() const
Definition ArrayView.h:370
Definition Array.h:670
Definition UniquePtr.h:107
Definition DerivedData.cpp:40
U16 Index
Definition radfft.cpp:71
Definition ShaderSerialization.h:174
virtual ~FShaderCacheLoadContext()=default
RENDERCORE_API void Reuse()
Definition ShaderSerialization.cpp:127
FShaderCacheLoadContext()=default
virtual RENDERCORE_API void SerializeCode(FShaderCodeResource &Resource, int32 Index) override
Definition ShaderSerialization.cpp:122
TUniquePtr< FMemoryReaderView > Reader
Definition ShaderSerialization.h:198
Definition ShaderSerialization.h:138
RENDERCORE_API FShaderCacheSaveContext()
Definition ShaderSerialization.cpp:13
TUniquePtr< FMemoryWriter64 > Writer
Definition ShaderSerialization.h:169
TArray64< uint8 > ShaderObjectRawData
Definition ShaderSerialization.h:166
virtual ~FShaderCacheSaveContext()=default
RENDERCORE_API void Reset()
Definition ShaderSerialization.cpp:45
virtual RENDERCORE_API void SerializeCode(FShaderCodeResource &Resource, int32 Index) override
Definition ShaderSerialization.cpp:26
RENDERCORE_API void Finalize()
Definition ShaderSerialization.cpp:54
virtual RENDERCORE_API void ReserveCode(int32 Count) override
Definition ShaderSerialization.cpp:39
Definition ShaderSerialization.h:66
bool HasData() const
Definition ShaderSerialization.h:133
virtual ~FShaderCacheSerializeContext()=default
TArray< FCompressedBuffer > OwnedShaderSymbols
Definition ShaderSerialization.h:98
virtual bool EnableCustomCodeSerialize() override
Definition ShaderSerialization.h:101
FShaderCacheSerializeContext()
Definition ShaderSerialization.h:67
TArray< FCompositeBuffer > OwnedShaderCode
Definition ShaderSerialization.h:93
TArrayView< FCompositeBuffer > ShaderCode
Definition ShaderSerialization.h:83
TArrayView< FCompressedBuffer > ShaderSymbols
Definition ShaderSerialization.h:88
void MoveCode(TArray< FCompositeBuffer > &TargetCode, TArray< FCompressedBuffer > &TargetSymbols)
Definition ShaderSerialization.h:122
int64 GetSerializedSize() const
Definition ShaderSerialization.h:106
FSharedBuffer ShaderObjectData
Definition ShaderSerialization.h:78
Definition ShaderSerialization.h:26
virtual void ReserveCode(int32 Count)
Definition ShaderSerialization.h:42
virtual bool EnableCustomCodeSerialize()
Definition ShaderSerialization.h:36
bool bLoadingCooked
Definition ShaderSerialization.h:48
FShaderSerializeContext()=default
FName SerializingAsset
Definition ShaderSerialization.h:51
FShaderSerializeContext(FArchive &InArchive)
Definition ShaderSerialization.h:31
FArchive & GetMainArchive()
Definition ShaderSerialization.h:54
virtual ~FShaderSerializeContext()=default
virtual void SerializeCode(FShaderCodeResource &Resource, int32 Index)
Definition ShaderSerialization.h:39
FArchive * Ar
Definition ShaderSerialization.h:45