Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d / HLSLCompiler.cpp
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 "libGLESv2/renderer/d3d/HLSLCompiler.h"
8 #include "libGLESv2/Program.h"
9 #include "libGLESv2/main.h"
10
11 #include "common/features.h"
12 #include "common/utilities.h"
13
14 #include "third_party/trace_event/trace_event.h"
15
16 // Definitions local to the translation unit
17 namespace
18 {
19
20 #ifdef CREATE_COMPILER_FLAG_INFO
21     #undef CREATE_COMPILER_FLAG_INFO
22 #endif
23
24 #define CREATE_COMPILER_FLAG_INFO(flag) { flag, #flag }
25
26 struct CompilerFlagInfo
27 {
28     UINT mFlag;
29     const char *mName;
30 };
31
32 CompilerFlagInfo CompilerFlagInfos[] =
33 {
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)
57 };
58
59 #undef CREATE_COMPILER_FLAG_INFO
60
61 bool IsCompilerFlagSet(UINT mask, UINT flag)
62 {
63     bool isFlagSet = IsMaskFlagSet(mask, flag);
64
65     switch(flag)
66     {
67       case D3DCOMPILE_OPTIMIZATION_LEVEL0:
68         return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL3));
69
70       case D3DCOMPILE_OPTIMIZATION_LEVEL1:
71         return (mask & D3DCOMPILE_OPTIMIZATION_LEVEL2) == UINT(0);
72
73       case D3DCOMPILE_OPTIMIZATION_LEVEL3:
74         return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL0));
75
76       default:
77         return isFlagSet;
78     }
79 }
80
81 const char *GetCompilerFlagName(UINT mask, size_t flagIx)
82 {
83     const CompilerFlagInfo &flagInfo = CompilerFlagInfos[flagIx];
84     if (IsCompilerFlagSet(mask, flagInfo.mFlag))
85     {
86         return flagInfo.mName;
87     }
88
89     return nullptr;
90 }
91
92 }
93
94 namespace rx
95 {
96
97 CompileConfig::CompileConfig()
98     : flags(0),
99       name()
100 {
101 }
102
103 CompileConfig::CompileConfig(UINT flags, const std::string &name)
104     : flags(flags),
105       name(name)
106 {
107 }
108
109 HLSLCompiler::HLSLCompiler()
110     : mD3DCompilerModule(NULL),
111       mD3DCompileFunc(NULL),
112       mD3DDisassembleFunc(NULL)
113 {
114 }
115
116 HLSLCompiler::~HLSLCompiler()
117 {
118     release();
119 }
120
121 bool HLSLCompiler::initialize()
122 {
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;
128
129     for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
130     {
131         if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
132         {
133             break;
134         }
135     }
136 #endif  // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
137
138     if (!mD3DCompilerModule)
139     {
140         // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
141         mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
142     }
143
144     if (!mD3DCompilerModule)
145     {
146         ERR("No D3D compiler module found - aborting!\n");
147         return false;
148     }
149
150     mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
151     ASSERT(mD3DCompileFunc);
152
153     mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble"));
154     ASSERT(mD3DDisassembleFunc);
155
156 #else
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);
162 #endif
163
164     return mD3DCompileFunc != NULL;
165 }
166
167 void HLSLCompiler::release()
168 {
169     if (mD3DCompilerModule)
170     {
171         FreeLibrary(mD3DCompilerModule);
172         mD3DCompilerModule = NULL;
173         mD3DCompileFunc = NULL;
174         mD3DDisassembleFunc = NULL;
175     }
176 }
177
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
181 {
182 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
183     ASSERT(mD3DCompilerModule);
184 #endif
185     ASSERT(mD3DCompileFunc);
186
187 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
188     if (gl::perfActive())
189     {
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());
193     }
194 #endif
195
196     const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : NULL;
197
198     for (size_t i = 0; i < configs.size(); ++i)
199     {
200         ID3DBlob *errorMessage = NULL;
201         ID3DBlob *binary = NULL;
202
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);
205
206         if (errorMessage)
207         {
208             std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer());
209             SafeRelease(errorMessage);
210
211             infoLog.appendSanitized(message.c_str());
212             TRACE("\n%s", hlsl.c_str());
213             TRACE("\n%s", message.c_str());
214
215             if (message.find("error X3531:") != std::string::npos)   // "can't unroll loops marked with loop attribute"
216             {
217                 macros = NULL;   // Disable [loop] and [flatten]
218
219                 // Retry without changing compiler flags
220                 i--;
221                 continue;
222             }
223         }
224
225         if (SUCCEEDED(result))
226         {
227             *outCompiledBlob = binary;
228
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)
234             {
235                 const char *flagName = GetCompilerFlagName(configs[i].flags, fIx);
236                 if (flagName != nullptr)
237                 {
238                     (*outDebugInfo) += std::string("// ") + flagName + "\n";
239                 }
240             }
241
242             (*outDebugInfo) += "// Macros:\n";
243             if (macros == nullptr)
244             {
245                 (*outDebugInfo) += "// - : -\n";
246             }
247             else
248             {
249                 for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt)
250                 {
251                     (*outDebugInfo) += std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n";
252                 }
253             }
254
255             (*outDebugInfo) += "\n" + disassembleBinary(binary) + "\n// ASSEMBLY END\n";
256 #endif
257
258             return gl::Error(GL_NO_ERROR);
259         }
260         else
261         {
262             if (result == E_OUTOFMEMORY)
263             {
264                 *outCompiledBlob = NULL;
265                 return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result);
266             }
267
268             infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str());
269
270             if (i + 1 < configs.size())
271             {
272                 infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str());
273             }
274         }
275     }
276
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);
280 }
281
282 std::string HLSLCompiler::disassembleBinary(ID3DBlob *shaderBinary) const
283 {
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);
291
292     std::string asmSrc;
293     if (SUCCEEDED(result))
294     {
295         asmSrc = reinterpret_cast<const char*>(disassembly->GetBufferPointer());
296     }
297
298     SafeRelease(disassembly);
299
300     return asmSrc;
301 }
302
303 }