blob: fd2af1eb41fa765d99528221317edef1df1e961b [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +00002//
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +00004// 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
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +000010#include <EGL/eglext.h>
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"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000016#include "common/utilities.h"
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000017
18#if !defined(ANGLE_ENABLE_D3D11)
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +000019// Enables use of the Direct3D 11 API for a default display, when available
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000020#define ANGLE_ENABLE_D3D11 0
21#endif
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000022
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000023namespace rx
24{
25
26Renderer::Renderer(egl::Display *display) : mDisplay(display)
27{
28 mD3dCompilerModule = NULL;
29 mD3DCompileFunc = NULL;
shannonwoods@chromium.orge9438a52013-05-30 00:09:25 +000030 mCurrentClientVersion = 2;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000031}
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
apatrick@chromium.org25b5f1d2013-03-01 00:59:21 +000048 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000049 {
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
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +000066 mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000067 ASSERT(mD3DCompileFunc);
68
69 return mD3DCompileFunc != NULL;
70}
71
72// Compiles HLSL code into executable binaries
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000073ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000074{
75 if (!hlsl)
76 {
77 return NULL;
78 }
79
80 HRESULT result = S_OK;
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +000081 UINT flags = 0;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000082 std::string sourceText;
83 if (gl::perfActive())
84 {
85 flags |= D3DCOMPILE_DEBUG;
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000086
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000087#ifdef NDEBUG
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000088 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000089#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 {
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000099 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000100 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
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000119 int attempts = alternateFlags ? ArraySize(extraFlags) : 1;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000120 pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000121 for (int i = 0; i < attempts; ++i)
122 {
123 ID3DBlob *errorMessage = NULL;
124 ID3DBlob *binary = NULL;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000125
126 result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL,
127 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000128 if (errorMessage)
129 {
130 const char *message = (const char*)errorMessage->GetBufferPointer();
131
132 infoLog.appendSanitized(message);
133 TRACE("\n%s", hlsl);
134 TRACE("\n%s", message);
135
136 errorMessage->Release();
137 errorMessage = NULL;
138 }
139
140 if (SUCCEEDED(result))
141 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000142 return (ShaderBlob*)binary;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000143 }
144 else
145 {
146 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
147 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000148 return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000149 }
150
151 infoLog.append("Warning: D3D shader compilation failed with ");
152 infoLog.append(extraFlagNames[i]);
153 infoLog.append(" flags.");
154 if (i + 1 < attempts)
155 {
156 infoLog.append(" Retrying with ");
157 infoLog.append(extraFlagNames[i + 1]);
158 infoLog.append(".\n");
159 }
160 }
161 }
162
163 return NULL;
164}
165
166}
167
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000168extern "C"
169{
170
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000171rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId)
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000172{
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000173 rx::Renderer *renderer = NULL;
174 EGLint status = EGL_BAD_ALLOC;
175
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000176 if (ANGLE_ENABLE_D3D11 ||
177 displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE ||
178 displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000179 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000180 renderer = new rx::Renderer11(display, hDc);
181
182 if (renderer)
183 {
184 status = renderer->initialize();
185 }
186
187 if (status == EGL_SUCCESS)
188 {
189 return renderer;
190 }
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000191 else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
192 {
193 return NULL;
194 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000195
196 // Failed to create a D3D11 renderer, try creating a D3D9 renderer
197 delete renderer;
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000198 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000199
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000200 bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000201 renderer = new rx::Renderer9(display, hDc, softwareDevice);
202
203 if (renderer)
204 {
205 status = renderer->initialize();
206 }
207
208 if (status == EGL_SUCCESS)
209 {
210 return renderer;
211 }
212
213 return NULL;
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000214}
215
216void glDestroyRenderer(rx::Renderer *renderer)
217{
218 delete renderer;
219}
220
221}