blob: ae94b79e87be44297b23b4412a010be52c50d5c9 [file] [log] [blame]
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +00001//
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +00003// 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
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +00009#include <D3Dcompiler.h>
10
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000011#include "libGLESv2/main.h"
12#include "libGLESv2/Program.h"
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000013#include "libGLESv2/renderer/Renderer.h"
14#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000015#include "libGLESv2/renderer/Renderer11.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000016#include "libGLESv2/utilities.h"
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000017
18#if !defined(ANGLE_ENABLE_D3D11)
19// Enables use of the Direct3D 11 API, when available
20#define ANGLE_ENABLE_D3D11 0
21#endif
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000022
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000023#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
24#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
25#endif
26
27namespace rx
28{
29
30Renderer::Renderer(egl::Display *display) : mDisplay(display)
31{
32 mD3dCompilerModule = NULL;
33 mD3DCompileFunc = NULL;
34}
35
36Renderer::~Renderer()
37{
38 if (mD3dCompilerModule)
39 {
40 FreeLibrary(mD3dCompilerModule);
41 mD3dCompilerModule = NULL;
42 }
43}
44
45bool Renderer::initializeCompiler()
46{
47#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
48 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
49 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
50
51 for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
52 {
53 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
54 {
55 break;
56 }
57 }
58#else
59 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
60 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
61#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
62
63 if (!mD3dCompilerModule)
64 {
65 ERR("No D3D compiler module found - aborting!\n");
66 return false;
67 }
68
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +000069 mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000070 ASSERT(mD3DCompileFunc);
71
72 return mD3DCompileFunc != NULL;
73}
74
75// Compiles HLSL code into executable binaries
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000076ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000077{
78 if (!hlsl)
79 {
80 return NULL;
81 }
82
83 HRESULT result = S_OK;
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +000084 UINT flags = 0;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000085 std::string sourceText;
86 if (gl::perfActive())
87 {
88 flags |= D3DCOMPILE_DEBUG;
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000089
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000090#ifdef NDEBUG
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000091 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000092#else
93 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
94#endif
95
96 std::string sourcePath = getTempPath();
97 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
98 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
99 }
100 else
101 {
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +0000102 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000103 sourceText = hlsl;
104 }
105
106 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
107 // Try the default flags first and if compilation fails, try some alternatives.
108 const static UINT extraFlags[] =
109 {
110 0,
111 D3DCOMPILE_AVOID_FLOW_CONTROL,
112 D3DCOMPILE_PREFER_FLOW_CONTROL
113 };
114
115 const static char * const extraFlagNames[] =
116 {
117 "default",
118 "avoid flow control",
119 "prefer flow control"
120 };
121
122 int attempts = (alternateFlags ? sizeof(extraFlags) / sizeof(UINT) : 1);
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000123 pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000124
125 for (int i = 0; i < attempts; ++i)
126 {
127 ID3DBlob *errorMessage = NULL;
128 ID3DBlob *binary = NULL;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000129
130 result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL,
131 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000132 if (errorMessage)
133 {
134 const char *message = (const char*)errorMessage->GetBufferPointer();
135
136 infoLog.appendSanitized(message);
137 TRACE("\n%s", hlsl);
138 TRACE("\n%s", message);
139
140 errorMessage->Release();
141 errorMessage = NULL;
142 }
143
144 if (SUCCEEDED(result))
145 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000146 return (ShaderBlob*)binary;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000147 }
148 else
149 {
150 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
151 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000152 return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000153 }
154
155 infoLog.append("Warning: D3D shader compilation failed with ");
156 infoLog.append(extraFlagNames[i]);
157 infoLog.append(" flags.");
158 if (i + 1 < attempts)
159 {
160 infoLog.append(" Retrying with ");
161 infoLog.append(extraFlagNames[i + 1]);
162 infoLog.append(".\n");
163 }
164 }
165 }
166
167 return NULL;
168}
169
170}
171
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000172extern "C"
173{
174
175rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, bool softwareDevice)
176{
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000177 rx::Renderer *renderer = NULL;
178 EGLint status = EGL_BAD_ALLOC;
179
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000180 if (ANGLE_ENABLE_D3D11)
181 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000182 renderer = new rx::Renderer11(display, hDc);
183
184 if (renderer)
185 {
186 status = renderer->initialize();
187 }
188
189 if (status == EGL_SUCCESS)
190 {
191 return renderer;
192 }
193
194 // Failed to create a D3D11 renderer, try creating a D3D9 renderer
195 delete renderer;
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000196 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000197
198 renderer = new rx::Renderer9(display, hDc, softwareDevice);
199
200 if (renderer)
201 {
202 status = renderer->initialize();
203 }
204
205 if (status == EGL_SUCCESS)
206 {
207 return renderer;
208 }
209
210 return NULL;
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000211}
212
213void glDestroyRenderer(rx::Renderer *renderer)
214{
215 delete renderer;
216}
217
218}