|
1 #include "precompiled.h" |
|
2 // |
|
3 // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. |
|
4 // Use of this source code is governed by a BSD-style license that can be |
|
5 // found in the LICENSE file. |
|
6 // |
|
7 |
|
8 // Renderer.cpp: Implements EGL dependencies for creating and destroying Renderer instances. |
|
9 |
|
10 #include <EGL/eglext.h> |
|
11 #include "libGLESv2/main.h" |
|
12 #include "libGLESv2/Program.h" |
|
13 #include "libGLESv2/renderer/Renderer.h" |
|
14 #include "libGLESv2/renderer/Renderer9.h" |
|
15 #include "libGLESv2/renderer/Renderer11.h" |
|
16 #include "libGLESv2/utilities.h" |
|
17 |
|
18 #if !defined(ANGLE_ENABLE_D3D11) |
|
19 // Enables use of the Direct3D 11 API for a default display, when available |
|
20 #define ANGLE_ENABLE_D3D11 0 |
|
21 #endif |
|
22 |
|
23 #define ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES \ |
|
24 { \ |
|
25 TEXT("d3dcompiler_47.dll"), \ |
|
26 TEXT("d3dcompiler_46.dll"), \ |
|
27 TEXT("d3dcompiler_43.dll") \ |
|
28 } |
|
29 |
|
30 |
|
31 namespace rx |
|
32 { |
|
33 |
|
34 Renderer::Renderer(egl::Display *display) : mDisplay(display) |
|
35 { |
|
36 mD3dCompilerModule = NULL; |
|
37 mD3DCompileFunc = NULL; |
|
38 } |
|
39 |
|
40 Renderer::~Renderer() |
|
41 { |
|
42 if (mD3dCompilerModule) |
|
43 { |
|
44 FreeLibrary(mD3dCompilerModule); |
|
45 mD3dCompilerModule = NULL; |
|
46 } |
|
47 } |
|
48 |
|
49 bool Renderer::initializeCompiler() |
|
50 { |
|
51 #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) |
|
52 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. |
|
53 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; |
|
54 |
|
55 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) |
|
56 { |
|
57 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule)) |
|
58 { |
|
59 break; |
|
60 } |
|
61 } |
|
62 #else |
|
63 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. |
|
64 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL); |
|
65 #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES |
|
66 |
|
67 if (!mD3dCompilerModule) |
|
68 { |
|
69 ERR("No D3D compiler module found - aborting!\n"); |
|
70 return false; |
|
71 } |
|
72 |
|
73 mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile")); |
|
74 ASSERT(mD3DCompileFunc); |
|
75 |
|
76 return mD3DCompileFunc != NULL; |
|
77 } |
|
78 |
|
79 // Compiles HLSL code into executable binaries |
|
80 ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags) |
|
81 { |
|
82 if (!hlsl) |
|
83 { |
|
84 return NULL; |
|
85 } |
|
86 |
|
87 HRESULT result = S_OK; |
|
88 UINT flags = 0; |
|
89 std::string sourceText; |
|
90 if (gl::perfActive()) |
|
91 { |
|
92 flags |= D3DCOMPILE_DEBUG; |
|
93 |
|
94 #ifdef NDEBUG |
|
95 flags |= optimizationFlags; |
|
96 #else |
|
97 flags |= D3DCOMPILE_SKIP_OPTIMIZATION; |
|
98 #endif |
|
99 |
|
100 std::string sourcePath = getTempPath(); |
|
101 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); |
|
102 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); |
|
103 } |
|
104 else |
|
105 { |
|
106 flags |= optimizationFlags; |
|
107 sourceText = hlsl; |
|
108 } |
|
109 |
|
110 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. |
|
111 // Try the default flags first and if compilation fails, try some alternatives. |
|
112 const static UINT extraFlags[] = |
|
113 { |
|
114 0, |
|
115 D3DCOMPILE_AVOID_FLOW_CONTROL, |
|
116 D3DCOMPILE_PREFER_FLOW_CONTROL |
|
117 }; |
|
118 |
|
119 const static char * const extraFlagNames[] = |
|
120 { |
|
121 "default", |
|
122 "avoid flow control", |
|
123 "prefer flow control" |
|
124 }; |
|
125 |
|
126 int attempts = alternateFlags ? ArraySize(extraFlags) : 1; |
|
127 pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc); |
|
128 for (int i = 0; i < attempts; ++i) |
|
129 { |
|
130 ID3DBlob *errorMessage = NULL; |
|
131 ID3DBlob *binary = NULL; |
|
132 |
|
133 result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, |
|
134 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage); |
|
135 if (errorMessage) |
|
136 { |
|
137 const char *message = (const char*)errorMessage->GetBufferPointer(); |
|
138 |
|
139 infoLog.appendSanitized(message); |
|
140 TRACE("\n%s", hlsl); |
|
141 TRACE("\n%s", message); |
|
142 |
|
143 errorMessage->Release(); |
|
144 errorMessage = NULL; |
|
145 } |
|
146 |
|
147 if (SUCCEEDED(result)) |
|
148 { |
|
149 return (ShaderBlob*)binary; |
|
150 } |
|
151 else |
|
152 { |
|
153 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
|
154 { |
|
155 return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL); |
|
156 } |
|
157 |
|
158 infoLog.append("Warning: D3D shader compilation failed with "); |
|
159 infoLog.append(extraFlagNames[i]); |
|
160 infoLog.append(" flags."); |
|
161 if (i + 1 < attempts) |
|
162 { |
|
163 infoLog.append(" Retrying with "); |
|
164 infoLog.append(extraFlagNames[i + 1]); |
|
165 infoLog.append(".\n"); |
|
166 } |
|
167 } |
|
168 } |
|
169 |
|
170 return NULL; |
|
171 } |
|
172 |
|
173 } |
|
174 |
|
175 extern "C" |
|
176 { |
|
177 |
|
178 rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId) |
|
179 { |
|
180 rx::Renderer *renderer = NULL; |
|
181 EGLint status = EGL_BAD_ALLOC; |
|
182 |
|
183 if (ANGLE_ENABLE_D3D11 || |
|
184 displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE || |
|
185 displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) |
|
186 { |
|
187 renderer = new rx::Renderer11(display, hDc); |
|
188 |
|
189 if (renderer) |
|
190 { |
|
191 status = renderer->initialize(); |
|
192 } |
|
193 |
|
194 if (status == EGL_SUCCESS) |
|
195 { |
|
196 return renderer; |
|
197 } |
|
198 else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) |
|
199 { |
|
200 return NULL; |
|
201 } |
|
202 |
|
203 // Failed to create a D3D11 renderer, try creating a D3D9 renderer |
|
204 delete renderer; |
|
205 } |
|
206 |
|
207 bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE); |
|
208 renderer = new rx::Renderer9(display, hDc, softwareDevice); |
|
209 |
|
210 if (renderer) |
|
211 { |
|
212 status = renderer->initialize(); |
|
213 } |
|
214 |
|
215 if (status == EGL_SUCCESS) |
|
216 { |
|
217 return renderer; |
|
218 } |
|
219 |
|
220 return NULL; |
|
221 } |
|
222 |
|
223 void glDestroyRenderer(rx::Renderer *renderer) |
|
224 { |
|
225 delete renderer; |
|
226 } |
|
227 |
|
228 } |