xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/d3d/HLSLCompiler.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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     auto *platform = ANGLEPlatformCurrent();
219 
220     const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : nullptr;
221 
222     for (size_t i = 0; i < configs.size(); ++i)
223     {
224         ID3DBlob *errorMessage = nullptr;
225         ID3DBlob *binary       = nullptr;
226         HRESULT result         = S_OK;
227 
228         double startTime = platform->currentTime(platform);
229 
230         {
231             ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile", "source", hlsl);
232             result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, nullptr,
233                                      "main", profile.c_str(), configs[i].flags, 0, &binary,
234                                      &errorMessage);
235         }
236 
237         double compileTime = platform->currentTime(platform) - startTime;
238 
239         if (errorMessage)
240         {
241             std::string message = static_cast<const char *>(errorMessage->GetBufferPointer());
242             SafeRelease(errorMessage);
243             ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile::Error", "error", errorMessage);
244 
245             infoLog.appendSanitized(message.c_str());
246 
247             // This produces unbelievable amounts of spam in about:gpu.
248             // WARN() << std::endl << hlsl;
249 
250             WARN() << std::endl << message;
251 
252             if (macros != nullptr)
253             {
254                 constexpr const char *kLoopRelatedErrors[] = {
255                     // "can't unroll loops marked with loop attribute"
256                     "error X3531:",
257 
258                     // "cannot have gradient operations inside loops with divergent flow control",
259                     // even though it is counter-intuitive to disable unrolling for this error, some
260                     // very long shaders have trouble deciding which loops to unroll and turning off
261                     // forced unrolls allows them to compile properly.
262                     "error X4014:",
263 
264                     // "array index out of bounds", loop unrolling can result in invalid array
265                     // access if the indices become constant, causing loops that may never be
266                     // executed to generate compilation errors
267                     "error X3504:",
268                 };
269 
270                 bool hasLoopRelatedError = false;
271                 for (const char *errorType : kLoopRelatedErrors)
272                 {
273                     if (message.find(errorType) != std::string::npos)
274                     {
275                         hasLoopRelatedError = true;
276                         break;
277                     }
278                 }
279 
280                 if (hasLoopRelatedError)
281                 {
282                     // Disable [loop] and [flatten]
283                     macros = nullptr;
284 
285                     // Retry without changing compiler flags
286                     i--;
287                     continue;
288                 }
289             }
290         }
291 
292         if (SUCCEEDED(result))
293         {
294             int compileUs = static_cast<int>(compileTime * 1000'000.0);
295             ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.D3DShaderCompilationTimeUs", compileUs);
296 
297             ANGLE_HISTOGRAM_MEMORY_KB("GPU.ANGLE.D3DShaderBlobSizeKB",
298                                       static_cast<int>(binary->GetBufferSize() / 1024));
299 
300             *outCompiledBlob = binary;
301 
302             (*outDebugInfo) +=
303                 "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n";
304 
305 #if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
306             (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n";
307             (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n";
308             for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx)
309             {
310                 if (IsCompilerFlagSet(configs[i].flags, CompilerFlagInfos[fIx].mFlag))
311                 {
312                     (*outDebugInfo) += std::string("// ") + CompilerFlagInfos[fIx].mName + "\n";
313                 }
314             }
315 
316             (*outDebugInfo) += "// Macros:\n";
317             if (macros == nullptr)
318             {
319                 (*outDebugInfo) += "// - : -\n";
320             }
321             else
322             {
323                 for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt)
324                 {
325                     (*outDebugInfo) +=
326                         std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n";
327                 }
328             }
329 
330             std::string disassembly;
331             ANGLE_TRY(disassembleBinary(context, binary, &disassembly));
332             (*outDebugInfo) += "\n" + disassembly + "\n// ASSEMBLY END\n";
333 #endif  // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
334             return angle::Result::Continue;
335         }
336 
337         if (result == E_OUTOFMEMORY)
338         {
339             *outCompiledBlob = nullptr;
340             ANGLE_TRY_HR(context, result, "HLSL compiler had an unexpected failure");
341         }
342 
343         infoLog << "Warning: D3D shader compilation failed with " << configs[i].name << " flags. ("
344                 << profile << ")";
345 
346         if (i + 1 < configs.size())
347         {
348             infoLog << " Retrying with " << configs[i + 1].name;
349         }
350     }
351 
352     // None of the configurations succeeded in compiling this shader but the compiler is still
353     // intact
354     *outCompiledBlob = nullptr;
355     return angle::Result::Continue;
356 }
357 
disassembleBinary(d3d::Context * context,ID3DBlob * shaderBinary,std::string * disassemblyOut)358 angle::Result HLSLCompiler::disassembleBinary(d3d::Context *context,
359                                               ID3DBlob *shaderBinary,
360                                               std::string *disassemblyOut)
361 {
362     ANGLE_TRY(ensureInitialized(context));
363 
364     // Retrieve disassembly
365     UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING;
366     ID3DBlob *disassembly           = nullptr;
367     pD3DDisassemble disassembleFunc = reinterpret_cast<pD3DDisassemble>(mD3DDisassembleFunc);
368     LPCVOID buffer                  = shaderBinary->GetBufferPointer();
369     SIZE_T bufSize                  = shaderBinary->GetBufferSize();
370     HRESULT result                  = disassembleFunc(buffer, bufSize, flags, "", &disassembly);
371 
372     if (SUCCEEDED(result))
373     {
374         *disassemblyOut = std::string(static_cast<const char *>(disassembly->GetBufferPointer()));
375     }
376     else
377     {
378         *disassemblyOut = "";
379     }
380 
381     SafeRelease(disassembly);
382 
383     return angle::Result::Continue;
384 }
385 
386 }  // namespace rx
387