blob: 4de66db5fcd2e452b66ea4c5251b7d6cb1d9ef9d [file] [log] [blame]
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +00001//
2// Copyright (c) 2012 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
7// Renderer.cpp: Implements EGL dependencies for creating and destroying Renderer instances.
8
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00009#include "libGLESv2/main.h"
10#include "libGLESv2/Program.h"
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000011#include "libGLESv2/renderer/Renderer.h"
12#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000013#include "libGLESv2/renderer/Renderer11.h"
14
15#if !defined(ANGLE_ENABLE_D3D11)
16// Enables use of the Direct3D 11 API, when available
17#define ANGLE_ENABLE_D3D11 0
18#endif
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000019
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000020#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
21#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
22#endif
23
24namespace rx
25{
26
27Renderer::Renderer(egl::Display *display) : mDisplay(display)
28{
29 mD3dCompilerModule = NULL;
30 mD3DCompileFunc = NULL;
31}
32
33Renderer::~Renderer()
34{
35 if (mD3dCompilerModule)
36 {
37 FreeLibrary(mD3dCompilerModule);
38 mD3dCompilerModule = NULL;
39 }
40}
41
42bool Renderer::initializeCompiler()
43{
44#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
45 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
46 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
47
48 for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
49 {
50 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
51 {
52 break;
53 }
54 }
55#else
56 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
57 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
58#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
59
60 if (!mD3dCompilerModule)
61 {
62 ERR("No D3D compiler module found - aborting!\n");
63 return false;
64 }
65
66 mD3DCompileFunc = reinterpret_cast<pD3DCompile>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
67 ASSERT(mD3DCompileFunc);
68
69 return mD3DCompileFunc != NULL;
70}
71
72// Compiles HLSL code into executable binaries
73ID3DBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, bool alternateFlags)
74{
75 if (!hlsl)
76 {
77 return NULL;
78 }
79
80 HRESULT result = S_OK;
daniel@transgaming.comda8d3802012-12-20 21:12:55 +000081 UINT flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY; // Enables using legacy sampler syntax for Shader Model 4+
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000082 std::string sourceText;
83 if (gl::perfActive())
84 {
85 flags |= D3DCOMPILE_DEBUG;
86#ifdef NDEBUG
87 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
88#else
89 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
90#endif
91
92 std::string sourcePath = getTempPath();
93 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
94 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
95 }
96 else
97 {
98 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
99 sourceText = hlsl;
100 }
101
102 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
103 // Try the default flags first and if compilation fails, try some alternatives.
104 const static UINT extraFlags[] =
105 {
106 0,
107 D3DCOMPILE_AVOID_FLOW_CONTROL,
108 D3DCOMPILE_PREFER_FLOW_CONTROL
109 };
110
111 const static char * const extraFlagNames[] =
112 {
113 "default",
114 "avoid flow control",
115 "prefer flow control"
116 };
117
118 int attempts = (alternateFlags ? sizeof(extraFlags) / sizeof(UINT) : 1);
119
120 for (int i = 0; i < attempts; ++i)
121 {
122 ID3DBlob *errorMessage = NULL;
123 ID3DBlob *binary = NULL;
124 result = mD3DCompileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL,
125 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
126 if (errorMessage)
127 {
128 const char *message = (const char*)errorMessage->GetBufferPointer();
129
130 infoLog.appendSanitized(message);
131 TRACE("\n%s", hlsl);
132 TRACE("\n%s", message);
133
134 errorMessage->Release();
135 errorMessage = NULL;
136 }
137
138 if (SUCCEEDED(result))
139 {
140 return binary;
141 }
142 else
143 {
144 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
145 {
146 return error(GL_OUT_OF_MEMORY, (ID3DBlob*) NULL);
147 }
148
149 infoLog.append("Warning: D3D shader compilation failed with ");
150 infoLog.append(extraFlagNames[i]);
151 infoLog.append(" flags.");
152 if (i + 1 < attempts)
153 {
154 infoLog.append(" Retrying with ");
155 infoLog.append(extraFlagNames[i + 1]);
156 infoLog.append(".\n");
157 }
158 }
159 }
160
161 return NULL;
162}
163
164}
165
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000166extern "C"
167{
168
169rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, bool softwareDevice)
170{
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000171 rx::Renderer *renderer = NULL;
172 EGLint status = EGL_BAD_ALLOC;
173
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000174 if (ANGLE_ENABLE_D3D11)
175 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000176 renderer = new rx::Renderer11(display, hDc);
177
178 if (renderer)
179 {
180 status = renderer->initialize();
181 }
182
183 if (status == EGL_SUCCESS)
184 {
185 return renderer;
186 }
187
188 // Failed to create a D3D11 renderer, try creating a D3D9 renderer
189 delete renderer;
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000190 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000191
192 renderer = new rx::Renderer9(display, hDc, softwareDevice);
193
194 if (renderer)
195 {
196 status = renderer->initialize();
197 }
198
199 if (status == EGL_SUCCESS)
200 {
201 return renderer;
202 }
203
204 return NULL;
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000205}
206
207void glDestroyRenderer(rx::Renderer *renderer)
208{
209 delete renderer;
210}
211
212}