blob: a46a823f4168a006dd72a773b44b247cb71c5f3f [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Renderer.cpp: Implements a back-end specific class that hides the details of the
8// implementation-specific renderer.
9
10#include "libGLESv2/renderer/Renderer.h"
11#include "common/debug.h"
12#include "libGLESv2/utilities.h"
13
daniel@transgaming.com3281f972012-10-31 18:38:51 +000014#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000015#include "libEGL/Display.h"
16
daniel@transgaming.com621ce052012-10-31 17:52:29 +000017// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
18#define REF_RAST 0
19
20// The "Debug This Pixel..." feature in PIX often fails when using the
21// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
22// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
23#if !defined(ANGLE_ENABLE_D3D9EX)
24// Enables use of the IDirect3D9Ex interface, when available
25#define ANGLE_ENABLE_D3D9EX 1
26#endif // !defined(ANGLE_ENABLE_D3D9EX)
27
28namespace renderer
29{
daniel@transgaming.com92955622012-10-31 18:38:41 +000030const D3DFORMAT Renderer::mRenderTargetFormats[] =
31 {
32 D3DFMT_A1R5G5B5,
33 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
34 D3DFMT_A8R8G8B8,
35 D3DFMT_R5G6B5,
36 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
37 D3DFMT_X8R8G8B8
38 };
39
40const D3DFORMAT Renderer::mDepthStencilFormats[] =
41 {
42 D3DFMT_UNKNOWN,
43 // D3DFMT_D16_LOCKABLE,
44 D3DFMT_D32,
45 // D3DFMT_D15S1,
46 D3DFMT_D24S8,
47 D3DFMT_D24X8,
48 // D3DFMT_D24X4S4,
49 D3DFMT_D16,
50 // D3DFMT_D32F_LOCKABLE,
51 // D3DFMT_D24FS8
52 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000053
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000054Renderer::Renderer(egl::Display *display, HMODULE hModule, HDC hDc): mDc(hDc)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000055{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000056 mDisplay = display;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000057 mD3d9Module = hModule;
58
59 mD3d9 = NULL;
60 mD3d9Ex = NULL;
61 mDevice = NULL;
62 mDeviceEx = NULL;
63 mDeviceWindow = NULL;
64
65 mAdapter = D3DADAPTER_DEFAULT;
66
67 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
68 mDeviceType = D3DDEVTYPE_REF;
69 #else
70 mDeviceType = D3DDEVTYPE_HAL;
71 #endif
72
73 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000074
75 mMaxSupportedSamples = 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000076}
77
78Renderer::~Renderer()
79{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000080 releaseDeviceResources();
81
daniel@transgaming.com621ce052012-10-31 17:52:29 +000082 if (mDevice)
83 {
84 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +000085 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +000086 {
87 resetDevice();
88 }
89
90 mDevice->Release();
91 mDevice = NULL;
92 }
93
94 if (mDeviceEx)
95 {
96 mDeviceEx->Release();
97 mDeviceEx = NULL;
98 }
99
100 if (mD3d9)
101 {
102 mD3d9->Release();
103 mD3d9 = NULL;
104 }
105
106 if (mDeviceWindow)
107 {
108 DestroyWindow(mDeviceWindow);
109 mDeviceWindow = NULL;
110 }
111
112 if (mD3d9Ex)
113 {
114 mD3d9Ex->Release();
115 mD3d9Ex = NULL;
116 }
117
118 if (mD3d9Module)
119 {
120 mD3d9Module = NULL;
121 }
122
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000123 while (!mMultiSampleSupport.empty())
124 {
125 delete [] mMultiSampleSupport.begin()->second;
126 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
127 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000128}
129
130EGLint Renderer::initialize()
131{
132 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
133 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
134
135 // Use Direct3D9Ex if available. Among other things, this version is less
136 // inclined to report a lost context, for example when the user switches
137 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
138 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
139 {
140 ASSERT(mD3d9Ex);
141 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
142 ASSERT(mD3d9);
143 }
144 else
145 {
146 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
147 }
148
149 if (!mD3d9)
150 {
151 ERR("Could not create D3D9 device - aborting!\n");
152 return EGL_NOT_INITIALIZED;
153 }
154 if (mDc != NULL)
155 {
156 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
157 }
158
159 HRESULT result;
160
161 // Give up on getting device caps after about one second.
162 for (int i = 0; i < 10; ++i)
163 {
164 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
165 if (SUCCEEDED(result))
166 {
167 break;
168 }
169 else if (result == D3DERR_NOTAVAILABLE)
170 {
171 Sleep(100); // Give the driver some time to initialize/recover
172 }
173 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
174 {
175 ERR("failed to get device caps (0x%x)\n", result);
176 return EGL_NOT_INITIALIZED;
177 }
178 }
179
180 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
181 {
182 ERR("Renderer does not support PS 2.0. aborting!\n");
183 return EGL_NOT_INITIALIZED;
184 }
185
186 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
187 // This is required by Texture2D::convertToRenderTarget.
188 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
189 {
190 ERR("Renderer does not support stretctrect from textures!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
195
196 // ATI cards on XP have problems with non-power-of-two textures.
197 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
198 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
199 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
200 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
201
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000202 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
203 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
204
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000205 mMinSwapInterval = 4;
206 mMaxSwapInterval = 0;
207
208 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
209 {
210 mMinSwapInterval = std::min(mMinSwapInterval, 0);
211 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
212 }
213 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
214 {
215 mMinSwapInterval = std::min(mMinSwapInterval, 1);
216 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
217 }
218 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
219 {
220 mMinSwapInterval = std::min(mMinSwapInterval, 2);
221 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
222 }
223 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
224 {
225 mMinSwapInterval = std::min(mMinSwapInterval, 3);
226 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
227 }
228 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
229 {
230 mMinSwapInterval = std::min(mMinSwapInterval, 4);
231 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
232 }
233
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000234 int max = 0;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000235 for (int i = 0; i < sizeof(mRenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000236 {
237 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com92955622012-10-31 18:38:41 +0000238 getMultiSampleSupport(mRenderTargetFormats[i], multisampleArray);
239 mMultiSampleSupport[mRenderTargetFormats[i]] = multisampleArray;
240
241 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
242 {
243 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
244 {
245 max = j;
246 }
247 }
248 }
249
250 for (int i = 0; i < sizeof(mDepthStencilFormats) / sizeof(D3DFORMAT); ++i)
251 {
252 if (mDepthStencilFormats[i] == D3DFMT_UNKNOWN)
253 continue;
254
255 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
256 getMultiSampleSupport(mDepthStencilFormats[i], multisampleArray);
257 mMultiSampleSupport[mDepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000258
259 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
260 {
261 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
262 {
263 max = j;
264 }
265 }
266 }
267
268 mMaxSupportedSamples = max;
269
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000270 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
271 static const TCHAR className[] = TEXT("STATIC");
272
273 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
274
275 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
276 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
277
278 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
279 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
280 {
281 return EGL_BAD_ALLOC;
282 }
283
284 if (FAILED(result))
285 {
286 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
287
288 if (FAILED(result))
289 {
290 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
291 return EGL_BAD_ALLOC;
292 }
293 }
294
295 if (mD3d9Ex)
296 {
297 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
298 ASSERT(SUCCEEDED(result));
299 }
300
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000301 mVertexShaderCache.initialize(mDevice);
302 mPixelShaderCache.initialize(mDevice);
303
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000304 initializeDevice();
305
306 return EGL_SUCCESS;
307}
308
309// do any one-time device initialization
310// NOTE: this is also needed after a device lost/reset
311// to reset the scene status and ensure the default states are reset.
312void Renderer::initializeDevice()
313{
314 // Permanent non-default states
315 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
316 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
317
318 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
319 {
320 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
321 }
322 else
323 {
324 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
325 }
326
327 mSceneStarted = false;
328}
329
330D3DPRESENT_PARAMETERS Renderer::getDefaultPresentParameters()
331{
332 D3DPRESENT_PARAMETERS presentParameters = {0};
333
334 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
335 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
336 presentParameters.BackBufferCount = 1;
337 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
338 presentParameters.BackBufferWidth = 1;
339 presentParameters.BackBufferHeight = 1;
340 presentParameters.EnableAutoDepthStencil = FALSE;
341 presentParameters.Flags = 0;
342 presentParameters.hDeviceWindow = mDeviceWindow;
343 presentParameters.MultiSampleQuality = 0;
344 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
345 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
346 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
347 presentParameters.Windowed = TRUE;
348
349 return presentParameters;
350}
351
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000352int Renderer::generateConfigs(ConfigDesc **configDescList)
353{
354 D3DDISPLAYMODE currentDisplayMode;
355 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
356
357 int numRenderFormats = sizeof(mRenderTargetFormats) / sizeof(mRenderTargetFormats[0]);
358 int numDepthFormats = sizeof(mDepthStencilFormats) / sizeof(mDepthStencilFormats[0]);
359 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
360 int numConfigs = 0;
361
362 for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
363 {
364 D3DFORMAT renderTargetFormat = mRenderTargetFormats[formatIndex];
365
366 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
367
368 if (SUCCEEDED(result))
369 {
370 for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
371 {
372 D3DFORMAT depthStencilFormat = mDepthStencilFormats[depthStencilIndex];
373 HRESULT result = D3D_OK;
374
375 if(depthStencilFormat != D3DFMT_UNKNOWN)
376 {
377 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
378 }
379
380 if (SUCCEEDED(result))
381 {
382 if(depthStencilFormat != D3DFMT_UNKNOWN)
383 {
384 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
385 }
386
387 if (SUCCEEDED(result))
388 {
389 ConfigDesc newConfig;
390 newConfig.renderTargetFormat = dx2es::ConvertBackBufferFormat(renderTargetFormat);
391 newConfig.depthStencilFormat = dx2es::ConvertDepthStencilFormat(depthStencilFormat);
392 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
393 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
394
395 (*configDescList)[numConfigs++] = newConfig;
396 }
397 }
398 }
399 }
400 }
401
402 return numConfigs;
403}
404
405void Renderer::deleteConfigs(ConfigDesc *configDescList)
406{
407 delete [] (configDescList);
408}
409
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000410void Renderer::startScene()
411{
412 if (!mSceneStarted)
413 {
414 long result = mDevice->BeginScene();
415 if (SUCCEEDED(result)) {
416 // This is defensive checking against the device being
417 // lost at unexpected times.
418 mSceneStarted = true;
419 }
420 }
421}
422
423void Renderer::endScene()
424{
425 if (mSceneStarted)
426 {
427 // EndScene can fail if the device was lost, for example due
428 // to a TDR during a draw call.
429 mDevice->EndScene();
430 mSceneStarted = false;
431 }
432}
433
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000434// D3D9_REPLACE
435void Renderer::sync(bool block)
436{
437 HRESULT result;
438
439 IDirect3DQuery9* query = allocateEventQuery();
440 if (!query)
441 {
442 return;
443 }
444
445 result = query->Issue(D3DISSUE_END);
446 ASSERT(SUCCEEDED(result));
447
448 do
449 {
450 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
451
452 if(block && result == S_FALSE)
453 {
454 // Keep polling, but allow other threads to do something useful first
455 Sleep(0);
456 // explicitly check for device loss
457 // some drivers seem to return S_FALSE even if the device is lost
458 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000459 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000460 {
461 result = D3DERR_DEVICELOST;
462 }
463 }
464 }
465 while(block && result == S_FALSE);
466
467 freeEventQuery(query);
468
469 if (isDeviceLostError(result))
470 {
471 mDisplay->notifyDeviceLost();
472 }
473}
474
475// D3D9_REPLACE
476IDirect3DQuery9* Renderer::allocateEventQuery()
477{
478 IDirect3DQuery9 *query = NULL;
479
480 if (mEventQueryPool.empty())
481 {
482 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
483 ASSERT(SUCCEEDED(result));
484 }
485 else
486 {
487 query = mEventQueryPool.back();
488 mEventQueryPool.pop_back();
489 }
490
491 return query;
492}
493
494// D3D9_REPLACE
495void Renderer::freeEventQuery(IDirect3DQuery9* query)
496{
497 if (mEventQueryPool.size() > 1000)
498 {
499 query->Release();
500 }
501 else
502 {
503 mEventQueryPool.push_back(query);
504 }
505}
506
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000507IDirect3DVertexShader9 *Renderer::createVertexShader(const DWORD *function, size_t length)
508{
509 return mVertexShaderCache.create(function, length);
510}
511
512IDirect3DPixelShader9 *Renderer::createPixelShader(const DWORD *function, size_t length)
513{
514 return mPixelShaderCache.create(function, length);
515}
516
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000517
518void Renderer::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
519{
520 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
521 int d3dSampler = index + d3dSamplerOffset;
522
523 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(samplerState.wrapS));
524 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(samplerState.wrapT));
525
526 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
527 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
528 es2dx::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
529 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
530 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
531 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
532 if (mSupportsTextureFilterAnisotropy)
533 {
534 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
535 }
536}
537
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000538void Renderer::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
539{
540 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
541 int d3dSampler = index + d3dSamplerOffset;
542 IDirect3DBaseTexture9 *d3dTexture = NULL;
543
544 if (texture)
545 {
546 d3dTexture = texture->getD3DTexture();
547 // If we get NULL back from getTexture here, something went wrong
548 // in the texture class and we're unexpectedly missing the d3d texture
549 ASSERT(d3dTexture != NULL);
550 }
551
552 mDevice->SetTexture(d3dSampler, d3dTexture);
553}
554
555
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000556void Renderer::releaseDeviceResources()
557{
558 while (!mEventQueryPool.empty())
559 {
560 mEventQueryPool.back()->Release();
561 mEventQueryPool.pop_back();
562 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000563
564 mVertexShaderCache.clear();
565 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000566}
567
568
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000569void Renderer::markDeviceLost()
570{
571 mDeviceLost = true;
572}
573
574bool Renderer::isDeviceLost()
575{
576 return mDeviceLost;
577}
578
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000579// set notify to true to broadcast a message to all contexts of the device loss
580bool Renderer::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000581{
582 bool isLost = false;
583
584 if (mDeviceEx)
585 {
586 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
587 }
588 else if (mDevice)
589 {
590 isLost = FAILED(mDevice->TestCooperativeLevel());
591 }
592 else
593 {
594 // No device yet, so no reset required
595 }
596
597 if (isLost)
598 {
599 // ensure we note the device loss --
600 // we'll probably get this done again by markDeviceLost
601 // but best to remember it!
602 // Note that we don't want to clear the device loss status here
603 // -- this needs to be done by resetDevice
604 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000605 if (notify)
606 {
607 mDisplay->notifyDeviceLost();
608 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000609 }
610
611 return isLost;
612}
613
614bool Renderer::testDeviceResettable()
615{
616 HRESULT status = D3D_OK;
617
618 if (mDeviceEx)
619 {
620 status = mDeviceEx->CheckDeviceState(NULL);
621 }
622 else if (mDevice)
623 {
624 status = mDevice->TestCooperativeLevel();
625 }
626
627 switch (status)
628 {
629 case D3DERR_DEVICENOTRESET:
630 case D3DERR_DEVICEHUNG:
631 return true;
632 default:
633 return false;
634 }
635}
636
637bool Renderer::resetDevice()
638{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000639 releaseDeviceResources();
640
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000641 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
642
643 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000644 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000645 int attempts = 3;
646
647 while (lost && attempts > 0)
648 {
649 if (mDeviceEx)
650 {
651 Sleep(500); // Give the graphics driver some CPU time
652 result = mDeviceEx->ResetEx(&presentParameters, NULL);
653 }
654 else
655 {
656 result = mDevice->TestCooperativeLevel();
657 while (result == D3DERR_DEVICELOST)
658 {
659 Sleep(100); // Give the graphics driver some CPU time
660 result = mDevice->TestCooperativeLevel();
661 }
662
663 if (result == D3DERR_DEVICENOTRESET)
664 {
665 result = mDevice->Reset(&presentParameters);
666 }
667 }
668
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000669 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000670 attempts --;
671 }
672
673 if (FAILED(result))
674 {
675 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
676 return false;
677 }
678
679 // reset device defaults
680 initializeDevice();
681 mDeviceLost = false;
682
683 return true;
684}
685
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +0000686DWORD Renderer::getAdapterVendor() const
687{
688 return mAdapterIdentifier.VendorId;
689}
690
691const char *Renderer::getAdapterDescription() const
692{
693 return mAdapterIdentifier.Description;
694}
695
696GUID Renderer::getAdapterIdentifier() const
697{
698 return mAdapterIdentifier.DeviceIdentifier;
699}
700
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000701void Renderer::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
702{
703 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
704 {
705 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
706 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
707
708 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
709 }
710}
711
712bool Renderer::getDXT1TextureSupport()
713{
714 D3DDISPLAYMODE currentDisplayMode;
715 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
716
717 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
718}
719
720bool Renderer::getDXT3TextureSupport()
721{
722 D3DDISPLAYMODE currentDisplayMode;
723 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
724
725 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
726}
727
728bool Renderer::getDXT5TextureSupport()
729{
730 D3DDISPLAYMODE currentDisplayMode;
731 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
732
733 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
734}
735
736// we use INTZ for depth textures in Direct3D9
737// we also want NULL texture support to ensure the we can make depth-only FBOs
738// see http://aras-p.info/texts/D3D9GPUHacks.html
739bool Renderer::getDepthTextureSupport() const
740{
741 D3DDISPLAYMODE currentDisplayMode;
742 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
743
744 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
745 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
746 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
747 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
748
749 return intz && null;
750}
751
752bool Renderer::getFloat32TextureSupport(bool *filtering, bool *renderable)
753{
754 D3DDISPLAYMODE currentDisplayMode;
755 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
756
757 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
758 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
759 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
760 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
761
762 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
763 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
764 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
765 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
766
767 if (!*filtering && !*renderable)
768 {
769 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
770 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
771 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
772 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
773 }
774 else
775 {
776 return true;
777 }
778}
779
780bool Renderer::getFloat16TextureSupport(bool *filtering, bool *renderable)
781{
782 D3DDISPLAYMODE currentDisplayMode;
783 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
784
785 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
786 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
787 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
788 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
789
790 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
791 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
792 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
793 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
794
795 if (!*filtering && !*renderable)
796 {
797 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
798 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
799 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
800 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
801 }
802 else
803 {
804 return true;
805 }
806}
807
808bool Renderer::getLuminanceTextureSupport()
809{
810 D3DDISPLAYMODE currentDisplayMode;
811 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
812
813 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
814}
815
816bool Renderer::getLuminanceAlphaTextureSupport()
817{
818 D3DDISPLAYMODE currentDisplayMode;
819 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
820
821 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
822}
823
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000824bool Renderer::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000825{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000826 return mSupportsTextureFilterAnisotropy;
827}
828
829float Renderer::getTextureMaxAnisotropy() const
830{
831 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000832 {
833 return mDeviceCaps.MaxAnisotropy;
834 }
835 return 1.0f;
836}
837
838bool Renderer::getEventQuerySupport()
839{
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000840 IDirect3DQuery9 *query = allocateEventQuery();
841 if (query)
842 {
843 freeEventQuery(query);
844 return true;
845 }
846 else
847 {
848 return false;
849 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000850 return true;
851}
852
853// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
854// We test this using D3D9 by checking support for the R16F format.
855bool Renderer::getVertexTextureSupport() const
856{
857 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
858 {
859 return false;
860 }
861
862 D3DDISPLAYMODE currentDisplayMode;
863 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
864
865 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
866
867 return SUCCEEDED(result);
868}
869
870bool Renderer::getNonPower2TextureSupport() const
871{
872 return mSupportsNonPower2Textures;
873}
874
875bool Renderer::getOcclusionQuerySupport() const
876{
877 if (!mDevice)
878 {
879 return false;
880 }
881
882 IDirect3DQuery9 *query = NULL;
883 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
884 if (SUCCEEDED(result) && query)
885 {
886 query->Release();
887 return true;
888 }
889 else
890 {
891 return false;
892 }
893}
894
895bool Renderer::getInstancingSupport() const
896{
897 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
898}
899
daniel@transgaming.com313e3922012-10-31 17:52:39 +0000900bool Renderer::getShareHandleSupport() const
901{
902 // PIX doesn't seem to support using share handles, so disable them.
903 // D3D9_REPLACE
904 return isD3d9ExDevice() && !gl::perfActive();
905}
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000906
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000907bool Renderer::getShaderModel3Support() const
908{
909 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
910}
911
912float Renderer::getMaxPointSize() const
913{
914 return mDeviceCaps.MaxPointSize;
915}
916
917int Renderer::getMaxTextureWidth() const
918{
919 return (int)mDeviceCaps.MaxTextureWidth;
920}
921
922int Renderer::getMaxTextureHeight() const
923{
924 return (int)mDeviceCaps.MaxTextureHeight;
925}
926
927bool Renderer::get32BitIndexSupport() const
928{
929 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
930}
931
932DWORD Renderer::getCapsDeclTypes() const
933{
934 return mDeviceCaps.DeclTypes;
935}
936
937int Renderer::getMinSwapInterval() const
938{
939 return mMinSwapInterval;
940}
941
942int Renderer::getMaxSwapInterval() const
943{
944 return mMaxSwapInterval;
945}
946
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000947int Renderer::getMaxSupportedSamples() const
948{
949 return mMaxSupportedSamples;
950}
951
952int Renderer::getNearestSupportedSamples(D3DFORMAT format, int requested) const
953{
954 if (requested == 0)
955 {
956 return requested;
957 }
958
959 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
960 if (itr == mMultiSampleSupport.end())
961 {
daniel@transgaming.com92955622012-10-31 18:38:41 +0000962 if (format == D3DFMT_UNKNOWN)
963 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000964 return -1;
965 }
966
967 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
968 {
969 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
970 {
971 return i;
972 }
973 }
974
975 return -1;
976}
977
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000978D3DPOOL Renderer::getBufferPool(DWORD usage) const
979{
980 if (mD3d9Ex != NULL)
981 {
982 return D3DPOOL_DEFAULT;
983 }
984 else
985 {
986 if (!(usage & D3DUSAGE_DYNAMIC))
987 {
988 return D3DPOOL_MANAGED;
989 }
990 }
991
992 return D3DPOOL_DEFAULT;
993}
994
995D3DPOOL Renderer::getTexturePool(DWORD usage) const
996{
997 if (mD3d9Ex != NULL)
998 {
999 return D3DPOOL_DEFAULT;
1000 }
1001 else
1002 {
1003 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
1004 {
1005 return D3DPOOL_MANAGED;
1006 }
1007 }
1008
1009 return D3DPOOL_DEFAULT;
1010}
1011
1012}