blob: 534431d98a48770c7fbdd61417b342e512a692f6 [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"
daniel@transgaming.come979ead2010-09-23 18:03:14 +000019
20#include "libEGL/main.h"
21
baustin@google.com250f06c2011-06-06 16:59:53 +000022// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
23#define REF_RAST 0
24
25// The "Debug This Pixel..." feature in PIX often fails when using the
26// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
27// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
28#if !defined(ANGLE_ENABLE_D3D9EX)
29// Enables use of the IDirect3D9Ex interface, when available
30#define ANGLE_ENABLE_D3D9EX 1
31#endif // !defined(ANGLE_ENABLE_D3D9EX)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000032
33namespace egl
34{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000035namespace
36{
37 typedef std::map<EGLNativeDisplayType, Display*> DisplayMap;
38 DisplayMap displays;
39}
40
41egl::Display *Display::getDisplay(EGLNativeDisplayType displayId)
42{
43 if (displays.find(displayId) != displays.end())
44 {
45 return displays[displayId];
46 }
47
48 egl::Display *display = NULL;
49
50 if (displayId == EGL_DEFAULT_DISPLAY)
51 {
52 display = new egl::Display(displayId, (HDC)NULL, false);
53 }
54 else if (displayId == EGL_SOFTWARE_DISPLAY_ANGLE)
55 {
56 display = new egl::Display(displayId, (HDC)NULL, true);
57 }
58 else
59 {
60 // FIXME: Check if displayId is a valid display device context
61
62 display = new egl::Display(displayId, (HDC)displayId, false);
63 }
64
65 displays[displayId] = display;
66 return display;
67}
68
69Display::Display(EGLNativeDisplayType displayId, HDC deviceContext, bool software) : mDc(deviceContext)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000070{
71 mD3d9Module = NULL;
72
73 mD3d9 = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000074 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000075 mDevice = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000076 mDeviceEx = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000077 mDeviceWindow = NULL;
78
79 mAdapter = D3DADAPTER_DEFAULT;
80
81 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
82 mDeviceType = D3DDEVTYPE_REF;
83 #else
84 mDeviceType = D3DDEVTYPE_HAL;
85 #endif
86
87 mMinSwapInterval = 1;
88 mMaxSwapInterval = 1;
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000089 mSoftwareDevice = software;
90 mDisplayId = displayId;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +000091 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000092}
93
94Display::~Display()
95{
96 terminate();
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000097
98 DisplayMap::iterator thisDisplay = displays.find(mDisplayId);
99
100 if (thisDisplay != displays.end())
101 {
102 displays.erase(thisDisplay);
103 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000104}
105
106bool Display::initialize()
107{
108 if (isInitialized())
109 {
110 return true;
111 }
112
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000113 if (mSoftwareDevice)
114 {
115 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
116 }
117 else
118 {
119 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
120 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000121 if (mD3d9Module == NULL)
122 {
123 terminate();
124 return false;
125 }
126
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000127 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
128 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
129
130 // Use Direct3D9Ex if available. Among other things, this version is less
131 // inclined to report a lost context, for example when the user switches
132 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
baustin@google.com250f06c2011-06-06 16:59:53 +0000133 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000134 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000135 ASSERT(mD3d9Ex);
136 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000137 ASSERT(mD3d9);
138 }
139 else
140 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000141 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000142 }
143
144 if (mD3d9)
145 {
146 if (mDc != NULL)
147 {
148 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
149 }
150
151 HRESULT result;
152
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000153 // Give up on getting device caps after about one second.
154 for (int i = 0; i < 10; ++i)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000155 {
156 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000157
158 if (SUCCEEDED(result))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000159 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000160 break;
161 }
162 else if (result == D3DERR_NOTAVAILABLE)
163 {
164 Sleep(100); // Give the driver some time to initialize/recover
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000165 }
166 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
167 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000168 terminate();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000169 return error(EGL_BAD_ALLOC, false);
170 }
171 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000172
173 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
174 {
175 terminate();
176 return error(EGL_NOT_INITIALIZED, false);
177 }
178
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000179 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
180 // This is required by Texture2D::convertToRenderTarget.
181 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
182 {
183 terminate();
184 return error(EGL_NOT_INITIALIZED, false);
185 }
186
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000187 mMinSwapInterval = 4;
188 mMaxSwapInterval = 0;
189
190 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
191 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
192 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
193 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
194 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
195
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000196 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
197
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000198 const D3DFORMAT renderTargetFormats[] =
199 {
200 D3DFMT_A1R5G5B5,
201 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
202 D3DFMT_A8R8G8B8,
203 D3DFMT_R5G6B5,
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000204 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000205 D3DFMT_X8R8G8B8
206 };
207
208 const D3DFORMAT depthStencilFormats[] =
209 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000210 D3DFMT_UNKNOWN,
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000211 // D3DFMT_D16_LOCKABLE,
212 D3DFMT_D32,
213 // D3DFMT_D15S1,
214 D3DFMT_D24S8,
215 D3DFMT_D24X8,
216 // D3DFMT_D24X4S4,
217 D3DFMT_D16,
218 // D3DFMT_D32F_LOCKABLE,
219 // D3DFMT_D24FS8
220 };
221
222 D3DDISPLAYMODE currentDisplayMode;
223 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
224
225 ConfigSet configSet;
226
227 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
228 {
229 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
230
231 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
232
233 if (SUCCEEDED(result))
234 {
235 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
236 {
237 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000238 HRESULT result = D3D_OK;
239
240 if(depthStencilFormat != D3DFMT_UNKNOWN)
241 {
242 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
243 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000244
245 if (SUCCEEDED(result))
246 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000247 if(depthStencilFormat != D3DFMT_UNKNOWN)
248 {
249 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
250 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000251
252 if (SUCCEEDED(result))
253 {
254 // FIXME: enumerate multi-sampling
255
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000256 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0,
257 mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000258 }
259 }
260 }
261 }
262 }
263
264 // Give the sorted configs a unique ID and store them internally
265 EGLint index = 1;
266 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
267 {
268 Config configuration = *config;
269 configuration.mConfigID = index;
270 index++;
271
272 mConfigSet.mSet.insert(configuration);
273 }
274 }
275
276 if (!isInitialized())
277 {
278 terminate();
279
280 return false;
281 }
282
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000283 initExtensionString();
284
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000285 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
286 static const TCHAR className[] = TEXT("STATIC");
287
288 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
289
290 return true;
291}
292
293void Display::terminate()
294{
295 while (!mSurfaceSet.empty())
296 {
297 destroySurface(*mSurfaceSet.begin());
298 }
299
300 while (!mContextSet.empty())
301 {
302 destroyContext(*mContextSet.begin());
303 }
304
305 if (mDevice)
306 {
307 // 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 +0000308 if (testDeviceLost())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000309 {
310 resetDevice();
311 }
312
313 mDevice->Release();
314 mDevice = NULL;
315 }
316
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000317 if (mDeviceEx)
318 {
319 mDeviceEx->Release();
320 mDeviceEx = NULL;
321 }
322
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000323 if (mD3d9)
324 {
325 mD3d9->Release();
326 mD3d9 = NULL;
327 }
328
329 if (mDeviceWindow)
330 {
331 DestroyWindow(mDeviceWindow);
332 mDeviceWindow = NULL;
333 }
334
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000335 if (mD3d9Ex)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000336 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000337 mD3d9Ex->Release();
338 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000339 }
340
341 if (mD3d9Module)
342 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000343 mD3d9Module = NULL;
344 }
345}
346
347void Display::startScene()
348{
349 if (!mSceneStarted)
350 {
351 long result = mDevice->BeginScene();
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000352 if (SUCCEEDED(result)) {
353 // This is defensive checking against the device being
354 // lost at unexpected times.
355 mSceneStarted = true;
356 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000357 }
358}
359
360void Display::endScene()
361{
362 if (mSceneStarted)
363 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000364 // EndScene can fail if the device was lost, for example due
365 // to a TDR during a draw call.
366 mDevice->EndScene();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000367 mSceneStarted = false;
368 }
369}
370
371bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
372{
373 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
374}
375
376bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
377{
378 const egl::Config *configuration = mConfigSet.get(config);
379
380 switch (attribute)
381 {
382 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
383 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
384 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
385 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
386 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
387 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
388 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
389 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
390 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
391 case EGL_LEVEL: *value = configuration->mLevel; break;
392 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
393 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
394 case EGL_SAMPLES: *value = configuration->mSamples; break;
395 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
396 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
397 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
398 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
399 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
400 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
401 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
402 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
403 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
404 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
405 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
406 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
407 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
408 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
409 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
410 case EGL_CONFORMANT: *value = configuration->mConformant; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000411 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
412 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
413 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000414 default:
415 return false;
416 }
417
418 return true;
419}
420
421bool Display::createDevice()
422{
423 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
424 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
425
426 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
427
428 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
429 {
430 return error(EGL_BAD_ALLOC, false);
431 }
432
433 if (FAILED(result))
434 {
435 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
436
437 if (FAILED(result))
438 {
439 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
440 return error(EGL_BAD_ALLOC, false);
441 }
442 }
443
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000444 if (mD3d9Ex)
445 {
446 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
447 ASSERT(SUCCEEDED(result));
448 }
449
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000450 initializeDevice();
451
452 return true;
453}
454
455// do any one-time device initialization
456// NOTE: this is also needed after a device lost/reset
457// to reset the scene status and ensure the default states are reset.
458void Display::initializeDevice()
459{
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000460 // Permanent non-default states
461 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
daniel@transgaming.com7a2fdc92011-11-24 22:34:09 +0000462 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000463
464 mSceneStarted = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000465}
466
467bool Display::resetDevice()
468{
469 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000470
daniel@transgaming.com10435352011-11-09 17:44:21 +0000471 HRESULT result = D3D_OK;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000472 bool lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000473 int attempts = 3;
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000474
daniel@transgaming.com10435352011-11-09 17:44:21 +0000475 while (lost && attempts > 0)
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000476 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000477 if (mDeviceEx)
478 {
479 Sleep(500); // Give the graphics driver some CPU time
480 result = mDeviceEx->ResetEx(&presentParameters, NULL);
481 }
482 else
483 {
484 result = mDevice->TestCooperativeLevel();
485
486 while (result == D3DERR_DEVICELOST)
487 {
488 Sleep(100); // Give the graphics driver some CPU time
489 result = mDevice->TestCooperativeLevel();
490 }
491
492 if (result == D3DERR_DEVICENOTRESET)
493 {
494 result = mDevice->Reset(&presentParameters);
495 }
496 }
497
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000498 lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000499 attempts --;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000500 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000501
502 if (FAILED(result))
503 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000504 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000505 return error(EGL_BAD_ALLOC, false);
506 }
507
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000508 // reset device defaults
509 initializeDevice();
510
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000511 return true;
512}
513
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000514EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000515{
516 const Config *configuration = mConfigSet.get(config);
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000517 EGLint postSubBufferSupported = EGL_FALSE;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000518
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000519 if (attribList)
520 {
521 while (*attribList != EGL_NONE)
522 {
523 switch (attribList[0])
524 {
525 case EGL_RENDER_BUFFER:
526 switch (attribList[1])
527 {
528 case EGL_BACK_BUFFER:
529 break;
530 case EGL_SINGLE_BUFFER:
531 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
532 default:
533 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
534 }
535 break;
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000536 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
537 postSubBufferSupported = attribList[1];
538 break;
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000539 case EGL_VG_COLORSPACE:
540 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
541 case EGL_VG_ALPHA_FORMAT:
542 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
543 default:
544 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
545 }
546
547 attribList += 2;
548 }
549 }
550
551 if (hasExistingWindowSurface(window))
552 {
553 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
554 }
555
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000556 if (testDeviceLost())
557 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000558 if (!restoreLostDevice())
559 return EGL_NO_SURFACE;
560 }
561
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000562 Surface *surface = new Surface(this, configuration, window, postSubBufferSupported);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000563
564 if (!surface->initialize())
565 {
566 delete surface;
567 return EGL_NO_SURFACE;
568 }
569
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000570 mSurfaceSet.insert(surface);
571
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000572 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000573}
574
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000575EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000576{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000577 EGLint width = 0, height = 0;
578 EGLenum textureFormat = EGL_NO_TEXTURE;
579 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000580 const Config *configuration = mConfigSet.get(config);
581
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000582 if (attribList)
583 {
584 while (*attribList != EGL_NONE)
585 {
586 switch (attribList[0])
587 {
588 case EGL_WIDTH:
589 width = attribList[1];
590 break;
591 case EGL_HEIGHT:
592 height = attribList[1];
593 break;
594 case EGL_LARGEST_PBUFFER:
595 if (attribList[1] != EGL_FALSE)
596 UNIMPLEMENTED(); // FIXME
597 break;
598 case EGL_TEXTURE_FORMAT:
599 switch (attribList[1])
600 {
601 case EGL_NO_TEXTURE:
602 case EGL_TEXTURE_RGB:
603 case EGL_TEXTURE_RGBA:
604 textureFormat = attribList[1];
605 break;
606 default:
607 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
608 }
609 break;
610 case EGL_TEXTURE_TARGET:
611 switch (attribList[1])
612 {
613 case EGL_NO_TEXTURE:
614 case EGL_TEXTURE_2D:
615 textureTarget = attribList[1];
616 break;
617 default:
618 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
619 }
620 break;
621 case EGL_MIPMAP_TEXTURE:
622 if (attribList[1] != EGL_FALSE)
623 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
624 break;
625 case EGL_VG_COLORSPACE:
626 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
627 case EGL_VG_ALPHA_FORMAT:
628 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
629 default:
630 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
631 }
632
633 attribList += 2;
634 }
635 }
636
637 if (width < 0 || height < 0)
638 {
639 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
640 }
641
642 if (width == 0 || height == 0)
643 {
644 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
645 }
646
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000647 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000648 {
649 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
650 }
651
652 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
653 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
654 {
655 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
656 }
657
658 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
659 {
660 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
661 }
662
663 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
664 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
665 {
666 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
667 }
668
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000669 if (testDeviceLost())
670 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000671 if (!restoreLostDevice())
672 return EGL_NO_SURFACE;
673 }
674
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000675 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
676
677 if (!surface->initialize())
678 {
679 delete surface;
680 return EGL_NO_SURFACE;
681 }
682
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000683 mSurfaceSet.insert(surface);
684
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000685 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000686}
687
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000688EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000689{
690 if (!mDevice)
691 {
692 if (!createDevice())
693 {
694 return NULL;
695 }
696 }
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000697 else if (testDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000698 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000699 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000700 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000701 }
702
703 const egl::Config *config = mConfigSet.get(configHandle);
704
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000705 gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000706 mContextSet.insert(context);
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000707 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000708
709 return context;
710}
711
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000712bool Display::restoreLostDevice()
713{
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000714 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
715 {
716 if ((*ctx)->isResetNotificationEnabled())
717 return false; // If reset notifications have been requested, application must delete all contexts first
718 }
719
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000720 // Release surface resources to make the Reset() succeed
721 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
722 {
723 (*surface)->release();
724 }
725
726 if (!resetDevice())
727 {
728 return false;
729 }
730
731 // Restore any surfaces that may have been lost
732 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
733 {
734 (*surface)->resetSwapChain();
735 }
736
737 return true;
738}
739
740
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000741void Display::destroySurface(egl::Surface *surface)
742{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000743 delete surface;
744 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000745}
746
747void Display::destroyContext(gl::Context *context)
748{
749 glDestroyContext(context);
750 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000751}
752
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000753void Display::notifyDeviceLost()
754{
755 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
756 {
757 (*context)->markContextLost();
758 }
759 mDeviceLost = true;
760 error(EGL_CONTEXT_LOST);
761}
762
763bool Display::isDeviceLost()
764{
765 return mDeviceLost;
766}
767
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000768bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000769{
770 return mD3d9 != NULL && mConfigSet.size() > 0;
771}
772
773bool Display::isValidConfig(EGLConfig config)
774{
775 return mConfigSet.get(config) != NULL;
776}
777
778bool Display::isValidContext(gl::Context *context)
779{
780 return mContextSet.find(context) != mContextSet.end();
781}
782
783bool Display::isValidSurface(egl::Surface *surface)
784{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000785 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000786}
787
788bool Display::hasExistingWindowSurface(HWND window)
789{
790 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
791 {
792 if ((*surface)->getWindowHandle() == window)
793 {
794 return true;
795 }
796 }
797
798 return false;
799}
800
801EGLint Display::getMinSwapInterval()
802{
803 return mMinSwapInterval;
804}
805
806EGLint Display::getMaxSwapInterval()
807{
808 return mMaxSwapInterval;
809}
810
811IDirect3DDevice9 *Display::getDevice()
812{
813 if (!mDevice)
814 {
815 if (!createDevice())
816 {
817 return NULL;
818 }
819 }
820
821 return mDevice;
822}
823
824D3DCAPS9 Display::getDeviceCaps()
825{
826 return mDeviceCaps;
827}
828
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000829D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
830{
831 return &mAdapterIdentifier;
832}
833
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000834bool Display::testDeviceLost()
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000835{
836 if (mDeviceEx)
837 {
838 return FAILED(mDeviceEx->CheckDeviceState(NULL));
839 }
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000840 else if (mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000841 {
842 return FAILED(mDevice->TestCooperativeLevel());
843 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000844
845 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000846}
847
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000848bool Display::testDeviceResettable()
849{
850 HRESULT status = D3D_OK;
851
852 if (mDeviceEx)
853 {
854 status = mDeviceEx->CheckDeviceState(NULL);
855 }
856 else if (mDevice)
857 {
858 status = mDevice->TestCooperativeLevel();
859 }
860
861 switch (status)
862 {
863 case D3DERR_DEVICENOTRESET:
864 case D3DERR_DEVICEHUNG:
865 return true;
866 default:
867 return false;
868 }
869}
870
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000871void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
872{
873 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
874 {
875 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
876 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
877
878 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
879 }
880}
881
gman@chromium.org50c526d2011-08-10 05:19:44 +0000882bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000883{
884 D3DDISPLAYMODE currentDisplayMode;
885 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
886
887 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
888}
889
gman@chromium.org50c526d2011-08-10 05:19:44 +0000890bool Display::getDXT3TextureSupport()
891{
892 D3DDISPLAYMODE currentDisplayMode;
893 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
894
895 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
896}
897
898bool Display::getDXT5TextureSupport()
899{
900 D3DDISPLAYMODE currentDisplayMode;
901 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
902
903 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
904}
905
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000906bool Display::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000907{
908 D3DDISPLAYMODE currentDisplayMode;
909 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
910
911 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
912 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
913 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
914 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
915
916 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
917 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
918 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
919 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
920
921 if (!filtering && !renderable)
922 {
923 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
924 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
925 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
926 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
927 }
928 else
929 {
930 return true;
931 }
932}
933
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000934bool Display::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000935{
936 D3DDISPLAYMODE currentDisplayMode;
937 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
938
939 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
940 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
941 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
942 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
943
944 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
945 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
946 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
947 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
948
949 if (!filtering && !renderable)
950 {
951 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
952 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
953 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
954 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
955 }
956 else
957 {
958 return true;
959 }
960}
961
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000962bool Display::getLuminanceTextureSupport()
963{
964 D3DDISPLAYMODE currentDisplayMode;
965 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
966
967 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
968}
969
970bool Display::getLuminanceAlphaTextureSupport()
971{
972 D3DDISPLAYMODE currentDisplayMode;
973 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
974
975 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
976}
977
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000978D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000979{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000980 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000981 {
982 return D3DPOOL_DEFAULT;
983 }
984 else
985 {
986 if (!(usage & D3DUSAGE_DYNAMIC))
987 {
988 return D3DPOOL_MANAGED;
989 }
990 }
991
992 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000993}
994
daniel@transgaming.com59580a32011-11-12 04:23:44 +0000995D3DPOOL Display::getTexturePool(bool renderable) const
996{
997 if (mD3d9Ex != NULL)
998 {
999 return D3DPOOL_DEFAULT;
1000 }
1001 else
1002 {
1003 if (!renderable)
1004 {
1005 return D3DPOOL_MANAGED;
1006 }
1007 }
1008
1009 return D3DPOOL_DEFAULT;
1010}
1011
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001012bool Display::getEventQuerySupport()
1013{
1014 IDirect3DQuery9 *query;
1015 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
1016 if (SUCCEEDED(result))
1017 {
1018 query->Release();
1019 }
1020
1021 return result != D3DERR_NOTAVAILABLE;
1022}
1023
1024D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
1025{
1026 D3DPRESENT_PARAMETERS presentParameters = {0};
1027
1028 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
1029 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1030 presentParameters.BackBufferCount = 1;
1031 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
1032 presentParameters.BackBufferWidth = 1;
1033 presentParameters.BackBufferHeight = 1;
1034 presentParameters.EnableAutoDepthStencil = FALSE;
1035 presentParameters.Flags = 0;
1036 presentParameters.hDeviceWindow = mDeviceWindow;
1037 presentParameters.MultiSampleQuality = 0;
1038 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
1039 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
1040 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1041 presentParameters.Windowed = TRUE;
1042
1043 return presentParameters;
1044}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001045
1046void Display::initExtensionString()
1047{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001048 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001049 bool isd3d9ex = isD3d9ExDevice();
1050
1051 mExtensionString = "";
1052
daniel@transgaming.com8747f182011-11-09 17:50:38 +00001053 // Multi-vendor (EXT) extensions
1054 mExtensionString += "EGL_EXT_create_context_robustness ";
1055
1056 // ANGLE-specific extensions
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001057 if (isd3d9ex)
1058 {
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001059 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
1060 }
1061
1062 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001063
1064 if (swiftShader)
1065 {
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001066 mExtensionString += "EGL_ANGLE_software_display ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001067 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001068
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001069 if (isd3d9ex)
1070 {
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001071 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
1072 }
1073
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001074 mExtensionString += "EGL_NV_post_sub_buffer";
1075
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001076 std::string::size_type end = mExtensionString.find_last_not_of(' ');
1077 if (end != std::string::npos)
1078 {
1079 mExtensionString.resize(end+1);
1080 }
1081}
1082
1083const char *Display::getExtensionString() const
1084{
1085 return mExtensionString.c_str();
1086}
1087
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001088// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1089// We test this using D3D9 by checking support for the R16F format.
1090bool Display::getVertexTextureSupport() const
1091{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001092 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1093 {
1094 return false;
1095 }
1096
1097 D3DDISPLAYMODE currentDisplayMode;
1098 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1099
1100 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1101
1102 return SUCCEEDED(result);
1103}
1104
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001105bool Display::getNonPower2TextureSupport() const
1106{
1107 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
1108 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
1109 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
1110}
1111
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001112}