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 | |
| 7 | // SwapChain.cpp: Implements a back-end specific class that hides the details of the |
| 8 | // implementation-specific swapchain. |
| 9 | |
| 10 | #include "libGLESv2/renderer/SwapChain.h" |
| 11 | |
| 12 | #include "common/debug.h" |
| 13 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame] | 14 | #include "libGLESv2/renderer/renderer9_utils.h" // D3D9_REPLACE |
| 15 | #include "libGLESv2/renderer/Renderer9.h" // D3D9_REPLACE |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 16 | #include "libGLESv2/Context.h" |
| 17 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame^] | 18 | namespace rx |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 19 | { |
| 20 | |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame] | 21 | SwapChain::SwapChain(Renderer9 *renderer, HWND window, HANDLE shareHandle, |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 22 | GLenum backBufferFormat, GLenum depthBufferFormat) |
| 23 | : mRenderer(renderer), mWindow(window), mShareHandle(shareHandle), |
| 24 | mBackBufferFormat(backBufferFormat), mDepthBufferFormat(depthBufferFormat) |
| 25 | { |
| 26 | mSwapChain = NULL; |
| 27 | mBackBuffer = NULL; |
| 28 | mDepthStencil = NULL; |
| 29 | mRenderTarget = NULL; |
| 30 | mOffscreenTexture = NULL; |
| 31 | mWidth = -1; |
| 32 | mHeight = -1; |
| 33 | } |
| 34 | |
| 35 | SwapChain::~SwapChain() |
| 36 | { |
| 37 | release(); |
| 38 | } |
| 39 | |
| 40 | void SwapChain::release() |
| 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 | |
| 91 | // D3D9_REPLACE |
| 92 | EGLint SwapChain::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) |
| 93 | { |
| 94 | IDirect3DDevice9 *device = mRenderer->getDevice(); |
| 95 | |
| 96 | if (device == NULL) |
| 97 | { |
| 98 | return EGL_BAD_ACCESS; |
| 99 | } |
| 100 | |
| 101 | // Evict all non-render target textures to system memory and release all resources |
| 102 | // before reallocating them to free up as much video memory as possible. |
| 103 | device->EvictManagedResources(); |
| 104 | |
| 105 | HRESULT result; |
| 106 | |
| 107 | // Release specific resources to free up memory for the new render target, while the |
| 108 | // old render target still exists for the purpose of preserving its contents. |
| 109 | if (mSwapChain) |
| 110 | { |
| 111 | mSwapChain->Release(); |
| 112 | mSwapChain = NULL; |
| 113 | } |
| 114 | |
| 115 | if (mBackBuffer) |
| 116 | { |
| 117 | mBackBuffer->Release(); |
| 118 | mBackBuffer = NULL; |
| 119 | } |
| 120 | |
| 121 | if (mOffscreenTexture) |
| 122 | { |
| 123 | mOffscreenTexture->Release(); |
| 124 | mOffscreenTexture = NULL; |
| 125 | } |
| 126 | |
| 127 | if (mDepthStencil) |
| 128 | { |
| 129 | mDepthStencil->Release(); |
| 130 | mDepthStencil = NULL; |
| 131 | } |
| 132 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 133 | HANDLE *pShareHandle = NULL; |
| 134 | if (!mWindow && mRenderer->getShareHandleSupport()) |
| 135 | { |
| 136 | pShareHandle = &mShareHandle; |
| 137 | } |
| 138 | |
| 139 | result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET, |
| 140 | es2dx::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT, |
| 141 | &mOffscreenTexture, pShareHandle); |
| 142 | if (FAILED(result)) |
| 143 | { |
| 144 | ERR("Could not create offscreen texture: %08lX", result); |
| 145 | release(); |
| 146 | |
| 147 | if(isDeviceLostError(result)) |
| 148 | { |
| 149 | return EGL_CONTEXT_LOST; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | return EGL_BAD_ALLOC; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | IDirect3DSurface9 *oldRenderTarget = mRenderTarget; |
| 158 | |
| 159 | result = mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); |
| 160 | ASSERT(SUCCEEDED(result)); |
| 161 | |
| 162 | if (oldRenderTarget) |
| 163 | { |
| 164 | RECT rect = |
| 165 | { |
| 166 | 0, 0, |
| 167 | mWidth, mHeight |
| 168 | }; |
| 169 | |
| 170 | if (rect.right > static_cast<LONG>(backbufferWidth)) |
| 171 | { |
| 172 | rect.right = backbufferWidth; |
| 173 | } |
| 174 | |
| 175 | if (rect.bottom > static_cast<LONG>(backbufferHeight)) |
| 176 | { |
| 177 | rect.bottom = backbufferHeight; |
| 178 | } |
| 179 | |
| 180 | mRenderer->endScene(); |
| 181 | |
| 182 | result = device->StretchRect(oldRenderTarget, &rect, mRenderTarget, &rect, D3DTEXF_NONE); |
| 183 | ASSERT(SUCCEEDED(result)); |
| 184 | |
| 185 | oldRenderTarget->Release(); |
| 186 | } |
| 187 | |
| 188 | if (mWindow) |
| 189 | { |
| 190 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 191 | presentParameters.AutoDepthStencilFormat = es2dx::ConvertRenderbufferFormat(mDepthBufferFormat); |
| 192 | presentParameters.BackBufferCount = 1; |
| 193 | presentParameters.BackBufferFormat = es2dx::ConvertRenderbufferFormat(mBackBufferFormat); |
| 194 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 195 | presentParameters.Flags = 0; |
| 196 | presentParameters.hDeviceWindow = mWindow; |
| 197 | presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented |
| 198 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented |
| 199 | presentParameters.PresentationInterval = convertInterval(swapInterval); |
| 200 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 201 | presentParameters.Windowed = TRUE; |
| 202 | presentParameters.BackBufferWidth = backbufferWidth; |
| 203 | presentParameters.BackBufferHeight = backbufferHeight; |
| 204 | |
| 205 | // http://crbug.com/140239 |
| 206 | // http://crbug.com/143434 |
| 207 | // |
| 208 | // Some AMD/Intel switchable systems / drivers appear to round swap chain surfaces to a multiple of 64 pixels in width |
| 209 | // when using the integrated Intel. This rounds the width up rather than down. |
| 210 | // |
| 211 | // Some non-switchable AMD GPUs / drivers do not respect the source rectangle to Present. Therefore, when the vendor ID |
| 212 | // 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] | 213 | if (mRenderer->getAdapterVendor() == VENDOR_ID_INTEL) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 214 | { |
| 215 | presentParameters.BackBufferWidth = (presentParameters.BackBufferWidth + 63) / 64 * 64; |
| 216 | } |
| 217 | |
| 218 | result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); |
| 219 | |
| 220 | if (FAILED(result)) |
| 221 | { |
| 222 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL || result == D3DERR_DEVICELOST); |
| 223 | |
| 224 | ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); |
| 225 | release(); |
| 226 | |
| 227 | if(isDeviceLostError(result)) |
| 228 | { |
| 229 | return EGL_CONTEXT_LOST; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | return EGL_BAD_ALLOC; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer); |
| 238 | ASSERT(SUCCEEDED(result)); |
| 239 | } |
| 240 | |
| 241 | if (mDepthBufferFormat != D3DFMT_UNKNOWN) |
| 242 | { |
| 243 | result = device->CreateDepthStencilSurface(backbufferWidth, backbufferHeight, |
| 244 | es2dx::ConvertRenderbufferFormat(mDepthBufferFormat), |
| 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 | |
| 254 | if(isDeviceLostError(result)) |
| 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 |
| 272 | EGLint SwapChain::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
| 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 | } |
| 345 | |
| 346 | if (isDeviceLostError(result)) |
| 347 | { |
| 348 | return EGL_CONTEXT_LOST; |
| 349 | } |
| 350 | |
| 351 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR) |
| 352 | { |
| 353 | return EGL_BAD_ALLOC; |
| 354 | } |
| 355 | |
| 356 | ASSERT(SUCCEEDED(result)); |
| 357 | |
| 358 | return EGL_SUCCESS; |
| 359 | } |
| 360 | |
| 361 | // Increments refcount on surface. |
| 362 | // caller must Release() the returned surface |
| 363 | IDirect3DSurface9 *SwapChain::getRenderTarget() |
| 364 | { |
| 365 | if (mRenderTarget) |
| 366 | { |
| 367 | mRenderTarget->AddRef(); |
| 368 | } |
| 369 | |
| 370 | return mRenderTarget; |
| 371 | } |
| 372 | |
| 373 | // Increments refcount on surface. |
| 374 | // caller must Release() the returned surface |
| 375 | IDirect3DSurface9 *SwapChain::getDepthStencil() |
| 376 | { |
| 377 | if (mDepthStencil) |
| 378 | { |
| 379 | mDepthStencil->AddRef(); |
| 380 | } |
| 381 | |
| 382 | return mDepthStencil; |
| 383 | } |
| 384 | |
| 385 | // Increments refcount on texture. |
| 386 | // caller must Release() the returned texture |
| 387 | IDirect3DTexture9 *SwapChain::getOffscreenTexture() |
| 388 | { |
| 389 | if (mOffscreenTexture) |
| 390 | { |
| 391 | mOffscreenTexture->AddRef(); |
| 392 | } |
| 393 | |
| 394 | return mOffscreenTexture; |
| 395 | } |
| 396 | |
| 397 | } |