UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ShaderSource.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "Containers/Array.h"
10
11#define SHADER_SOURCE_ANSI 1 UE_DEPRECATED_MACRO(5.6, "SHADER_SOURCE_ANSI has been deprecated and should be assumed to always be 1")
12#define SHADER_SOURCE_LITERAL(S) S UE_DEPRECATED_MACRO(5.6, "SHADER_SOURCE_LITERAL has been deprecated and should be replaced with just plain string literal")
13#define SHADER_SOURCE_VIEWLITERAL(S) ANSITEXTVIEW(S) UE_DEPRECATED_MACRO(5.6, "SHADER_SOURCE_VIEWLITERAL has been deprecated and should be replaced with ANSITEXTVIEW")
14
15/* Class used in shader compilation pipelines which wraps source code and ensures sufficient padding such that 16-byte wide SIMD
16 * operations on the source are guaranteed to read valid memory even if starting from the last character.
17 */
19{
20private:
22 {
23 Source.SetNumUninitialized(Num + ShaderSourceSimdPadding, AllowShrinking);
24 FMemory::Memzero(Source.GetData() + Num, sizeof(CharType) * ShaderSourceSimdPadding);
25
26 SourceCompressed.Empty();
27 DecompressedCharCount = 0;
28 }
29
30public:
35
36 template <int NumChars>
38
40 UE_DEPRECATED(5.6, "Shader code is always assumed to be ANSI")
41 static constexpr bool IsWide() { return false; }
43 UE_DEPRECATED(5.6, "Shader code is always assumed to be ANSI, so GetSimdCharCount() is always 16")
46 UE_DEPRECATED(5.6, "Shader code is always assumed to be ANSI, so GetSingleCharMask() is always 1")
48
49 /* Construct an empty shader source object; will still contain padding */
50 FShaderSource() { SetLen(0); };
51
52 /* Given a string view construct a shader source object containing the contents of that string.
53 * Note that this will incur a memcpy of the string contents.
54 * @param InSrc The source string to be copied
55 * @param AdditionalSlack optional additional space to allocate; this is on top of the automatic padding.
56 */
58
59 /* Set the given string as the contents of this shader source object. The inner allocation will grow to fit
60 * the string contents as needed.
61 * Note that this will incur a memcpy of the string contents.
62 * @param InSrc The source string to be copied
63 * @param AdditionalSlack optional additional space to allocate; this is on top of the automatic padding.
64 */
66
67 /* Move assignment operator accepting a string object. This will append padding bytes to the existing string, as such it's
68 * best if there's sufficient extra capacity in the string storage to avoid incurring a realloc-and-copy here.
69 * @param InSrc The source string whose data this object will take ownership of.
70 */
72
73 /* Reduces the set size of the stored string length, optionally shrinking the allocation.
74 * @param Num the desired allocation size (padding bytes will be added on top of this)
75 * @param AllowShrinking whether to reallocate or keep the existing larger size allocation
76 */
78 {
79 checkf(Num <= Len(), TEXT("Trying to shrink to %d characters but existing allocation is smaller (%d characters)"), Num, Len());
80 SetLen(Num, AllowShrinking);
81 }
87
88 /* String view accessor. Will decompress data if it was previously compressed.
89 * @return a string view pointing to the source contents, excluding padding.
90 */
91 inline FViewType GetView() const
92 {
93 checkf(!IsCompressed(), TEXT("FShaderSource is compressed; must decompress prior to calling GetView"));
94 return { Source.GetData(), Len() };
95 }
96
97 /* Direct data pointer accessor. Will decompress data if it was previously compressed.
98 * @return a pointer to the source data; will be null terminated by the SIMD padding.
99 */
101 {
102 checkf(!IsCompressed(), TEXT("FShaderSource is compressed; must decompress prior to calling GetData"));
103 return Source.GetData();
104 }
105
106 /* IsEmpty predicate
107 * @return true if this source object is empty excluding the SIMD padding, false otherwise.
108 */
109 inline bool IsEmpty() const { return Len() == 0; }
110
111 /* Length accessor.
112 * @return the non-padded length of the source (also excluding null terminator)
113 */
114 inline int32 Len() const
115 {
116 checkf(!IsCompressed(), TEXT("Len should not be called on compressed FShaderSource."))
117 return Source.Num() - ShaderSourceSimdPadding;
118 };
119
120 inline bool IsCompressed() const
121 {
122 return DecompressedCharCount != 0; // DecompressedCharCount member is only set when compression occurs
123 }
124
126 {
127 return DecompressedCharCount * sizeof(FShaderSource::CharType);
128 }
129
130 /* FArchive serialization operator. Note this currently serializes padding for simplicity's sake.
131 * @param Ar The archive to serialize from/to
132 * @param ShaderSource the source object to serialize
133 */
135
138
139private:
140 static_assert(sizeof(CharType) == 1);
141 static constexpr int32 ShaderSourceSimdPadding = 15;
142 TArray<CharType> Source;
143 TArray<uint8> SourceCompressed;
144 int32 DecompressedCharCount = 0;
145};
146
EAllowShrinking
Definition AllowShrinking.h:10
#define UE_ALLOWSHRINKING_BOOL_DEPRECATED(FunctionName)
Definition AllowShrinking.h:31
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
Definition Archive.h:1208
Definition ShaderSource.h:19
void ShrinkToLen(int32 Num, EAllowShrinking AllowShrinking=EAllowShrinking::Default)
Definition ShaderSource.h:77
int32 Len() const
Definition ShaderSource.h:114
RENDERCORE_API void Decompress()
Definition ShaderSource.cpp:76
RENDERCORE_API void Compress()
Definition ShaderSource.cpp:48
bool IsCompressed() const
Definition ShaderSource.h:120
RENDERCORE_API FShaderSource & operator=(FStringType &&InSrc)
Definition ShaderSource.cpp:35
ANSICHAR CharType
Definition ShaderSource.h:31
FShaderSource()
Definition ShaderSource.h:50
CharType * GetData()
Definition ShaderSource.h:100
FCStringAnsi FCStringType
Definition ShaderSource.h:34
bool IsEmpty() const
Definition ShaderSource.h:109
static constexpr int32 GetSimdCharCount()
Definition ShaderSource.h:44
static constexpr bool IsWide()
Definition ShaderSource.h:41
int32 GetDecompressedSize() const
Definition ShaderSource.h:125
FAnsiStringView FViewType
Definition ShaderSource.h:32
FViewType GetView() const
Definition ShaderSource.h:91
FAnsiString FStringType
Definition ShaderSource.h:33
friend FArchive & operator<<(FArchive &Ar, FShaderSource &ShaderSource)
Definition ShaderSource.cpp:87
static constexpr int32 GetSingleCharMask()
Definition ShaderSource.h:47
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
void SetNumUninitialized(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2369
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition StringBuilder.h:509
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131
Definition CString.h:60