michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Renderer.cpp: Implements EGL dependencies for creating and destroying Renderer instances. michael@0: michael@0: #include michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/Program.h" michael@0: #include "libGLESv2/renderer/Renderer.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: #include "libGLESv2/renderer/Renderer11.h" michael@0: #include "libGLESv2/utilities.h" michael@0: michael@0: #if !defined(ANGLE_ENABLE_D3D11) michael@0: // Enables use of the Direct3D 11 API for a default display, when available michael@0: #define ANGLE_ENABLE_D3D11 0 michael@0: #endif michael@0: michael@0: #define ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES \ michael@0: { \ michael@0: TEXT("d3dcompiler_47.dll"), \ michael@0: TEXT("d3dcompiler_46.dll"), \ michael@0: TEXT("d3dcompiler_43.dll") \ michael@0: } michael@0: michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: Renderer::Renderer(egl::Display *display) : mDisplay(display) michael@0: { michael@0: mD3dCompilerModule = NULL; michael@0: mD3DCompileFunc = NULL; michael@0: } michael@0: michael@0: Renderer::~Renderer() michael@0: { michael@0: if (mD3dCompilerModule) michael@0: { michael@0: FreeLibrary(mD3dCompilerModule); michael@0: mD3dCompilerModule = NULL; michael@0: } michael@0: } michael@0: michael@0: bool Renderer::initializeCompiler() michael@0: { michael@0: #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) michael@0: // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. michael@0: static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; michael@0: michael@0: for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) michael@0: { michael@0: if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule)) michael@0: { michael@0: break; michael@0: } michael@0: } michael@0: #else michael@0: // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. michael@0: mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL); michael@0: #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES michael@0: michael@0: if (!mD3dCompilerModule) michael@0: { michael@0: ERR("No D3D compiler module found - aborting!\n"); michael@0: return false; michael@0: } michael@0: michael@0: mD3DCompileFunc = reinterpret_cast(GetProcAddress(mD3dCompilerModule, "D3DCompile")); michael@0: ASSERT(mD3DCompileFunc); michael@0: michael@0: return mD3DCompileFunc != NULL; michael@0: } michael@0: michael@0: // Compiles HLSL code into executable binaries michael@0: ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags) michael@0: { michael@0: if (!hlsl) michael@0: { michael@0: return NULL; michael@0: } michael@0: michael@0: HRESULT result = S_OK; michael@0: UINT flags = 0; michael@0: std::string sourceText; michael@0: if (gl::perfActive()) michael@0: { michael@0: flags |= D3DCOMPILE_DEBUG; michael@0: michael@0: #ifdef NDEBUG michael@0: flags |= optimizationFlags; michael@0: #else michael@0: flags |= D3DCOMPILE_SKIP_OPTIMIZATION; michael@0: #endif michael@0: michael@0: std::string sourcePath = getTempPath(); michael@0: sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); michael@0: writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); michael@0: } michael@0: else michael@0: { michael@0: flags |= optimizationFlags; michael@0: sourceText = hlsl; michael@0: } michael@0: michael@0: // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. michael@0: // Try the default flags first and if compilation fails, try some alternatives. michael@0: const static UINT extraFlags[] = michael@0: { michael@0: 0, michael@0: D3DCOMPILE_AVOID_FLOW_CONTROL, michael@0: D3DCOMPILE_PREFER_FLOW_CONTROL michael@0: }; michael@0: michael@0: const static char * const extraFlagNames[] = michael@0: { michael@0: "default", michael@0: "avoid flow control", michael@0: "prefer flow control" michael@0: }; michael@0: michael@0: int attempts = alternateFlags ? ArraySize(extraFlags) : 1; michael@0: pD3DCompile compileFunc = reinterpret_cast(mD3DCompileFunc); michael@0: for (int i = 0; i < attempts; ++i) michael@0: { michael@0: ID3DBlob *errorMessage = NULL; michael@0: ID3DBlob *binary = NULL; michael@0: michael@0: result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, michael@0: "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage); michael@0: if (errorMessage) michael@0: { michael@0: const char *message = (const char*)errorMessage->GetBufferPointer(); michael@0: michael@0: infoLog.appendSanitized(message); michael@0: TRACE("\n%s", hlsl); michael@0: TRACE("\n%s", message); michael@0: michael@0: errorMessage->Release(); michael@0: errorMessage = NULL; michael@0: } michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: return (ShaderBlob*)binary; michael@0: } michael@0: else michael@0: { michael@0: if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) michael@0: { michael@0: return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL); michael@0: } michael@0: michael@0: infoLog.append("Warning: D3D shader compilation failed with "); michael@0: infoLog.append(extraFlagNames[i]); michael@0: infoLog.append(" flags."); michael@0: if (i + 1 < attempts) michael@0: { michael@0: infoLog.append(" Retrying with "); michael@0: infoLog.append(extraFlagNames[i + 1]); michael@0: infoLog.append(".\n"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: } michael@0: michael@0: extern "C" michael@0: { michael@0: michael@0: rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId) michael@0: { michael@0: rx::Renderer *renderer = NULL; michael@0: EGLint status = EGL_BAD_ALLOC; michael@0: michael@0: if (ANGLE_ENABLE_D3D11 || michael@0: displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE || michael@0: displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) michael@0: { michael@0: renderer = new rx::Renderer11(display, hDc); michael@0: michael@0: if (renderer) michael@0: { michael@0: status = renderer->initialize(); michael@0: } michael@0: michael@0: if (status == EGL_SUCCESS) michael@0: { michael@0: return renderer; michael@0: } michael@0: else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) michael@0: { michael@0: return NULL; michael@0: } michael@0: michael@0: // Failed to create a D3D11 renderer, try creating a D3D9 renderer michael@0: delete renderer; michael@0: } michael@0: michael@0: bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE); michael@0: renderer = new rx::Renderer9(display, hDc, softwareDevice); michael@0: michael@0: if (renderer) michael@0: { michael@0: status = renderer->initialize(); michael@0: } michael@0: michael@0: if (status == EGL_SUCCESS) michael@0: { michael@0: return renderer; michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: void glDestroyRenderer(rx::Renderer *renderer) michael@0: { michael@0: delete renderer; michael@0: } michael@0: michael@0: }