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.
7 #include "libGLESv2/renderer/d3d/HLSLCompiler.h"
8 #include "libGLESv2/Program.h"
9 #include "libGLESv2/main.h"
11 #include "common/features.h"
12 #include "common/utilities.h"
14 #include "third_party/trace_event/trace_event.h"
16 // Definitions local to the translation unit
20 #ifdef CREATE_COMPILER_FLAG_INFO
21 #undef CREATE_COMPILER_FLAG_INFO
24 #define CREATE_COMPILER_FLAG_INFO(flag) { flag, #flag }
26 struct CompilerFlagInfo
32 CompilerFlagInfo CompilerFlagInfos[] =
34 // NOTE: The data below is copied from d3dcompiler.h
35 // If something changes there it should be changed here as well
36 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_DEBUG), // (1 << 0)
37 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_VALIDATION), // (1 << 1)
38 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_OPTIMIZATION), // (1 << 2)
39 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_ROW_MAJOR), // (1 << 3)
40 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR), // (1 << 4)
41 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PARTIAL_PRECISION), // (1 << 5)
42 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_VS_SOFTWARE_NO_OPT), // (1 << 6)
43 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_PS_SOFTWARE_NO_OPT), // (1 << 7)
44 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_NO_PRESHADER), // (1 << 8)
45 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_AVOID_FLOW_CONTROL), // (1 << 9)
46 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PREFER_FLOW_CONTROL), // (1 << 10)
47 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_STRICTNESS), // (1 << 11)
48 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY), // (1 << 12)
49 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_IEEE_STRICTNESS), // (1 << 13)
50 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL0), // (1 << 14)
51 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL1), // 0
52 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL2), // ((1 << 14) | (1 << 15))
53 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL3), // (1 << 15)
54 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED16), // (1 << 16)
55 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED17), // (1 << 17)
56 CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_WARNINGS_ARE_ERRORS) // (1 << 18)
59 #undef CREATE_COMPILER_FLAG_INFO
61 bool IsCompilerFlagSet(UINT mask, UINT flag)
63 bool isFlagSet = IsMaskFlagSet(mask, flag);
67 case D3DCOMPILE_OPTIMIZATION_LEVEL0:
68 return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL3));
70 case D3DCOMPILE_OPTIMIZATION_LEVEL1:
71 return (mask & D3DCOMPILE_OPTIMIZATION_LEVEL2) == UINT(0);
73 case D3DCOMPILE_OPTIMIZATION_LEVEL3:
74 return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL0));
81 const char *GetCompilerFlagName(UINT mask, size_t flagIx)
83 const CompilerFlagInfo &flagInfo = CompilerFlagInfos[flagIx];
84 if (IsCompilerFlagSet(mask, flagInfo.mFlag))
86 return flagInfo.mName;
97 CompileConfig::CompileConfig()
103 CompileConfig::CompileConfig(UINT flags, const std::string &name)
109 HLSLCompiler::HLSLCompiler()
110 : mD3DCompilerModule(NULL),
111 mD3DCompileFunc(NULL),
112 mD3DDisassembleFunc(NULL)
116 HLSLCompiler::~HLSLCompiler()
121 bool HLSLCompiler::initialize()
123 TRACE_EVENT0("gpu", "initializeCompiler");
124 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
125 #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
126 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
127 static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
129 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
131 if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
136 #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
138 if (!mD3DCompilerModule)
140 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
141 mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
144 if (!mD3DCompilerModule)
146 ERR("No D3D compiler module found - aborting!\n");
150 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
151 ASSERT(mD3DCompileFunc);
153 mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble"));
154 ASSERT(mD3DDisassembleFunc);
157 // D3D Shader compiler is linked already into this module, so the export
158 // can be directly assigned.
159 mD3DCompilerModule = NULL;
160 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(D3DCompile);
161 mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(D3DDisassemble);
164 return mD3DCompileFunc != NULL;
167 void HLSLCompiler::release()
169 if (mD3DCompilerModule)
171 FreeLibrary(mD3DCompilerModule);
172 mD3DCompilerModule = NULL;
173 mD3DCompileFunc = NULL;
174 mD3DDisassembleFunc = NULL;
178 gl::Error HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const std::string &hlsl, const std::string &profile,
179 const std::vector<CompileConfig> &configs, const D3D_SHADER_MACRO *overrideMacros,
180 ID3DBlob **outCompiledBlob, std::string *outDebugInfo) const
182 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
183 ASSERT(mD3DCompilerModule);
185 ASSERT(mD3DCompileFunc);
187 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
188 if (gl::perfActive())
190 std::string sourcePath = getTempPath();
191 std::string sourceText = FormatString("#line 2 \"%s\"\n\n%s", sourcePath.c_str(), hlsl.c_str());
192 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
196 const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : NULL;
198 for (size_t i = 0; i < configs.size(); ++i)
200 ID3DBlob *errorMessage = NULL;
201 ID3DBlob *binary = NULL;
203 HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, NULL, "main", profile.c_str(),
204 configs[i].flags, 0, &binary, &errorMessage);
208 std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer());
209 SafeRelease(errorMessage);
211 infoLog.appendSanitized(message.c_str());
212 TRACE("\n%s", hlsl.c_str());
213 TRACE("\n%s", message.c_str());
215 if (message.find("error X3531:") != std::string::npos) // "can't unroll loops marked with loop attribute"
217 macros = NULL; // Disable [loop] and [flatten]
219 // Retry without changing compiler flags
225 if (SUCCEEDED(result))
227 *outCompiledBlob = binary;
229 #if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
230 (*outDebugInfo) += "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n";
231 (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n";
232 (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n";
233 for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx)
235 const char *flagName = GetCompilerFlagName(configs[i].flags, fIx);
236 if (flagName != nullptr)
238 (*outDebugInfo) += std::string("// ") + flagName + "\n";
242 (*outDebugInfo) += "// Macros:\n";
243 if (macros == nullptr)
245 (*outDebugInfo) += "// - : -\n";
249 for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt)
251 (*outDebugInfo) += std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n";
255 (*outDebugInfo) += "\n" + disassembleBinary(binary) + "\n// ASSEMBLY END\n";
258 return gl::Error(GL_NO_ERROR);
262 if (result == E_OUTOFMEMORY)
264 *outCompiledBlob = NULL;
265 return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result);
268 infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str());
270 if (i + 1 < configs.size())
272 infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str());
277 // None of the configurations succeeded in compiling this shader but the compiler is still intact
278 *outCompiledBlob = NULL;
279 return gl::Error(GL_NO_ERROR);
282 std::string HLSLCompiler::disassembleBinary(ID3DBlob *shaderBinary) const
284 // Retrieve disassembly
285 UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING;
286 ID3DBlob *disassembly = NULL;
287 pD3DDisassemble disassembleFunc = reinterpret_cast<pD3DDisassemble>(mD3DDisassembleFunc);
288 LPCVOID buffer = shaderBinary->GetBufferPointer();
289 SIZE_T bufSize = shaderBinary->GetBufferSize();
290 HRESULT result = disassembleFunc(buffer, bufSize, flags, "", &disassembly);
293 if (SUCCEEDED(result))
295 asmSrc = reinterpret_cast<const char*>(disassembly->GetBufferPointer());
298 SafeRelease(disassembly);