blob: a66a15b3d764408caea45a9233b1d12972b887eb [file] [log] [blame]
Jamie Madillea247592014-08-28 10:37:08 -04001//
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
Geoff Langdad5ed32014-02-10 12:59:17 -05007#include "libGLESv2/renderer/d3d/HLSLCompiler.h"
8#include "libGLESv2/Program.h"
9#include "libGLESv2/main.h"
10
11#include "common/utilities.h"
12
13#include "third_party/trace_event/trace_event.h"
14
15namespace rx
16{
17
Geoff Lang3935e512014-09-23 13:15:19 -040018CompileConfig::CompileConfig()
19 : flags(0),
20 name()
21{
22}
23
24CompileConfig::CompileConfig(UINT flags, const std::string &name)
25 : flags(flags),
26 name(name)
27{
28}
29
Geoff Langdad5ed32014-02-10 12:59:17 -050030HLSLCompiler::HLSLCompiler()
31 : mD3DCompilerModule(NULL),
Tibor den Ouden97049c62014-10-06 21:39:16 +020032 mD3DCompileFunc(NULL),
33 mD3DDisassembleFunc(NULL)
Geoff Langdad5ed32014-02-10 12:59:17 -050034{
35}
36
37HLSLCompiler::~HLSLCompiler()
38{
39 release();
40}
41
42bool HLSLCompiler::initialize()
43{
44 TRACE_EVENT0("gpu", "initializeCompiler");
45#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
46 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
Jamie Madill07d49ef2014-07-25 11:52:38 -040047 static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
Geoff Langdad5ed32014-02-10 12:59:17 -050048
49 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
50 {
Jamie Madill07d49ef2014-07-25 11:52:38 -040051 if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
Geoff Langdad5ed32014-02-10 12:59:17 -050052 {
53 break;
54 }
55 }
56#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
57
58 if (!mD3DCompilerModule)
59 {
60 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
61 mD3DCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
62 }
63
64 if (!mD3DCompilerModule)
65 {
66 ERR("No D3D compiler module found - aborting!\n");
67 return false;
68 }
69
Geoff Lang3935e512014-09-23 13:15:19 -040070 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
Geoff Langdad5ed32014-02-10 12:59:17 -050071 ASSERT(mD3DCompileFunc);
72
Tibor den Ouden97049c62014-10-06 21:39:16 +020073 mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble"));
74 ASSERT(mD3DDisassembleFunc);
75
Geoff Langdad5ed32014-02-10 12:59:17 -050076 return mD3DCompileFunc != NULL;
77}
78
79void HLSLCompiler::release()
80{
81 if (mD3DCompilerModule)
82 {
83 FreeLibrary(mD3DCompilerModule);
84 mD3DCompilerModule = NULL;
85 mD3DCompileFunc = NULL;
Tibor den Ouden97049c62014-10-06 21:39:16 +020086 mD3DDisassembleFunc = NULL;
Geoff Langdad5ed32014-02-10 12:59:17 -050087 }
88}
89
Geoff Langb543aff2014-09-30 14:52:54 -040090gl::Error HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const std::string &hlsl, const std::string &profile,
Nicolas Capens5e0c80a2014-10-10 10:11:54 -040091 const std::vector<CompileConfig> &configs, const D3D_SHADER_MACRO *overrideMacros,
Tibor den Ouden97049c62014-10-06 21:39:16 +020092 ID3DBlob **outCompiledBlob, std::string *outDebugInfo) const
Geoff Langdad5ed32014-02-10 12:59:17 -050093{
Jamie Madille020bed2014-10-20 16:16:46 +000094 ASSERT(mD3DCompilerModule && mD3DCompileFunc);
Geoff Langdad5ed32014-02-10 12:59:17 -050095
Geoff Langf7ed7052014-09-23 13:39:31 -040096 if (gl::perfActive())
97 {
98 std::string sourcePath = getTempPath();
99 std::string sourceText = FormatString("#line 2 \"%s\"\n\n%s", sourcePath.c_str(), hlsl.c_str());
100 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
101 }
102
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400103 const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : NULL;
104
Geoff Lang3935e512014-09-23 13:15:19 -0400105 for (size_t i = 0; i < configs.size(); ++i)
Geoff Langdad5ed32014-02-10 12:59:17 -0500106 {
107 ID3DBlob *errorMessage = NULL;
108 ID3DBlob *binary = NULL;
109
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400110 HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, NULL, "main", profile.c_str(),
Geoff Lang3935e512014-09-23 13:15:19 -0400111 configs[i].flags, 0, &binary, &errorMessage);
Nicolas Capens93faad92014-05-10 12:14:13 -0400112
Geoff Langdad5ed32014-02-10 12:59:17 -0500113 if (errorMessage)
114 {
Tibor den Ouden97049c62014-10-06 21:39:16 +0200115 std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer());
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400116 SafeRelease(errorMessage);
Geoff Langdad5ed32014-02-10 12:59:17 -0500117
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400118 infoLog.appendSanitized(message.c_str());
Tibor den Ouden97049c62014-10-06 21:39:16 +0200119 TRACE("\n%s", hlsl.c_str());
120 TRACE("\n%s", message.c_str());
Geoff Langdad5ed32014-02-10 12:59:17 -0500121
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400122 if (message.find("error X3531:") != std::string::npos) // "can't unroll loops marked with loop attribute"
123 {
124 macros = NULL; // Disable [loop] and [flatten]
125
126 // Retry without changing compiler flags
127 i--;
128 continue;
129 }
Geoff Langdad5ed32014-02-10 12:59:17 -0500130 }
131
132 if (SUCCEEDED(result))
133 {
Geoff Langb543aff2014-09-30 14:52:54 -0400134 *outCompiledBlob = binary;
Tibor den Ouden97049c62014-10-06 21:39:16 +0200135
136#ifdef ANGLE_GENERATE_SHADER_DEBUG_INFO
137 (*outDebugInfo) += "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n";
138 (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n";
139 (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n";
140 (*outDebugInfo) += "\n" + disassembleBinary(binary) + "\n// ASSEMBLY END\n";
141#endif
142
Geoff Langb543aff2014-09-30 14:52:54 -0400143 return gl::Error(GL_NO_ERROR);
Geoff Langdad5ed32014-02-10 12:59:17 -0500144 }
145 else
146 {
147 if (result == E_OUTOFMEMORY)
148 {
Geoff Langb543aff2014-09-30 14:52:54 -0400149 *outCompiledBlob = NULL;
150 return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result);
Geoff Langdad5ed32014-02-10 12:59:17 -0500151 }
152
Geoff Lang3935e512014-09-23 13:15:19 -0400153 infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str());
Jamie Madill2c976a42014-08-04 11:37:53 -0400154
Geoff Lang3935e512014-09-23 13:15:19 -0400155 if (i + 1 < configs.size())
Geoff Langdad5ed32014-02-10 12:59:17 -0500156 {
Geoff Lang3935e512014-09-23 13:15:19 -0400157 infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str());
Geoff Langdad5ed32014-02-10 12:59:17 -0500158 }
159 }
160 }
161
Geoff Langb543aff2014-09-30 14:52:54 -0400162 // None of the configurations succeeded in compiling this shader but the compiler is still intact
163 *outCompiledBlob = NULL;
164 return gl::Error(GL_NO_ERROR);
Geoff Langdad5ed32014-02-10 12:59:17 -0500165}
166
Tibor den Ouden97049c62014-10-06 21:39:16 +0200167std::string HLSLCompiler::disassembleBinary(ID3DBlob *shaderBinary) const
168{
169 // Retrieve disassembly
170 UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING;
171 ID3DBlob *disassembly = NULL;
172 pD3DDisassemble disassembleFunc = reinterpret_cast<pD3DDisassemble>(mD3DDisassembleFunc);
173 LPCVOID buffer = shaderBinary->GetBufferPointer();
174 SIZE_T bufSize = shaderBinary->GetBufferSize();
175 HRESULT result = disassembleFunc(buffer, bufSize, flags, "", &disassembly);
176
177 std::string asmSrc;
178 if (SUCCEEDED(result))
179 {
180 asmSrc = reinterpret_cast<const char*>(disassembly->GetBufferPointer());
181 }
182
183 SafeRelease(disassembly);
184
185 return asmSrc;
186}
187
Geoff Langdad5ed32014-02-10 12:59:17 -0500188}