daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1 | // |
| 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.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 14 | #include "libEGL/Display.h" |
| 15 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 16 | // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 17 | #define REF_RAST 0 |
| 18 | |
| 19 | // The "Debug This Pixel..." feature in PIX often fails when using the |
| 20 | // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 |
| 21 | // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. |
| 22 | #if !defined(ANGLE_ENABLE_D3D9EX) |
| 23 | // Enables use of the IDirect3D9Ex interface, when available |
| 24 | #define ANGLE_ENABLE_D3D9EX 1 |
| 25 | #endif // !defined(ANGLE_ENABLE_D3D9EX) |
| 26 | |
| 27 | namespace renderer |
| 28 | { |
| 29 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 30 | Renderer::Renderer(egl::Display *display, HMODULE hModule, HDC hDc): mDc(hDc) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 31 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 32 | mDisplay = display; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 33 | mD3d9Module = hModule; |
| 34 | |
| 35 | mD3d9 = NULL; |
| 36 | mD3d9Ex = NULL; |
| 37 | mDevice = NULL; |
| 38 | mDeviceEx = NULL; |
| 39 | mDeviceWindow = NULL; |
| 40 | |
| 41 | mAdapter = D3DADAPTER_DEFAULT; |
| 42 | |
| 43 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 44 | mDeviceType = D3DDEVTYPE_REF; |
| 45 | #else |
| 46 | mDeviceType = D3DDEVTYPE_HAL; |
| 47 | #endif |
| 48 | |
| 49 | mDeviceLost = false; |
| 50 | } |
| 51 | |
| 52 | Renderer::~Renderer() |
| 53 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 54 | releaseDeviceResources(); |
| 55 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 56 | if (mDevice) |
| 57 | { |
| 58 | // If the device is lost, reset it first to prevent leaving the driver in an unstable state |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 59 | if (testDeviceLost(false)) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 60 | { |
| 61 | resetDevice(); |
| 62 | } |
| 63 | |
| 64 | mDevice->Release(); |
| 65 | mDevice = NULL; |
| 66 | } |
| 67 | |
| 68 | if (mDeviceEx) |
| 69 | { |
| 70 | mDeviceEx->Release(); |
| 71 | mDeviceEx = NULL; |
| 72 | } |
| 73 | |
| 74 | if (mD3d9) |
| 75 | { |
| 76 | mD3d9->Release(); |
| 77 | mD3d9 = NULL; |
| 78 | } |
| 79 | |
| 80 | if (mDeviceWindow) |
| 81 | { |
| 82 | DestroyWindow(mDeviceWindow); |
| 83 | mDeviceWindow = NULL; |
| 84 | } |
| 85 | |
| 86 | if (mD3d9Ex) |
| 87 | { |
| 88 | mD3d9Ex->Release(); |
| 89 | mD3d9Ex = NULL; |
| 90 | } |
| 91 | |
| 92 | if (mD3d9Module) |
| 93 | { |
| 94 | mD3d9Module = NULL; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | EGLint Renderer::initialize() |
| 100 | { |
| 101 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 102 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 103 | |
| 104 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 105 | // inclined to report a lost context, for example when the user switches |
| 106 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
| 107 | if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
| 108 | { |
| 109 | ASSERT(mD3d9Ex); |
| 110 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
| 111 | ASSERT(mD3d9); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 116 | } |
| 117 | |
| 118 | if (!mD3d9) |
| 119 | { |
| 120 | ERR("Could not create D3D9 device - aborting!\n"); |
| 121 | return EGL_NOT_INITIALIZED; |
| 122 | } |
| 123 | if (mDc != NULL) |
| 124 | { |
| 125 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 126 | } |
| 127 | |
| 128 | HRESULT result; |
| 129 | |
| 130 | // Give up on getting device caps after about one second. |
| 131 | for (int i = 0; i < 10; ++i) |
| 132 | { |
| 133 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
| 134 | if (SUCCEEDED(result)) |
| 135 | { |
| 136 | break; |
| 137 | } |
| 138 | else if (result == D3DERR_NOTAVAILABLE) |
| 139 | { |
| 140 | Sleep(100); // Give the driver some time to initialize/recover |
| 141 | } |
| 142 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 143 | { |
| 144 | ERR("failed to get device caps (0x%x)\n", result); |
| 145 | return EGL_NOT_INITIALIZED; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 150 | { |
| 151 | ERR("Renderer does not support PS 2.0. aborting!\n"); |
| 152 | return EGL_NOT_INITIALIZED; |
| 153 | } |
| 154 | |
| 155 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 156 | // This is required by Texture2D::convertToRenderTarget. |
| 157 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 158 | { |
| 159 | ERR("Renderer does not support stretctrect from textures!\n"); |
| 160 | return EGL_NOT_INITIALIZED; |
| 161 | } |
| 162 | |
| 163 | mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); |
| 164 | |
| 165 | // ATI cards on XP have problems with non-power-of-two textures. |
| 166 | mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && |
| 167 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && |
| 168 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && |
| 169 | !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); |
| 170 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 171 | // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec |
| 172 | mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); |
| 173 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 174 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 175 | static const TCHAR className[] = TEXT("STATIC"); |
| 176 | |
| 177 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 178 | |
| 179 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 180 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 181 | |
| 182 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 183 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 184 | { |
| 185 | return EGL_BAD_ALLOC; |
| 186 | } |
| 187 | |
| 188 | if (FAILED(result)) |
| 189 | { |
| 190 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 191 | |
| 192 | if (FAILED(result)) |
| 193 | { |
| 194 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 195 | return EGL_BAD_ALLOC; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (mD3d9Ex) |
| 200 | { |
| 201 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 202 | ASSERT(SUCCEEDED(result)); |
| 203 | } |
| 204 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 205 | mVertexShaderCache.initialize(mDevice); |
| 206 | mPixelShaderCache.initialize(mDevice); |
| 207 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 208 | initializeDevice(); |
| 209 | |
| 210 | return EGL_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | // do any one-time device initialization |
| 214 | // NOTE: this is also needed after a device lost/reset |
| 215 | // to reset the scene status and ensure the default states are reset. |
| 216 | void Renderer::initializeDevice() |
| 217 | { |
| 218 | // Permanent non-default states |
| 219 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 220 | mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); |
| 221 | |
| 222 | if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) |
| 223 | { |
| 224 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f |
| 229 | } |
| 230 | |
| 231 | mSceneStarted = false; |
| 232 | } |
| 233 | |
| 234 | D3DPRESENT_PARAMETERS Renderer::getDefaultPresentParameters() |
| 235 | { |
| 236 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 237 | |
| 238 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 239 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 240 | presentParameters.BackBufferCount = 1; |
| 241 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 242 | presentParameters.BackBufferWidth = 1; |
| 243 | presentParameters.BackBufferHeight = 1; |
| 244 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 245 | presentParameters.Flags = 0; |
| 246 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 247 | presentParameters.MultiSampleQuality = 0; |
| 248 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 249 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 250 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 251 | presentParameters.Windowed = TRUE; |
| 252 | |
| 253 | return presentParameters; |
| 254 | } |
| 255 | |
| 256 | void Renderer::startScene() |
| 257 | { |
| 258 | if (!mSceneStarted) |
| 259 | { |
| 260 | long result = mDevice->BeginScene(); |
| 261 | if (SUCCEEDED(result)) { |
| 262 | // This is defensive checking against the device being |
| 263 | // lost at unexpected times. |
| 264 | mSceneStarted = true; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void Renderer::endScene() |
| 270 | { |
| 271 | if (mSceneStarted) |
| 272 | { |
| 273 | // EndScene can fail if the device was lost, for example due |
| 274 | // to a TDR during a draw call. |
| 275 | mDevice->EndScene(); |
| 276 | mSceneStarted = false; |
| 277 | } |
| 278 | } |
| 279 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 280 | // D3D9_REPLACE |
| 281 | void Renderer::sync(bool block) |
| 282 | { |
| 283 | HRESULT result; |
| 284 | |
| 285 | IDirect3DQuery9* query = allocateEventQuery(); |
| 286 | if (!query) |
| 287 | { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | result = query->Issue(D3DISSUE_END); |
| 292 | ASSERT(SUCCEEDED(result)); |
| 293 | |
| 294 | do |
| 295 | { |
| 296 | result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); |
| 297 | |
| 298 | if(block && result == S_FALSE) |
| 299 | { |
| 300 | // Keep polling, but allow other threads to do something useful first |
| 301 | Sleep(0); |
| 302 | // explicitly check for device loss |
| 303 | // some drivers seem to return S_FALSE even if the device is lost |
| 304 | // instead of D3DERR_DEVICELOST like they should |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 305 | if (testDeviceLost(false)) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 306 | { |
| 307 | result = D3DERR_DEVICELOST; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | while(block && result == S_FALSE); |
| 312 | |
| 313 | freeEventQuery(query); |
| 314 | |
| 315 | if (isDeviceLostError(result)) |
| 316 | { |
| 317 | mDisplay->notifyDeviceLost(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // D3D9_REPLACE |
| 322 | IDirect3DQuery9* Renderer::allocateEventQuery() |
| 323 | { |
| 324 | IDirect3DQuery9 *query = NULL; |
| 325 | |
| 326 | if (mEventQueryPool.empty()) |
| 327 | { |
| 328 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 329 | ASSERT(SUCCEEDED(result)); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | query = mEventQueryPool.back(); |
| 334 | mEventQueryPool.pop_back(); |
| 335 | } |
| 336 | |
| 337 | return query; |
| 338 | } |
| 339 | |
| 340 | // D3D9_REPLACE |
| 341 | void Renderer::freeEventQuery(IDirect3DQuery9* query) |
| 342 | { |
| 343 | if (mEventQueryPool.size() > 1000) |
| 344 | { |
| 345 | query->Release(); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | mEventQueryPool.push_back(query); |
| 350 | } |
| 351 | } |
| 352 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 353 | IDirect3DVertexShader9 *Renderer::createVertexShader(const DWORD *function, size_t length) |
| 354 | { |
| 355 | return mVertexShaderCache.create(function, length); |
| 356 | } |
| 357 | |
| 358 | IDirect3DPixelShader9 *Renderer::createPixelShader(const DWORD *function, size_t length) |
| 359 | { |
| 360 | return mPixelShaderCache.create(function, length); |
| 361 | } |
| 362 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 363 | |
| 364 | void Renderer::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) |
| 365 | { |
| 366 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 367 | int d3dSampler = index + d3dSamplerOffset; |
| 368 | |
| 369 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(samplerState.wrapS)); |
| 370 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(samplerState.wrapT)); |
| 371 | |
| 372 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy)); |
| 373 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
| 374 | es2dx::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
| 375 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 376 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 377 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 378 | if (mSupportsTextureFilterAnisotropy) |
| 379 | { |
| 380 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 381 | } |
| 382 | } |
| 383 | |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame^] | 384 | void Renderer::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
| 385 | { |
| 386 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 387 | int d3dSampler = index + d3dSamplerOffset; |
| 388 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
| 389 | |
| 390 | if (texture) |
| 391 | { |
| 392 | d3dTexture = texture->getD3DTexture(); |
| 393 | // If we get NULL back from getTexture here, something went wrong |
| 394 | // in the texture class and we're unexpectedly missing the d3d texture |
| 395 | ASSERT(d3dTexture != NULL); |
| 396 | } |
| 397 | |
| 398 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 399 | } |
| 400 | |
| 401 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 402 | void Renderer::releaseDeviceResources() |
| 403 | { |
| 404 | while (!mEventQueryPool.empty()) |
| 405 | { |
| 406 | mEventQueryPool.back()->Release(); |
| 407 | mEventQueryPool.pop_back(); |
| 408 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 409 | |
| 410 | mVertexShaderCache.clear(); |
| 411 | mPixelShaderCache.clear(); |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 415 | void Renderer::markDeviceLost() |
| 416 | { |
| 417 | mDeviceLost = true; |
| 418 | } |
| 419 | |
| 420 | bool Renderer::isDeviceLost() |
| 421 | { |
| 422 | return mDeviceLost; |
| 423 | } |
| 424 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 425 | // set notify to true to broadcast a message to all contexts of the device loss |
| 426 | bool Renderer::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 427 | { |
| 428 | bool isLost = false; |
| 429 | |
| 430 | if (mDeviceEx) |
| 431 | { |
| 432 | isLost = FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 433 | } |
| 434 | else if (mDevice) |
| 435 | { |
| 436 | isLost = FAILED(mDevice->TestCooperativeLevel()); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | // No device yet, so no reset required |
| 441 | } |
| 442 | |
| 443 | if (isLost) |
| 444 | { |
| 445 | // ensure we note the device loss -- |
| 446 | // we'll probably get this done again by markDeviceLost |
| 447 | // but best to remember it! |
| 448 | // Note that we don't want to clear the device loss status here |
| 449 | // -- this needs to be done by resetDevice |
| 450 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 451 | if (notify) |
| 452 | { |
| 453 | mDisplay->notifyDeviceLost(); |
| 454 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return isLost; |
| 458 | } |
| 459 | |
| 460 | bool Renderer::testDeviceResettable() |
| 461 | { |
| 462 | HRESULT status = D3D_OK; |
| 463 | |
| 464 | if (mDeviceEx) |
| 465 | { |
| 466 | status = mDeviceEx->CheckDeviceState(NULL); |
| 467 | } |
| 468 | else if (mDevice) |
| 469 | { |
| 470 | status = mDevice->TestCooperativeLevel(); |
| 471 | } |
| 472 | |
| 473 | switch (status) |
| 474 | { |
| 475 | case D3DERR_DEVICENOTRESET: |
| 476 | case D3DERR_DEVICEHUNG: |
| 477 | return true; |
| 478 | default: |
| 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | bool Renderer::resetDevice() |
| 484 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 485 | releaseDeviceResources(); |
| 486 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 487 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 488 | |
| 489 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 490 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 491 | int attempts = 3; |
| 492 | |
| 493 | while (lost && attempts > 0) |
| 494 | { |
| 495 | if (mDeviceEx) |
| 496 | { |
| 497 | Sleep(500); // Give the graphics driver some CPU time |
| 498 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | result = mDevice->TestCooperativeLevel(); |
| 503 | while (result == D3DERR_DEVICELOST) |
| 504 | { |
| 505 | Sleep(100); // Give the graphics driver some CPU time |
| 506 | result = mDevice->TestCooperativeLevel(); |
| 507 | } |
| 508 | |
| 509 | if (result == D3DERR_DEVICENOTRESET) |
| 510 | { |
| 511 | result = mDevice->Reset(&presentParameters); |
| 512 | } |
| 513 | } |
| 514 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 515 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 516 | attempts --; |
| 517 | } |
| 518 | |
| 519 | if (FAILED(result)) |
| 520 | { |
| 521 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | // reset device defaults |
| 526 | initializeDevice(); |
| 527 | mDeviceLost = false; |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | void Renderer::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
| 533 | { |
| 534 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 535 | { |
| 536 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 537 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 538 | |
| 539 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | bool Renderer::getDXT1TextureSupport() |
| 544 | { |
| 545 | D3DDISPLAYMODE currentDisplayMode; |
| 546 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 547 | |
| 548 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 549 | } |
| 550 | |
| 551 | bool Renderer::getDXT3TextureSupport() |
| 552 | { |
| 553 | D3DDISPLAYMODE currentDisplayMode; |
| 554 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 555 | |
| 556 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 557 | } |
| 558 | |
| 559 | bool Renderer::getDXT5TextureSupport() |
| 560 | { |
| 561 | D3DDISPLAYMODE currentDisplayMode; |
| 562 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 563 | |
| 564 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 565 | } |
| 566 | |
| 567 | // we use INTZ for depth textures in Direct3D9 |
| 568 | // we also want NULL texture support to ensure the we can make depth-only FBOs |
| 569 | // see http://aras-p.info/texts/D3D9GPUHacks.html |
| 570 | bool Renderer::getDepthTextureSupport() const |
| 571 | { |
| 572 | D3DDISPLAYMODE currentDisplayMode; |
| 573 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 574 | |
| 575 | bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 576 | D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)); |
| 577 | bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 578 | D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); |
| 579 | |
| 580 | return intz && null; |
| 581 | } |
| 582 | |
| 583 | bool Renderer::getFloat32TextureSupport(bool *filtering, bool *renderable) |
| 584 | { |
| 585 | D3DDISPLAYMODE currentDisplayMode; |
| 586 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 587 | |
| 588 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 589 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 590 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 591 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 592 | |
| 593 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 594 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 595 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 596 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 597 | |
| 598 | if (!*filtering && !*renderable) |
| 599 | { |
| 600 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 601 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 602 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 603 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | return true; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | bool Renderer::getFloat16TextureSupport(bool *filtering, bool *renderable) |
| 612 | { |
| 613 | D3DDISPLAYMODE currentDisplayMode; |
| 614 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 615 | |
| 616 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 617 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 618 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 619 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 620 | |
| 621 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 622 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 623 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 624 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 625 | |
| 626 | if (!*filtering && !*renderable) |
| 627 | { |
| 628 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 629 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 630 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 631 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | return true; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | bool Renderer::getLuminanceTextureSupport() |
| 640 | { |
| 641 | D3DDISPLAYMODE currentDisplayMode; |
| 642 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 643 | |
| 644 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 645 | } |
| 646 | |
| 647 | bool Renderer::getLuminanceAlphaTextureSupport() |
| 648 | { |
| 649 | D3DDISPLAYMODE currentDisplayMode; |
| 650 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 651 | |
| 652 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 653 | } |
| 654 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 655 | bool Renderer::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 656 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 657 | return mSupportsTextureFilterAnisotropy; |
| 658 | } |
| 659 | |
| 660 | float Renderer::getTextureMaxAnisotropy() const |
| 661 | { |
| 662 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 663 | { |
| 664 | return mDeviceCaps.MaxAnisotropy; |
| 665 | } |
| 666 | return 1.0f; |
| 667 | } |
| 668 | |
| 669 | bool Renderer::getEventQuerySupport() |
| 670 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 671 | IDirect3DQuery9 *query = allocateEventQuery(); |
| 672 | if (query) |
| 673 | { |
| 674 | freeEventQuery(query); |
| 675 | return true; |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | return false; |
| 680 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 681 | return true; |
| 682 | } |
| 683 | |
| 684 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 685 | // We test this using D3D9 by checking support for the R16F format. |
| 686 | bool Renderer::getVertexTextureSupport() const |
| 687 | { |
| 688 | if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 689 | { |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | D3DDISPLAYMODE currentDisplayMode; |
| 694 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 695 | |
| 696 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 697 | |
| 698 | return SUCCEEDED(result); |
| 699 | } |
| 700 | |
| 701 | bool Renderer::getNonPower2TextureSupport() const |
| 702 | { |
| 703 | return mSupportsNonPower2Textures; |
| 704 | } |
| 705 | |
| 706 | bool Renderer::getOcclusionQuerySupport() const |
| 707 | { |
| 708 | if (!mDevice) |
| 709 | { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | IDirect3DQuery9 *query = NULL; |
| 714 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query); |
| 715 | if (SUCCEEDED(result) && query) |
| 716 | { |
| 717 | query->Release(); |
| 718 | return true; |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | return false; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | bool Renderer::getInstancingSupport() const |
| 727 | { |
| 728 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 729 | } |
| 730 | |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 731 | bool Renderer::getShareHandleSupport() const |
| 732 | { |
| 733 | // PIX doesn't seem to support using share handles, so disable them. |
| 734 | // D3D9_REPLACE |
| 735 | return isD3d9ExDevice() && !gl::perfActive(); |
| 736 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 737 | |
| 738 | D3DPOOL Renderer::getBufferPool(DWORD usage) const |
| 739 | { |
| 740 | if (mD3d9Ex != NULL) |
| 741 | { |
| 742 | return D3DPOOL_DEFAULT; |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 747 | { |
| 748 | return D3DPOOL_MANAGED; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | return D3DPOOL_DEFAULT; |
| 753 | } |
| 754 | |
| 755 | D3DPOOL Renderer::getTexturePool(DWORD usage) const |
| 756 | { |
| 757 | if (mD3d9Ex != NULL) |
| 758 | { |
| 759 | return D3DPOOL_DEFAULT; |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 764 | { |
| 765 | return D3DPOOL_MANAGED; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return D3DPOOL_DEFAULT; |
| 770 | } |
| 771 | |
| 772 | } |