gl::Error HLSLCompiler::compileToBinary(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) const { #if !defined(ANGLE_ENABLE_WINDOWS_STORE) ASSERT(mD3DCompilerModule); #endif ASSERT(mD3DCompileFunc); #if !defined(ANGLE_ENABLE_WINDOWS_STORE) if (gl::perfActive()) { std::string sourcePath = getTempPath(); std::string sourceText = FormatString("#line 2 \"%s\"\n\n%s", sourcePath.c_str(), hlsl.c_str()); writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); } #endif const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : NULL; for (size_t i = 0; i < configs.size(); ++i) { ID3DBlob *errorMessage = NULL; ID3DBlob *binary = NULL; HRESULT result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, NULL, "main", profile.c_str(), configs[i].flags, 0, &binary, &errorMessage); if (errorMessage) { std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer()); SafeRelease(errorMessage); infoLog.appendSanitized(message.c_str()); TRACE("\n%s", hlsl.c_str()); TRACE("\n%s", message.c_str()); if (message.find("error X3531:") != std::string::npos) // "can't unroll loops marked with loop attribute" { macros = NULL; // Disable [loop] and [flatten] // Retry without changing compiler flags i--; continue; } } if (SUCCEEDED(result)) { *outCompiledBlob = binary; #if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED (*outDebugInfo) += "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n"; (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n"; (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n"; for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx) { const char *flagName = GetCompilerFlagName(configs[i].flags, fIx); if (flagName != nullptr) { (*outDebugInfo) += std::string("// ") + flagName + "\n"; } } (*outDebugInfo) += "// Macros:\n"; if (macros == nullptr) { (*outDebugInfo) += "// - : -\n"; } else { for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt) { (*outDebugInfo) += std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n"; } } (*outDebugInfo) += "\n" + disassembleBinary(binary) + "\n// ASSEMBLY END\n"; #endif return gl::Error(GL_NO_ERROR); } else { if (result == E_OUTOFMEMORY) { *outCompiledBlob = NULL; return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result); } infoLog.append("Warning: D3D shader compilation failed with %s flags.", configs[i].name.c_str()); if (i + 1 < configs.size()) { infoLog.append(" Retrying with %s.\n", configs[i + 1].name.c_str()); } } } // None of the configurations succeeded in compiling this shader but the compiler is still intact *outCompiledBlob = NULL; return gl::Error(GL_NO_ERROR); }
gl::Error HLSLCompiler::compileToBinary(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) { ASSERT(mInitialized); #if !defined(ANGLE_ENABLE_WINDOWS_STORE) ASSERT(mD3DCompilerModule); #endif ASSERT(mD3DCompileFunc); #if !defined(ANGLE_ENABLE_WINDOWS_STORE) if (gl::DebugAnnotationsActive()) { std::string sourcePath = getTempPath(); std::string sourceText = FormatString("#line 2 \"%s\"\n\n%s", sourcePath.c_str(), hlsl.c_str()); writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); } #endif const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : nullptr; for (size_t i = 0; i < configs.size(); ++i) { ID3DBlob *errorMessage = nullptr; ID3DBlob *binary = nullptr; HRESULT result = S_OK; { TRACE_EVENT0("gpu.angle", "D3DCompile"); SCOPED_ANGLE_HISTOGRAM_TIMER("GPU.ANGLE.D3DCompileMS"); result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, nullptr, "main", profile.c_str(), configs[i].flags, 0, &binary, &errorMessage); } if (errorMessage) { std::string message = reinterpret_cast<const char*>(errorMessage->GetBufferPointer()); SafeRelease(errorMessage); infoLog.appendSanitized(message.c_str()); TRACE("\n%s", hlsl.c_str()); TRACE("\n%s", message.c_str()); if ((message.find("error X3531:") != std::string::npos || // "can't unroll loops marked with loop attribute" message.find("error X4014:") != std::string::npos) && // "cannot have gradient operations inside loops with divergent flow control", // even though it is counter-intuitive to disable unrolling for this error, // some very long shaders have trouble deciding which loops to unroll and // turning off forced unrolls allows them to compile properly. macros != nullptr) { macros = nullptr; // Disable [loop] and [flatten] // Retry without changing compiler flags i--; continue; } } if (SUCCEEDED(result)) { *outCompiledBlob = binary; (*outDebugInfo) += "// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n"; #if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO == ANGLE_ENABLED (*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n"; (*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n"; for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx) { if (IsCompilerFlagSet(configs[i].flags, CompilerFlagInfos[fIx].mFlag)) { (*outDebugInfo) += std::string("// ") + CompilerFlagInfos[fIx].mName + "\n"; } } (*outDebugInfo) += "// Macros:\n"; if (macros == nullptr) { (*outDebugInfo) += "// - : -\n"; } else { for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt) { (*outDebugInfo) += std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n"; } } std::string disassembly; ANGLE_TRY(disassembleBinary(binary, &disassembly)); (*outDebugInfo) += "\n" + disassembly + "\n// ASSEMBLY END\n"; #endif // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO == ANGLE_ENABLED return gl::NoError(); } if (result == E_OUTOFMEMORY) { *outCompiledBlob = nullptr; return gl::Error(GL_OUT_OF_MEMORY, "HLSL compiler had an unexpected failure, result: 0x%X.", result); } infoLog << "Warning: D3D shader compilation failed with " << configs[i].name << " flags. (" << profile << ")"; if (i + 1 < configs.size()) { infoLog << " Retrying with " << configs[i + 1].name; } } // None of the configurations succeeded in compiling this shader but the compiler is still intact *outCompiledBlob = nullptr; return gl::NoError(); }