blob: 69330aea58d9d029e3ec8b1f6f54363afbbcc384 [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"
Al Patrick3c6344e2013-09-23 14:57:00 -040017#include "third_party/trace_event/trace_event.h"
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000018
19#if !defined(ANGLE_ENABLE_D3D11)
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +000020// Enables use of the Direct3D 11 API for a default display, when available
daniel@transgaming.comc1e26342012-11-28 19:31:16 +000021#define ANGLE_ENABLE_D3D11 0
22#endif
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +000023
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000024namespace rx
25{
26
27Renderer::Renderer(egl::Display *display) : mDisplay(display)
28{
29 mD3dCompilerModule = NULL;
30 mD3DCompileFunc = NULL;
shannonwoods@chromium.orge9438a52013-05-30 00:09:25 +000031 mCurrentClientVersion = 2;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000032}
33
34Renderer::~Renderer()
35{
36 if (mD3dCompilerModule)
37 {
38 FreeLibrary(mD3dCompilerModule);
39 mD3dCompilerModule = NULL;
40 }
41}
42
43bool Renderer::initializeCompiler()
44{
Al Patrick3c6344e2013-09-23 14:57:00 -040045 TRACE_EVENT0("gpu", "initializeCompiler");
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000046#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
47 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
48 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
49
apatrick@chromium.org25b5f1d2013-03-01 00:59:21 +000050 for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000051 {
52 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
53 {
54 break;
55 }
56 }
57#else
58 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
59 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
60#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
61
62 if (!mD3dCompilerModule)
63 {
64 ERR("No D3D compiler module found - aborting!\n");
65 return false;
66 }
67
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +000068 mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000069 ASSERT(mD3DCompileFunc);
70
71 return mD3DCompileFunc != NULL;
72}
73
74// Compiles HLSL code into executable binaries
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000075ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags)
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000076{
77 if (!hlsl)
78 {
79 return NULL;
80 }
81
82 HRESULT result = S_OK;
shannon.woods@transgaming.com45262362013-01-25 21:51:27 +000083 UINT flags = 0;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000084 std::string sourceText;
85 if (gl::perfActive())
86 {
87 flags |= D3DCOMPILE_DEBUG;
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000088
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000089#ifdef NDEBUG
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000090 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +000091#else
92 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
93#endif
94
95 std::string sourcePath = getTempPath();
96 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
97 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
98 }
99 else
100 {
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +0000101 flags |= optimizationFlags;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000102 sourceText = hlsl;
103 }
104
105 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
106 // Try the default flags first and if compilation fails, try some alternatives.
107 const static UINT extraFlags[] =
108 {
109 0,
110 D3DCOMPILE_AVOID_FLOW_CONTROL,
111 D3DCOMPILE_PREFER_FLOW_CONTROL
112 };
113
114 const static char * const extraFlagNames[] =
115 {
116 "default",
117 "avoid flow control",
118 "prefer flow control"
119 };
120
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000121 int attempts = alternateFlags ? ArraySize(extraFlags) : 1;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000122 pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000123 for (int i = 0; i < attempts; ++i)
124 {
125 ID3DBlob *errorMessage = NULL;
126 ID3DBlob *binary = NULL;
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000127
128 result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL,
129 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000130 if (errorMessage)
131 {
132 const char *message = (const char*)errorMessage->GetBufferPointer();
133
134 infoLog.appendSanitized(message);
135 TRACE("\n%s", hlsl);
136 TRACE("\n%s", message);
137
Geoff Langea228632013-07-30 15:17:12 -0400138 SafeRelease(errorMessage);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000139 }
140
141 if (SUCCEEDED(result))
142 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000143 return (ShaderBlob*)binary;
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000144 }
145 else
146 {
147 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
148 {
shannon.woods@transgaming.com4e91d562013-02-28 23:12:09 +0000149 return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL);
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000150 }
151
152 infoLog.append("Warning: D3D shader compilation failed with ");
153 infoLog.append(extraFlagNames[i]);
154 infoLog.append(" flags.");
155 if (i + 1 < attempts)
156 {
157 infoLog.append(" Retrying with ");
158 infoLog.append(extraFlagNames[i + 1]);
159 infoLog.append(".\n");
160 }
161 }
162 }
163
164 return NULL;
165}
166
167}
168
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000169extern "C"
170{
171
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000172rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId)
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000173{
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000174 rx::Renderer *renderer = NULL;
175 EGLint status = EGL_BAD_ALLOC;
176
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000177 if (ANGLE_ENABLE_D3D11 ||
178 displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE ||
179 displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000180 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000181 renderer = new rx::Renderer11(display, hDc);
182
183 if (renderer)
184 {
185 status = renderer->initialize();
186 }
187
188 if (status == EGL_SUCCESS)
189 {
190 return renderer;
191 }
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000192 else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE)
193 {
194 return NULL;
195 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000196
197 // Failed to create a D3D11 renderer, try creating a D3D9 renderer
198 delete renderer;
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000199 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000200
shannonwoods@chromium.orgeff3a122013-05-30 00:10:04 +0000201 bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000202 renderer = new rx::Renderer9(display, hDc, softwareDevice);
203
204 if (renderer)
205 {
206 status = renderer->initialize();
207 }
208
209 if (status == EGL_SUCCESS)
210 {
211 return renderer;
212 }
213
214 return NULL;
daniel@transgaming.comae4f4d42012-11-28 19:31:06 +0000215}
216
217void glDestroyRenderer(rx::Renderer *renderer)
218{
219 delete renderer;
220}
221
222}