blob: 3ef90a24a3b5e585b117aebd196bdf4261c4c4ee [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
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000297 return true;
298}
299
300void Display::terminate()
301{
302 while (!mSurfaceSet.empty())
303 {
304 destroySurface(*mSurfaceSet.begin());
305 }
306
307 while (!mContextSet.empty())
308 {
309 destroyContext(*mContextSet.begin());
310 }
311
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000312 while (!mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000313 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000314 mEventQueryPool.back()->Release();
315 mEventQueryPool.pop_back();
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000316 }
317
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000318 if (mDevice)
319 {
320 // 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 +0000321 if (testDeviceLost())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000322 {
323 resetDevice();
324 }
325
326 mDevice->Release();
327 mDevice = NULL;
328 }
329
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000330 if (mDeviceEx)
331 {
332 mDeviceEx->Release();
333 mDeviceEx = NULL;
334 }
335
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000336 if (mD3d9)
337 {
338 mD3d9->Release();
339 mD3d9 = NULL;
340 }
341
342 if (mDeviceWindow)
343 {
344 DestroyWindow(mDeviceWindow);
345 mDeviceWindow = NULL;
346 }
347
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000348 if (mD3d9Ex)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000349 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000350 mD3d9Ex->Release();
351 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000352 }
353
354 if (mD3d9Module)
355 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000356 mD3d9Module = NULL;
357 }
358}
359
360void Display::startScene()
361{
362 if (!mSceneStarted)
363 {
364 long result = mDevice->BeginScene();
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000365 if (SUCCEEDED(result)) {
366 // This is defensive checking against the device being
367 // lost at unexpected times.
368 mSceneStarted = true;
369 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000370 }
371}
372
373void Display::endScene()
374{
375 if (mSceneStarted)
376 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000377 // EndScene can fail if the device was lost, for example due
378 // to a TDR during a draw call.
379 mDevice->EndScene();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000380 mSceneStarted = false;
381 }
382}
383
384bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
385{
386 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
387}
388
389bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
390{
391 const egl::Config *configuration = mConfigSet.get(config);
392
393 switch (attribute)
394 {
395 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
396 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
397 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
398 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
399 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
400 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
401 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
402 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
403 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
404 case EGL_LEVEL: *value = configuration->mLevel; break;
405 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
406 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
407 case EGL_SAMPLES: *value = configuration->mSamples; break;
408 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
409 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
410 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
411 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
412 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
413 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
414 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
415 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
416 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
417 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
418 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
419 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
420 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
421 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
422 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
423 case EGL_CONFORMANT: *value = configuration->mConformant; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000424 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
425 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
426 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000427 default:
428 return false;
429 }
430
431 return true;
432}
433
434bool Display::createDevice()
435{
436 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
437 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
438
439 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
440
441 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
442 {
443 return error(EGL_BAD_ALLOC, false);
444 }
445
446 if (FAILED(result))
447 {
448 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
449
450 if (FAILED(result))
451 {
452 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
453 return error(EGL_BAD_ALLOC, false);
454 }
455 }
456
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000457 if (mD3d9Ex)
458 {
459 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
460 ASSERT(SUCCEEDED(result));
461 }
462
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000463 initializeDevice();
464
465 return true;
466}
467
468// do any one-time device initialization
469// NOTE: this is also needed after a device lost/reset
470// to reset the scene status and ensure the default states are reset.
471void Display::initializeDevice()
472{
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000473 // Permanent non-default states
474 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
daniel@transgaming.com7a2fdc92011-11-24 22:34:09 +0000475 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000476
daniel@transgaming.com13be3e42012-07-04 19:16:24 +0000477 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
478 {
479 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
480 }
481 else
482 {
483 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
484 }
485
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000486 mSceneStarted = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000487}
488
489bool Display::resetDevice()
490{
491 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000492
daniel@transgaming.com10435352011-11-09 17:44:21 +0000493 HRESULT result = D3D_OK;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000494 bool lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000495 int attempts = 3;
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000496
daniel@transgaming.com10435352011-11-09 17:44:21 +0000497 while (lost && attempts > 0)
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000498 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000499 if (mDeviceEx)
500 {
501 Sleep(500); // Give the graphics driver some CPU time
502 result = mDeviceEx->ResetEx(&presentParameters, NULL);
503 }
504 else
505 {
506 result = mDevice->TestCooperativeLevel();
507
508 while (result == D3DERR_DEVICELOST)
509 {
510 Sleep(100); // Give the graphics driver some CPU time
511 result = mDevice->TestCooperativeLevel();
512 }
513
514 if (result == D3DERR_DEVICENOTRESET)
515 {
516 result = mDevice->Reset(&presentParameters);
517 }
518 }
519
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000520 lost = testDeviceLost();
daniel@transgaming.com10435352011-11-09 17:44:21 +0000521 attempts --;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000522 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000523
524 if (FAILED(result))
525 {
daniel@transgaming.com10435352011-11-09 17:44:21 +0000526 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000527 return error(EGL_BAD_ALLOC, false);
528 }
529
daniel@transgaming.come19d8322011-12-13 17:30:48 +0000530 // reset device defaults
531 initializeDevice();
532
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000533 return true;
534}
535
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000536EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000537{
538 const Config *configuration = mConfigSet.get(config);
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000539 EGLint postSubBufferSupported = EGL_FALSE;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000540
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000541 if (attribList)
542 {
543 while (*attribList != EGL_NONE)
544 {
545 switch (attribList[0])
546 {
547 case EGL_RENDER_BUFFER:
548 switch (attribList[1])
549 {
550 case EGL_BACK_BUFFER:
551 break;
552 case EGL_SINGLE_BUFFER:
553 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
554 default:
555 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
556 }
557 break;
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000558 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
559 postSubBufferSupported = attribList[1];
560 break;
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000561 case EGL_VG_COLORSPACE:
562 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
563 case EGL_VG_ALPHA_FORMAT:
564 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
565 default:
566 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
567 }
568
569 attribList += 2;
570 }
571 }
572
573 if (hasExistingWindowSurface(window))
574 {
575 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
576 }
577
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000578 if (testDeviceLost())
579 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000580 if (!restoreLostDevice())
581 return EGL_NO_SURFACE;
582 }
583
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +0000584 Surface *surface = new Surface(this, configuration, window, postSubBufferSupported);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000585
586 if (!surface->initialize())
587 {
588 delete surface;
589 return EGL_NO_SURFACE;
590 }
591
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000592 mSurfaceSet.insert(surface);
593
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000594 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000595}
596
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000597EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000598{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000599 EGLint width = 0, height = 0;
600 EGLenum textureFormat = EGL_NO_TEXTURE;
601 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000602 const Config *configuration = mConfigSet.get(config);
603
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000604 if (attribList)
605 {
606 while (*attribList != EGL_NONE)
607 {
608 switch (attribList[0])
609 {
610 case EGL_WIDTH:
611 width = attribList[1];
612 break;
613 case EGL_HEIGHT:
614 height = attribList[1];
615 break;
616 case EGL_LARGEST_PBUFFER:
617 if (attribList[1] != EGL_FALSE)
618 UNIMPLEMENTED(); // FIXME
619 break;
620 case EGL_TEXTURE_FORMAT:
621 switch (attribList[1])
622 {
623 case EGL_NO_TEXTURE:
624 case EGL_TEXTURE_RGB:
625 case EGL_TEXTURE_RGBA:
626 textureFormat = attribList[1];
627 break;
628 default:
629 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
630 }
631 break;
632 case EGL_TEXTURE_TARGET:
633 switch (attribList[1])
634 {
635 case EGL_NO_TEXTURE:
636 case EGL_TEXTURE_2D:
637 textureTarget = attribList[1];
638 break;
639 default:
640 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
641 }
642 break;
643 case EGL_MIPMAP_TEXTURE:
644 if (attribList[1] != EGL_FALSE)
645 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
646 break;
647 case EGL_VG_COLORSPACE:
648 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
649 case EGL_VG_ALPHA_FORMAT:
650 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
651 default:
652 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
653 }
654
655 attribList += 2;
656 }
657 }
658
659 if (width < 0 || height < 0)
660 {
661 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
662 }
663
664 if (width == 0 || height == 0)
665 {
666 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
667 }
668
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000669 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000670 {
671 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
672 }
673
674 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
675 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
676 {
677 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
678 }
679
680 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
681 {
682 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
683 }
684
685 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
686 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
687 {
688 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
689 }
690
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000691 if (testDeviceLost())
692 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000693 if (!restoreLostDevice())
694 return EGL_NO_SURFACE;
695 }
696
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000697 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
698
699 if (!surface->initialize())
700 {
701 delete surface;
702 return EGL_NO_SURFACE;
703 }
704
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000705 mSurfaceSet.insert(surface);
706
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000707 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000708}
709
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000710EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000711{
712 if (!mDevice)
713 {
714 if (!createDevice())
715 {
716 return NULL;
717 }
718 }
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000719 else if (testDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000720 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000721 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000722 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000723 }
724
725 const egl::Config *config = mConfigSet.get(configHandle);
726
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000727 gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000728 mContextSet.insert(context);
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000729 mDeviceLost = false;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000730
731 return context;
732}
733
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000734bool Display::restoreLostDevice()
735{
daniel@transgaming.comcf23c452011-11-09 17:47:26 +0000736 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
737 {
738 if ((*ctx)->isResetNotificationEnabled())
739 return false; // If reset notifications have been requested, application must delete all contexts first
740 }
741
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000742 // Release surface resources to make the Reset() succeed
743 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
744 {
745 (*surface)->release();
746 }
747
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000748 while (!mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000749 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000750 mEventQueryPool.back()->Release();
751 mEventQueryPool.pop_back();
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000752 }
753
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000754 if (!resetDevice())
755 {
756 return false;
757 }
758
759 // Restore any surfaces that may have been lost
760 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
761 {
762 (*surface)->resetSwapChain();
763 }
764
765 return true;
766}
767
768
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000769void Display::destroySurface(egl::Surface *surface)
770{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000771 delete surface;
772 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000773}
774
775void Display::destroyContext(gl::Context *context)
776{
777 glDestroyContext(context);
778 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000779}
780
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000781void Display::notifyDeviceLost()
782{
783 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
784 {
785 (*context)->markContextLost();
786 }
787 mDeviceLost = true;
788 error(EGL_CONTEXT_LOST);
789}
790
791bool Display::isDeviceLost()
792{
793 return mDeviceLost;
794}
795
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000796bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000797{
798 return mD3d9 != NULL && mConfigSet.size() > 0;
799}
800
801bool Display::isValidConfig(EGLConfig config)
802{
803 return mConfigSet.get(config) != NULL;
804}
805
806bool Display::isValidContext(gl::Context *context)
807{
808 return mContextSet.find(context) != mContextSet.end();
809}
810
811bool Display::isValidSurface(egl::Surface *surface)
812{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000813 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000814}
815
816bool Display::hasExistingWindowSurface(HWND window)
817{
818 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
819 {
820 if ((*surface)->getWindowHandle() == window)
821 {
822 return true;
823 }
824 }
825
826 return false;
827}
828
829EGLint Display::getMinSwapInterval()
830{
831 return mMinSwapInterval;
832}
833
834EGLint Display::getMaxSwapInterval()
835{
836 return mMaxSwapInterval;
837}
838
839IDirect3DDevice9 *Display::getDevice()
840{
841 if (!mDevice)
842 {
843 if (!createDevice())
844 {
845 return NULL;
846 }
847 }
848
849 return mDevice;
850}
851
852D3DCAPS9 Display::getDeviceCaps()
853{
854 return mDeviceCaps;
855}
856
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000857D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
858{
859 return &mAdapterIdentifier;
860}
861
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000862bool Display::testDeviceLost()
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000863{
864 if (mDeviceEx)
865 {
866 return FAILED(mDeviceEx->CheckDeviceState(NULL));
867 }
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000868 else if (mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000869 {
870 return FAILED(mDevice->TestCooperativeLevel());
871 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000872
873 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000874}
875
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000876bool Display::testDeviceResettable()
877{
878 HRESULT status = D3D_OK;
879
880 if (mDeviceEx)
881 {
882 status = mDeviceEx->CheckDeviceState(NULL);
883 }
884 else if (mDevice)
885 {
886 status = mDevice->TestCooperativeLevel();
887 }
888
889 switch (status)
890 {
891 case D3DERR_DEVICENOTRESET:
892 case D3DERR_DEVICEHUNG:
893 return true;
894 default:
895 return false;
896 }
897}
898
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000899void Display::sync(bool block)
900{
901 HRESULT result;
902
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000903 IDirect3DQuery9* query = allocateEventQuery();
904 if (!query)
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000905 {
906 return;
907 }
908
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000909 result = query->Issue(D3DISSUE_END);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000910 ASSERT(SUCCEEDED(result));
911
912 do
913 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000914 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000915
916 if(block && result == S_FALSE)
917 {
918 // Keep polling, but allow other threads to do something useful first
919 Sleep(0);
920 // explicitly check for device loss
921 // some drivers seem to return S_FALSE even if the device is lost
922 // instead of D3DERR_DEVICELOST like they should
923 if (testDeviceLost())
924 {
925 result = D3DERR_DEVICELOST;
926 }
927 }
928 }
929 while(block && result == S_FALSE);
930
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000931 freeEventQuery(query);
932
daniel@transgaming.com2678b342012-01-18 16:29:40 +0000933 if (isDeviceLostError(result))
934 {
935 notifyDeviceLost();
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000936 }
937}
938
939IDirect3DQuery9* Display::allocateEventQuery()
940{
941 IDirect3DQuery9 *query = NULL;
942
943 if (mEventQueryPool.empty())
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000944 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +0000945 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
946 ASSERT(SUCCEEDED(result));
947 }
948 else
949 {
950 query = mEventQueryPool.back();
951 mEventQueryPool.pop_back();
952 }
953
954 return query;
955}
956
957void Display::freeEventQuery(IDirect3DQuery9* query)
958{
959 if (mEventQueryPool.size() > 1000)
960 {
961 query->Release();
962 }
963 else
964 {
965 mEventQueryPool.push_back(query);
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +0000966 }
967}
968
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000969void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
970{
971 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
972 {
973 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
974 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
975
976 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
977 }
978}
979
gman@chromium.org50c526d2011-08-10 05:19:44 +0000980bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000981{
982 D3DDISPLAYMODE currentDisplayMode;
983 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
984
985 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
986}
987
gman@chromium.org50c526d2011-08-10 05:19:44 +0000988bool Display::getDXT3TextureSupport()
989{
990 D3DDISPLAYMODE currentDisplayMode;
991 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
992
993 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
994}
995
996bool Display::getDXT5TextureSupport()
997{
998 D3DDISPLAYMODE currentDisplayMode;
999 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1000
1001 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1002}
1003
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00001004// we use INTZ for depth textures in Direct3D9
1005// we also want NULL texture support to ensure the we can make depth-only FBOs
1006// see http://aras-p.info/texts/D3D9GPUHacks.html
1007bool Display::getDepthTextureSupport() const
1008{
1009 D3DDISPLAYMODE currentDisplayMode;
1010 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1011
1012 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1013 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1014 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1015 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1016
1017 return intz && null;
1018}
1019
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00001020bool Display::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001021{
1022 D3DDISPLAYMODE currentDisplayMode;
1023 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1024
1025 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1026 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1027 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1028 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1029
1030 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1031 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1032 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1033 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1034
daniel@transgaming.combb5223d2012-01-24 13:38:10 +00001035 if (!*filtering && !*renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001036 {
1037 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1038 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1039 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1040 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1041 }
1042 else
1043 {
1044 return true;
1045 }
1046}
1047
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00001048bool Display::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001049{
1050 D3DDISPLAYMODE currentDisplayMode;
1051 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1052
1053 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1054 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1055 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1056 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1057
1058 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1059 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1060 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1061 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1062
daniel@transgaming.combb5223d2012-01-24 13:38:10 +00001063 if (!*filtering && !*renderable)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001064 {
1065 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1066 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1067 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1068 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1069 }
1070 else
1071 {
1072 return true;
1073 }
1074}
1075
daniel@transgaming.comed828e52010-10-15 17:57:30 +00001076bool Display::getLuminanceTextureSupport()
1077{
1078 D3DDISPLAYMODE currentDisplayMode;
1079 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1080
1081 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1082}
1083
1084bool Display::getLuminanceAlphaTextureSupport()
1085{
1086 D3DDISPLAYMODE currentDisplayMode;
1087 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1088
1089 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1090}
1091
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001092float Display::getTextureFilterAnisotropySupport() const
1093{
1094 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
1095 if ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2))
1096 {
1097 return mDeviceCaps.MaxAnisotropy;
1098 }
1099 return 1.0f;
1100}
1101
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001102D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001103{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +00001104 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +00001105 {
1106 return D3DPOOL_DEFAULT;
1107 }
1108 else
1109 {
1110 if (!(usage & D3DUSAGE_DYNAMIC))
1111 {
1112 return D3DPOOL_MANAGED;
1113 }
1114 }
1115
1116 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +00001117}
1118
daniel@transgaming.come32d5692012-05-31 01:14:22 +00001119D3DPOOL Display::getTexturePool(DWORD usage) const
daniel@transgaming.com59580a32011-11-12 04:23:44 +00001120{
1121 if (mD3d9Ex != NULL)
1122 {
1123 return D3DPOOL_DEFAULT;
1124 }
1125 else
1126 {
daniel@transgaming.come32d5692012-05-31 01:14:22 +00001127 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
daniel@transgaming.com59580a32011-11-12 04:23:44 +00001128 {
1129 return D3DPOOL_MANAGED;
1130 }
1131 }
1132
1133 return D3DPOOL_DEFAULT;
1134}
1135
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001136bool Display::getEventQuerySupport()
1137{
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001138 IDirect3DQuery9 *query = allocateEventQuery();
1139 if (query)
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001140 {
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001141 freeEventQuery(query);
1142 return true;
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001143 }
apatrick@chromium.orgf289ee82012-01-11 20:03:29 +00001144 else
1145 {
1146 return false;
1147 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001148}
1149
1150D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
1151{
1152 D3DPRESENT_PARAMETERS presentParameters = {0};
1153
1154 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
1155 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1156 presentParameters.BackBufferCount = 1;
1157 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
1158 presentParameters.BackBufferWidth = 1;
1159 presentParameters.BackBufferHeight = 1;
1160 presentParameters.EnableAutoDepthStencil = FALSE;
1161 presentParameters.Flags = 0;
1162 presentParameters.hDeviceWindow = mDeviceWindow;
1163 presentParameters.MultiSampleQuality = 0;
1164 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
1165 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
1166 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1167 presentParameters.Windowed = TRUE;
1168
1169 return presentParameters;
1170}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001171
1172void Display::initExtensionString()
1173{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001174 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001175
1176 mExtensionString = "";
1177
daniel@transgaming.com8747f182011-11-09 17:50:38 +00001178 // Multi-vendor (EXT) extensions
1179 mExtensionString += "EGL_EXT_create_context_robustness ";
1180
1181 // ANGLE-specific extensions
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001182 if (shareHandleSupported())
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001183 {
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +00001184 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
1185 }
1186
1187 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001188
1189 if (swiftShader)
1190 {
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001191 mExtensionString += "EGL_ANGLE_software_display ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +00001192 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001193
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001194 if (shareHandleSupported())
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001195 {
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001196 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
1197 }
1198
apatrick@chromium.orgf4490e22011-12-06 02:05:22 +00001199 mExtensionString += "EGL_NV_post_sub_buffer";
1200
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001201 std::string::size_type end = mExtensionString.find_last_not_of(' ');
1202 if (end != std::string::npos)
1203 {
1204 mExtensionString.resize(end+1);
1205 }
1206}
1207
1208const char *Display::getExtensionString() const
1209{
1210 return mExtensionString.c_str();
1211}
1212
jbauman@chromium.org6c762d02012-02-03 23:31:53 +00001213bool Display::shareHandleSupported() const
1214{
1215 // PIX doesn't seem to support using share handles, so disable them.
1216 return isD3d9ExDevice() && !gl::perfActive();
1217}
1218
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001219// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1220// We test this using D3D9 by checking support for the R16F format.
1221bool Display::getVertexTextureSupport() const
1222{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00001223 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1224 {
1225 return false;
1226 }
1227
1228 D3DDISPLAYMODE currentDisplayMode;
1229 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1230
1231 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1232
1233 return SUCCEEDED(result);
1234}
1235
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00001236bool Display::getNonPower2TextureSupport() const
1237{
1238 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
1239 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
1240 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
1241}
1242
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001243bool Display::getOcclusionQuerySupport() const
1244{
1245 if (!isInitialized())
1246 {
1247 return false;
1248 }
1249
1250 IDirect3DQuery9 *query = NULL;
1251 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1252
1253 if (SUCCEEDED(result) && query)
1254 {
1255 query->Release();
1256 return true;
1257 }
1258 else
1259 {
1260 return false;
1261 }
1262}
1263
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00001264bool Display::getInstancingSupport() const
1265{
1266 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1267}
1268
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001269}