1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Surface.cpp: Implements the egl::Surface class, representing a drawing surface
// such as the client area of a window, including any back buffers.
// Implements EGLSurface and related functionality. [EGL 1.4] section 2.2 page 3.
#include "libEGL/Surface.h"
#include "common/debug.h"
#include "libEGL/main.h"
#include "libEGL/Display.h"
namespace egl
{
Surface::Surface(Display *display, IDirect3DSwapChain9 *swapChain, IDirect3DSurface9 *depthStencil, const Config *config)
: mDisplay(display), mSwapChain(swapChain), mDepthStencil(depthStencil), mConfig(config)
{
mBackBuffer = NULL;
mRenderTarget = NULL;
mFlipTexture = NULL;
mFlipState = NULL;
mPreFlipState = NULL;
mPixelAspectRatio = (EGLint)(1.0 * EGL_DISPLAY_SCALING); // FIXME: Determine actual pixel aspect ratio
mRenderBuffer = EGL_BACK_BUFFER;
mSwapBehavior = EGL_BUFFER_PRESERVED;
if (mSwapChain)
{
mSwapChain->AddRef();
mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer);
D3DSURFACE_DESC description;
mBackBuffer->GetDesc(&description);
mWidth = description.Width;
mHeight = description.Height;
IDirect3DDevice9 *device = display->getDevice();
HRESULT result = device->CreateRenderTarget(mWidth, mHeight, description.Format, description.MultiSampleType, description.MultiSampleQuality, FALSE, &mRenderTarget, NULL);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{
error(EGL_BAD_ALLOC);
return;
}
ASSERT(SUCCEEDED(result));
result = device->CreateTexture(mWidth, mHeight, 1, D3DUSAGE_RENDERTARGET, description.Format, D3DPOOL_DEFAULT, &mFlipTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
error(EGL_BAD_ALLOC);
mRenderTarget->Release();
return;
}
}
}
Surface::~Surface()
{
if (mSwapChain)
{
mSwapChain->Release();
}
if (mBackBuffer)
{
mBackBuffer->Release();
}
if (mRenderTarget)
{
mRenderTarget->Release();
}
if (mDepthStencil)
{
mDepthStencil->Release();
}
if (mFlipTexture)
{
mFlipTexture->Release();
}
if (mFlipState)
{
mFlipState->Release();
}
if (mPreFlipState)
{
mPreFlipState->Release();
}
}
HWND Surface::getWindowHandle()
{
if (mSwapChain)
{
D3DPRESENT_PARAMETERS presentParameters;
mSwapChain->GetPresentParameters(&presentParameters);
return presentParameters.hDeviceWindow;
}
return NULL;
}
void Surface::writeRecordableFlipState(IDirect3DDevice9 *device, IDirect3DTexture9 *source)
{
// Disable all pipeline operations
device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
device->SetPixelShader(NULL);
device->SetVertexShader(NULL);
// Just sample the texture
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
device->SetTexture(0, source);
device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE);
device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
device->SetStreamSourceFreq(0, 1); // DrawPrimitiveUP only cares about stream 0, not the rest.
}
void Surface::applyFlipState(IDirect3DDevice9 *device, IDirect3DTexture9 *source)
{
HRESULT hr;
if (mFlipState == NULL)
{
// Create two state blocks both recording the states that are changed when swapping.
// mPreFlipState will record the original state each entry.
hr = device->BeginStateBlock();
ASSERT(SUCCEEDED(hr));
writeRecordableFlipState(device, source);
hr = device->EndStateBlock(&mPreFlipState);
ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
if (SUCCEEDED(hr))
{
mPreFlipState->Capture();
}
// mFlipState will record the state for the swap operation.
hr = device->BeginStateBlock();
ASSERT(SUCCEEDED(hr));
writeRecordableFlipState(device, source);
hr = device->EndStateBlock(&mFlipState);
ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
if (FAILED(hr))
{
mFlipState = NULL;
mPreFlipState->Release();
mPreFlipState = NULL;
}
else
{
hr = mFlipState->Apply();
ASSERT(SUCCEEDED(hr));
}
}
else
{
hr = mPreFlipState->Capture();
ASSERT(SUCCEEDED(hr));
hr = mFlipState->Apply();
ASSERT(SUCCEEDED(hr));
}
device->GetRenderTarget(0, &mPreFlipBackBuffer);
device->GetDepthStencilSurface(&mPreFlipDepthStencil);
device->SetRenderTarget(0, mBackBuffer);
device->SetDepthStencilSurface(NULL);
}
void Surface::restoreState(IDirect3DDevice9 *device)
{
mPreFlipState->Apply();
device->SetRenderTarget(0, mPreFlipBackBuffer);
device->SetDepthStencilSurface(mPreFlipDepthStencil);
if (mPreFlipBackBuffer)
{
mPreFlipBackBuffer->Release();
mPreFlipBackBuffer = NULL;
}
if (mPreFlipDepthStencil)
{
mPreFlipDepthStencil->Release();
mPreFlipDepthStencil = NULL;
}
}
bool Surface::swap()
{
if (mSwapChain)
{
IDirect3DDevice9 *device = mDisplay->getDevice();
IDirect3DSurface9 *textureSurface;
mFlipTexture->GetSurfaceLevel(0, &textureSurface);
mDisplay->endScene();
device->StretchRect(mRenderTarget, NULL, textureSurface, NULL, D3DTEXF_NONE);
applyFlipState(device, mFlipTexture);
// Render the texture upside down into the back buffer
float quad[4][6] = {{ 0 - 0.5f, 0 - 0.5f, 0.0f, 1.0f, 0.0f, 1.0f},
{mWidth - 0.5f, 0 - 0.5f, 0.0f, 1.0f, 1.0f, 1.0f},
{mWidth - 0.5f, mHeight - 0.5f, 0.0f, 1.0f, 1.0f, 0.0f},
{ 0 - 0.5f, mHeight - 0.5f, 0.0f, 1.0f, 0.0f, 0.0f}}; // x, y, z, rhw, u, v
mDisplay->startScene();
device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float));
textureSurface->Release();
restoreState(device);
mDisplay->endScene();
HRESULT result = mSwapChain->Present(NULL, NULL, NULL, NULL, mDisplay->getPresentInterval());
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR)
{
return error(EGL_BAD_ALLOC, false);
}
if (result == D3DERR_DEVICELOST)
{
return error(EGL_CONTEXT_LOST, false);
}
ASSERT(SUCCEEDED(result));
}
return true;
}
EGLint Surface::getWidth() const
{
return mWidth;
}
EGLint Surface::getHeight() const
{
return mHeight;
}
IDirect3DSurface9 *Surface::getRenderTarget()
{
if (mRenderTarget)
{
mRenderTarget->AddRef();
}
return mRenderTarget;
}
IDirect3DSurface9 *Surface::getDepthStencil()
{
if (mDepthStencil)
{
mDepthStencil->AddRef();
}
return mDepthStencil;
}
}
|