UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ShaderBinaryUtilities.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "RHICore.h"
6#include "Math/Int128.h"
7
9{
11 {
12 FString FunctionName;
13 FString PDBName;
14 FString Hash;
15 };
16
17#if PLATFORM_SUPPORTS_PRAGMA_PACK
18#pragma pack(push, 1)
19#endif // PLATFORM_SUPPORTS_PRAGMA_PACK
20
21 namespace DXBC
22 {
29
35 }
36
37 namespace DXIL
38 {
44
50
56 }
57
58#if PLATFORM_SUPPORTS_PRAGMA_PACK
59#pragma pack(pop)
60#endif // PLATFORM_SUPPORTS_PRAGMA_PACK
61
63 {
65 {
66
67 }
68
69 template<typename T>
70 T Get()
71 {
72 if (Offset + sizeof(T) > ByteSize)
73 {
74 checkf(false, TEXT("Parsing beyond EOS"));
75 return T{};
76 }
77
78 return *reinterpret_cast<const T*>(Start + Offset);
79 }
80
81 template<typename T>
83 {
84 if (Offset + sizeof(T) > ByteSize)
85 {
86 checkf(false, TEXT("Parsing beyond EOS"));
87 return T{};
88 }
89
90 const T* Value = reinterpret_cast<const T*>(Start + Offset);
91 Offset += sizeof(T);
92 return *Value;
93 }
94
95 FParseContext Split(uint64 InOffset) const
96 {
97 FParseContext ctx = *this;
98 ctx.Offset = InOffset;
99 return ctx;
100 }
101
102 void Skip(uint64 InOffset)
103 {
104 Offset += InOffset;
105 }
106
108 {
109 return ByteSize - Offset;
110 }
111
112 const void* Data() const
113 {
114 return Start + Offset;
115 }
116
117 const uint8* Start = nullptr;
118 const uint64 ByteSize = 0;
120 };
121
122 static void GetShaderBinaryDebugHashDXBC(const void* ShaderBinary, uint32 ByteSize, FShaderDebugData& OutData)
123 {
124 if (ByteSize < sizeof(DXBC::FHeader))
125 {
126 UE_LOG(LogRHICore, Error, TEXT("Shader byte size too small"));
127 return;
128 }
129
130 FParseContext Ctx(ShaderBinary, ByteSize);
131
132 auto Header = Ctx.Consume<DXBC::FHeader>();
133
134 for (uint32 ChunkIndex = 0; ChunkIndex < Header.ChunkCount; ++ChunkIndex)
135 {
136 uint32 ChunkOffset = Ctx.Consume<uint32>();
137
138 FParseContext ChunkCtx = Ctx.Split(ChunkOffset);
139
140 auto ChunkHeader = ChunkCtx.Consume<DXBC::FChunkHeader>();
141 switch (ChunkHeader.Type)
142 {
143 default:
144 {
145 continue;
146 }
147 case 'NDLI':
148 {
149 auto DXILDebugInfo = ChunkCtx.Consume<DXIL::FShaderDebugNameInfo>();
150
151 const uint32 HashLength = 32 + FCString::Strlen(TEXT(".pdb"));
152
153 if (DXILDebugInfo.NameLength != HashLength)
154 {
155 UE_LOG(LogRHICore, Display, TEXT("DXIL name length not the expected hash"));
156 continue;
157 }
158
159 if (ChunkCtx.PendingBytes() < HashLength)
160 {
161 UE_LOG(LogRHICore, Display, TEXT("ILDN block corrupt"));
162 continue;
163 }
164
165 OutData.PDBName = FString::ConstructFromPtrSize(static_cast<const ANSICHAR*>(ChunkCtx.Data()), HashLength);
166 break;
167 }
168 case 'HSAH':
169 {
170 auto DXILDebugInfo = ChunkCtx.Consume<DXIL::FShaderHashInfo>();
171 static_assert(sizeof(FUInt128) == sizeof(DXILDebugInfo.Digest), "Unexpected digest size");
172
173 FUInt128 HashBytes;
174 FMemory::Memcpy(&HashBytes, DXILDebugInfo.Digest, sizeof(HashBytes));
175
176 OutData.Hash = BytesToHex(reinterpret_cast<const uint8*>(&HashBytes), sizeof(FUInt128));
177 break;
178 }
179 case '0VSP':
180 {
182
183 if (RevisionByteSize >= sizeof(DXIL::FPSVRevision3Info))
184 {
185 DXIL::FPSVRevision3Info PSVInfo = ChunkCtx.Get<DXIL::FPSVRevision3Info>();
187
188 if (uint32 ResourceCount = ChunkCtx.Consume<uint32>())
189 {
192 }
193
195 if (StringTableSize <= PSVInfo.EntryNameOffset)
196 {
197 UE_LOG(LogRHICore, Error, TEXT("Pipeline state validation string table too small"));
198 continue;
199 }
200
201 ChunkCtx.Skip(PSVInfo.EntryNameOffset);
202 OutData.FunctionName = static_cast<const char*>(ChunkCtx.Data());
203 }
204
205 break;
206 }
207 }
208 }
209 }
210}
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
void BytesToHex(const uint8 *In, int32 Count, FString &Out)
Definition BytesToHex.cpp:97
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
DIRECTLINK_API Display
Definition DirectLinkLog.h:8
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Int128.h:9
Definition ShaderBinaryUtilities.h:9
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
static int32 Strlen(const CharType *String)
Definition CString.h:1047
Definition ShaderBinaryUtilities.h:31
uint32 Size
Definition ShaderBinaryUtilities.h:33
uint32 Type
Definition ShaderBinaryUtilities.h:32
Definition ShaderBinaryUtilities.h:24
uint32 ChunkCount
Definition ShaderBinaryUtilities.h:27
uint32 Identifier
Definition ShaderBinaryUtilities.h:25
uint32 Ignore[6]
Definition ShaderBinaryUtilities.h:26
Definition ShaderBinaryUtilities.h:52
uint8 Ignore[48]
Definition ShaderBinaryUtilities.h:53
uint32 EntryNameOffset
Definition ShaderBinaryUtilities.h:54
Definition ShaderBinaryUtilities.h:40
uint16 Flags
Definition ShaderBinaryUtilities.h:41
uint16 NameLength
Definition ShaderBinaryUtilities.h:42
Definition ShaderBinaryUtilities.h:46
uint8 Digest[16]
Definition ShaderBinaryUtilities.h:48
uint32 Flags
Definition ShaderBinaryUtilities.h:47
Definition ShaderBinaryUtilities.h:63
uint64 PendingBytes() const
Definition ShaderBinaryUtilities.h:107
FParseContext Split(uint64 InOffset) const
Definition ShaderBinaryUtilities.h:95
T Get()
Definition ShaderBinaryUtilities.h:70
const void * Data() const
Definition ShaderBinaryUtilities.h:112
uint64 Offset
Definition ShaderBinaryUtilities.h:119
void Skip(uint64 InOffset)
Definition ShaderBinaryUtilities.h:102
const uint8 * Start
Definition ShaderBinaryUtilities.h:117
T Consume()
Definition ShaderBinaryUtilities.h:82
FParseContext(const void *Start, const uint64 ByteSize)
Definition ShaderBinaryUtilities.h:64
const uint64 ByteSize
Definition ShaderBinaryUtilities.h:118
Definition ShaderBinaryUtilities.h:11
FString Hash
Definition ShaderBinaryUtilities.h:14
FString PDBName
Definition ShaderBinaryUtilities.h:13
FString FunctionName
Definition ShaderBinaryUtilities.h:12