daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | 3b1703f | 2011-05-11 15:36:26 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Display.cpp: Implements the egl::Display class, representing the abstract |
| 8 | // display on which graphics are drawn. Implements EGLDisplay. |
| 9 | // [EGL 1.4] section 2.1.2 page 3. |
| 10 | |
| 11 | #include "libEGL/Display.h" |
| 12 | |
| 13 | #include <algorithm> |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 14 | #include <map> |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "common/debug.h" |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 18 | #include "libGLESv2/mathutil.h" |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 19 | |
| 20 | #include "libEGL/main.h" |
| 21 | |
baustin@google.com | 250f06c | 2011-06-06 16:59:53 +0000 | [diff] [blame] | 22 | // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 23 | #define REF_RAST 0 |
| 24 | |
| 25 | // The "Debug This Pixel..." feature in PIX often fails when using the |
| 26 | // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 |
| 27 | // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. |
| 28 | #if !defined(ANGLE_ENABLE_D3D9EX) |
| 29 | // Enables use of the IDirect3D9Ex interface, when available |
| 30 | #define ANGLE_ENABLE_D3D9EX 1 |
| 31 | #endif // !defined(ANGLE_ENABLE_D3D9EX) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 32 | |
| 33 | namespace egl |
| 34 | { |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 35 | namespace |
| 36 | { |
| 37 | typedef std::map<EGLNativeDisplayType, Display*> DisplayMap; |
| 38 | DisplayMap displays; |
| 39 | } |
| 40 | |
| 41 | egl::Display *Display::getDisplay(EGLNativeDisplayType displayId) |
| 42 | { |
| 43 | if (displays.find(displayId) != displays.end()) |
| 44 | { |
| 45 | return displays[displayId]; |
| 46 | } |
| 47 | |
| 48 | egl::Display *display = NULL; |
| 49 | |
| 50 | if (displayId == EGL_DEFAULT_DISPLAY) |
| 51 | { |
| 52 | display = new egl::Display(displayId, (HDC)NULL, false); |
| 53 | } |
| 54 | else if (displayId == EGL_SOFTWARE_DISPLAY_ANGLE) |
| 55 | { |
| 56 | display = new egl::Display(displayId, (HDC)NULL, true); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | // FIXME: Check if displayId is a valid display device context |
| 61 | |
| 62 | display = new egl::Display(displayId, (HDC)displayId, false); |
| 63 | } |
| 64 | |
| 65 | displays[displayId] = display; |
| 66 | return display; |
| 67 | } |
| 68 | |
| 69 | Display::Display(EGLNativeDisplayType displayId, HDC deviceContext, bool software) : mDc(deviceContext) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 70 | { |
| 71 | mD3d9Module = NULL; |
| 72 | |
| 73 | mD3d9 = NULL; |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 74 | mD3d9Ex = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 75 | mDevice = NULL; |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 76 | mDeviceEx = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 77 | mDeviceWindow = NULL; |
| 78 | |
| 79 | mAdapter = D3DADAPTER_DEFAULT; |
| 80 | |
| 81 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 82 | mDeviceType = D3DDEVTYPE_REF; |
| 83 | #else |
| 84 | mDeviceType = D3DDEVTYPE_HAL; |
| 85 | #endif |
| 86 | |
| 87 | mMinSwapInterval = 1; |
| 88 | mMaxSwapInterval = 1; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 89 | mSoftwareDevice = software; |
| 90 | mDisplayId = displayId; |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 91 | mDeviceLost = false; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Display::~Display() |
| 95 | { |
| 96 | terminate(); |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 97 | |
| 98 | DisplayMap::iterator thisDisplay = displays.find(mDisplayId); |
| 99 | |
| 100 | if (thisDisplay != displays.end()) |
| 101 | { |
| 102 | displays.erase(thisDisplay); |
| 103 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool Display::initialize() |
| 107 | { |
| 108 | if (isInitialized()) |
| 109 | { |
| 110 | return true; |
| 111 | } |
| 112 | |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 113 | if (mSoftwareDevice) |
| 114 | { |
| 115 | mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); |
| 120 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 121 | if (mD3d9Module == NULL) |
| 122 | { |
| 123 | terminate(); |
| 124 | return false; |
| 125 | } |
| 126 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 127 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 128 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 129 | |
| 130 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 131 | // inclined to report a lost context, for example when the user switches |
| 132 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
baustin@google.com | 250f06c | 2011-06-06 16:59:53 +0000 | [diff] [blame] | 133 | if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 134 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 135 | ASSERT(mD3d9Ex); |
| 136 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 137 | ASSERT(mD3d9); |
| 138 | } |
| 139 | else |
| 140 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 141 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (mD3d9) |
| 145 | { |
| 146 | if (mDc != NULL) |
| 147 | { |
| 148 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 149 | } |
| 150 | |
| 151 | HRESULT result; |
| 152 | |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 153 | // Give up on getting device caps after about one second. |
| 154 | for (int i = 0; i < 10; ++i) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 155 | { |
| 156 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 157 | |
| 158 | if (SUCCEEDED(result)) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 159 | { |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | else if (result == D3DERR_NOTAVAILABLE) |
| 163 | { |
| 164 | Sleep(100); // Give the driver some time to initialize/recover |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 165 | } |
| 166 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 167 | { |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 168 | terminate(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 169 | return error(EGL_BAD_ALLOC, false); |
| 170 | } |
| 171 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 172 | |
| 173 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 174 | { |
| 175 | terminate(); |
| 176 | return error(EGL_NOT_INITIALIZED, false); |
| 177 | } |
| 178 | |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 179 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 180 | // This is required by Texture2D::convertToRenderTarget. |
| 181 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 182 | { |
| 183 | terminate(); |
| 184 | return error(EGL_NOT_INITIALIZED, false); |
| 185 | } |
| 186 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 187 | mMinSwapInterval = 4; |
| 188 | mMaxSwapInterval = 0; |
| 189 | |
| 190 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} |
| 191 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} |
| 192 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} |
| 193 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} |
| 194 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} |
| 195 | |
jbauman@chromium.org | 03208d5 | 2011-06-15 01:15:24 +0000 | [diff] [blame] | 196 | mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); |
| 197 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 198 | const D3DFORMAT renderTargetFormats[] = |
| 199 | { |
| 200 | D3DFMT_A1R5G5B5, |
| 201 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 202 | D3DFMT_A8R8G8B8, |
| 203 | D3DFMT_R5G6B5, |
daniel@transgaming.com | 73a5db6 | 2010-10-15 17:58:13 +0000 | [diff] [blame] | 204 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 205 | D3DFMT_X8R8G8B8 |
| 206 | }; |
| 207 | |
| 208 | const D3DFORMAT depthStencilFormats[] = |
| 209 | { |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 210 | D3DFMT_UNKNOWN, |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 211 | // D3DFMT_D16_LOCKABLE, |
| 212 | D3DFMT_D32, |
| 213 | // D3DFMT_D15S1, |
| 214 | D3DFMT_D24S8, |
| 215 | D3DFMT_D24X8, |
| 216 | // D3DFMT_D24X4S4, |
| 217 | D3DFMT_D16, |
| 218 | // D3DFMT_D32F_LOCKABLE, |
| 219 | // D3DFMT_D24FS8 |
| 220 | }; |
| 221 | |
| 222 | D3DDISPLAYMODE currentDisplayMode; |
| 223 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 224 | |
| 225 | ConfigSet configSet; |
| 226 | |
| 227 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
| 228 | { |
| 229 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
| 230 | |
| 231 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 232 | |
| 233 | if (SUCCEEDED(result)) |
| 234 | { |
| 235 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
| 236 | { |
| 237 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 238 | HRESULT result = D3D_OK; |
| 239 | |
| 240 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 241 | { |
| 242 | result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 243 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 244 | |
| 245 | if (SUCCEEDED(result)) |
| 246 | { |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 247 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 248 | { |
| 249 | result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
| 250 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 251 | |
| 252 | if (SUCCEEDED(result)) |
| 253 | { |
| 254 | // FIXME: enumerate multi-sampling |
| 255 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 256 | configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0, |
| 257 | mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Give the sorted configs a unique ID and store them internally |
| 265 | EGLint index = 1; |
| 266 | for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
| 267 | { |
| 268 | Config configuration = *config; |
| 269 | configuration.mConfigID = index; |
| 270 | index++; |
| 271 | |
| 272 | mConfigSet.mSet.insert(configuration); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (!isInitialized()) |
| 277 | { |
| 278 | terminate(); |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 283 | initExtensionString(); |
| 284 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 285 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 286 | static const TCHAR className[] = TEXT("STATIC"); |
| 287 | |
| 288 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | void Display::terminate() |
| 294 | { |
| 295 | while (!mSurfaceSet.empty()) |
| 296 | { |
| 297 | destroySurface(*mSurfaceSet.begin()); |
| 298 | } |
| 299 | |
| 300 | while (!mContextSet.empty()) |
| 301 | { |
| 302 | destroyContext(*mContextSet.begin()); |
| 303 | } |
| 304 | |
| 305 | if (mDevice) |
| 306 | { |
| 307 | // If the device is lost, reset it first to prevent leaving the driver in an unstable state |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 308 | if (testDeviceLost()) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 309 | { |
| 310 | resetDevice(); |
| 311 | } |
| 312 | |
| 313 | mDevice->Release(); |
| 314 | mDevice = NULL; |
| 315 | } |
| 316 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 317 | if (mDeviceEx) |
| 318 | { |
| 319 | mDeviceEx->Release(); |
| 320 | mDeviceEx = NULL; |
| 321 | } |
| 322 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 323 | if (mD3d9) |
| 324 | { |
| 325 | mD3d9->Release(); |
| 326 | mD3d9 = NULL; |
| 327 | } |
| 328 | |
| 329 | if (mDeviceWindow) |
| 330 | { |
| 331 | DestroyWindow(mDeviceWindow); |
| 332 | mDeviceWindow = NULL; |
| 333 | } |
| 334 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 335 | if (mD3d9Ex) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 336 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 337 | mD3d9Ex->Release(); |
| 338 | mD3d9Ex = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (mD3d9Module) |
| 342 | { |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 343 | mD3d9Module = NULL; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void Display::startScene() |
| 348 | { |
| 349 | if (!mSceneStarted) |
| 350 | { |
| 351 | long result = mDevice->BeginScene(); |
kbr@chromium.org | 1a2cd26 | 2011-07-08 17:30:18 +0000 | [diff] [blame] | 352 | if (SUCCEEDED(result)) { |
| 353 | // This is defensive checking against the device being |
| 354 | // lost at unexpected times. |
| 355 | mSceneStarted = true; |
| 356 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | void Display::endScene() |
| 361 | { |
| 362 | if (mSceneStarted) |
| 363 | { |
kbr@chromium.org | 1a2cd26 | 2011-07-08 17:30:18 +0000 | [diff] [blame] | 364 | // EndScene can fail if the device was lost, for example due |
| 365 | // to a TDR during a draw call. |
| 366 | mDevice->EndScene(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 367 | mSceneStarted = false; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 372 | { |
| 373 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 374 | } |
| 375 | |
| 376 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 377 | { |
| 378 | const egl::Config *configuration = mConfigSet.get(config); |
| 379 | |
| 380 | switch (attribute) |
| 381 | { |
| 382 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
| 383 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
| 384 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
| 385 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
| 386 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
| 387 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
| 388 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
| 389 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
| 390 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
| 391 | case EGL_LEVEL: *value = configuration->mLevel; break; |
| 392 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
| 393 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
| 394 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 395 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 396 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 397 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 398 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 399 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 400 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 401 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 402 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 403 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 404 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 405 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 406 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 407 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 408 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
| 409 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
| 410 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 411 | case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break; |
| 412 | case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break; |
| 413 | case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 414 | default: |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | bool Display::createDevice() |
| 422 | { |
| 423 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 424 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 425 | |
| 426 | HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 427 | |
| 428 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 429 | { |
| 430 | return error(EGL_BAD_ALLOC, false); |
| 431 | } |
| 432 | |
| 433 | if (FAILED(result)) |
| 434 | { |
| 435 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 436 | |
| 437 | if (FAILED(result)) |
| 438 | { |
| 439 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 440 | return error(EGL_BAD_ALLOC, false); |
| 441 | } |
| 442 | } |
| 443 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 444 | if (mD3d9Ex) |
| 445 | { |
| 446 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 447 | ASSERT(SUCCEEDED(result)); |
| 448 | } |
| 449 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 450 | // Permanent non-default states |
| 451 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
daniel@transgaming.com | 7a2fdc9 | 2011-11-24 22:34:09 +0000 | [diff] [blame] | 452 | mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 453 | |
| 454 | mSceneStarted = false; |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | bool Display::resetDevice() |
| 460 | { |
| 461 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 462 | |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 463 | HRESULT result = D3D_OK; |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 464 | bool lost = testDeviceLost(); |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 465 | int attempts = 3; |
daniel@transgaming.com | 94910c9 | 2011-11-09 17:44:15 +0000 | [diff] [blame] | 466 | |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 467 | while (lost && attempts > 0) |
daniel@transgaming.com | 94910c9 | 2011-11-09 17:44:15 +0000 | [diff] [blame] | 468 | { |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 469 | if (mDeviceEx) |
| 470 | { |
| 471 | Sleep(500); // Give the graphics driver some CPU time |
| 472 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | result = mDevice->TestCooperativeLevel(); |
| 477 | |
| 478 | while (result == D3DERR_DEVICELOST) |
| 479 | { |
| 480 | Sleep(100); // Give the graphics driver some CPU time |
| 481 | result = mDevice->TestCooperativeLevel(); |
| 482 | } |
| 483 | |
| 484 | if (result == D3DERR_DEVICENOTRESET) |
| 485 | { |
| 486 | result = mDevice->Reset(&presentParameters); |
| 487 | } |
| 488 | } |
| 489 | |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 490 | lost = testDeviceLost(); |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 491 | attempts --; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 492 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 493 | |
| 494 | if (FAILED(result)) |
| 495 | { |
daniel@transgaming.com | 1043535 | 2011-11-09 17:44:21 +0000 | [diff] [blame] | 496 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 497 | return error(EGL_BAD_ALLOC, false); |
| 498 | } |
| 499 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 500 | return true; |
| 501 | } |
| 502 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 503 | EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 504 | { |
| 505 | const Config *configuration = mConfigSet.get(config); |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 506 | EGLint postSubBufferSupported = EGL_FALSE; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 507 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 508 | if (attribList) |
| 509 | { |
| 510 | while (*attribList != EGL_NONE) |
| 511 | { |
| 512 | switch (attribList[0]) |
| 513 | { |
| 514 | case EGL_RENDER_BUFFER: |
| 515 | switch (attribList[1]) |
| 516 | { |
| 517 | case EGL_BACK_BUFFER: |
| 518 | break; |
| 519 | case EGL_SINGLE_BUFFER: |
| 520 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported |
| 521 | default: |
| 522 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 523 | } |
| 524 | break; |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 525 | case EGL_POST_SUB_BUFFER_SUPPORTED_NV: |
| 526 | postSubBufferSupported = attribList[1]; |
| 527 | break; |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 528 | case EGL_VG_COLORSPACE: |
| 529 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 530 | case EGL_VG_ALPHA_FORMAT: |
| 531 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 532 | default: |
| 533 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 534 | } |
| 535 | |
| 536 | attribList += 2; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (hasExistingWindowSurface(window)) |
| 541 | { |
| 542 | return error(EGL_BAD_ALLOC, EGL_NO_SURFACE); |
| 543 | } |
| 544 | |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 545 | if (testDeviceLost()) |
| 546 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 547 | if (!restoreLostDevice()) |
| 548 | return EGL_NO_SURFACE; |
| 549 | } |
| 550 | |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 551 | Surface *surface = new Surface(this, configuration, window, postSubBufferSupported); |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 552 | |
| 553 | if (!surface->initialize()) |
| 554 | { |
| 555 | delete surface; |
| 556 | return EGL_NO_SURFACE; |
| 557 | } |
| 558 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 559 | mSurfaceSet.insert(surface); |
| 560 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 561 | return success(surface); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 562 | } |
| 563 | |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 564 | EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList) |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 565 | { |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 566 | EGLint width = 0, height = 0; |
| 567 | EGLenum textureFormat = EGL_NO_TEXTURE; |
| 568 | EGLenum textureTarget = EGL_NO_TEXTURE; |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 569 | const Config *configuration = mConfigSet.get(config); |
| 570 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 571 | if (attribList) |
| 572 | { |
| 573 | while (*attribList != EGL_NONE) |
| 574 | { |
| 575 | switch (attribList[0]) |
| 576 | { |
| 577 | case EGL_WIDTH: |
| 578 | width = attribList[1]; |
| 579 | break; |
| 580 | case EGL_HEIGHT: |
| 581 | height = attribList[1]; |
| 582 | break; |
| 583 | case EGL_LARGEST_PBUFFER: |
| 584 | if (attribList[1] != EGL_FALSE) |
| 585 | UNIMPLEMENTED(); // FIXME |
| 586 | break; |
| 587 | case EGL_TEXTURE_FORMAT: |
| 588 | switch (attribList[1]) |
| 589 | { |
| 590 | case EGL_NO_TEXTURE: |
| 591 | case EGL_TEXTURE_RGB: |
| 592 | case EGL_TEXTURE_RGBA: |
| 593 | textureFormat = attribList[1]; |
| 594 | break; |
| 595 | default: |
| 596 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 597 | } |
| 598 | break; |
| 599 | case EGL_TEXTURE_TARGET: |
| 600 | switch (attribList[1]) |
| 601 | { |
| 602 | case EGL_NO_TEXTURE: |
| 603 | case EGL_TEXTURE_2D: |
| 604 | textureTarget = attribList[1]; |
| 605 | break; |
| 606 | default: |
| 607 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 608 | } |
| 609 | break; |
| 610 | case EGL_MIPMAP_TEXTURE: |
| 611 | if (attribList[1] != EGL_FALSE) |
| 612 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 613 | break; |
| 614 | case EGL_VG_COLORSPACE: |
| 615 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 616 | case EGL_VG_ALPHA_FORMAT: |
| 617 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 618 | default: |
| 619 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 620 | } |
| 621 | |
| 622 | attribList += 2; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (width < 0 || height < 0) |
| 627 | { |
| 628 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 629 | } |
| 630 | |
| 631 | if (width == 0 || height == 0) |
| 632 | { |
| 633 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 634 | } |
| 635 | |
daniel@transgaming.com | 4f9ef0d | 2011-05-30 23:51:19 +0000 | [diff] [blame] | 636 | if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height))) |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 637 | { |
| 638 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 639 | } |
| 640 | |
| 641 | if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) || |
| 642 | (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE)) |
| 643 | { |
| 644 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 645 | } |
| 646 | |
| 647 | if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT)) |
| 648 | { |
| 649 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 650 | } |
| 651 | |
| 652 | if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) || |
| 653 | (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE)) |
| 654 | { |
| 655 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 656 | } |
| 657 | |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 658 | if (testDeviceLost()) |
| 659 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 660 | if (!restoreLostDevice()) |
| 661 | return EGL_NO_SURFACE; |
| 662 | } |
| 663 | |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 664 | Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget); |
| 665 | |
| 666 | if (!surface->initialize()) |
| 667 | { |
| 668 | delete surface; |
| 669 | return EGL_NO_SURFACE; |
| 670 | } |
| 671 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 672 | mSurfaceSet.insert(surface); |
| 673 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 674 | return success(surface); |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 675 | } |
| 676 | |
daniel@transgaming.com | 4ff960d | 2011-11-09 17:47:09 +0000 | [diff] [blame] | 677 | EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 678 | { |
| 679 | if (!mDevice) |
| 680 | { |
| 681 | if (!createDevice()) |
| 682 | { |
| 683 | return NULL; |
| 684 | } |
| 685 | } |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 686 | else if (testDeviceLost()) // Lost device |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 687 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 688 | if (!restoreLostDevice()) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 689 | return NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | const egl::Config *config = mConfigSet.get(configHandle); |
| 693 | |
daniel@transgaming.com | 4ff960d | 2011-11-09 17:47:09 +0000 | [diff] [blame] | 694 | gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 695 | mContextSet.insert(context); |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 696 | mDeviceLost = false; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 697 | |
| 698 | return context; |
| 699 | } |
| 700 | |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 701 | bool Display::restoreLostDevice() |
| 702 | { |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 703 | for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++) |
| 704 | { |
| 705 | if ((*ctx)->isResetNotificationEnabled()) |
| 706 | return false; // If reset notifications have been requested, application must delete all contexts first |
| 707 | } |
| 708 | |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 709 | // Release surface resources to make the Reset() succeed |
| 710 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 711 | { |
| 712 | (*surface)->release(); |
| 713 | } |
| 714 | |
| 715 | if (!resetDevice()) |
| 716 | { |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | // Restore any surfaces that may have been lost |
| 721 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 722 | { |
| 723 | (*surface)->resetSwapChain(); |
| 724 | } |
| 725 | |
| 726 | return true; |
| 727 | } |
| 728 | |
| 729 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 730 | void Display::destroySurface(egl::Surface *surface) |
| 731 | { |
daniel@transgaming.com | c556fa5 | 2011-05-26 14:13:29 +0000 | [diff] [blame] | 732 | delete surface; |
| 733 | mSurfaceSet.erase(surface); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void Display::destroyContext(gl::Context *context) |
| 737 | { |
| 738 | glDestroyContext(context); |
| 739 | mContextSet.erase(context); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 740 | } |
| 741 | |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 742 | void Display::notifyDeviceLost() |
| 743 | { |
| 744 | for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) |
| 745 | { |
| 746 | (*context)->markContextLost(); |
| 747 | } |
| 748 | mDeviceLost = true; |
| 749 | error(EGL_CONTEXT_LOST); |
| 750 | } |
| 751 | |
| 752 | bool Display::isDeviceLost() |
| 753 | { |
| 754 | return mDeviceLost; |
| 755 | } |
| 756 | |
daniel@transgaming.com | 3b1703f | 2011-05-11 15:36:26 +0000 | [diff] [blame] | 757 | bool Display::isInitialized() const |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 758 | { |
| 759 | return mD3d9 != NULL && mConfigSet.size() > 0; |
| 760 | } |
| 761 | |
| 762 | bool Display::isValidConfig(EGLConfig config) |
| 763 | { |
| 764 | return mConfigSet.get(config) != NULL; |
| 765 | } |
| 766 | |
| 767 | bool Display::isValidContext(gl::Context *context) |
| 768 | { |
| 769 | return mContextSet.find(context) != mContextSet.end(); |
| 770 | } |
| 771 | |
| 772 | bool Display::isValidSurface(egl::Surface *surface) |
| 773 | { |
daniel@transgaming.com | c556fa5 | 2011-05-26 14:13:29 +0000 | [diff] [blame] | 774 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | bool Display::hasExistingWindowSurface(HWND window) |
| 778 | { |
| 779 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 780 | { |
| 781 | if ((*surface)->getWindowHandle() == window) |
| 782 | { |
| 783 | return true; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return false; |
| 788 | } |
| 789 | |
| 790 | EGLint Display::getMinSwapInterval() |
| 791 | { |
| 792 | return mMinSwapInterval; |
| 793 | } |
| 794 | |
| 795 | EGLint Display::getMaxSwapInterval() |
| 796 | { |
| 797 | return mMaxSwapInterval; |
| 798 | } |
| 799 | |
| 800 | IDirect3DDevice9 *Display::getDevice() |
| 801 | { |
| 802 | if (!mDevice) |
| 803 | { |
| 804 | if (!createDevice()) |
| 805 | { |
| 806 | return NULL; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | return mDevice; |
| 811 | } |
| 812 | |
| 813 | D3DCAPS9 Display::getDeviceCaps() |
| 814 | { |
| 815 | return mDeviceCaps; |
| 816 | } |
| 817 | |
jbauman@chromium.org | 03208d5 | 2011-06-15 01:15:24 +0000 | [diff] [blame] | 818 | D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier() |
| 819 | { |
| 820 | return &mAdapterIdentifier; |
| 821 | } |
| 822 | |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 823 | bool Display::testDeviceLost() |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 824 | { |
| 825 | if (mDeviceEx) |
| 826 | { |
| 827 | return FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 828 | } |
daniel@transgaming.com | 17f548c | 2011-11-09 17:47:02 +0000 | [diff] [blame] | 829 | else if (mDevice) |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 830 | { |
| 831 | return FAILED(mDevice->TestCooperativeLevel()); |
| 832 | } |
daniel@transgaming.com | fe4b0c9 | 2011-09-13 00:53:11 +0000 | [diff] [blame] | 833 | |
| 834 | return false; // No device yet, so no reset required |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 835 | } |
| 836 | |
daniel@transgaming.com | 17f548c | 2011-11-09 17:47:02 +0000 | [diff] [blame] | 837 | bool Display::testDeviceResettable() |
| 838 | { |
| 839 | HRESULT status = D3D_OK; |
| 840 | |
| 841 | if (mDeviceEx) |
| 842 | { |
| 843 | status = mDeviceEx->CheckDeviceState(NULL); |
| 844 | } |
| 845 | else if (mDevice) |
| 846 | { |
| 847 | status = mDevice->TestCooperativeLevel(); |
| 848 | } |
| 849 | |
| 850 | switch (status) |
| 851 | { |
| 852 | case D3DERR_DEVICENOTRESET: |
| 853 | case D3DERR_DEVICEHUNG: |
| 854 | return true; |
| 855 | default: |
| 856 | return false; |
| 857 | } |
| 858 | } |
| 859 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 860 | void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
| 861 | { |
| 862 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 863 | { |
| 864 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 865 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 866 | |
| 867 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 868 | } |
| 869 | } |
| 870 | |
gman@chromium.org | 50c526d | 2011-08-10 05:19:44 +0000 | [diff] [blame] | 871 | bool Display::getDXT1TextureSupport() |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 872 | { |
| 873 | D3DDISPLAYMODE currentDisplayMode; |
| 874 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 875 | |
| 876 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 877 | } |
| 878 | |
gman@chromium.org | 50c526d | 2011-08-10 05:19:44 +0000 | [diff] [blame] | 879 | bool Display::getDXT3TextureSupport() |
| 880 | { |
| 881 | D3DDISPLAYMODE currentDisplayMode; |
| 882 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 883 | |
| 884 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 885 | } |
| 886 | |
| 887 | bool Display::getDXT5TextureSupport() |
| 888 | { |
| 889 | D3DDISPLAYMODE currentDisplayMode; |
| 890 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 891 | |
| 892 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 893 | } |
| 894 | |
daniel@transgaming.com | bbeffbb | 2011-11-09 17:46:11 +0000 | [diff] [blame] | 895 | bool Display::getFloat32TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 896 | { |
| 897 | D3DDISPLAYMODE currentDisplayMode; |
| 898 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 899 | |
| 900 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 901 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 902 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 903 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 904 | |
| 905 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 906 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 907 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 908 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 909 | |
| 910 | if (!filtering && !renderable) |
| 911 | { |
| 912 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 913 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 914 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 915 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 916 | } |
| 917 | else |
| 918 | { |
| 919 | return true; |
| 920 | } |
| 921 | } |
| 922 | |
daniel@transgaming.com | bbeffbb | 2011-11-09 17:46:11 +0000 | [diff] [blame] | 923 | bool Display::getFloat16TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 924 | { |
| 925 | D3DDISPLAYMODE currentDisplayMode; |
| 926 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 927 | |
| 928 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 929 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 930 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 931 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 932 | |
| 933 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 934 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 935 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 936 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 937 | |
| 938 | if (!filtering && !renderable) |
| 939 | { |
| 940 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 941 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 942 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 943 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 944 | } |
| 945 | else |
| 946 | { |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | |
daniel@transgaming.com | ed828e5 | 2010-10-15 17:57:30 +0000 | [diff] [blame] | 951 | bool Display::getLuminanceTextureSupport() |
| 952 | { |
| 953 | D3DDISPLAYMODE currentDisplayMode; |
| 954 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 955 | |
| 956 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 957 | } |
| 958 | |
| 959 | bool Display::getLuminanceAlphaTextureSupport() |
| 960 | { |
| 961 | D3DDISPLAYMODE currentDisplayMode; |
| 962 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 963 | |
| 964 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 965 | } |
| 966 | |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 967 | D3DPOOL Display::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 968 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 969 | if (mD3d9Ex != NULL) |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 970 | { |
| 971 | return D3DPOOL_DEFAULT; |
| 972 | } |
| 973 | else |
| 974 | { |
| 975 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 976 | { |
| 977 | return D3DPOOL_MANAGED; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | return D3DPOOL_DEFAULT; |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 982 | } |
| 983 | |
daniel@transgaming.com | 59580a3 | 2011-11-12 04:23:44 +0000 | [diff] [blame] | 984 | D3DPOOL Display::getTexturePool(bool renderable) const |
| 985 | { |
| 986 | if (mD3d9Ex != NULL) |
| 987 | { |
| 988 | return D3DPOOL_DEFAULT; |
| 989 | } |
| 990 | else |
| 991 | { |
| 992 | if (!renderable) |
| 993 | { |
| 994 | return D3DPOOL_MANAGED; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | return D3DPOOL_DEFAULT; |
| 999 | } |
| 1000 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 1001 | bool Display::getEventQuerySupport() |
| 1002 | { |
| 1003 | IDirect3DQuery9 *query; |
| 1004 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 1005 | if (SUCCEEDED(result)) |
| 1006 | { |
| 1007 | query->Release(); |
| 1008 | } |
| 1009 | |
| 1010 | return result != D3DERR_NOTAVAILABLE; |
| 1011 | } |
| 1012 | |
| 1013 | D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters() |
| 1014 | { |
| 1015 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 1016 | |
| 1017 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 1018 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 1019 | presentParameters.BackBufferCount = 1; |
| 1020 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 1021 | presentParameters.BackBufferWidth = 1; |
| 1022 | presentParameters.BackBufferHeight = 1; |
| 1023 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 1024 | presentParameters.Flags = 0; |
| 1025 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 1026 | presentParameters.MultiSampleQuality = 0; |
| 1027 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 1028 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 1029 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 1030 | presentParameters.Windowed = TRUE; |
| 1031 | |
| 1032 | return presentParameters; |
| 1033 | } |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 1034 | |
| 1035 | void Display::initExtensionString() |
| 1036 | { |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 1037 | HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
daniel@transgaming.com | 1a1fe24 | 2011-09-26 18:25:45 +0000 | [diff] [blame] | 1038 | bool isd3d9ex = isD3d9ExDevice(); |
| 1039 | |
| 1040 | mExtensionString = ""; |
| 1041 | |
daniel@transgaming.com | 8747f18 | 2011-11-09 17:50:38 +0000 | [diff] [blame] | 1042 | // Multi-vendor (EXT) extensions |
| 1043 | mExtensionString += "EGL_EXT_create_context_robustness "; |
| 1044 | |
| 1045 | // ANGLE-specific extensions |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 1046 | if (isd3d9ex) |
| 1047 | { |
daniel@transgaming.com | 1a1fe24 | 2011-09-26 18:25:45 +0000 | [diff] [blame] | 1048 | mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer "; |
| 1049 | } |
| 1050 | |
| 1051 | mExtensionString += "EGL_ANGLE_query_surface_pointer "; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (swiftShader) |
| 1054 | { |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 1055 | mExtensionString += "EGL_ANGLE_software_display "; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 1056 | } |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 1057 | |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 1058 | if (isd3d9ex) |
| 1059 | { |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 1060 | mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle "; |
| 1061 | } |
| 1062 | |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame^] | 1063 | mExtensionString += "EGL_NV_post_sub_buffer"; |
| 1064 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 1065 | std::string::size_type end = mExtensionString.find_last_not_of(' '); |
| 1066 | if (end != std::string::npos) |
| 1067 | { |
| 1068 | mExtensionString.resize(end+1); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | const char *Display::getExtensionString() const |
| 1073 | { |
| 1074 | return mExtensionString.c_str(); |
| 1075 | } |
| 1076 | |
daniel@transgaming.com | 3b1703f | 2011-05-11 15:36:26 +0000 | [diff] [blame] | 1077 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 1078 | // We test this using D3D9 by checking support for the R16F format. |
| 1079 | bool Display::getVertexTextureSupport() const |
| 1080 | { |
daniel@transgaming.com | 3b1703f | 2011-05-11 15:36:26 +0000 | [diff] [blame] | 1081 | if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 1082 | { |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | D3DDISPLAYMODE currentDisplayMode; |
| 1087 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1088 | |
| 1089 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 1090 | |
| 1091 | return SUCCEEDED(result); |
| 1092 | } |
| 1093 | |
daniel@transgaming.com | 4f9ef0d | 2011-05-30 23:51:19 +0000 | [diff] [blame] | 1094 | bool Display::getNonPower2TextureSupport() const |
| 1095 | { |
| 1096 | return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && |
| 1097 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && |
| 1098 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL); |
| 1099 | } |
| 1100 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 1101 | } |