1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "libANGLE/renderer/d3d/HLSLCompiler.h"
8
9 #include <sstream>
10
11 #include "common/system_utils.h"
12 #include "common/utilities.h"
13 #include "libANGLE/Context.h"
14 #include "libANGLE/Program.h"
15 #include "libANGLE/features.h"
16 #include "libANGLE/histogram_macros.h"
17 #include "libANGLE/renderer/d3d/ContextD3D.h"
18 #include "libANGLE/trace.h"
19
20 namespace
21 {
22 #if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
23 # ifdef CREATE_COMPILER_FLAG_INFO
24 # undef CREATE_COMPILER_FLAG_INFO
25 # endif
26
27 # define CREATE_COMPILER_FLAG_INFO(flag) \
28 { \
29 flag, #flag \
30 }
31
32 struct CompilerFlagInfo
33 {
34 UINT mFlag;
35 const char *mName;
36 };
37
38 CompilerFlagInfo CompilerFlagInfos[] = {
39 // NOTE: The data below is copied from d3dcompiler.h
40 // If something changes there it should be changed here as well
41 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_DEBUG), // (1 << 0)
42 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_VALIDATION), // (1 << 1)
43 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_OPTIMIZATION), // (1 << 2)
44 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_ROW_MAJOR), // (1 << 3)
45 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR), // (1 << 4)
46 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PARTIAL_PRECISION), // (1 << 5)
47 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_VS_SOFTWARE_NO_OPT), // (1 << 6)
48 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_PS_SOFTWARE_NO_OPT), // (1 << 7)
49 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_NO_PRESHADER), // (1 << 8)
50 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_AVOID_FLOW_CONTROL), // (1 << 9)
51 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PREFER_FLOW_CONTROL), // (1 << 10)
52 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_STRICTNESS), // (1 << 11)
53 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY), // (1 << 12)
54 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_IEEE_STRICTNESS), // (1 << 13)
55 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL0), // (1 << 14)
56 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL1), // 0
57 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL2), // ((1 << 14) | (1 << 15))
58 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL3), // (1 << 15)
59 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED16), // (1 << 16)
60 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED17), // (1 << 17)
61 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_WARNINGS_ARE_ERRORS) // (1 << 18)
62 };
63
64 # undef CREATE_COMPILER_FLAG_INFO
65
IsCompilerFlagSet(UINT mask,UINT flag)66 bool IsCompilerFlagSet(UINT mask, UINT flag)
67 {
68 bool isFlagSet = IsMaskFlagSet(mask, flag);
69
70 switch (flag)
71 {
72 case D3DCOMPILE_OPTIMIZATION_LEVEL0:
73 return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL3));
74
75 case D3DCOMPILE_OPTIMIZATION_LEVEL1:
76 return (mask & D3DCOMPILE_OPTIMIZATION_LEVEL2) == UINT(0);
77
78 case D3DCOMPILE_OPTIMIZATION_LEVEL3:
79 return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL0));
80
81 default:
82 return isFlagSet;
83 }
84 }
85 #endif // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
86
87 enum D3DCompilerLoadLibraryResult
88 {
89 D3DCompilerDefaultLibrarySuccess,
90 D3DCompilerFailure,
91 D3DCompilerEnumBoundary,
92 };
93 } // anonymous namespace
94
95 namespace rx
96 {
97
CompileConfig()98 CompileConfig::CompileConfig() : flags(0), name() {}
99
CompileConfig(UINT flags,const std::string & name)100 CompileConfig::CompileConfig(UINT flags, const std::string &name) : flags(flags), name(name) {}
101
HLSLCompiler()102 HLSLCompiler::HLSLCompiler()
103 : mInitialized(false),
104 mD3DCompilerModule(nullptr),
105 mD3DCompileFunc(nullptr),
106 mD3DDisassembleFunc(nullptr)
107 {}
108
~HLSLCompiler()109 HLSLCompiler::~HLSLCompiler()
110 {
111 release();
112 }
113
ensureInitialized(d3d::Context * context)114 angle::Result HLSLCompiler::ensureInitialized(d3d::Context *context)
115 {
116 if (mInitialized)
117 {
118 return angle::Result::Continue;
119 }
120
121 ANGLE_TRACE_EVENT0("gpu.angle", "HLSLCompiler::initialize");
122 #if !defined(ANGLE_ENABLE_WINDOWS_UWP)
123 # if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
124 // Find a D3DCompiler module that had already been loaded based on a predefined list of
125 // versions.
126 static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
127
128 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
129 {
130 if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
131 {
132 break;
133 }
134 }
135 # endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
136
137 if (!mD3DCompilerModule)
138 {
139 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was
140 // built with.
141 mD3DCompilerModule = LoadLibraryA(D3DCOMPILER_DLL_A);
142
143 if (mD3DCompilerModule)
144 {
145 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.D3DCompilerLoadLibraryResult",
146 D3DCompilerDefaultLibrarySuccess, D3DCompilerEnumBoundary);
147 }
148 }
149
150 if (!mD3DCompilerModule)
151 {
152 DWORD lastError = GetLastError();
153 ERR() << "D3D Compiler LoadLibrary failed. GetLastError=" << lastError;
154 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.D3DCompilerLoadLibraryResult", D3DCompilerFailure,
155 D3DCompilerEnumBoundary);
156 ANGLE_TRY_HR(context, E_OUTOFMEMORY, "LoadLibrary failed to load D3D Compiler DLL.");
157 }
158
159 mD3DCompileFunc =
160 reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
161 ASSERT(mD3DCompileFunc);
162
163 mD3DDisassembleFunc =
164 reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble"));
165 ASSERT(mD3DDisassembleFunc);
166
167 #else
168 // D3D Shader compiler is linked already into this module, so the export
169 // can be directly assigned.
170 mD3DCompilerModule = nullptr;
171 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(D3DCompile);
172 mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(D3DDisassemble);
173 #endif
174
175 ANGLE_CHECK_HR(context, mD3DCompileFunc, "Error finding D3DCompile entry point.",
176 E_OUTOFMEMORY);
177
178 mInitialized = true;
179 return angle::Result::Continue;
180 }
181
release()182 void HLSLCompiler::release()
183 {
184 if (mInitialized)
185 {
186 FreeLibrary(mD3DCompilerModule);
187 mD3DCompilerModule = nullptr;
188 mD3DCompileFunc = nullptr;
189 mD3DDisassembleFunc = nullptr;
190 mInitialized = false;
191 }
192 }
193
compileToBinary(d3d::Context * context,gl::InfoLog & infoLog,const std::string & hlsl,const std::string & profile,const std::vector<CompileConfig> & configs,const D3D_SHADER_MACRO * overrideMacros,ID3DBlob ** outCompiledBlob,std::string * outDebugInfo)194 angle::Result HLSLCompiler::compileToBinary(d3d::Context *context,
195 gl::InfoLog &infoLog,
196 const std::string &hlsl,
197 const std::string &profile,
198 const std::vector<CompileConfig> &configs,
199 const D3D_SHADER_MACRO *overrideMacros,
200 ID3DBlob **outCompiledBlob,
201 std::string *outDebugInfo)
202 {
203 ASSERT(mInitialized);
204
205 #if !defined(ANGLE_ENABLE_WINDOWS_UWP)
206 ASSERT(mD3DCompilerModule);
207 #endif
208 ASSERT(mD3DCompileFunc);
209
210 #if !defined(ANGLE_ENABLE_WINDOWS_UWP) && defined(ANGLE_ENABLE_DEBUG_TRACE)
211 std::string sourcePath = angle::CreateTemporaryFile().value();
212 std::ostringstream stream;
213 stream << "#line 2 \"" << sourcePath << "\"\n\n" << hlsl;
214 std::string sourceText = stream.str();
215 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
216 #endif
217
218 const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : nullptr;
219
220 for (size_t i = 0; i < configs.size(); ++i)
221 {
222 ID3DBlob *errorMessage = nullptr;
223 ID3DBlob *binary = nullptr;
224 HRESULT result = S_OK;
225
226 {
227 ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile", "source", hlsl);
228 result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, nullptr,
229 "main", profile.c_str(), configs[i].flags, 0, &binary,
230 &errorMessage);
231 }
232
233 if (errorMessage)
234 {
235 std::string message = static_cast<const char *>(errorMessage->GetBufferPointer());
236 SafeRelease(errorMessage);
237 ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile::Error", "error", errorMessage);
238
239 infoLog.appendSanitized(message.c_str());
240
241 // This produces unbelievable amounts of spam in about:gpu.
242 // WARN() << std::endl << hlsl;
243
244 WARN() << std::endl << message;
245
246 if (macros != nullptr)
247 {
248 constexpr const char *kLoopRelatedErrors[] = {
249 // "can't unroll loops marked with loop attribute"
250 "error X3531:",
251
252 // "cannot have gradient operations inside loops with divergent flow control",
253 // even though it is counter-intuitive to disable unrolling for this error, some
254 // very long shaders have trouble deciding which loops to unroll and turning off
255 // forced unrolls allows them to compile properly.
256 "error X4014:",
257
258 // "array index out of bounds", loop unrolling can result in invalid array
259 // access if the indices become constant, causing loops that may never be
260 // executed to generate compilation errors
261 "error X3504:",
262 };
263
264 bool hasLoopRelatedError = false;
265 for (const char *errorType : kLoopRelatedErrors)
266 {
267 if (message.find(errorType) != std::string::npos)
268 {
269 hasLoopRelatedError = true;
270 break;
271 }
272 }
273
274 if (hasLoopRelatedError)
275 {
276 // Disable [loop] and [flatten]
277 macros = nullptr;
278
279 // Retry without changing compiler flags
280 i--;
281 continue;
282 }
283 }
284 }
285
286 if (SUCCEEDED(result))
287 {
288 *outCompiledBlob = binary;
289
290 (*outDebugInfo) +=
291 "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n";
292
293 #if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
294 (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n";
295 (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n";
296 for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx)
297 {
298 if (IsCompilerFlagSet(configs[i].flags, CompilerFlagInfos[fIx].mFlag))
299 {
300 (*outDebugInfo) += std::string("// ") + CompilerFlagInfos[fIx].mName + "\n";
301 }
302 }
303
304 (*outDebugInfo) += "// Macros:\n";
305 if (macros == nullptr)
306 {
307 (*outDebugInfo) += "// - : -\n";
308 }
309 else
310 {
311 for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt)
312 {
313 (*outDebugInfo) +=
314 std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n";
315 }
316 }
317
318 std::string disassembly;
319 ANGLE_TRY(disassembleBinary(context, binary, &disassembly));
320 (*outDebugInfo) += "\n" + disassembly + "\n// ASSEMBLY END\n";
321 #endif // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
322 return angle::Result::Continue;
323 }
324
325 if (result == E_OUTOFMEMORY)
326 {
327 *outCompiledBlob = nullptr;
328 ANGLE_TRY_HR(context, result, "HLSL compiler had an unexpected failure");
329 }
330
331 infoLog << "Warning: D3D shader compilation failed with " << configs[i].name << " flags. ("
332 << profile << ")";
333
334 if (i + 1 < configs.size())
335 {
336 infoLog << " Retrying with " << configs[i + 1].name;
337 }
338 }
339
340 // None of the configurations succeeded in compiling this shader but the compiler is still
341 // intact
342 *outCompiledBlob = nullptr;
343 return angle::Result::Continue;
344 }
345
disassembleBinary(d3d::Context * context,ID3DBlob * shaderBinary,std::string * disassemblyOut)346 angle::Result HLSLCompiler::disassembleBinary(d3d::Context *context,
347 ID3DBlob *shaderBinary,
348 std::string *disassemblyOut)
349 {
350 ANGLE_TRY(ensureInitialized(context));
351
352 // Retrieve disassembly
353 UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING;
354 ID3DBlob *disassembly = nullptr;
355 pD3DDisassemble disassembleFunc = reinterpret_cast<pD3DDisassemble>(mD3DDisassembleFunc);
356 LPCVOID buffer = shaderBinary->GetBufferPointer();
357 SIZE_T bufSize = shaderBinary->GetBufferSize();
358 HRESULT result = disassembleFunc(buffer, bufSize, flags, "", &disassembly);
359
360 if (SUCCEEDED(result))
361 {
362 *disassemblyOut = std::string(static_cast<const char *>(disassembly->GetBufferPointer()));
363 }
364 else
365 {
366 *disassemblyOut = "";
367 }
368
369 SafeRelease(disassembly);
370
371 return angle::Result::Continue;
372 }
373
374 } // namespace rx
375