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