Move HLSL compilation to Renderer.

TRAC #22205
Signed-off-by: Daniel Koch
Signed-off-by: Geoff Lang
Author: Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1546 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/renderer/Renderer11.cpp b/src/libGLESv2/renderer/Renderer11.cpp
index 8831ae5..8045b98 100644
--- a/src/libGLESv2/renderer/Renderer11.cpp
+++ b/src/libGLESv2/renderer/Renderer11.cpp
@@ -23,10 +23,6 @@
 #include "libEGL/Config.h"
 #include "libEGL/Display.h"
 
-#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
-#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
-#endif
-
 namespace rx
 {
 static const DXGI_FORMAT RenderTargetFormats[] =
@@ -43,7 +39,6 @@
 {
     mD3d11Module = NULL;
     mDxgiModule = NULL;
-    mD3dCompilerModule = NULL;
 
     mDeviceLost = false;
 
@@ -92,12 +87,6 @@
         FreeLibrary(mDxgiModule);
         mDxgiModule = NULL;
     }
-
-    if (mD3dCompilerModule)
-    {
-        FreeLibrary(mD3dCompilerModule);
-        mD3dCompilerModule = NULL;
-    }
 }
 
 Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
@@ -108,6 +97,11 @@
 
 EGLint Renderer11::initialize()
 {
+    if (!initializeCompiler())
+    {
+        return EGL_NOT_INITIALIZED;
+    }
+
     mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
     mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
 
@@ -184,31 +178,6 @@
         return EGL_NOT_INITIALIZED;
     }
 
-#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
-    // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
-    static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
-
-    for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
-    {
-        if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
-        {
-            break;
-        }
-    }
-#else
-    // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
-    mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
-#endif  // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
-
-    if (!mD3dCompilerModule)
-    {
-        terminate();
-        return false;
-    }
-
-    mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
-    ASSERT(mD3DCompileFunc);
-
     initializeDevice();
 
     return EGL_SUCCESS;
@@ -1244,7 +1213,7 @@
         return NULL;
     }
 
-    ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile);
+    ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false);
     if (!binary)
         return NULL;
 
@@ -1254,96 +1223,6 @@
     return executable;
 }
 
-// Compiles the HLSL code of the attached shaders into executable binaries
-ID3DBlob *Renderer11::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile)
-{
-    if (!hlsl)
-    {
-        return NULL;
-    }
-
-    HRESULT result = S_OK;
-    UINT flags = 0;
-    std::string sourceText;
-    if (gl::perfActive())
-    {
-        flags |= D3DCOMPILE_DEBUG;
-#ifdef NDEBUG
-        flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
-#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 |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
-        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"
-    };
-
-    for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i)
-    {
-        ID3DBlob *errorMessage = NULL;
-        ID3DBlob *binary = NULL;
-        result = mD3DCompileFunc(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 binary;
-        }
-        else
-        {
-            if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
-            {
-                return error(GL_OUT_OF_MEMORY, (ID3DBlob*) NULL);
-            }
-
-            infoLog.append("Warning: D3D shader compilation failed with ");
-            infoLog.append(extraFlagNames[i]);
-            infoLog.append(" flags.");
-            if (i + 1 < sizeof(extraFlagNames) / sizeof(char*))
-            {
-                infoLog.append(" Retrying with ");
-                infoLog.append(extraFlagNames[i + 1]);
-                infoLog.append(".\n");
-            }
-        }
-    }
-
-    return NULL;
-}
-
 bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
                           bool blitRenderTarget, bool blitDepthStencil)
 {