Jamie Madill | ea24759 | 2014-08-28 10:37:08 -0400 | [diff] [blame] | 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 | |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 7 | #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 | |
| 15 | namespace rx |
| 16 | { |
| 17 | |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 18 | CompileConfig::CompileConfig() |
| 19 | : flags(0), |
| 20 | name() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | CompileConfig::CompileConfig(UINT flags, const std::string &name) |
| 25 | : flags(flags), |
| 26 | name(name) |
| 27 | { |
| 28 | } |
| 29 | |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 30 | HLSLCompiler::HLSLCompiler() |
| 31 | : mD3DCompilerModule(NULL), |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 32 | mD3DCompileFunc(NULL), |
| 33 | mD3DDisassembleFunc(NULL) |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | HLSLCompiler::~HLSLCompiler() |
| 38 | { |
| 39 | release(); |
| 40 | } |
| 41 | |
| 42 | bool 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 Madill | 07d49ef | 2014-07-25 11:52:38 -0400 | [diff] [blame] | 47 | static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 48 | |
| 49 | for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) |
| 50 | { |
Jamie Madill | 07d49ef | 2014-07-25 11:52:38 -0400 | [diff] [blame] | 51 | if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule)) |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 52 | { |
| 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 Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 70 | mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile")); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 71 | ASSERT(mD3DCompileFunc); |
| 72 | |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 73 | mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble")); |
| 74 | ASSERT(mD3DDisassembleFunc); |
| 75 | |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 76 | return mD3DCompileFunc != NULL; |
| 77 | } |
| 78 | |
| 79 | void HLSLCompiler::release() |
| 80 | { |
| 81 | if (mD3DCompilerModule) |
| 82 | { |
| 83 | FreeLibrary(mD3DCompilerModule); |
| 84 | mD3DCompilerModule = NULL; |
| 85 | mD3DCompileFunc = NULL; |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 86 | mD3DDisassembleFunc = NULL; |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 90 | gl::Error HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const std::string &hlsl, const std::string &profile, |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 91 | const std::vector<CompileConfig> &configs, const D3D_SHADER_MACRO *overrideMacros, |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 92 | ID3DBlob **outCompiledBlob, std::string *outDebugInfo) const |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 93 | { |
Jamie Madill | e020bed | 2014-10-20 16:16:46 +0000 | [diff] [blame] | 94 | ASSERT(mD3DCompilerModule && mD3DCompileFunc); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 95 | |
Geoff Lang | f7ed705 | 2014-09-23 13:39:31 -0400 | [diff] [blame] | 96 | 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 Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 103 | const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : NULL; |
| 104 | |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 105 | for (size_t i = 0; i < configs.size(); ++i) |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 106 | { |
| 107 | ID3DBlob *errorMessage = NULL; |
| 108 | ID3DBlob *binary = NULL; |
| 109 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 110 | HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, NULL, "main", profile.c_str(), |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 111 | configs[i].flags, 0, &binary, &errorMessage); |
Nicolas Capens | 93faad9 | 2014-05-10 12:14:13 -0400 | [diff] [blame] | 112 | |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 113 | if (errorMessage) |
| 114 | { |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 115 | std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer()); |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 116 | SafeRelease(errorMessage); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 117 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 118 | infoLog.appendSanitized(message.c_str()); |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 119 | TRACE("\n%s", hlsl.c_str()); |
| 120 | TRACE("\n%s", message.c_str()); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 121 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 122 | 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 Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (SUCCEEDED(result)) |
| 133 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 134 | *outCompiledBlob = binary; |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 135 | |
| 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 Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 143 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
| 147 | if (result == E_OUTOFMEMORY) |
| 148 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 149 | *outCompiledBlob = NULL; |
| 150 | return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 153 | infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str()); |
Jamie Madill | 2c976a4 | 2014-08-04 11:37:53 -0400 | [diff] [blame] | 154 | |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 155 | if (i + 1 < configs.size()) |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 156 | { |
Geoff Lang | 3935e51 | 2014-09-23 13:15:19 -0400 | [diff] [blame] | 157 | infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str()); |
Geoff Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 162 | // 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 Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 167 | std::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 Lang | dad5ed3 | 2014-02-10 12:59:17 -0500 | [diff] [blame] | 188 | } |