blob: 3c59cffe246b93a924b290f3a901211beec2bf52 [file] [log] [blame]
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001//
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00002// Copyright (c) 2002-2012 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;
79
80 mAdapter = D3DADAPTER_DEFAULT;
81
82 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
83 mDeviceType = D3DDEVTYPE_REF;
84 #else
85 mDeviceType = D3DDEVTYPE_HAL;
86 #endif
87
88 mMinSwapInterval = 1;
89 mMaxSwapInterval = 1;
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000090 mSoftwareDevice = software;
91 mDisplayId = displayId;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +000092 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000093}
94
95Display::~Display()
96{
97 terminate();
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000098
99 DisplayMap::iterator thisDisplay = displays.find(mDisplayId);
100
101 if (thisDisplay != displays.end())
102 {
103 displays.erase(thisDisplay);
104 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000105}
106
107bool Display::initialize()
108{
109 if (isInitialized())
110 {
111 return true;
112 }
113
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000114 if (mSoftwareDevice)
115 {
116 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
117 }
118 else
119 {
120 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
121 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000122 if (mD3d9Module == NULL)
123 {
124 terminate();
125 return false;
126 }
127
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000128 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
129 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
130
131 // Use Direct3D9Ex if available. Among other things, this version is less
132 // inclined to report a lost context, for example when the user switches
133 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
baustin@google.com250f06c2011-06-06 16:59:53 +0000134 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000135 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000136 ASSERT(mD3d9Ex);
137 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000138 ASSERT(mD3d9);
139 }
140 else
141 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000142 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000143 }
144
145 if (mD3d9)
146 {
147 if (mDc != NULL)
148 {
149 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
150 }
151
152 HRESULT result;
153
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000154 // Give up on getting device caps after about one second.
155 for (int i = 0; i < 10; ++i)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000156 {
157 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000158
159 if (SUCCEEDED(result))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000160 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000161 break;
162 }
163 else if (result == D3DERR_NOTAVAILABLE)
164 {
165 Sleep(100); // Give the driver some time to initialize/recover
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000166 }
167 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
168 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000169 terminate();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000170 return error(EGL_BAD_ALLOC, false);
171 }
172 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000173
174 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
175 {
176 terminate();
177 return error(EGL_NOT_INITIALIZED, false);
178 }
179
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000180 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
181 // This is required by Texture2D::convertToRenderTarget.
182 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
183 {
184 terminate();
185 return error(EGL_NOT_INITIALIZED, false);
186 }
187
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000188 mMinSwapInterval = 4;
189 mMaxSwapInterval = 0;
190
191 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
192 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
193 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
194 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
195 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
196
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000197 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
198
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000199 const D3DFORMAT renderTargetFormats[] =
200 {
201 D3DFMT_A1R5G5B5,
202 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
203 D3DFMT_A8R8G8B8,
204 D3DFMT_R5G6B5,
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000205 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000206 D3DFMT_X8R8G8B8
207 };
208
209 const D3DFORMAT depthStencilFormats[] =
210 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000211 D3DFMT_UNKNOWN,
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000212 // D3DFMT_D16_LOCKABLE,
213 D3DFMT_D32,
214 // D3DFMT_D15S1,
215 D3DFMT_D24S8,
216 D3DFMT_D24X8,
217 // D3DFMT_D24X4S4,
218 D3DFMT_D16,
219 // D3DFMT_D32F_LOCKABLE,
220 // D3DFMT_D24FS8
221 };
222
223 D3DDISPLAYMODE currentDisplayMode;
224 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
225
226 ConfigSet configSet;
227
228 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
229 {
230 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
231
232 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
233
234 if (SUCCEEDED(result))
235 {
236 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
237 {
238 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000239 HRESULT result = D3D_OK;
240
241 if(depthStencilFormat != D3DFMT_UNKNOWN)
242 {
243 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
244 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000245
246 if (SUCCEEDED(result))
247 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000248 if(depthStencilFormat != D3DFMT_UNKNOWN)
249 {
250 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
251 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000252
253 if (SUCCEEDED(result))
254 {
255 // FIXME: enumerate multi-sampling
256
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000257 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0,
258 mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000259 }
260 }
261 }
262 }
263 }
264
265 // Give the sorted configs a unique ID and store them internally
266 EGLint index = 1;
267 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
268 {
269 Config configuration = *config;
270 configuration.mConfigID = index;
271 index++;
272
273 mConfigSet.mSet.insert(configuration);
274 }
275 }
276
277 if (!isInitialized())
278 {
279 terminate();
280
281 return false;
282 }
283
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000284 initExtensionString();
285
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000286 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
287 static const TCHAR className[] = TEXT("STATIC");
288
289 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
290
apatrick@chromium.orgcca2b702012-02-08 00:59:46 +0000291 if (!createDevice())
292 {
293 terminate();
294 return false;
295 }
296
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +0000297 mVertexShaderCache.initialize(mDevice);
298 mPixelShaderCache.initialize(mDevice);
299
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000300 return true;
301}
302
303void Display::terminate()
304{
305 while (!mSurfaceSet.empty())
306 {
307 destroySurface(*mSurfaceSet.begin());
308 }
309
310 while (!mContextSet.empty())
311 {
312 destroyContext(*mContextSet.begin());
313 }
314
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000315 while (!mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000316 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000317 mEventQueryPool.back()->Release();
318 mEventQueryPool.pop_back();
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000319 }
320
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +0000321 mVertexShaderCache.clear();
322 mPixelShaderCache.clear();
323
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000324 if (mDevice)
325 {
326 // 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 +0000327 if (testDeviceLost())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000328 {
329 resetDevice();
330 }
331
332 mDevice->Release();
333 mDevice = NULL;
334 }
335
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000336 if (mDeviceEx)
337 {
338 mDeviceEx->Release();
339 mDeviceEx = NULL;
340 }
341
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000342 if (mD3d9)
343 {
344 mD3d9->Release();
345 mD3d9 = NULL;
346 }
347
348 if (mDeviceWindow)
349 {
350 DestroyWindow(mDeviceWindow);
351 mDeviceWindow = NULL;
352 }
353
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000354 if (mD3d9Ex)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000355 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000356 mD3d9Ex->Release();
357 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000358 }
359
360 if (mD3d9Module)
361 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000362 mD3d9Module = NULL;
363 }
364}
365
366void Display::startScene()
367{
368 if (!mSceneStarted)
369 {
370 long result = mDevice->BeginScene();
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000371 if (SUCCEEDED(result)) {
372 // This is defensive checking against the device being
373 // lost at unexpected times.
374 mSceneStarted = true;
375 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000376 }
377}
378
379void Display::endScene()
380{
381 if (mSceneStarted)
382 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000383 // EndScene can fail if the device was lost, for example due
384 // to a TDR during a draw call.
385 mDevice->EndScene();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000386 mSceneStarted = false;
387 }
388}
389
390bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
391{
392 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
393}
394
395bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
396{
397 const egl::Config *configuration = mConfigSet.get(config);
398
399 switch (attribute)
400 {
401 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
402 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
403 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
404 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
405 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
406 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
407 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
408 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
409 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
410 case EGL_LEVEL: *value = configuration->mLevel; break;
411 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
412 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
413 case EGL_SAMPLES: *value = configuration->mSamples; break;
414 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
415 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
416 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
417 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
418 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
419 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
420 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
421 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
422 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
423 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
424 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
425 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
426 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
427 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
428 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
429 case EGL_CONFORMANT: *value = configuration->mConformant; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000430 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
431 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
432 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000433 default:
434 return false;
435 }
436
437 return true;
438}
439
440bool Display::createDevice()
441{
442 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
443 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
444
445 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
446
447 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
448 {
449 return error(EGL_BAD_ALLOC, false);
450 }
451
452 if (FAILED(result))
453 {
454 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
455
456 if (FAILED(result))
457 {
458 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
459 return error(EGL_BAD_ALLOC, false);
460 }
461 }
462
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000463 if (mD3d9Ex)
464 {
465 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
466 ASSERT(SUCCEEDED(result));
467 }
468
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000469 initializeDevice();
470
471 return true;
472}
473
474// do any one-time device initialization
475// NOTE: this is also needed after a device lost/reset
476// to reset the scene status and ensure the default states are reset.
477void Display::initializeDevice()
478{
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000479 // Permanent non-default states
480 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
daniel@transgaming.com7a2fdc92011-11-24 22:34:09 +0000481 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000482
daniel@transgaming.com13be3e42012-07-04 19:16:24 +0000483 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
484 {
485 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
486 }
487 else
488 {
489 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
490 }
491
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000492 mSceneStarted = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000493}
494
495bool Display::resetDevice()
496{
497 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000498
daniel@transgaming.com10435352011-11-09 17:44:21 +0000499 HRESULT result = D3D_OK;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000500 bool lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000501 int attempts = 3;
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000502
daniel@transgaming.com10435352011-11-09 17:44:21 +0000503 while (lost && attempts > 0)
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000504 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000505 if (mDeviceEx)
506 {
507 Sleep(500); // Give the graphics driver some CPU time
508 result = mDeviceEx->ResetEx(&presentParameters, NULL);
509 }
510 else
511 {
512 result = mDevice->TestCooperativeLevel();
513
514 while (result == D3DERR_DEVICELOST)
515 {
516 Sleep(100); // Give the graphics driver some CPU time
517 result = mDevice->TestCooperativeLevel();
518 }
519
520 if (result == D3DERR_DEVICENOTRESET)
521 {
522 result = mDevice->Reset(&presentParameters);
523 }
524 }
525
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000526 lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000527 attempts --;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000528 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000529
530 if (FAILED(result))
531 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000532 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000533 return error(EGL_BAD_ALLOC, false);
534 }
535
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000536 // reset device defaults
537 initializeDevice();
538
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000539 return true;
540}
541
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000542EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000543{
544 const Config *configuration = mConfigSet.get(config);
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000545 EGLint postSubBufferSupported = EGL_FALSE;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000546
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000547 if (attribList)
548 {
549 while (*attribList != EGL_NONE)
550 {
551 switch (attribList[0])
552 {
553 case EGL_RENDER_BUFFER:
554 switch (attribList[1])
555 {
556 case EGL_BACK_BUFFER:
557 break;
558 case EGL_SINGLE_BUFFER:
559 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
560 default:
561 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
562 }
563 break;
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000564 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
565 postSubBufferSupported = attribList[1];
566 break;
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000567 case EGL_VG_COLORSPACE:
568 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
569 case EGL_VG_ALPHA_FORMAT:
570 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
571 default:
572 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
573 }
574
575 attribList += 2;
576 }
577 }
578
579 if (hasExistingWindowSurface(window))
580 {
581 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
582 }
583
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000584 if (testDeviceLost())
585 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000586 if (!restoreLostDevice())
587 return EGL_NO_SURFACE;
588 }
589
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000590 Surface *surface = new Surface(this, configuration, window, postSubBufferSupported);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000591
592 if (!surface->initialize())
593 {
594 delete surface;
595 return EGL_NO_SURFACE;
596 }
597
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000598 mSurfaceSet.insert(surface);
599
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000600 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000601}
602
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000603EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000604{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000605 EGLint width = 0, height = 0;
606 EGLenum textureFormat = EGL_NO_TEXTURE;
607 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000608 const Config *configuration = mConfigSet.get(config);
609
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000610 if (attribList)
611 {
612 while (*attribList != EGL_NONE)
613 {
614 switch (attribList[0])
615 {
616 case EGL_WIDTH:
617 width = attribList[1];
618 break;
619 case EGL_HEIGHT:
620 height = attribList[1];
621 break;
622 case EGL_LARGEST_PBUFFER:
623 if (attribList[1] != EGL_FALSE)
624 UNIMPLEMENTED(); // FIXME
625 break;
626 case EGL_TEXTURE_FORMAT:
627 switch (attribList[1])
628 {
629 case EGL_NO_TEXTURE:
630 case EGL_TEXTURE_RGB:
631 case EGL_TEXTURE_RGBA:
632 textureFormat = attribList[1];
633 break;
634 default:
635 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
636 }
637 break;
638 case EGL_TEXTURE_TARGET:
639 switch (attribList[1])
640 {
641 case EGL_NO_TEXTURE:
642 case EGL_TEXTURE_2D:
643 textureTarget = attribList[1];
644 break;
645 default:
646 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
647 }
648 break;
649 case EGL_MIPMAP_TEXTURE:
650 if (attribList[1] != EGL_FALSE)
651 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
652 break;
653 case EGL_VG_COLORSPACE:
654 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
655 case EGL_VG_ALPHA_FORMAT:
656 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
657 default:
658 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
659 }
660
661 attribList += 2;
662 }
663 }
664
665 if (width < 0 || height < 0)
666 {
667 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
668 }
669
670 if (width == 0 || height == 0)
671 {
672 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
673 }
674
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000675 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000676 {
677 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
678 }
679
680 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
681 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
682 {
683 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
684 }
685
686 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
687 {
688 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
689 }
690
691 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
692 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
693 {
694 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
695 }
696
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000697 if (testDeviceLost())
698 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000699 if (!restoreLostDevice())
700 return EGL_NO_SURFACE;
701 }
702
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000703 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
704
705 if (!surface->initialize())
706 {
707 delete surface;
708 return EGL_NO_SURFACE;
709 }
710
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000711 mSurfaceSet.insert(surface);
712
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000713 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000714}
715
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000716EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000717{
718 if (!mDevice)
719 {
720 if (!createDevice())
721 {
722 return NULL;
723 }
724 }
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000725 else if (testDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000726 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000727 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000728 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000729 }
730
731 const egl::Config *config = mConfigSet.get(configHandle);
732
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000733 gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000734 mContextSet.insert(context);
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000735 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000736
737 return context;
738}
739
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000740bool Display::restoreLostDevice()
741{
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000742 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
743 {
744 if ((*ctx)->isResetNotificationEnabled())
745 return false; // If reset notifications have been requested, application must delete all contexts first
746 }
747
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000748 // Release surface resources to make the Reset() succeed
749 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
750 {
751 (*surface)->release();
752 }
753
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000754 while (!mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000755 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000756 mEventQueryPool.back()->Release();
757 mEventQueryPool.pop_back();
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000758 }
759
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +0000760 mVertexShaderCache.clear();
761 mPixelShaderCache.clear();
762
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000763 if (!resetDevice())
764 {
765 return false;
766 }
767
768 // Restore any surfaces that may have been lost
769 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
770 {
771 (*surface)->resetSwapChain();
772 }
773
774 return true;
775}
776
777
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000778void Display::destroySurface(egl::Surface *surface)
779{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000780 delete surface;
781 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000782}
783
784void Display::destroyContext(gl::Context *context)
785{
786 glDestroyContext(context);
787 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000788}
789
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000790void Display::notifyDeviceLost()
791{
792 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
793 {
794 (*context)->markContextLost();
795 }
796 mDeviceLost = true;
797 error(EGL_CONTEXT_LOST);
798}
799
800bool Display::isDeviceLost()
801{
802 return mDeviceLost;
803}
804
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000805bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000806{
807 return mD3d9 != NULL && mConfigSet.size() > 0;
808}
809
810bool Display::isValidConfig(EGLConfig config)
811{
812 return mConfigSet.get(config) != NULL;
813}
814
815bool Display::isValidContext(gl::Context *context)
816{
817 return mContextSet.find(context) != mContextSet.end();
818}
819
820bool Display::isValidSurface(egl::Surface *surface)
821{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000822 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000823}
824
825bool Display::hasExistingWindowSurface(HWND window)
826{
827 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
828 {
829 if ((*surface)->getWindowHandle() == window)
830 {
831 return true;
832 }
833 }
834
835 return false;
836}
837
838EGLint Display::getMinSwapInterval()
839{
840 return mMinSwapInterval;
841}
842
843EGLint Display::getMaxSwapInterval()
844{
845 return mMaxSwapInterval;
846}
847
848IDirect3DDevice9 *Display::getDevice()
849{
850 if (!mDevice)
851 {
852 if (!createDevice())
853 {
854 return NULL;
855 }
856 }
857
858 return mDevice;
859}
860
861D3DCAPS9 Display::getDeviceCaps()
862{
863 return mDeviceCaps;
864}
865
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000866D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
867{
868 return &mAdapterIdentifier;
869}
870
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000871bool Display::testDeviceLost()
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000872{
873 if (mDeviceEx)
874 {
875 return FAILED(mDeviceEx->CheckDeviceState(NULL));
876 }
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000877 else if (mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000878 {
879 return FAILED(mDevice->TestCooperativeLevel());
880 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000881
882 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000883}
884
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000885bool Display::testDeviceResettable()
886{
887 HRESULT status = D3D_OK;
888
889 if (mDeviceEx)
890 {
891 status = mDeviceEx->CheckDeviceState(NULL);
892 }
893 else if (mDevice)
894 {
895 status = mDevice->TestCooperativeLevel();
896 }
897
898 switch (status)
899 {
900 case D3DERR_DEVICENOTRESET:
901 case D3DERR_DEVICEHUNG:
902 return true;
903 default:
904 return false;
905 }
906}
907
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000908void Display::sync(bool block)
909{
910 HRESULT result;
911
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000912 IDirect3DQuery9* query = allocateEventQuery();
913 if (!query)
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000914 {
915 return;
916 }
917
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000918 result = query->Issue(D3DISSUE_END);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000919 ASSERT(SUCCEEDED(result));
920
921 do
922 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000923 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000924
925 if(block && result == S_FALSE)
926 {
927 // Keep polling, but allow other threads to do something useful first
928 Sleep(0);
929 // explicitly check for device loss
930 // some drivers seem to return S_FALSE even if the device is lost
931 // instead of D3DERR_DEVICELOST like they should
932 if (testDeviceLost())
933 {
934 result = D3DERR_DEVICELOST;
935 }
936 }
937 }
938 while(block && result == S_FALSE);
939
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000940 freeEventQuery(query);
941
daniel@transgaming.com2678b342012-01-18 16:29:40 +0000942 if (isDeviceLostError(result))
943 {
944 notifyDeviceLost();
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000945 }
946}
947
948IDirect3DQuery9* Display::allocateEventQuery()
949{
950 IDirect3DQuery9 *query = NULL;
951
952 if (mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000953 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000954 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
955 ASSERT(SUCCEEDED(result));
956 }
957 else
958 {
959 query = mEventQueryPool.back();
960 mEventQueryPool.pop_back();
961 }
962
963 return query;
964}
965
966void Display::freeEventQuery(IDirect3DQuery9* query)
967{
968 if (mEventQueryPool.size() > 1000)
969 {
970 query->Release();
971 }
972 else
973 {
974 mEventQueryPool.push_back(query);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000975 }
976}
977
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000978void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
979{
980 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
981 {
982 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
983 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
984
985 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
986 }
987}
988
gman@chromium.org50c526d2011-08-10 05:19:44 +0000989bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000990{
991 D3DDISPLAYMODE currentDisplayMode;
992 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
993
994 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
995}
996
gman@chromium.org50c526d2011-08-10 05:19:44 +0000997bool Display::getDXT3TextureSupport()
998{
999 D3DDISPLAYMODE currentDisplayMode;
1000 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1001
1002 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1003}
1004
1005bool Display::getDXT5TextureSupport()
1006{
1007 D3DDISPLAYMODE currentDisplayMode;
1008 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1009
1010 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1011}
1012
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00001013// we use INTZ for depth textures in Direct3D9
1014// we also want NULL texture support to ensure the we can make depth-only FBOs
1015// see http://aras-p.info/texts/D3D9GPUHacks.html
1016bool Display::getDepthTextureSupport() const
1017{
1018 D3DDISPLAYMODE currentDisplayMode;
1019 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1020
1021 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1022 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1023 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1024 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1025
1026 return intz && null;
1027}
1028
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00001029bool Display::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001030{
1031 D3DDISPLAYMODE currentDisplayMode;
1032 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1033
1034 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1035 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1036 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1037 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1038
1039 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1040 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1041 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1042 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1043
daniel@transgaming.combb5223d2012-01-24 13:38:10 +00001044 if (!*filtering && !*renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001045 {
1046 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1047 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1048 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1049 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1050 }
1051 else
1052 {
1053 return true;
1054 }
1055}
1056
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00001057bool Display::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001058{
1059 D3DDISPLAYMODE currentDisplayMode;
1060 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1061
1062 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1063 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1064 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1065 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1066
1067 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1068 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1069 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1070 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1071
daniel@transgaming.combb5223d2012-01-24 13:38:10 +00001072 if (!*filtering && !*renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001073 {
1074 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1075 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1076 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1077 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1078 }
1079 else
1080 {
1081 return true;
1082 }
1083}
1084
daniel@transgaming.comed828e52010-10-15 17:57:30 +00001085bool Display::getLuminanceTextureSupport()
1086{
1087 D3DDISPLAYMODE currentDisplayMode;
1088 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1089
1090 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1091}
1092
1093bool Display::getLuminanceAlphaTextureSupport()
1094{
1095 D3DDISPLAYMODE currentDisplayMode;
1096 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1097
1098 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1099}
1100
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001101float Display::getTextureFilterAnisotropySupport() const
1102{
1103 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
1104 if ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2))
1105 {
1106 return mDeviceCaps.MaxAnisotropy;
1107 }
1108 return 1.0f;
1109}
1110
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001111D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001112{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +00001113 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001114 {
1115 return D3DPOOL_DEFAULT;
1116 }
1117 else
1118 {
1119 if (!(usage & D3DUSAGE_DYNAMIC))
1120 {
1121 return D3DPOOL_MANAGED;
1122 }
1123 }
1124
1125 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001126}
1127
daniel@transgaming.come32d5692012-05-31 01:14:22 +00001128D3DPOOL Display::getTexturePool(DWORD usage) const
daniel@transgaming.com59580a32011-11-12 04:23:44 +00001129{
1130 if (mD3d9Ex != NULL)
1131 {
1132 return D3DPOOL_DEFAULT;
1133 }
1134 else
1135 {
daniel@transgaming.come32d5692012-05-31 01:14:22 +00001136 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
daniel@transgaming.com59580a32011-11-12 04:23:44 +00001137 {
1138 return D3DPOOL_MANAGED;
1139 }
1140 }
1141
1142 return D3DPOOL_DEFAULT;
1143}
1144
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001145bool Display::getEventQuerySupport()
1146{
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001147 IDirect3DQuery9 *query = allocateEventQuery();
1148 if (query)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001149 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001150 freeEventQuery(query);
1151 return true;
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001152 }
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001153 else
1154 {
1155 return false;
1156 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001157}
1158
1159D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
1160{
1161 D3DPRESENT_PARAMETERS presentParameters = {0};
1162
1163 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
1164 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1165 presentParameters.BackBufferCount = 1;
1166 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
1167 presentParameters.BackBufferWidth = 1;
1168 presentParameters.BackBufferHeight = 1;
1169 presentParameters.EnableAutoDepthStencil = FALSE;
1170 presentParameters.Flags = 0;
1171 presentParameters.hDeviceWindow = mDeviceWindow;
1172 presentParameters.MultiSampleQuality = 0;
1173 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
1174 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
1175 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1176 presentParameters.Windowed = TRUE;
1177
1178 return presentParameters;
1179}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001180
1181void Display::initExtensionString()
1182{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001183 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001184
1185 mExtensionString = "";
1186
daniel@transgaming.com8747f182011-11-09 17:50:38 +00001187 // Multi-vendor (EXT) extensions
1188 mExtensionString += "EGL_EXT_create_context_robustness ";
1189
1190 // ANGLE-specific extensions
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001191 if (shareHandleSupported())
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001192 {
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001193 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
1194 }
1195
1196 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001197
1198 if (swiftShader)
1199 {
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001200 mExtensionString += "EGL_ANGLE_software_display ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001201 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001202
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001203 if (shareHandleSupported())
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001204 {
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001205 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
1206 }
1207
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001208 mExtensionString += "EGL_NV_post_sub_buffer";
1209
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001210 std::string::size_type end = mExtensionString.find_last_not_of(' ');
1211 if (end != std::string::npos)
1212 {
1213 mExtensionString.resize(end+1);
1214 }
1215}
1216
1217const char *Display::getExtensionString() const
1218{
1219 return mExtensionString.c_str();
1220}
1221
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001222bool Display::shareHandleSupported() const
1223{
1224 // PIX doesn't seem to support using share handles, so disable them.
1225 return isD3d9ExDevice() && !gl::perfActive();
1226}
1227
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001228IDirect3DVertexShader9 *Display::createVertexShader(const DWORD *function, size_t length)
1229{
1230 return mVertexShaderCache.create(function, length);
1231}
1232
1233IDirect3DPixelShader9 *Display::createPixelShader(const DWORD *function, size_t length)
1234{
1235 return mPixelShaderCache.create(function, length);
1236}
1237
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001238// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1239// We test this using D3D9 by checking support for the R16F format.
1240bool Display::getVertexTextureSupport() const
1241{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001242 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1243 {
1244 return false;
1245 }
1246
1247 D3DDISPLAYMODE currentDisplayMode;
1248 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1249
1250 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1251
1252 return SUCCEEDED(result);
1253}
1254
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001255bool Display::getNonPower2TextureSupport() const
1256{
1257 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
1258 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
1259 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
1260}
1261
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001262bool Display::getOcclusionQuerySupport() const
1263{
1264 if (!isInitialized())
1265 {
1266 return false;
1267 }
1268
1269 IDirect3DQuery9 *query = NULL;
1270 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1271
1272 if (SUCCEEDED(result) && query)
1273 {
1274 query->Release();
1275 return true;
1276 }
1277 else
1278 {
1279 return false;
1280 }
1281}
1282
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00001283bool Display::getInstancingSupport() const
1284{
1285 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1286}
1287
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001288}