daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 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> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "common/debug.h" |
| 17 | |
| 18 | #include "libEGL/main.h" |
| 19 | |
| 20 | #define REF_RAST 0 // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 21 | #define ENABLE_D3D9EX 1 // Enables use of the IDirect3D9Ex interface, when available |
| 22 | |
| 23 | namespace egl |
| 24 | { |
| 25 | Display::Display(HDC deviceContext) : mDc(deviceContext) |
| 26 | { |
| 27 | mD3d9Module = NULL; |
| 28 | |
| 29 | mD3d9 = NULL; |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 30 | mD3d9Ex = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 31 | mDevice = NULL; |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 32 | mDeviceEx = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 33 | mDeviceWindow = NULL; |
| 34 | |
| 35 | mAdapter = D3DADAPTER_DEFAULT; |
| 36 | |
| 37 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 38 | mDeviceType = D3DDEVTYPE_REF; |
| 39 | #else |
| 40 | mDeviceType = D3DDEVTYPE_HAL; |
| 41 | #endif |
| 42 | |
| 43 | mMinSwapInterval = 1; |
| 44 | mMaxSwapInterval = 1; |
| 45 | } |
| 46 | |
| 47 | Display::~Display() |
| 48 | { |
| 49 | terminate(); |
| 50 | } |
| 51 | |
| 52 | bool Display::initialize() |
| 53 | { |
| 54 | if (isInitialized()) |
| 55 | { |
| 56 | return true; |
| 57 | } |
| 58 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 59 | mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 60 | if (mD3d9Module == NULL) |
| 61 | { |
| 62 | terminate(); |
| 63 | return false; |
| 64 | } |
| 65 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 66 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 67 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 68 | |
| 69 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 70 | // inclined to report a lost context, for example when the user switches |
| 71 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 72 | if (ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 73 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 74 | ASSERT(mD3d9Ex); |
| 75 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 76 | ASSERT(mD3d9); |
| 77 | } |
| 78 | else |
| 79 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 80 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (mD3d9) |
| 84 | { |
| 85 | if (mDc != NULL) |
| 86 | { |
| 87 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 88 | } |
| 89 | |
| 90 | HRESULT result; |
| 91 | |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 92 | // Give up on getting device caps after about one second. |
| 93 | for (int i = 0; i < 10; ++i) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 94 | { |
| 95 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 96 | |
| 97 | if (SUCCEEDED(result)) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 98 | { |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 99 | break; |
| 100 | } |
| 101 | else if (result == D3DERR_NOTAVAILABLE) |
| 102 | { |
| 103 | Sleep(100); // Give the driver some time to initialize/recover |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 104 | } |
| 105 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 106 | { |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 107 | terminate(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 108 | return error(EGL_BAD_ALLOC, false); |
| 109 | } |
| 110 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 111 | |
| 112 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 113 | { |
| 114 | terminate(); |
| 115 | return error(EGL_NOT_INITIALIZED, false); |
| 116 | } |
| 117 | |
apatrick@chromium.org | 1c76801 | 2010-10-07 00:35:24 +0000 | [diff] [blame] | 118 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 119 | // This is required by Texture2D::convertToRenderTarget. |
| 120 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 121 | { |
| 122 | terminate(); |
| 123 | return error(EGL_NOT_INITIALIZED, false); |
| 124 | } |
| 125 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 126 | mMinSwapInterval = 4; |
| 127 | mMaxSwapInterval = 0; |
| 128 | |
| 129 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} |
| 130 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} |
| 131 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} |
| 132 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} |
| 133 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} |
| 134 | |
| 135 | const D3DFORMAT renderTargetFormats[] = |
| 136 | { |
| 137 | D3DFMT_A1R5G5B5, |
| 138 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 139 | D3DFMT_A8R8G8B8, |
| 140 | D3DFMT_R5G6B5, |
daniel@transgaming.com | 73a5db6 | 2010-10-15 17:58:13 +0000 | [diff] [blame] | 141 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 142 | D3DFMT_X8R8G8B8 |
| 143 | }; |
| 144 | |
| 145 | const D3DFORMAT depthStencilFormats[] = |
| 146 | { |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame^] | 147 | D3DFMT_UNKNOWN, |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 148 | // D3DFMT_D16_LOCKABLE, |
| 149 | D3DFMT_D32, |
| 150 | // D3DFMT_D15S1, |
| 151 | D3DFMT_D24S8, |
| 152 | D3DFMT_D24X8, |
| 153 | // D3DFMT_D24X4S4, |
| 154 | D3DFMT_D16, |
| 155 | // D3DFMT_D32F_LOCKABLE, |
| 156 | // D3DFMT_D24FS8 |
| 157 | }; |
| 158 | |
| 159 | D3DDISPLAYMODE currentDisplayMode; |
| 160 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 161 | |
| 162 | ConfigSet configSet; |
| 163 | |
| 164 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
| 165 | { |
| 166 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
| 167 | |
| 168 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 169 | |
| 170 | if (SUCCEEDED(result)) |
| 171 | { |
| 172 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
| 173 | { |
| 174 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame^] | 175 | HRESULT result = D3D_OK; |
| 176 | |
| 177 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 178 | { |
| 179 | result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 180 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 181 | |
| 182 | if (SUCCEEDED(result)) |
| 183 | { |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame^] | 184 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 185 | { |
| 186 | result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
| 187 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 188 | |
| 189 | if (SUCCEEDED(result)) |
| 190 | { |
| 191 | // FIXME: enumerate multi-sampling |
| 192 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 193 | configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0, |
| 194 | mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Give the sorted configs a unique ID and store them internally |
| 202 | EGLint index = 1; |
| 203 | for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
| 204 | { |
| 205 | Config configuration = *config; |
| 206 | configuration.mConfigID = index; |
| 207 | index++; |
| 208 | |
| 209 | mConfigSet.mSet.insert(configuration); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (!isInitialized()) |
| 214 | { |
| 215 | terminate(); |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 220 | initExtensionString(); |
| 221 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 222 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 223 | static const TCHAR className[] = TEXT("STATIC"); |
| 224 | |
| 225 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | void Display::terminate() |
| 231 | { |
| 232 | while (!mSurfaceSet.empty()) |
| 233 | { |
| 234 | destroySurface(*mSurfaceSet.begin()); |
| 235 | } |
| 236 | |
| 237 | while (!mContextSet.empty()) |
| 238 | { |
| 239 | destroyContext(*mContextSet.begin()); |
| 240 | } |
| 241 | |
| 242 | if (mDevice) |
| 243 | { |
| 244 | // If the device is lost, reset it first to prevent leaving the driver in an unstable state |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 245 | if (isDeviceLost()) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 246 | { |
| 247 | resetDevice(); |
| 248 | } |
| 249 | |
| 250 | mDevice->Release(); |
| 251 | mDevice = NULL; |
| 252 | } |
| 253 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 254 | if (mDeviceEx) |
| 255 | { |
| 256 | mDeviceEx->Release(); |
| 257 | mDeviceEx = NULL; |
| 258 | } |
| 259 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 260 | if (mD3d9) |
| 261 | { |
| 262 | mD3d9->Release(); |
| 263 | mD3d9 = NULL; |
| 264 | } |
| 265 | |
| 266 | if (mDeviceWindow) |
| 267 | { |
| 268 | DestroyWindow(mDeviceWindow); |
| 269 | mDeviceWindow = NULL; |
| 270 | } |
| 271 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 272 | if (mD3d9Ex) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 273 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 274 | mD3d9Ex->Release(); |
| 275 | mD3d9Ex = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (mD3d9Module) |
| 279 | { |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 280 | mD3d9Module = NULL; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void Display::startScene() |
| 285 | { |
| 286 | if (!mSceneStarted) |
| 287 | { |
| 288 | long result = mDevice->BeginScene(); |
| 289 | ASSERT(SUCCEEDED(result)); |
| 290 | mSceneStarted = true; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void Display::endScene() |
| 295 | { |
| 296 | if (mSceneStarted) |
| 297 | { |
| 298 | long result = mDevice->EndScene(); |
| 299 | ASSERT(SUCCEEDED(result)); |
| 300 | mSceneStarted = false; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 305 | { |
| 306 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 307 | } |
| 308 | |
| 309 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 310 | { |
| 311 | const egl::Config *configuration = mConfigSet.get(config); |
| 312 | |
| 313 | switch (attribute) |
| 314 | { |
| 315 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
| 316 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
| 317 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
| 318 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
| 319 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
| 320 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
| 321 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
| 322 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
| 323 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
| 324 | case EGL_LEVEL: *value = configuration->mLevel; break; |
| 325 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
| 326 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
| 327 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 328 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 329 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 330 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 331 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 332 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 333 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 334 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 335 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 336 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 337 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 338 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 339 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 340 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 341 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
| 342 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
| 343 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 344 | case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break; |
| 345 | case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break; |
| 346 | case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 347 | default: |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | bool Display::createDevice() |
| 355 | { |
| 356 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 357 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 358 | |
| 359 | HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 360 | |
| 361 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 362 | { |
| 363 | return error(EGL_BAD_ALLOC, false); |
| 364 | } |
| 365 | |
| 366 | if (FAILED(result)) |
| 367 | { |
| 368 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 369 | |
| 370 | if (FAILED(result)) |
| 371 | { |
| 372 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 373 | return error(EGL_BAD_ALLOC, false); |
| 374 | } |
| 375 | } |
| 376 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 377 | if (mD3d9Ex) |
| 378 | { |
| 379 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 380 | ASSERT(SUCCEEDED(result)); |
| 381 | } |
| 382 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 383 | // Permanent non-default states |
| 384 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 385 | |
| 386 | mSceneStarted = false; |
| 387 | |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | bool Display::resetDevice() |
| 392 | { |
| 393 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 394 | HRESULT result; |
| 395 | |
| 396 | do |
| 397 | { |
| 398 | Sleep(0); // Give the graphics driver some CPU time |
| 399 | |
| 400 | result = mDevice->Reset(&presentParameters); |
| 401 | } |
| 402 | while (result == D3DERR_DEVICELOST); |
| 403 | |
| 404 | if (FAILED(result)) |
| 405 | { |
| 406 | return error(EGL_BAD_ALLOC, false); |
| 407 | } |
| 408 | |
| 409 | ASSERT(SUCCEEDED(result)); |
| 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | Surface *Display::createWindowSurface(HWND window, EGLConfig config) |
| 415 | { |
| 416 | const Config *configuration = mConfigSet.get(config); |
| 417 | |
| 418 | Surface *surface = new Surface(this, configuration, window); |
| 419 | mSurfaceSet.insert(surface); |
| 420 | |
| 421 | return surface; |
| 422 | } |
| 423 | |
jbauman@chromium.org | ae34580 | 2011-03-30 22:04:25 +0000 | [diff] [blame] | 424 | Surface *Display::createOffscreenSurface(int width, int height, EGLConfig config, EGLenum textureFormat, EGLenum textureTarget) |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 425 | { |
| 426 | const Config *configuration = mConfigSet.get(config); |
| 427 | |
jbauman@chromium.org | ae34580 | 2011-03-30 22:04:25 +0000 | [diff] [blame] | 428 | Surface *surface = new Surface(this, configuration, width, height, textureFormat, textureTarget); |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 429 | mSurfaceSet.insert(surface); |
| 430 | |
| 431 | return surface; |
| 432 | } |
| 433 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 434 | EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext) |
| 435 | { |
| 436 | if (!mDevice) |
| 437 | { |
| 438 | if (!createDevice()) |
| 439 | { |
| 440 | return NULL; |
| 441 | } |
| 442 | } |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 443 | else if (isDeviceLost()) // Lost device |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 444 | { |
| 445 | if (!resetDevice()) |
| 446 | { |
| 447 | return NULL; |
| 448 | } |
| 449 | |
| 450 | // Restore any surfaces that may have been lost |
| 451 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 452 | { |
| 453 | (*surface)->resetSwapChain(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | const egl::Config *config = mConfigSet.get(configHandle); |
| 458 | |
| 459 | gl::Context *context = glCreateContext(config, shareContext); |
| 460 | mContextSet.insert(context); |
| 461 | |
| 462 | return context; |
| 463 | } |
| 464 | |
| 465 | void Display::destroySurface(egl::Surface *surface) |
| 466 | { |
| 467 | delete surface; |
| 468 | mSurfaceSet.erase(surface); |
| 469 | } |
| 470 | |
| 471 | void Display::destroyContext(gl::Context *context) |
| 472 | { |
| 473 | glDestroyContext(context); |
| 474 | mContextSet.erase(context); |
| 475 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 476 | if (mContextSet.empty() && mDevice && isDeviceLost()) // Last context of a lost device |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 477 | { |
| 478 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 479 | { |
| 480 | (*surface)->release(); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | bool Display::isInitialized() |
| 486 | { |
| 487 | return mD3d9 != NULL && mConfigSet.size() > 0; |
| 488 | } |
| 489 | |
| 490 | bool Display::isValidConfig(EGLConfig config) |
| 491 | { |
| 492 | return mConfigSet.get(config) != NULL; |
| 493 | } |
| 494 | |
| 495 | bool Display::isValidContext(gl::Context *context) |
| 496 | { |
| 497 | return mContextSet.find(context) != mContextSet.end(); |
| 498 | } |
| 499 | |
| 500 | bool Display::isValidSurface(egl::Surface *surface) |
| 501 | { |
| 502 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
| 503 | } |
| 504 | |
| 505 | bool Display::hasExistingWindowSurface(HWND window) |
| 506 | { |
| 507 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 508 | { |
| 509 | if ((*surface)->getWindowHandle() == window) |
| 510 | { |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | EGLint Display::getMinSwapInterval() |
| 519 | { |
| 520 | return mMinSwapInterval; |
| 521 | } |
| 522 | |
| 523 | EGLint Display::getMaxSwapInterval() |
| 524 | { |
| 525 | return mMaxSwapInterval; |
| 526 | } |
| 527 | |
| 528 | IDirect3DDevice9 *Display::getDevice() |
| 529 | { |
| 530 | if (!mDevice) |
| 531 | { |
| 532 | if (!createDevice()) |
| 533 | { |
| 534 | return NULL; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return mDevice; |
| 539 | } |
| 540 | |
| 541 | D3DCAPS9 Display::getDeviceCaps() |
| 542 | { |
| 543 | return mDeviceCaps; |
| 544 | } |
| 545 | |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 546 | bool Display::isDeviceLost() |
| 547 | { |
| 548 | if (mDeviceEx) |
| 549 | { |
| 550 | return FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | return FAILED(mDevice->TestCooperativeLevel()); |
| 555 | } |
| 556 | } |
| 557 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 558 | void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
| 559 | { |
| 560 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 561 | { |
| 562 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 563 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 564 | |
| 565 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | bool Display::getCompressedTextureSupport() |
| 570 | { |
| 571 | D3DDISPLAYMODE currentDisplayMode; |
| 572 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 573 | |
| 574 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 575 | } |
| 576 | |
| 577 | bool Display::getFloatTextureSupport(bool *filtering, bool *renderable) |
| 578 | { |
| 579 | D3DDISPLAYMODE currentDisplayMode; |
| 580 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 581 | |
| 582 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 583 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 584 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 585 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 586 | |
| 587 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 588 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 589 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 590 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 591 | |
| 592 | if (!filtering && !renderable) |
| 593 | { |
| 594 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 595 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 596 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 597 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable) |
| 606 | { |
| 607 | D3DDISPLAYMODE currentDisplayMode; |
| 608 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 609 | |
| 610 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 611 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 612 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 613 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 614 | |
| 615 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 616 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 617 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 618 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 619 | |
| 620 | if (!filtering && !renderable) |
| 621 | { |
| 622 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 623 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 624 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 625 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | return true; |
| 630 | } |
| 631 | } |
| 632 | |
daniel@transgaming.com | ed828e5 | 2010-10-15 17:57:30 +0000 | [diff] [blame] | 633 | bool Display::getLuminanceTextureSupport() |
| 634 | { |
| 635 | D3DDISPLAYMODE currentDisplayMode; |
| 636 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 637 | |
| 638 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 639 | } |
| 640 | |
| 641 | bool Display::getLuminanceAlphaTextureSupport() |
| 642 | { |
| 643 | D3DDISPLAYMODE currentDisplayMode; |
| 644 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 645 | |
| 646 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 647 | } |
| 648 | |
jbauman@chromium.org | ae34580 | 2011-03-30 22:04:25 +0000 | [diff] [blame] | 649 | bool Display::getNonPow2TextureSupport() |
| 650 | { |
| 651 | return !(mDeviceCaps.TextureCaps & (D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL)); |
| 652 | } |
| 653 | |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 654 | D3DPOOL Display::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 655 | { |
apatrick@chromium.org | 9e83b59 | 2011-02-10 22:04:34 +0000 | [diff] [blame] | 656 | if (mD3d9Ex != NULL) |
daniel@transgaming.com | ee04e45 | 2011-01-08 05:46:27 +0000 | [diff] [blame] | 657 | { |
| 658 | return D3DPOOL_DEFAULT; |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 663 | { |
| 664 | return D3DPOOL_MANAGED; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return D3DPOOL_DEFAULT; |
daniel@transgaming.com | 37b141e | 2011-01-08 05:46:13 +0000 | [diff] [blame] | 669 | } |
| 670 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 671 | bool Display::getEventQuerySupport() |
| 672 | { |
| 673 | IDirect3DQuery9 *query; |
| 674 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 675 | if (SUCCEEDED(result)) |
| 676 | { |
| 677 | query->Release(); |
| 678 | } |
| 679 | |
| 680 | return result != D3DERR_NOTAVAILABLE; |
| 681 | } |
| 682 | |
| 683 | D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters() |
| 684 | { |
| 685 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 686 | |
| 687 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 688 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 689 | presentParameters.BackBufferCount = 1; |
| 690 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 691 | presentParameters.BackBufferWidth = 1; |
| 692 | presentParameters.BackBufferHeight = 1; |
| 693 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 694 | presentParameters.Flags = 0; |
| 695 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 696 | presentParameters.MultiSampleQuality = 0; |
| 697 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 698 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 699 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 700 | presentParameters.Windowed = TRUE; |
| 701 | |
| 702 | return presentParameters; |
| 703 | } |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 704 | |
| 705 | void Display::initExtensionString() |
| 706 | { |
| 707 | mExtensionString += "EGL_ANGLE_query_surface_pointer "; |
| 708 | |
| 709 | if (isD3d9ExDevice()) { |
| 710 | mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle "; |
| 711 | } |
| 712 | |
| 713 | std::string::size_type end = mExtensionString.find_last_not_of(' '); |
| 714 | if (end != std::string::npos) |
| 715 | { |
| 716 | mExtensionString.resize(end+1); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | const char *Display::getExtensionString() const |
| 721 | { |
| 722 | return mExtensionString.c_str(); |
| 723 | } |
| 724 | |
| 725 | } |