blob: c474933bd33351136c137ff1517c9af2bc276d2d [file] [log] [blame]
daniel@transgaming.com3c720782012-10-31 18:42:34 +00001//
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"
14#include "libGLESv2/renderer/Renderer.h"
15#include "libGLESv2/Context.h"
16
17namespace renderer
18{
19
20SwapChain::SwapChain(Renderer *renderer, HWND window, HANDLE shareHandle,
21 GLenum backBufferFormat, GLenum depthBufferFormat)
22 : mRenderer(renderer), mWindow(window), mShareHandle(shareHandle),
23 mBackBufferFormat(backBufferFormat), mDepthBufferFormat(depthBufferFormat)
24{
25 mSwapChain = NULL;
26 mBackBuffer = NULL;
27 mDepthStencil = NULL;
28 mRenderTarget = NULL;
29 mOffscreenTexture = NULL;
30 mWidth = -1;
31 mHeight = -1;
32}
33
34SwapChain::~SwapChain()
35{
36 release();
37}
38
39void SwapChain::release()
40{
41 if (mSwapChain)
42 {
43 mSwapChain->Release();
44 mSwapChain = NULL;
45 }
46
47 if (mBackBuffer)
48 {
49 mBackBuffer->Release();
50 mBackBuffer = NULL;
51 }
52
53 if (mDepthStencil)
54 {
55 mDepthStencil->Release();
56 mDepthStencil = NULL;
57 }
58
59 if (mRenderTarget)
60 {
61 mRenderTarget->Release();
62 mRenderTarget = NULL;
63 }
64
65 if (mOffscreenTexture)
66 {
67 mOffscreenTexture->Release();
68 mOffscreenTexture = NULL;
69 }
70
daniel@transgaming.com21cfaef2012-10-31 18:42:43 +000071 if (mWindow)
72 mShareHandle = NULL;
daniel@transgaming.com3c720782012-10-31 18:42:34 +000073}
74
75static DWORD convertInterval(EGLint interval)
76{
77 switch(interval)
78 {
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();
85 }
86
87 return D3DPRESENT_INTERVAL_DEFAULT;
88}
89
90// D3D9_REPLACE
91EGLint SwapChain::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval)
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.com3c720782012-10-31 18:42:34 +0000132 HANDLE *pShareHandle = NULL;
133 if (!mWindow && mRenderer->getShareHandleSupport())
134 {
135 pShareHandle = &mShareHandle;
136 }
137
138 result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET,
139 es2dx::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT,
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};
190 presentParameters.AutoDepthStencilFormat = es2dx::ConvertRenderbufferFormat(mDepthBufferFormat);
191 presentParameters.BackBufferCount = 1;
192 presentParameters.BackBufferFormat = es2dx::ConvertRenderbufferFormat(mBackBufferFormat);
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.
212 D3DADAPTER_IDENTIFIER9* adapterIdentifier = mRenderer->getAdapterIdentifier();
213 if (adapterIdentifier->VendorId == VENDOR_ID_INTEL)
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
272EGLint 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
363IDirect3DSurface9 *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
375IDirect3DSurface9 *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
387IDirect3DTexture9 *SwapChain::getOffscreenTexture()
388{
389 if (mOffscreenTexture)
390 {
391 mOffscreenTexture->AddRef();
392 }
393
394 return mOffscreenTexture;
395}
396
397}