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