daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 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 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 7 | // SwapChain9.cpp: Implements a back-end specific class for the D3D9 swap chain. |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 8 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 9 | #include "libGLESv2/renderer/SwapChain9.h" |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 10 | |
| 11 | #include "common/debug.h" |
| 12 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 13 | #include "libGLESv2/renderer/renderer9_utils.h" |
| 14 | #include "libGLESv2/renderer/Renderer9.h" |
| 15 | #include "libGLESv2/renderer/RenderTarget9.h" |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 16 | #include "libGLESv2/Context.h" |
daniel@transgaming.com | cfa8efd | 2012-11-28 19:30:55 +0000 | [diff] [blame] | 17 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 18 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 19 | namespace rx |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 20 | { |
| 21 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 22 | SwapChain9::SwapChain9(Renderer9 *renderer, HWND window, HANDLE shareHandle, |
| 23 | GLenum backBufferFormat, GLenum depthBufferFormat) |
| 24 | : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 25 | { |
| 26 | mSwapChain = NULL; |
| 27 | mBackBuffer = NULL; |
| 28 | mDepthStencil = NULL; |
| 29 | mRenderTarget = NULL; |
| 30 | mOffscreenTexture = NULL; |
| 31 | mWidth = -1; |
| 32 | mHeight = -1; |
| 33 | } |
| 34 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 35 | SwapChain9::~SwapChain9() |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 36 | { |
| 37 | release(); |
| 38 | } |
| 39 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 40 | void SwapChain9::release() |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 41 | { |
| 42 | if (mSwapChain) |
| 43 | { |
| 44 | mSwapChain->Release(); |
| 45 | mSwapChain = NULL; |
| 46 | } |
| 47 | |
| 48 | if (mBackBuffer) |
| 49 | { |
| 50 | mBackBuffer->Release(); |
| 51 | mBackBuffer = NULL; |
| 52 | } |
| 53 | |
| 54 | if (mDepthStencil) |
| 55 | { |
| 56 | mDepthStencil->Release(); |
| 57 | mDepthStencil = NULL; |
| 58 | } |
| 59 | |
| 60 | if (mRenderTarget) |
| 61 | { |
| 62 | mRenderTarget->Release(); |
| 63 | mRenderTarget = NULL; |
| 64 | } |
| 65 | |
| 66 | if (mOffscreenTexture) |
| 67 | { |
| 68 | mOffscreenTexture->Release(); |
| 69 | mOffscreenTexture = NULL; |
| 70 | } |
| 71 | |
daniel@transgaming.com | 21cfaef | 2012-10-31 18:42:43 +0000 | [diff] [blame] | 72 | if (mWindow) |
| 73 | mShareHandle = NULL; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static DWORD convertInterval(EGLint interval) |
| 77 | { |
| 78 | switch(interval) |
| 79 | { |
| 80 | case 0: return D3DPRESENT_INTERVAL_IMMEDIATE; |
| 81 | case 1: return D3DPRESENT_INTERVAL_ONE; |
| 82 | case 2: return D3DPRESENT_INTERVAL_TWO; |
| 83 | case 3: return D3DPRESENT_INTERVAL_THREE; |
| 84 | case 4: return D3DPRESENT_INTERVAL_FOUR; |
| 85 | default: UNREACHABLE(); |
| 86 | } |
| 87 | |
| 88 | return D3DPRESENT_INTERVAL_DEFAULT; |
| 89 | } |
| 90 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 91 | EGLint SwapChain9::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 92 | { |
| 93 | IDirect3DDevice9 *device = mRenderer->getDevice(); |
| 94 | |
| 95 | if (device == NULL) |
| 96 | { |
| 97 | return EGL_BAD_ACCESS; |
| 98 | } |
| 99 | |
| 100 | // Evict all non-render target textures to system memory and release all resources |
| 101 | // before reallocating them to free up as much video memory as possible. |
| 102 | device->EvictManagedResources(); |
| 103 | |
| 104 | HRESULT result; |
| 105 | |
| 106 | // Release specific resources to free up memory for the new render target, while the |
| 107 | // old render target still exists for the purpose of preserving its contents. |
| 108 | if (mSwapChain) |
| 109 | { |
| 110 | mSwapChain->Release(); |
| 111 | mSwapChain = NULL; |
| 112 | } |
| 113 | |
| 114 | if (mBackBuffer) |
| 115 | { |
| 116 | mBackBuffer->Release(); |
| 117 | mBackBuffer = NULL; |
| 118 | } |
| 119 | |
| 120 | if (mOffscreenTexture) |
| 121 | { |
| 122 | mOffscreenTexture->Release(); |
| 123 | mOffscreenTexture = NULL; |
| 124 | } |
| 125 | |
| 126 | if (mDepthStencil) |
| 127 | { |
| 128 | mDepthStencil->Release(); |
| 129 | mDepthStencil = NULL; |
| 130 | } |
| 131 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 132 | HANDLE *pShareHandle = NULL; |
| 133 | if (!mWindow && mRenderer->getShareHandleSupport()) |
| 134 | { |
| 135 | pShareHandle = &mShareHandle; |
| 136 | } |
| 137 | |
| 138 | result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET, |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 139 | gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT, |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 140 | &mOffscreenTexture, pShareHandle); |
| 141 | if (FAILED(result)) |
| 142 | { |
| 143 | ERR("Could not create offscreen texture: %08lX", result); |
| 144 | release(); |
| 145 | |
| 146 | if(isDeviceLostError(result)) |
| 147 | { |
| 148 | return EGL_CONTEXT_LOST; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | return EGL_BAD_ALLOC; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | IDirect3DSurface9 *oldRenderTarget = mRenderTarget; |
| 157 | |
| 158 | result = mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); |
| 159 | ASSERT(SUCCEEDED(result)); |
| 160 | |
| 161 | if (oldRenderTarget) |
| 162 | { |
| 163 | RECT rect = |
| 164 | { |
| 165 | 0, 0, |
| 166 | mWidth, mHeight |
| 167 | }; |
| 168 | |
| 169 | if (rect.right > static_cast<LONG>(backbufferWidth)) |
| 170 | { |
| 171 | rect.right = backbufferWidth; |
| 172 | } |
| 173 | |
| 174 | if (rect.bottom > static_cast<LONG>(backbufferHeight)) |
| 175 | { |
| 176 | rect.bottom = backbufferHeight; |
| 177 | } |
| 178 | |
| 179 | mRenderer->endScene(); |
| 180 | |
| 181 | result = device->StretchRect(oldRenderTarget, &rect, mRenderTarget, &rect, D3DTEXF_NONE); |
| 182 | ASSERT(SUCCEEDED(result)); |
| 183 | |
| 184 | oldRenderTarget->Release(); |
| 185 | } |
| 186 | |
| 187 | if (mWindow) |
| 188 | { |
| 189 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 190 | presentParameters.AutoDepthStencilFormat = gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 191 | presentParameters.BackBufferCount = 1; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 192 | presentParameters.BackBufferFormat = gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 193 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 194 | presentParameters.Flags = 0; |
| 195 | presentParameters.hDeviceWindow = mWindow; |
| 196 | presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented |
| 197 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented |
| 198 | presentParameters.PresentationInterval = convertInterval(swapInterval); |
| 199 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 200 | presentParameters.Windowed = TRUE; |
| 201 | presentParameters.BackBufferWidth = backbufferWidth; |
| 202 | presentParameters.BackBufferHeight = backbufferHeight; |
| 203 | |
| 204 | // http://crbug.com/140239 |
| 205 | // http://crbug.com/143434 |
| 206 | // |
| 207 | // Some AMD/Intel switchable systems / drivers appear to round swap chain surfaces to a multiple of 64 pixels in width |
| 208 | // when using the integrated Intel. This rounds the width up rather than down. |
| 209 | // |
| 210 | // Some non-switchable AMD GPUs / drivers do not respect the source rectangle to Present. Therefore, when the vendor ID |
| 211 | // is not Intel, the back buffer width must be exactly the same width as the window or horizontal scaling will occur. |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame] | 212 | if (mRenderer->getAdapterVendor() == VENDOR_ID_INTEL) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 213 | { |
| 214 | presentParameters.BackBufferWidth = (presentParameters.BackBufferWidth + 63) / 64 * 64; |
| 215 | } |
| 216 | |
| 217 | result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); |
| 218 | |
| 219 | if (FAILED(result)) |
| 220 | { |
| 221 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL || result == D3DERR_DEVICELOST); |
| 222 | |
| 223 | ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); |
| 224 | release(); |
| 225 | |
| 226 | if(isDeviceLostError(result)) |
| 227 | { |
| 228 | return EGL_CONTEXT_LOST; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | return EGL_BAD_ALLOC; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer); |
| 237 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | b323df0 | 2012-11-28 19:41:36 +0000 | [diff] [blame] | 238 | InvalidateRect(mWindow, NULL, FALSE); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 239 | } |
| 240 | |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 241 | if (mDepthBufferFormat != GL_NONE) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 242 | { |
| 243 | result = device->CreateDepthStencilSurface(backbufferWidth, backbufferHeight, |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 244 | gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat), |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 245 | D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, NULL); |
| 246 | |
| 247 | if (FAILED(result)) |
| 248 | { |
| 249 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL); |
| 250 | |
| 251 | ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); |
| 252 | release(); |
| 253 | |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 254 | if (isDeviceLostError(result)) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 255 | { |
| 256 | return EGL_CONTEXT_LOST; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | return EGL_BAD_ALLOC; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | mWidth = backbufferWidth; |
| 266 | mHeight = backbufferHeight; |
| 267 | |
| 268 | return EGL_SUCCESS; |
| 269 | } |
| 270 | |
| 271 | // parameters should be validated/clamped by caller |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 272 | EGLint SwapChain9::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 273 | { |
| 274 | if (!mSwapChain) |
| 275 | { |
| 276 | return EGL_SUCCESS; |
| 277 | } |
| 278 | |
| 279 | IDirect3DDevice9 *device = mRenderer->getDevice(); |
| 280 | |
| 281 | // Disable all pipeline operations |
| 282 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 283 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 284 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 285 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 286 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 287 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 288 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 289 | device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); |
| 290 | device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); |
| 291 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 292 | device->SetPixelShader(NULL); |
| 293 | device->SetVertexShader(NULL); |
| 294 | |
| 295 | device->SetRenderTarget(0, mBackBuffer); |
| 296 | device->SetDepthStencilSurface(NULL); |
| 297 | |
| 298 | device->SetTexture(0, mOffscreenTexture); |
| 299 | device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 300 | device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); |
| 301 | device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); |
| 302 | device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); |
| 303 | device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); |
| 304 | device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); |
| 305 | device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); |
| 306 | device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); |
| 307 | |
| 308 | D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f}; |
| 309 | device->SetViewport(&viewport); |
| 310 | |
| 311 | float x1 = x - 0.5f; |
| 312 | float y1 = (mHeight - y - height) - 0.5f; |
| 313 | float x2 = (x + width) - 0.5f; |
| 314 | float y2 = (mHeight - y) - 0.5f; |
| 315 | |
| 316 | float u1 = x / float(mWidth); |
| 317 | float v1 = y / float(mHeight); |
| 318 | float u2 = (x + width) / float(mWidth); |
| 319 | float v2 = (y + height) / float(mHeight); |
| 320 | |
| 321 | float quad[4][6] = {{x1, y1, 0.0f, 1.0f, u1, v2}, |
| 322 | {x2, y1, 0.0f, 1.0f, u2, v2}, |
| 323 | {x2, y2, 0.0f, 1.0f, u2, v1}, |
| 324 | {x1, y2, 0.0f, 1.0f, u1, v1}}; // x, y, z, rhw, u, v |
| 325 | |
| 326 | mRenderer->startScene(); |
| 327 | device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float)); |
| 328 | mRenderer->endScene(); |
| 329 | |
| 330 | device->SetTexture(0, NULL); |
| 331 | |
| 332 | RECT rect = |
| 333 | { |
| 334 | x, mHeight - y - height, |
| 335 | x + width, mHeight - y |
| 336 | }; |
| 337 | |
| 338 | HRESULT result = mSwapChain->Present(&rect, &rect, NULL, NULL, 0); |
| 339 | |
| 340 | gl::Context *context = static_cast<gl::Context*>(glGetCurrentContext()); |
| 341 | if (context) |
| 342 | { |
| 343 | context->markAllStateDirty(); |
| 344 | } |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 345 | mRenderer->markAllStateDirty(); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 346 | |
| 347 | if (isDeviceLostError(result)) |
| 348 | { |
| 349 | return EGL_CONTEXT_LOST; |
| 350 | } |
| 351 | |
| 352 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR) |
| 353 | { |
| 354 | return EGL_BAD_ALLOC; |
| 355 | } |
| 356 | |
| 357 | ASSERT(SUCCEEDED(result)); |
| 358 | |
| 359 | return EGL_SUCCESS; |
| 360 | } |
| 361 | |
| 362 | // Increments refcount on surface. |
| 363 | // caller must Release() the returned surface |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 364 | IDirect3DSurface9 *SwapChain9::getRenderTarget() |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 365 | { |
| 366 | if (mRenderTarget) |
| 367 | { |
| 368 | mRenderTarget->AddRef(); |
| 369 | } |
| 370 | |
| 371 | return mRenderTarget; |
| 372 | } |
| 373 | |
| 374 | // Increments refcount on surface. |
| 375 | // caller must Release() the returned surface |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 376 | IDirect3DSurface9 *SwapChain9::getDepthStencil() |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 377 | { |
| 378 | if (mDepthStencil) |
| 379 | { |
| 380 | mDepthStencil->AddRef(); |
| 381 | } |
| 382 | |
| 383 | return mDepthStencil; |
| 384 | } |
| 385 | |
| 386 | // Increments refcount on texture. |
| 387 | // caller must Release() the returned texture |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 388 | IDirect3DTexture9 *SwapChain9::getOffscreenTexture() |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 389 | { |
| 390 | if (mOffscreenTexture) |
| 391 | { |
| 392 | mOffscreenTexture->AddRef(); |
| 393 | } |
| 394 | |
| 395 | return mOffscreenTexture; |
| 396 | } |
| 397 | |
daniel@transgaming.com | d733bb8 | 2012-11-28 20:53:40 +0000 | [diff] [blame^] | 398 | SwapChain9 *SwapChain9::makeSwapChain9(SwapChain *swapChain) |
| 399 | { |
| 400 | ASSERT(dynamic_cast<rx::SwapChain9*>(swapChain) != NULL); |
| 401 | return static_cast<rx::SwapChain9*>(swapChain); |
| 402 | } |
| 403 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 404 | } |