1 #include "precompiled.h"
3 // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 // SwapChain9.cpp: Implements a back-end specific class for the D3D9 swap chain.
10 #include "libGLESv2/renderer/d3d9/SwapChain9.h"
11 #include "libGLESv2/renderer/d3d9/renderer9_utils.h"
12 #include "libGLESv2/renderer/d3d9/Renderer9.h"
17 SwapChain9::SwapChain9(Renderer9 *renderer, HWND window, HANDLE shareHandle,
18 GLenum backBufferFormat, GLenum depthBufferFormat)
19 : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat)
25 mOffscreenTexture = NULL;
31 SwapChain9::~SwapChain9()
36 void SwapChain9::release()
40 mSwapChain->Release();
46 mBackBuffer->Release();
52 mDepthStencil->Release();
58 mRenderTarget->Release();
62 if (mOffscreenTexture)
64 mOffscreenTexture->Release();
65 mOffscreenTexture = NULL;
72 static DWORD convertInterval(EGLint interval)
74 #if ANGLE_FORCE_VSYNC_OFF
75 return D3DPRESENT_INTERVAL_IMMEDIATE;
79 case 0: return D3DPRESENT_INTERVAL_IMMEDIATE;
80 case 1: return D3DPRESENT_INTERVAL_ONE;
81 case 2: return D3DPRESENT_INTERVAL_TWO;
82 case 3: return D3DPRESENT_INTERVAL_THREE;
83 case 4: return D3DPRESENT_INTERVAL_FOUR;
84 default: UNREACHABLE();
87 return D3DPRESENT_INTERVAL_DEFAULT;
91 EGLint SwapChain9::resize(int backbufferWidth, int backbufferHeight)
93 // D3D9 does not support resizing swap chains without recreating them
94 return reset(backbufferWidth, backbufferHeight, mSwapInterval);
97 EGLint SwapChain9::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval)
99 IDirect3DDevice9 *device = mRenderer->getDevice();
103 return EGL_BAD_ACCESS;
106 // Evict all non-render target textures to system memory and release all resources
107 // before reallocating them to free up as much video memory as possible.
108 device->EvictManagedResources();
112 // Release specific resources to free up memory for the new render target, while the
113 // old render target still exists for the purpose of preserving its contents.
116 mSwapChain->Release();
122 mBackBuffer->Release();
126 if (mOffscreenTexture)
128 mOffscreenTexture->Release();
129 mOffscreenTexture = NULL;
134 mDepthStencil->Release();
135 mDepthStencil = NULL;
138 HANDLE *pShareHandle = NULL;
139 if (!mWindow && mRenderer->getShareHandleSupport())
141 pShareHandle = &mShareHandle;
144 result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET,
145 gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT,
146 &mOffscreenTexture, pShareHandle);
149 ERR("Could not create offscreen texture: %08lX", result);
152 if (d3d9::isDeviceLostError(result))
154 return EGL_CONTEXT_LOST;
158 return EGL_BAD_ALLOC;
162 IDirect3DSurface9 *oldRenderTarget = mRenderTarget;
164 result = mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget);
165 ASSERT(SUCCEEDED(result));
175 if (rect.right > static_cast<LONG>(backbufferWidth))
177 rect.right = backbufferWidth;
180 if (rect.bottom > static_cast<LONG>(backbufferHeight))
182 rect.bottom = backbufferHeight;
185 mRenderer->endScene();
187 result = device->StretchRect(oldRenderTarget, &rect, mRenderTarget, &rect, D3DTEXF_NONE);
188 ASSERT(SUCCEEDED(result));
190 oldRenderTarget->Release();
195 D3DPRESENT_PARAMETERS presentParameters = {0};
196 presentParameters.AutoDepthStencilFormat = gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat);
197 presentParameters.BackBufferCount = 1;
198 presentParameters.BackBufferFormat = gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat);
199 presentParameters.EnableAutoDepthStencil = FALSE;
200 presentParameters.Flags = 0;
201 presentParameters.hDeviceWindow = mWindow;
202 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
203 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
204 presentParameters.PresentationInterval = convertInterval(swapInterval);
205 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
206 presentParameters.Windowed = TRUE;
207 presentParameters.BackBufferWidth = backbufferWidth;
208 presentParameters.BackBufferHeight = backbufferHeight;
210 // http://crbug.com/140239
211 // http://crbug.com/143434
213 // Some AMD/Intel switchable systems / drivers appear to round swap chain surfaces to a multiple of 64 pixels in width
214 // when using the integrated Intel. This rounds the width up rather than down.
216 // Some non-switchable AMD GPUs / drivers do not respect the source rectangle to Present. Therefore, when the vendor ID
217 // is not Intel, the back buffer width must be exactly the same width as the window or horizontal scaling will occur.
218 if (mRenderer->getAdapterVendor() == VENDOR_ID_INTEL)
220 presentParameters.BackBufferWidth = (presentParameters.BackBufferWidth + 63) / 64 * 64;
223 result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain);
227 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL || result == D3DERR_DEVICELOST);
229 ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result);
232 if (d3d9::isDeviceLostError(result))
234 return EGL_CONTEXT_LOST;
238 return EGL_BAD_ALLOC;
242 result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer);
243 ASSERT(SUCCEEDED(result));
244 InvalidateRect(mWindow, NULL, FALSE);
247 if (mDepthBufferFormat != GL_NONE)
249 result = device->CreateDepthStencilSurface(backbufferWidth, backbufferHeight,
250 gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat),
251 D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, NULL);
255 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL);
257 ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result);
260 if (d3d9::isDeviceLostError(result))
262 return EGL_CONTEXT_LOST;
266 return EGL_BAD_ALLOC;
271 mWidth = backbufferWidth;
272 mHeight = backbufferHeight;
273 mSwapInterval = swapInterval;
278 // parameters should be validated/clamped by caller
279 EGLint SwapChain9::swapRect(EGLint x, EGLint y, EGLint width, EGLint height)
286 IDirect3DDevice9 *device = mRenderer->getDevice();
288 // Disable all pipeline operations
289 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
290 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
291 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
292 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
293 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
294 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
295 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
296 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
297 device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
298 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
299 device->SetPixelShader(NULL);
300 device->SetVertexShader(NULL);
302 device->SetRenderTarget(0, mBackBuffer);
303 device->SetDepthStencilSurface(NULL);
305 device->SetTexture(0, mOffscreenTexture);
306 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
307 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
308 device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
309 device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
310 device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
311 device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
312 device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
313 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
315 for (UINT streamIndex = 0; streamIndex < gl::MAX_VERTEX_ATTRIBS; streamIndex++)
317 device->SetStreamSourceFreq(streamIndex, 1);
320 D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f};
321 device->SetViewport(&viewport);
324 float y1 = (mHeight - y - height) - 0.5f;
325 float x2 = (x + width) - 0.5f;
326 float y2 = (mHeight - y) - 0.5f;
328 float u1 = x / float(mWidth);
329 float v1 = y / float(mHeight);
330 float u2 = (x + width) / float(mWidth);
331 float v2 = (y + height) / float(mHeight);
333 float quad[4][6] = {{x1, y1, 0.0f, 1.0f, u1, v2},
334 {x2, y1, 0.0f, 1.0f, u2, v2},
335 {x2, y2, 0.0f, 1.0f, u2, v1},
336 {x1, y2, 0.0f, 1.0f, u1, v1}}; // x, y, z, rhw, u, v
338 mRenderer->startScene();
339 device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float));
340 mRenderer->endScene();
342 device->SetTexture(0, NULL);
346 x, mHeight - y - height,
347 x + width, mHeight - y
350 HRESULT result = mSwapChain->Present(&rect, &rect, NULL, NULL, 0);
352 mRenderer->markAllStateDirty();
354 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR)
356 return EGL_BAD_ALLOC;
359 // On Windows 8 systems, IDirect3DSwapChain9::Present sometimes returns 0x88760873 when the windows is
360 // in the process of entering/exiting fullscreen. This code doesn't seem to have any documentation. The
361 // device appears to be ok after emitting this error so simply return a failure to swap.
362 if (result == 0x88760873)
364 return EGL_BAD_NATIVE_WINDOW;
367 // http://crbug.com/313210
368 // If our swap failed, trigger a device lost event. Resetting will work around an AMD-specific
369 // device removed bug with lost contexts when reinstalling drivers.
372 mRenderer->notifyDeviceLost();
373 return EGL_CONTEXT_LOST;
379 // Increments refcount on surface.
380 // caller must Release() the returned surface
381 IDirect3DSurface9 *SwapChain9::getRenderTarget()
385 mRenderTarget->AddRef();
388 return mRenderTarget;
391 // Increments refcount on surface.
392 // caller must Release() the returned surface
393 IDirect3DSurface9 *SwapChain9::getDepthStencil()
397 mDepthStencil->AddRef();
400 return mDepthStencil;
403 // Increments refcount on texture.
404 // caller must Release() the returned texture
405 IDirect3DTexture9 *SwapChain9::getOffscreenTexture()
407 if (mOffscreenTexture)
409 mOffscreenTexture->AddRef();
412 return mOffscreenTexture;
415 SwapChain9 *SwapChain9::makeSwapChain9(SwapChain *swapChain)
417 ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain9*, swapChain));
418 return static_cast<rx::SwapChain9*>(swapChain);
421 void SwapChain9::recreate()
428 IDirect3DDevice9 *device = mRenderer->getDevice();
434 D3DPRESENT_PARAMETERS presentParameters;
435 HRESULT result = mSwapChain->GetPresentParameters(&presentParameters);
436 ASSERT(SUCCEEDED(result));
438 IDirect3DSwapChain9* newSwapChain = NULL;
439 result = device->CreateAdditionalSwapChain(&presentParameters, &newSwapChain);
445 mSwapChain->Release();
446 mSwapChain = newSwapChain;
448 mBackBuffer->Release();
449 result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer);
450 ASSERT(SUCCEEDED(result));