daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "libEGL/Display.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <vector> |
| 15 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 16 | #include "common/debug.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 18 | #include "libEGL/main.h" |
| 19 | |
daniel@transgaming.com | 741a588 | 2010-05-20 19:17:29 +0000 | [diff] [blame] | 20 | #define REF_RAST 0 // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 21 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 22 | namespace egl |
| 23 | { |
| 24 | Display::Display(HDC deviceContext) : mDc(deviceContext) |
| 25 | { |
| 26 | mD3d9 = NULL; |
| 27 | mDevice = NULL; |
| 28 | |
| 29 | mAdapter = D3DADAPTER_DEFAULT; |
daniel@transgaming.com | 741a588 | 2010-05-20 19:17:29 +0000 | [diff] [blame] | 30 | |
daniel@transgaming.com | 8b9f4cc | 2010-05-20 19:17:35 +0000 | [diff] [blame] | 31 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
daniel@transgaming.com | 741a588 | 2010-05-20 19:17:29 +0000 | [diff] [blame] | 32 | mDeviceType = D3DDEVTYPE_REF; |
| 33 | #else |
| 34 | mDeviceType = D3DDEVTYPE_HAL; |
| 35 | #endif |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 36 | |
| 37 | mMinSwapInterval = 1; |
| 38 | mMaxSwapInterval = 1; |
| 39 | setSwapInterval(1); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Display::~Display() |
| 43 | { |
| 44 | terminate(); |
| 45 | } |
| 46 | |
| 47 | bool Display::initialize() |
| 48 | { |
| 49 | if (isInitialized()) |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 55 | |
| 56 | if (mD3d9) |
| 57 | { |
| 58 | if (mDc != NULL) |
| 59 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 60 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 61 | } |
| 62 | |
daniel@transgaming.com | 353569a | 2010-06-24 13:02:12 +0000 | [diff] [blame] | 63 | HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | |
| 65 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 66 | { |
| 67 | return error(EGL_BAD_ALLOC, false); |
| 68 | } |
| 69 | |
daniel@transgaming.com | 353569a | 2010-06-24 13:02:12 +0000 | [diff] [blame] | 70 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | { |
| 72 | mD3d9->Release(); |
| 73 | mD3d9 = NULL; |
| 74 | } |
| 75 | else |
| 76 | { |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 77 | mMinSwapInterval = 4; |
| 78 | mMaxSwapInterval = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | |
daniel@transgaming.com | 353569a | 2010-06-24 13:02:12 +0000 | [diff] [blame] | 80 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} |
| 81 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} |
| 82 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} |
| 83 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} |
| 84 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 85 | |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 86 | const D3DFORMAT renderTargetFormats[] = |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 87 | { |
| 88 | D3DFMT_A1R5G5B5, |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 89 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 90 | D3DFMT_A8R8G8B8, |
| 91 | D3DFMT_R5G6B5, |
| 92 | D3DFMT_X1R5G5B5, |
| 93 | D3DFMT_X8R8G8B8 |
| 94 | }; |
| 95 | |
| 96 | const D3DFORMAT depthStencilFormats[] = |
| 97 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 98 | // D3DFMT_D16_LOCKABLE, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 99 | D3DFMT_D32, |
daniel@transgaming.com | d1f6fde | 2010-05-07 19:01:43 +0000 | [diff] [blame] | 100 | // D3DFMT_D15S1, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | D3DFMT_D24S8, |
| 102 | D3DFMT_D24X8, |
daniel@transgaming.com | d1f6fde | 2010-05-07 19:01:43 +0000 | [diff] [blame] | 103 | // D3DFMT_D24X4S4, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 104 | D3DFMT_D16, |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 105 | // D3DFMT_D32F_LOCKABLE, |
| 106 | // D3DFMT_D24FS8 |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | D3DDISPLAYMODE currentDisplayMode; |
| 110 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 111 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 112 | ConfigSet configSet; |
| 113 | |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 114 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 116 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | |
| 118 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 119 | |
| 120 | if (SUCCEEDED(result)) |
| 121 | { |
| 122 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
| 123 | { |
| 124 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
| 125 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 126 | |
| 127 | if (SUCCEEDED(result)) |
| 128 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 129 | HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 130 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 131 | if (SUCCEEDED(result)) |
| 132 | { |
| 133 | // FIXME: Enumerate multi-sampling |
| 134 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 135 | configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 142 | // Give the sorted configs a unique ID and store them internally |
| 143 | EGLint index = 1; |
| 144 | for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
| 145 | { |
| 146 | Config configuration = *config; |
| 147 | configuration.mConfigID = index; |
| 148 | index++; |
| 149 | |
| 150 | mConfigSet.mSet.insert(configuration); |
| 151 | } |
| 152 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (!isInitialized()) |
| 156 | { |
| 157 | terminate(); |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | void Display::terminate() |
| 166 | { |
| 167 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 168 | { |
| 169 | delete *surface; |
| 170 | } |
| 171 | |
| 172 | for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) |
| 173 | { |
| 174 | glDestroyContext(*context); |
| 175 | } |
| 176 | |
| 177 | if (mDevice) |
| 178 | { |
| 179 | mDevice->Release(); |
| 180 | mDevice = NULL; |
| 181 | } |
| 182 | |
| 183 | if (mD3d9) |
| 184 | { |
| 185 | mD3d9->Release(); |
| 186 | mD3d9 = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 190 | void Display::startScene() |
| 191 | { |
| 192 | if (!mSceneStarted) |
| 193 | { |
| 194 | long result = mDevice->BeginScene(); |
| 195 | ASSERT(SUCCEEDED(result)); |
| 196 | mSceneStarted = true; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void Display::endScene() |
| 201 | { |
| 202 | if (mSceneStarted) |
| 203 | { |
| 204 | long result = mDevice->EndScene(); |
| 205 | ASSERT(SUCCEEDED(result)); |
| 206 | mSceneStarted = false; |
| 207 | } |
| 208 | } |
| 209 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 210 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 211 | { |
| 212 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 213 | } |
| 214 | |
| 215 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 216 | { |
| 217 | const egl::Config *configuration = mConfigSet.get(config); |
| 218 | |
| 219 | switch (attribute) |
| 220 | { |
| 221 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
| 222 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
| 223 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
| 224 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
| 225 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
| 226 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
| 227 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
| 228 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
| 229 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
| 230 | case EGL_LEVEL: *value = configuration->mLevel; break; |
| 231 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
| 232 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
| 233 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 234 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 235 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 236 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 237 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 238 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 239 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 240 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 241 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 242 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 243 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 244 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 245 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 246 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 247 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 248 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 249 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
| 250 | default: |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 257 | Surface *Display::createWindowSurface(HWND window, EGLConfig config) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 258 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 259 | const Config *configuration = mConfigSet.get(config); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 260 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 261 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 262 | |
| 263 | presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat; |
| 264 | presentParameters.BackBufferCount = 1; |
| 265 | presentParameters.BackBufferFormat = configuration->mRenderTargetFormat; |
| 266 | presentParameters.BackBufferWidth = 0; |
| 267 | presentParameters.BackBufferHeight = 0; |
| 268 | presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE; |
| 269 | presentParameters.Flags = 0; |
| 270 | presentParameters.hDeviceWindow = window; |
| 271 | presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented |
| 272 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 273 | presentParameters.PresentationInterval = convertInterval(mMinSwapInterval); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 274 | presentParameters.SwapEffect = D3DSWAPEFFECT_COPY; |
| 275 | presentParameters.Windowed = TRUE; // FIXME |
| 276 | |
| 277 | IDirect3DSwapChain9 *swapChain = NULL; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 278 | IDirect3DSurface9 *depthStencilSurface = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 279 | |
| 280 | if (!mDevice) |
| 281 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 282 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 283 | |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 284 | HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 285 | |
| 286 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 287 | { |
| 288 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 289 | } |
| 290 | |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 291 | if (FAILED(result)) |
| 292 | { |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 293 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 294 | |
| 295 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 296 | { |
| 297 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 298 | } |
| 299 | } |
| 300 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 301 | ASSERT(SUCCEEDED(result)); |
| 302 | |
| 303 | if (mDevice) |
| 304 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 305 | mSceneStarted = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 306 | mDevice->GetSwapChain(0, &swapChain); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 307 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | else |
| 311 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 312 | if (!mSurfaceSet.empty()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 313 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 314 | // if the device already exists, and there are other surfaces/windows currently in use, we need to create |
| 315 | // a separate swap chain for the new draw surface. |
| 316 | HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 317 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 318 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 319 | { |
| 320 | ERR("Could not create additional swap chains. Out of memory."); |
| 321 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 322 | } |
| 323 | |
| 324 | ASSERT(SUCCEEDED(result)); |
| 325 | |
| 326 | // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike |
| 327 | // CreateDevice, so we must do so explicitly. |
| 328 | result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, |
| 329 | presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, |
| 330 | presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL); |
| 331 | |
| 332 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 333 | { |
| 334 | swapChain->Release(); |
| 335 | ERR("Could not create depthstencil surface for new swap chain. Out of memory."); |
| 336 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 337 | } |
| 338 | |
| 339 | ASSERT(SUCCEEDED(result)); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | // if the device already exists, but there are no surfaces in use, then all the surfaces/windows |
| 344 | // have been destroyed, and we should repurpose the originally created depthstencil surface for |
| 345 | // use with the new surface we are creating. |
| 346 | HRESULT result = mDevice->Reset(&presentParameters); |
| 347 | |
| 348 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 349 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 350 | ERR("Could not reset presentation parameters for device. Out of memory."); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 351 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 352 | } |
| 353 | |
| 354 | ASSERT(SUCCEEDED(result)); |
| 355 | |
| 356 | if (mDevice) |
| 357 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 358 | mSceneStarted = false; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 359 | mDevice->GetSwapChain(0, &swapChain); |
| 360 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
| 361 | } |
| 362 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 363 | } |
| 364 | |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 365 | // Permanent non-default states |
| 366 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 367 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 368 | Surface *surface = NULL; |
| 369 | |
| 370 | if (swapChain) |
| 371 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 372 | surface = new Surface(this, swapChain, depthStencilSurface, configuration); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 373 | mSurfaceSet.insert(surface); |
| 374 | |
| 375 | swapChain->Release(); |
| 376 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 377 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 378 | return surface; |
| 379 | } |
| 380 | |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame^] | 381 | EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 382 | { |
| 383 | const egl::Config *config = mConfigSet.get(configHandle); |
| 384 | |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame^] | 385 | gl::Context *context = glCreateContext(config, shareContext); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 386 | mContextSet.insert(context); |
| 387 | |
| 388 | return context; |
| 389 | } |
| 390 | |
| 391 | void Display::destroySurface(egl::Surface *surface) |
| 392 | { |
| 393 | delete surface; |
| 394 | mSurfaceSet.erase(surface); |
| 395 | } |
| 396 | |
| 397 | void Display::destroyContext(gl::Context *context) |
| 398 | { |
| 399 | glDestroyContext(context); |
| 400 | mContextSet.erase(context); |
| 401 | } |
| 402 | |
| 403 | bool Display::isInitialized() |
| 404 | { |
| 405 | return mD3d9 != NULL && mConfigSet.size() > 0; |
| 406 | } |
| 407 | |
| 408 | bool Display::isValidConfig(EGLConfig config) |
| 409 | { |
| 410 | return mConfigSet.get(config) != NULL; |
| 411 | } |
| 412 | |
| 413 | bool Display::isValidContext(gl::Context *context) |
| 414 | { |
| 415 | return mContextSet.find(context) != mContextSet.end(); |
| 416 | } |
| 417 | |
| 418 | bool Display::isValidSurface(egl::Surface *surface) |
| 419 | { |
| 420 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
| 421 | } |
| 422 | |
| 423 | bool Display::hasExistingWindowSurface(HWND window) |
| 424 | { |
| 425 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 426 | { |
| 427 | if ((*surface)->getWindowHandle() == window) |
| 428 | { |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return false; |
| 434 | } |
| 435 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 436 | void Display::setSwapInterval(GLint interval) |
| 437 | { |
| 438 | mSwapInterval = interval; |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 439 | mSwapInterval = std::max(mSwapInterval, mMinSwapInterval); |
| 440 | mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval); |
| 441 | |
| 442 | mPresentInterval = convertInterval(mSwapInterval); |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 443 | } |
| 444 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 445 | DWORD Display::getPresentInterval() |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 446 | { |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 447 | return mPresentInterval; |
| 448 | } |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 449 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 450 | DWORD Display::convertInterval(GLint interval) |
| 451 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 452 | switch(interval) |
| 453 | { |
| 454 | case 0: return D3DPRESENT_INTERVAL_IMMEDIATE; |
| 455 | case 1: return D3DPRESENT_INTERVAL_ONE; |
| 456 | case 2: return D3DPRESENT_INTERVAL_TWO; |
| 457 | case 3: return D3DPRESENT_INTERVAL_THREE; |
| 458 | case 4: return D3DPRESENT_INTERVAL_FOUR; |
| 459 | default: UNREACHABLE(); |
| 460 | } |
| 461 | |
| 462 | return D3DPRESENT_INTERVAL_DEFAULT; |
| 463 | } |
| 464 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 465 | IDirect3DDevice9 *Display::getDevice() |
| 466 | { |
| 467 | return mDevice; |
| 468 | } |
daniel@transgaming.com | 353569a | 2010-06-24 13:02:12 +0000 | [diff] [blame] | 469 | |
| 470 | D3DCAPS9 Display::getDeviceCaps() |
| 471 | { |
| 472 | return mDeviceCaps; |
| 473 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 474 | } |