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