blob: 6118fa8678f4ca1aa45fb588eab3ba760f5816f9 [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"
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)
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 +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;
shannonwoods@chromium.orge9438a52013-05-30 00:09:25 +000034 mCurrentClientVersion = 2;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000035}
36
37Renderer::~Renderer()
38{
39 if (mD3dCompilerModule)
40 {
41 FreeLibrary(mD3dCompilerModule);
42 mD3dCompilerModule = NULL;
43 }
44}
45
46bool Renderer::initializeCompiler()
47{
48#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
49 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
50 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
51
apatrick@chromium.org25b5f1d2013-03-01 00:59:21 +000052 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000053 {
54 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
55 {
56 break;
57 }
58 }
59#else
60 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
61 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
62#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
63
64 if (!mD3dCompilerModule)
65 {
66 ERR("No D3D compiler module found - aborting!\n");
67 return false;
68 }
69
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +000070 mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000071 ASSERT(mD3DCompileFunc);
72
73 return mD3DCompileFunc != NULL;
74}
75
76// Compiles HLSL code into executable binaries
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000077ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000078{
79 if (!hlsl)
80 {
81 return NULL;
82 }
83
84 HRESULT result = S_OK;
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +000085 UINT flags = 0;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000086 std::string sourceText;
87 if (gl::perfActive())
88 {
89 flags |= D3DCOMPILE_DEBUG;
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000090
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000091#ifdef NDEBUG
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000092 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000093#else
94 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
95#endif
96
97 std::string sourcePath = getTempPath();
98 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
99 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
100 }
101 else
102 {
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +0000103 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000104 sourceText = hlsl;
105 }
106
107 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
108 // Try the default flags first and if compilation fails, try some alternatives.
109 const static UINT extraFlags[] =
110 {
111 0,
112 D3DCOMPILE_AVOID_FLOW_CONTROL,
113 D3DCOMPILE_PREFER_FLOW_CONTROL
114 };
115
116 const static char * const extraFlagNames[] =
117 {
118 "default",
119 "avoid flow control",
120 "prefer flow control"
121 };
122
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000123 int attempts = alternateFlags ? ArraySize(extraFlags) : 1;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000124 pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000125 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
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000175rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId)
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000176{
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000177 rx::Renderer *renderer = NULL;
178 EGLint status = EGL_BAD_ALLOC;
179
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000180 if (ANGLE_ENABLE_D3D11 ||
181 displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE ||
182 displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000183 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000184 renderer = new rx::Renderer11(display, hDc);
185
186 if (renderer)
187 {
188 status = renderer->initialize();
189 }
190
191 if (status == EGL_SUCCESS)
192 {
193 return renderer;
194 }
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000195 else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
196 {
197 return NULL;
198 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000199
200 // Failed to create a D3D11 renderer, try creating a D3D9 renderer
201 delete renderer;
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000202 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000203
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000204 bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000205 renderer = new rx::Renderer9(display, hDc, softwareDevice);
206
207 if (renderer)
208 {
209 status = renderer->initialize();
210 }
211
212 if (status == EGL_SUCCESS)
213 {
214 return renderer;
215 }
216
217 return NULL;
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000218}
219
220void glDestroyRenderer(rx::Renderer *renderer)
221{
222 delete renderer;
223}
224
225}