ShaderBlob *HLSLCompiler::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, const UINT optimizationFlags[], const char *flagNames[], int attempts) const { ASSERT(mD3DCompilerModule && mD3DCompileFunc); if (!hlsl) { return NULL; } pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc); for (int i = 0; i < attempts; ++i) { ID3DBlob *errorMessage = NULL; ID3DBlob *binary = NULL; HRESULT result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, "main", profile, optimizationFlags[i], 0, &binary, &errorMessage); if (errorMessage) { const char *message = (const char*)errorMessage->GetBufferPointer(); infoLog.appendSanitized(message); TRACE("\n%s", hlsl); TRACE("\n%s", message); SafeRelease(errorMessage); } if (SUCCEEDED(result)) { return (ShaderBlob*)binary; } else { if (result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*)NULL); } infoLog.append("Warning: D3D shader compilation failed with "); infoLog.append(flagNames[i]); infoLog.append(" flags."); if (i + 1 < attempts) { infoLog.append(" Retrying with "); infoLog.append(flagNames[i + 1]); infoLog.append(".\n"); } } } return NULL; }
// Compiles HLSL code into executable binaries ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags) { if (!hlsl) { return NULL; } HRESULT result = S_OK; UINT flags = 0; std::string sourceText; if (gl::perfActive()) { flags |= D3DCOMPILE_DEBUG; #ifdef NDEBUG flags |= optimizationFlags; #else flags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif std::string sourcePath = getTempPath(); sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); } else { flags |= optimizationFlags; sourceText = hlsl; } // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. // Try the default flags first and if compilation fails, try some alternatives. const static UINT extraFlags[] = { 0, D3DCOMPILE_AVOID_FLOW_CONTROL, D3DCOMPILE_PREFER_FLOW_CONTROL }; const static char * const extraFlagNames[] = { "default", "avoid flow control", "prefer flow control" }; int attempts = alternateFlags ? ArraySize(extraFlags) : 1; pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc); for (int i = 0; i < attempts; ++i) { ID3DBlob *errorMessage = NULL; ID3DBlob *binary = NULL; result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage); if (errorMessage) { const char *message = (const char*)errorMessage->GetBufferPointer(); infoLog.appendSanitized(message); TRACE("\n%s", hlsl); TRACE("\n%s", message); errorMessage->Release(); errorMessage = NULL; } if (SUCCEEDED(result)) { return (ShaderBlob*)binary; } else { #if defined(ANGLE_PLATFORM_WINRT) if (result == E_OUTOFMEMORY) #else if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) #endif // #if defined(ANGLE_PLATFORM_WINRT) { return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL); } infoLog.append("Warning: D3D shader compilation failed with "); infoLog.append(extraFlagNames[i]); infoLog.append(" flags."); if (i + 1 < attempts) { infoLog.append(" Retrying with "); infoLog.append(extraFlagNames[i + 1]); infoLog.append(".\n"); } } } return NULL; }