blob: b9c84c025da6fb2dd2ff91f9279c712b30bbf951 [file] [log] [blame]
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001//
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.come979ead2010-09-23 18:03:14 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Display.cpp: Implements the egl::Display class, representing the abstract
8// display on which graphics are drawn. Implements EGLDisplay.
9// [EGL 1.4] section 2.1.2 page 3.
10
11#include "libEGL/Display.h"
12
13#include <algorithm>
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000014#include <map>
daniel@transgaming.come979ead2010-09-23 18:03:14 +000015#include <vector>
16
17#include "common/debug.h"
jbauman@chromium.org06d7a752011-04-30 01:02:52 +000018#include "libGLESv2/mathutil.h"
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +000019#include "libGLESv2/utilities.h"
daniel@transgaming.come979ead2010-09-23 18:03:14 +000020
21#include "libEGL/main.h"
22
baustin@google.com250f06c2011-06-06 16:59:53 +000023// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
24#define REF_RAST 0
25
26// The "Debug This Pixel..." feature in PIX often fails when using the
27// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
28// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
29#if !defined(ANGLE_ENABLE_D3D9EX)
30// Enables use of the IDirect3D9Ex interface, when available
31#define ANGLE_ENABLE_D3D9EX 1
32#endif // !defined(ANGLE_ENABLE_D3D9EX)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000033
34namespace egl
35{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000036namespace
37{
38 typedef std::map<EGLNativeDisplayType, Display*> DisplayMap;
39 DisplayMap displays;
40}
41
42egl::Display *Display::getDisplay(EGLNativeDisplayType displayId)
43{
44 if (displays.find(displayId) != displays.end())
45 {
46 return displays[displayId];
47 }
48
49 egl::Display *display = NULL;
50
51 if (displayId == EGL_DEFAULT_DISPLAY)
52 {
53 display = new egl::Display(displayId, (HDC)NULL, false);
54 }
55 else if (displayId == EGL_SOFTWARE_DISPLAY_ANGLE)
56 {
57 display = new egl::Display(displayId, (HDC)NULL, true);
58 }
59 else
60 {
61 // FIXME: Check if displayId is a valid display device context
62
63 display = new egl::Display(displayId, (HDC)displayId, false);
64 }
65
66 displays[displayId] = display;
67 return display;
68}
69
70Display::Display(EGLNativeDisplayType displayId, HDC deviceContext, bool software) : mDc(deviceContext)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000071{
72 mD3d9Module = NULL;
73
74 mD3d9 = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000075 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000076 mDevice = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000077 mDeviceEx = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000078 mDeviceWindow = NULL;
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +000079 mEventQuery = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000080
81 mAdapter = D3DADAPTER_DEFAULT;
82
83 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
84 mDeviceType = D3DDEVTYPE_REF;
85 #else
86 mDeviceType = D3DDEVTYPE_HAL;
87 #endif
88
89 mMinSwapInterval = 1;
90 mMaxSwapInterval = 1;
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000091 mSoftwareDevice = software;
92 mDisplayId = displayId;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +000093 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000094}
95
96Display::~Display()
97{
98 terminate();
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000099
100 DisplayMap::iterator thisDisplay = displays.find(mDisplayId);
101
102 if (thisDisplay != displays.end())
103 {
104 displays.erase(thisDisplay);
105 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000106}
107
108bool Display::initialize()
109{
110 if (isInitialized())
111 {
112 return true;
113 }
114
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000115 if (mSoftwareDevice)
116 {
117 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
118 }
119 else
120 {
121 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
122 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000123 if (mD3d9Module == NULL)
124 {
125 terminate();
126 return false;
127 }
128
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000129 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
130 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
131
132 // Use Direct3D9Ex if available. Among other things, this version is less
133 // inclined to report a lost context, for example when the user switches
134 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
baustin@google.com250f06c2011-06-06 16:59:53 +0000135 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000136 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000137 ASSERT(mD3d9Ex);
138 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000139 ASSERT(mD3d9);
140 }
141 else
142 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000143 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000144 }
145
146 if (mD3d9)
147 {
148 if (mDc != NULL)
149 {
150 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
151 }
152
153 HRESULT result;
154
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000155 // Give up on getting device caps after about one second.
156 for (int i = 0; i < 10; ++i)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000157 {
158 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000159
160 if (SUCCEEDED(result))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000161 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000162 break;
163 }
164 else if (result == D3DERR_NOTAVAILABLE)
165 {
166 Sleep(100); // Give the driver some time to initialize/recover
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000167 }
168 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
169 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000170 terminate();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000171 return error(EGL_BAD_ALLOC, false);
172 }
173 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000174
175 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
176 {
177 terminate();
178 return error(EGL_NOT_INITIALIZED, false);
179 }
180
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000181 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
182 // This is required by Texture2D::convertToRenderTarget.
183 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
184 {
185 terminate();
186 return error(EGL_NOT_INITIALIZED, false);
187 }
188
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000189 mMinSwapInterval = 4;
190 mMaxSwapInterval = 0;
191
192 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
193 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
194 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
195 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
196 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
197
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000198 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
199
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000200 const D3DFORMAT renderTargetFormats[] =
201 {
202 D3DFMT_A1R5G5B5,
203 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
204 D3DFMT_A8R8G8B8,
205 D3DFMT_R5G6B5,
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000206 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000207 D3DFMT_X8R8G8B8
208 };
209
210 const D3DFORMAT depthStencilFormats[] =
211 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000212 D3DFMT_UNKNOWN,
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000213 // D3DFMT_D16_LOCKABLE,
214 D3DFMT_D32,
215 // D3DFMT_D15S1,
216 D3DFMT_D24S8,
217 D3DFMT_D24X8,
218 // D3DFMT_D24X4S4,
219 D3DFMT_D16,
220 // D3DFMT_D32F_LOCKABLE,
221 // D3DFMT_D24FS8
222 };
223
224 D3DDISPLAYMODE currentDisplayMode;
225 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
226
227 ConfigSet configSet;
228
229 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
230 {
231 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
232
233 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
234
235 if (SUCCEEDED(result))
236 {
237 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
238 {
239 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000240 HRESULT result = D3D_OK;
241
242 if(depthStencilFormat != D3DFMT_UNKNOWN)
243 {
244 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
245 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000246
247 if (SUCCEEDED(result))
248 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000249 if(depthStencilFormat != D3DFMT_UNKNOWN)
250 {
251 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
252 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000253
254 if (SUCCEEDED(result))
255 {
256 // FIXME: enumerate multi-sampling
257
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000258 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0,
259 mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000260 }
261 }
262 }
263 }
264 }
265
266 // Give the sorted configs a unique ID and store them internally
267 EGLint index = 1;
268 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
269 {
270 Config configuration = *config;
271 configuration.mConfigID = index;
272 index++;
273
274 mConfigSet.mSet.insert(configuration);
275 }
276 }
277
278 if (!isInitialized())
279 {
280 terminate();
281
282 return false;
283 }
284
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000285 initExtensionString();
286
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000287 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
288 static const TCHAR className[] = TEXT("STATIC");
289
290 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
291
292 return true;
293}
294
295void Display::terminate()
296{
297 while (!mSurfaceSet.empty())
298 {
299 destroySurface(*mSurfaceSet.begin());
300 }
301
302 while (!mContextSet.empty())
303 {
304 destroyContext(*mContextSet.begin());
305 }
306
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000307 if (mEventQuery)
308 {
309 mEventQuery->Release();
310 mEventQuery = NULL;
311 }
312
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000313 if (mDevice)
314 {
315 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000316 if (testDeviceLost())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000317 {
318 resetDevice();
319 }
320
321 mDevice->Release();
322 mDevice = NULL;
323 }
324
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000325 if (mDeviceEx)
326 {
327 mDeviceEx->Release();
328 mDeviceEx = NULL;
329 }
330
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000331 if (mD3d9)
332 {
333 mD3d9->Release();
334 mD3d9 = NULL;
335 }
336
337 if (mDeviceWindow)
338 {
339 DestroyWindow(mDeviceWindow);
340 mDeviceWindow = NULL;
341 }
342
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000343 if (mD3d9Ex)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000344 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000345 mD3d9Ex->Release();
346 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000347 }
348
349 if (mD3d9Module)
350 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000351 mD3d9Module = NULL;
352 }
353}
354
355void Display::startScene()
356{
357 if (!mSceneStarted)
358 {
359 long result = mDevice->BeginScene();
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000360 if (SUCCEEDED(result)) {
361 // This is defensive checking against the device being
362 // lost at unexpected times.
363 mSceneStarted = true;
364 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000365 }
366}
367
368void Display::endScene()
369{
370 if (mSceneStarted)
371 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000372 // EndScene can fail if the device was lost, for example due
373 // to a TDR during a draw call.
374 mDevice->EndScene();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000375 mSceneStarted = false;
376 }
377}
378
379bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
380{
381 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
382}
383
384bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
385{
386 const egl::Config *configuration = mConfigSet.get(config);
387
388 switch (attribute)
389 {
390 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
391 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
392 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
393 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
394 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
395 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
396 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
397 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
398 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
399 case EGL_LEVEL: *value = configuration->mLevel; break;
400 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
401 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
402 case EGL_SAMPLES: *value = configuration->mSamples; break;
403 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
404 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
405 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
406 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
407 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
408 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
409 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
410 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
411 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
412 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
413 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
414 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
415 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
416 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
417 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
418 case EGL_CONFORMANT: *value = configuration->mConformant; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000419 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
420 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
421 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000422 default:
423 return false;
424 }
425
426 return true;
427}
428
429bool Display::createDevice()
430{
431 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
432 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
433
434 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
435
436 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
437 {
438 return error(EGL_BAD_ALLOC, false);
439 }
440
441 if (FAILED(result))
442 {
443 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
444
445 if (FAILED(result))
446 {
447 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
448 return error(EGL_BAD_ALLOC, false);
449 }
450 }
451
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000452 if (mD3d9Ex)
453 {
454 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
455 ASSERT(SUCCEEDED(result));
456 }
457
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000458 initializeDevice();
459
460 return true;
461}
462
463// do any one-time device initialization
464// NOTE: this is also needed after a device lost/reset
465// to reset the scene status and ensure the default states are reset.
466void Display::initializeDevice()
467{
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000468 // Permanent non-default states
469 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
daniel@transgaming.com7a2fdc92011-11-24 22:34:09 +0000470 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000471
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000472 // Leave mEventQuery null if CreateQuery fails. This will prevent sync from
473 // working correctly but otherwise mEventQuery is expected to be null when
474 // queries are not supported.
475 ASSERT(!mEventQuery);
476 mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &mEventQuery);
477
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000478 mSceneStarted = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000479}
480
481bool Display::resetDevice()
482{
483 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000484
daniel@transgaming.com10435352011-11-09 17:44:21 +0000485 HRESULT result = D3D_OK;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000486 bool lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000487 int attempts = 3;
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000488
daniel@transgaming.com10435352011-11-09 17:44:21 +0000489 while (lost && attempts > 0)
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000490 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000491 if (mDeviceEx)
492 {
493 Sleep(500); // Give the graphics driver some CPU time
494 result = mDeviceEx->ResetEx(&presentParameters, NULL);
495 }
496 else
497 {
498 result = mDevice->TestCooperativeLevel();
499
500 while (result == D3DERR_DEVICELOST)
501 {
502 Sleep(100); // Give the graphics driver some CPU time
503 result = mDevice->TestCooperativeLevel();
504 }
505
506 if (result == D3DERR_DEVICENOTRESET)
507 {
508 result = mDevice->Reset(&presentParameters);
509 }
510 }
511
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000512 lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000513 attempts --;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000514 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000515
516 if (FAILED(result))
517 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000518 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000519 return error(EGL_BAD_ALLOC, false);
520 }
521
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000522 // reset device defaults
523 initializeDevice();
524
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000525 return true;
526}
527
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000528EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000529{
530 const Config *configuration = mConfigSet.get(config);
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000531 EGLint postSubBufferSupported = EGL_FALSE;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000532
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000533 if (attribList)
534 {
535 while (*attribList != EGL_NONE)
536 {
537 switch (attribList[0])
538 {
539 case EGL_RENDER_BUFFER:
540 switch (attribList[1])
541 {
542 case EGL_BACK_BUFFER:
543 break;
544 case EGL_SINGLE_BUFFER:
545 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
546 default:
547 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
548 }
549 break;
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000550 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
551 postSubBufferSupported = attribList[1];
552 break;
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000553 case EGL_VG_COLORSPACE:
554 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
555 case EGL_VG_ALPHA_FORMAT:
556 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
557 default:
558 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
559 }
560
561 attribList += 2;
562 }
563 }
564
565 if (hasExistingWindowSurface(window))
566 {
567 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
568 }
569
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000570 if (testDeviceLost())
571 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000572 if (!restoreLostDevice())
573 return EGL_NO_SURFACE;
574 }
575
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000576 Surface *surface = new Surface(this, configuration, window, postSubBufferSupported);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000577
578 if (!surface->initialize())
579 {
580 delete surface;
581 return EGL_NO_SURFACE;
582 }
583
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000584 mSurfaceSet.insert(surface);
585
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000586 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000587}
588
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000589EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000590{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000591 EGLint width = 0, height = 0;
592 EGLenum textureFormat = EGL_NO_TEXTURE;
593 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000594 const Config *configuration = mConfigSet.get(config);
595
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000596 if (attribList)
597 {
598 while (*attribList != EGL_NONE)
599 {
600 switch (attribList[0])
601 {
602 case EGL_WIDTH:
603 width = attribList[1];
604 break;
605 case EGL_HEIGHT:
606 height = attribList[1];
607 break;
608 case EGL_LARGEST_PBUFFER:
609 if (attribList[1] != EGL_FALSE)
610 UNIMPLEMENTED(); // FIXME
611 break;
612 case EGL_TEXTURE_FORMAT:
613 switch (attribList[1])
614 {
615 case EGL_NO_TEXTURE:
616 case EGL_TEXTURE_RGB:
617 case EGL_TEXTURE_RGBA:
618 textureFormat = attribList[1];
619 break;
620 default:
621 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
622 }
623 break;
624 case EGL_TEXTURE_TARGET:
625 switch (attribList[1])
626 {
627 case EGL_NO_TEXTURE:
628 case EGL_TEXTURE_2D:
629 textureTarget = attribList[1];
630 break;
631 default:
632 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
633 }
634 break;
635 case EGL_MIPMAP_TEXTURE:
636 if (attribList[1] != EGL_FALSE)
637 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
638 break;
639 case EGL_VG_COLORSPACE:
640 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
641 case EGL_VG_ALPHA_FORMAT:
642 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
643 default:
644 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
645 }
646
647 attribList += 2;
648 }
649 }
650
651 if (width < 0 || height < 0)
652 {
653 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
654 }
655
656 if (width == 0 || height == 0)
657 {
658 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
659 }
660
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000661 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000662 {
663 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
664 }
665
666 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
667 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
668 {
669 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
670 }
671
672 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
673 {
674 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
675 }
676
677 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
678 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
679 {
680 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
681 }
682
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000683 if (testDeviceLost())
684 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000685 if (!restoreLostDevice())
686 return EGL_NO_SURFACE;
687 }
688
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000689 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
690
691 if (!surface->initialize())
692 {
693 delete surface;
694 return EGL_NO_SURFACE;
695 }
696
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000697 mSurfaceSet.insert(surface);
698
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000699 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000700}
701
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000702EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000703{
704 if (!mDevice)
705 {
706 if (!createDevice())
707 {
708 return NULL;
709 }
710 }
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000711 else if (testDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000712 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000713 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000714 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000715 }
716
717 const egl::Config *config = mConfigSet.get(configHandle);
718
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000719 gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000720 mContextSet.insert(context);
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000721 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000722
723 return context;
724}
725
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000726bool Display::restoreLostDevice()
727{
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000728 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
729 {
730 if ((*ctx)->isResetNotificationEnabled())
731 return false; // If reset notifications have been requested, application must delete all contexts first
732 }
733
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000734 // Release surface resources to make the Reset() succeed
735 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
736 {
737 (*surface)->release();
738 }
739
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000740 if (mEventQuery)
741 {
742 mEventQuery->Release();
743 mEventQuery = NULL;
744 }
745
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000746 if (!resetDevice())
747 {
748 return false;
749 }
750
751 // Restore any surfaces that may have been lost
752 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
753 {
754 (*surface)->resetSwapChain();
755 }
756
757 return true;
758}
759
760
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000761void Display::destroySurface(egl::Surface *surface)
762{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000763 delete surface;
764 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000765}
766
767void Display::destroyContext(gl::Context *context)
768{
769 glDestroyContext(context);
770 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000771}
772
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000773void Display::notifyDeviceLost()
774{
775 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
776 {
777 (*context)->markContextLost();
778 }
779 mDeviceLost = true;
780 error(EGL_CONTEXT_LOST);
781}
782
783bool Display::isDeviceLost()
784{
785 return mDeviceLost;
786}
787
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000788bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000789{
790 return mD3d9 != NULL && mConfigSet.size() > 0;
791}
792
793bool Display::isValidConfig(EGLConfig config)
794{
795 return mConfigSet.get(config) != NULL;
796}
797
798bool Display::isValidContext(gl::Context *context)
799{
800 return mContextSet.find(context) != mContextSet.end();
801}
802
803bool Display::isValidSurface(egl::Surface *surface)
804{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000805 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000806}
807
808bool Display::hasExistingWindowSurface(HWND window)
809{
810 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
811 {
812 if ((*surface)->getWindowHandle() == window)
813 {
814 return true;
815 }
816 }
817
818 return false;
819}
820
821EGLint Display::getMinSwapInterval()
822{
823 return mMinSwapInterval;
824}
825
826EGLint Display::getMaxSwapInterval()
827{
828 return mMaxSwapInterval;
829}
830
831IDirect3DDevice9 *Display::getDevice()
832{
833 if (!mDevice)
834 {
835 if (!createDevice())
836 {
837 return NULL;
838 }
839 }
840
841 return mDevice;
842}
843
844D3DCAPS9 Display::getDeviceCaps()
845{
846 return mDeviceCaps;
847}
848
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000849D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
850{
851 return &mAdapterIdentifier;
852}
853
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000854bool Display::testDeviceLost()
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000855{
856 if (mDeviceEx)
857 {
858 return FAILED(mDeviceEx->CheckDeviceState(NULL));
859 }
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000860 else if (mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000861 {
862 return FAILED(mDevice->TestCooperativeLevel());
863 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000864
865 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000866}
867
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000868bool Display::testDeviceResettable()
869{
870 HRESULT status = D3D_OK;
871
872 if (mDeviceEx)
873 {
874 status = mDeviceEx->CheckDeviceState(NULL);
875 }
876 else if (mDevice)
877 {
878 status = mDevice->TestCooperativeLevel();
879 }
880
881 switch (status)
882 {
883 case D3DERR_DEVICENOTRESET:
884 case D3DERR_DEVICEHUNG:
885 return true;
886 default:
887 return false;
888 }
889}
890
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000891void Display::sync(bool block)
892{
893 HRESULT result;
894
895 if (!mEventQuery)
896 {
897 return;
898 }
899
900 result = mEventQuery->Issue(D3DISSUE_END);
901 ASSERT(SUCCEEDED(result));
902
903 do
904 {
905 result = mEventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
906
907 if(block && result == S_FALSE)
908 {
909 // Keep polling, but allow other threads to do something useful first
910 Sleep(0);
911 // explicitly check for device loss
912 // some drivers seem to return S_FALSE even if the device is lost
913 // instead of D3DERR_DEVICELOST like they should
914 if (testDeviceLost())
915 {
916 result = D3DERR_DEVICELOST;
917 }
918 }
919 }
920 while(block && result == S_FALSE);
921
922 if (isDeviceLostError(result))
923 {
924 notifyDeviceLost();
925 }
926}
927
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000928void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
929{
930 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
931 {
932 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
933 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
934
935 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
936 }
937}
938
gman@chromium.org50c526d2011-08-10 05:19:44 +0000939bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000940{
941 D3DDISPLAYMODE currentDisplayMode;
942 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
943
944 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
945}
946
gman@chromium.org50c526d2011-08-10 05:19:44 +0000947bool Display::getDXT3TextureSupport()
948{
949 D3DDISPLAYMODE currentDisplayMode;
950 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
951
952 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
953}
954
955bool Display::getDXT5TextureSupport()
956{
957 D3DDISPLAYMODE currentDisplayMode;
958 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
959
960 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
961}
962
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000963bool Display::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000964{
965 D3DDISPLAYMODE currentDisplayMode;
966 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
967
968 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
969 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
970 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
971 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
972
973 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
974 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
975 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
976 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
977
978 if (!filtering && !renderable)
979 {
980 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
981 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
982 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
983 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
984 }
985 else
986 {
987 return true;
988 }
989}
990
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000991bool Display::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000992{
993 D3DDISPLAYMODE currentDisplayMode;
994 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
995
996 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
997 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
998 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
999 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1000
1001 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1002 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1003 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1004 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1005
1006 if (!filtering && !renderable)
1007 {
1008 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1009 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1010 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1011 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1012 }
1013 else
1014 {
1015 return true;
1016 }
1017}
1018
daniel@transgaming.comed828e52010-10-15 17:57:30 +00001019bool Display::getLuminanceTextureSupport()
1020{
1021 D3DDISPLAYMODE currentDisplayMode;
1022 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1023
1024 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1025}
1026
1027bool Display::getLuminanceAlphaTextureSupport()
1028{
1029 D3DDISPLAYMODE currentDisplayMode;
1030 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1031
1032 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1033}
1034
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001035D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001036{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +00001037 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001038 {
1039 return D3DPOOL_DEFAULT;
1040 }
1041 else
1042 {
1043 if (!(usage & D3DUSAGE_DYNAMIC))
1044 {
1045 return D3DPOOL_MANAGED;
1046 }
1047 }
1048
1049 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001050}
1051
daniel@transgaming.com59580a32011-11-12 04:23:44 +00001052D3DPOOL Display::getTexturePool(bool renderable) const
1053{
1054 if (mD3d9Ex != NULL)
1055 {
1056 return D3DPOOL_DEFAULT;
1057 }
1058 else
1059 {
1060 if (!renderable)
1061 {
1062 return D3DPOOL_MANAGED;
1063 }
1064 }
1065
1066 return D3DPOOL_DEFAULT;
1067}
1068
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001069bool Display::getEventQuerySupport()
1070{
1071 IDirect3DQuery9 *query;
1072 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
1073 if (SUCCEEDED(result))
1074 {
1075 query->Release();
1076 }
1077
1078 return result != D3DERR_NOTAVAILABLE;
1079}
1080
1081D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
1082{
1083 D3DPRESENT_PARAMETERS presentParameters = {0};
1084
1085 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
1086 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1087 presentParameters.BackBufferCount = 1;
1088 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
1089 presentParameters.BackBufferWidth = 1;
1090 presentParameters.BackBufferHeight = 1;
1091 presentParameters.EnableAutoDepthStencil = FALSE;
1092 presentParameters.Flags = 0;
1093 presentParameters.hDeviceWindow = mDeviceWindow;
1094 presentParameters.MultiSampleQuality = 0;
1095 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
1096 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
1097 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1098 presentParameters.Windowed = TRUE;
1099
1100 return presentParameters;
1101}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001102
1103void Display::initExtensionString()
1104{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001105 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001106 bool isd3d9ex = isD3d9ExDevice();
1107
1108 mExtensionString = "";
1109
daniel@transgaming.com8747f182011-11-09 17:50:38 +00001110 // Multi-vendor (EXT) extensions
1111 mExtensionString += "EGL_EXT_create_context_robustness ";
1112
1113 // ANGLE-specific extensions
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001114 if (isd3d9ex)
1115 {
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001116 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
1117 }
1118
1119 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001120
1121 if (swiftShader)
1122 {
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001123 mExtensionString += "EGL_ANGLE_software_display ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001124 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001125
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001126 if (isd3d9ex)
1127 {
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001128 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
1129 }
1130
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001131 mExtensionString += "EGL_NV_post_sub_buffer";
1132
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001133 std::string::size_type end = mExtensionString.find_last_not_of(' ');
1134 if (end != std::string::npos)
1135 {
1136 mExtensionString.resize(end+1);
1137 }
1138}
1139
1140const char *Display::getExtensionString() const
1141{
1142 return mExtensionString.c_str();
1143}
1144
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001145// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1146// We test this using D3D9 by checking support for the R16F format.
1147bool Display::getVertexTextureSupport() const
1148{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001149 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1150 {
1151 return false;
1152 }
1153
1154 D3DDISPLAYMODE currentDisplayMode;
1155 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1156
1157 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1158
1159 return SUCCEEDED(result);
1160}
1161
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001162bool Display::getNonPower2TextureSupport() const
1163{
1164 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
1165 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
1166 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
1167}
1168
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001169}