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 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 7 | // Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer. |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 8 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 9 | #include "common/debug.h" |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 10 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 11 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 12 | #include "libGLESv2/mathutil.h" |
| 13 | #include "libGLESv2/Framebuffer.h" |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 14 | #include "libGLESv2/renderer/Renderer9.h" |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame] | 15 | #include "libGLESv2/renderer/renderer9_utils.h" |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 16 | #include "libGLESv2/renderer/TextureStorage.h" |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/Image.h" |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 18 | #include "libGLESv2/renderer/Blit.h" |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 19 | |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 20 | #include "libEGL/Config.h" |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 21 | #include "libEGL/Display.h" |
| 22 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 23 | // 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) |
| 33 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 34 | namespace rx |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 35 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 36 | static const D3DFORMAT RenderTargetFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 37 | { |
| 38 | D3DFMT_A1R5G5B5, |
| 39 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 40 | D3DFMT_A8R8G8B8, |
| 41 | D3DFMT_R5G6B5, |
| 42 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
| 43 | D3DFMT_X8R8G8B8 |
| 44 | }; |
| 45 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 46 | static const D3DFORMAT DepthStencilFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 47 | { |
| 48 | D3DFMT_UNKNOWN, |
| 49 | // D3DFMT_D16_LOCKABLE, |
| 50 | D3DFMT_D32, |
| 51 | // D3DFMT_D15S1, |
| 52 | D3DFMT_D24S8, |
| 53 | D3DFMT_D24X8, |
| 54 | // D3DFMT_D24X4S4, |
| 55 | D3DFMT_D16, |
| 56 | // D3DFMT_D32F_LOCKABLE, |
| 57 | // D3DFMT_D24FS8 |
| 58 | }; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 59 | |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 60 | Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 61 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 62 | mD3d9Module = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 63 | |
| 64 | mD3d9 = NULL; |
| 65 | mD3d9Ex = NULL; |
| 66 | mDevice = NULL; |
| 67 | mDeviceEx = NULL; |
| 68 | mDeviceWindow = NULL; |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 69 | mBlit = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 70 | |
| 71 | mAdapter = D3DADAPTER_DEFAULT; |
| 72 | |
| 73 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 74 | mDeviceType = D3DDEVTYPE_REF; |
| 75 | #else |
| 76 | mDeviceType = D3DDEVTYPE_HAL; |
| 77 | #endif |
| 78 | |
| 79 | mDeviceLost = false; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 80 | |
| 81 | mMaxSupportedSamples = 0; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 84 | Renderer9::~Renderer9() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 85 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 86 | releaseDeviceResources(); |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 87 | |
| 88 | delete mBlit; |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 89 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 90 | if (mDevice) |
| 91 | { |
| 92 | // 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] | 93 | if (testDeviceLost(false)) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 94 | { |
| 95 | resetDevice(); |
| 96 | } |
| 97 | |
| 98 | mDevice->Release(); |
| 99 | mDevice = NULL; |
| 100 | } |
| 101 | |
| 102 | if (mDeviceEx) |
| 103 | { |
| 104 | mDeviceEx->Release(); |
| 105 | mDeviceEx = NULL; |
| 106 | } |
| 107 | |
| 108 | if (mD3d9) |
| 109 | { |
| 110 | mD3d9->Release(); |
| 111 | mD3d9 = NULL; |
| 112 | } |
| 113 | |
| 114 | if (mDeviceWindow) |
| 115 | { |
| 116 | DestroyWindow(mDeviceWindow); |
| 117 | mDeviceWindow = NULL; |
| 118 | } |
| 119 | |
| 120 | if (mD3d9Ex) |
| 121 | { |
| 122 | mD3d9Ex->Release(); |
| 123 | mD3d9Ex = NULL; |
| 124 | } |
| 125 | |
| 126 | if (mD3d9Module) |
| 127 | { |
| 128 | mD3d9Module = NULL; |
| 129 | } |
| 130 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 131 | while (!mMultiSampleSupport.empty()) |
| 132 | { |
| 133 | delete [] mMultiSampleSupport.begin()->second; |
| 134 | mMultiSampleSupport.erase(mMultiSampleSupport.begin()); |
| 135 | } |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 136 | |
| 137 | mForceSetDepthStencilState = true; |
| 138 | mForceSetRasterState = true; |
| 139 | mForceSetBlendState = true; |
| 140 | mForceSetScissor = true; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 141 | } |
| 142 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 143 | EGLint Renderer9::initialize() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 144 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 145 | if (mSoftwareDevice) |
| 146 | { |
| 147 | mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); |
| 152 | } |
| 153 | |
| 154 | if (mD3d9Module == NULL) |
| 155 | { |
| 156 | ERR("No D3D9 module found - aborting!\n"); |
| 157 | return EGL_NOT_INITIALIZED; |
| 158 | } |
| 159 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 160 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 161 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 162 | |
| 163 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 164 | // inclined to report a lost context, for example when the user switches |
| 165 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
| 166 | if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
| 167 | { |
| 168 | ASSERT(mD3d9Ex); |
| 169 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
| 170 | ASSERT(mD3d9); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 175 | } |
| 176 | |
| 177 | if (!mD3d9) |
| 178 | { |
| 179 | ERR("Could not create D3D9 device - aborting!\n"); |
| 180 | return EGL_NOT_INITIALIZED; |
| 181 | } |
| 182 | if (mDc != NULL) |
| 183 | { |
| 184 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 185 | } |
| 186 | |
| 187 | HRESULT result; |
| 188 | |
| 189 | // Give up on getting device caps after about one second. |
| 190 | for (int i = 0; i < 10; ++i) |
| 191 | { |
| 192 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
| 193 | if (SUCCEEDED(result)) |
| 194 | { |
| 195 | break; |
| 196 | } |
| 197 | else if (result == D3DERR_NOTAVAILABLE) |
| 198 | { |
| 199 | Sleep(100); // Give the driver some time to initialize/recover |
| 200 | } |
| 201 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 202 | { |
| 203 | ERR("failed to get device caps (0x%x)\n", result); |
| 204 | return EGL_NOT_INITIALIZED; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 209 | { |
| 210 | ERR("Renderer does not support PS 2.0. aborting!\n"); |
| 211 | return EGL_NOT_INITIALIZED; |
| 212 | } |
| 213 | |
| 214 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 215 | // This is required by Texture2D::convertToRenderTarget. |
| 216 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 217 | { |
| 218 | ERR("Renderer does not support stretctrect from textures!\n"); |
| 219 | return EGL_NOT_INITIALIZED; |
| 220 | } |
| 221 | |
| 222 | mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); |
| 223 | |
| 224 | // ATI cards on XP have problems with non-power-of-two textures. |
| 225 | mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && |
| 226 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && |
| 227 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && |
| 228 | !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); |
| 229 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 230 | // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec |
| 231 | mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); |
| 232 | |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 233 | mMinSwapInterval = 4; |
| 234 | mMaxSwapInterval = 0; |
| 235 | |
| 236 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) |
| 237 | { |
| 238 | mMinSwapInterval = std::min(mMinSwapInterval, 0); |
| 239 | mMaxSwapInterval = std::max(mMaxSwapInterval, 0); |
| 240 | } |
| 241 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) |
| 242 | { |
| 243 | mMinSwapInterval = std::min(mMinSwapInterval, 1); |
| 244 | mMaxSwapInterval = std::max(mMaxSwapInterval, 1); |
| 245 | } |
| 246 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) |
| 247 | { |
| 248 | mMinSwapInterval = std::min(mMinSwapInterval, 2); |
| 249 | mMaxSwapInterval = std::max(mMaxSwapInterval, 2); |
| 250 | } |
| 251 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) |
| 252 | { |
| 253 | mMinSwapInterval = std::min(mMinSwapInterval, 3); |
| 254 | mMaxSwapInterval = std::max(mMaxSwapInterval, 3); |
| 255 | } |
| 256 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) |
| 257 | { |
| 258 | mMinSwapInterval = std::min(mMinSwapInterval, 4); |
| 259 | mMaxSwapInterval = std::max(mMaxSwapInterval, 4); |
| 260 | } |
| 261 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 262 | int max = 0; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 263 | for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 264 | { |
| 265 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 266 | getMultiSampleSupport(RenderTargetFormats[i], multisampleArray); |
| 267 | mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray; |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 268 | |
| 269 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 270 | { |
| 271 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 272 | { |
| 273 | max = j; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 278 | for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 279 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 280 | if (DepthStencilFormats[i] == D3DFMT_UNKNOWN) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 281 | continue; |
| 282 | |
| 283 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 284 | getMultiSampleSupport(DepthStencilFormats[i], multisampleArray); |
| 285 | mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 286 | |
| 287 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 288 | { |
| 289 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 290 | { |
| 291 | max = j; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | mMaxSupportedSamples = max; |
| 297 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 298 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 299 | static const TCHAR className[] = TEXT("STATIC"); |
| 300 | |
| 301 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 302 | |
| 303 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 304 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 305 | |
| 306 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 307 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 308 | { |
| 309 | return EGL_BAD_ALLOC; |
| 310 | } |
| 311 | |
| 312 | if (FAILED(result)) |
| 313 | { |
| 314 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 315 | |
| 316 | if (FAILED(result)) |
| 317 | { |
| 318 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 319 | return EGL_BAD_ALLOC; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (mD3d9Ex) |
| 324 | { |
| 325 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 326 | ASSERT(SUCCEEDED(result)); |
| 327 | } |
| 328 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 329 | mVertexShaderCache.initialize(mDevice); |
| 330 | mPixelShaderCache.initialize(mDevice); |
| 331 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 332 | initializeDevice(); |
| 333 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 334 | mBlit = new Blit(this); |
| 335 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 336 | return EGL_SUCCESS; |
| 337 | } |
| 338 | |
| 339 | // do any one-time device initialization |
| 340 | // NOTE: this is also needed after a device lost/reset |
| 341 | // to reset the scene status and ensure the default states are reset. |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 342 | void Renderer9::initializeDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 343 | { |
| 344 | // Permanent non-default states |
| 345 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 346 | mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); |
| 347 | |
| 348 | if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) |
| 349 | { |
| 350 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f |
| 355 | } |
| 356 | |
| 357 | mSceneStarted = false; |
| 358 | } |
| 359 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 360 | D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 361 | { |
| 362 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 363 | |
| 364 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 365 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 366 | presentParameters.BackBufferCount = 1; |
| 367 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 368 | presentParameters.BackBufferWidth = 1; |
| 369 | presentParameters.BackBufferHeight = 1; |
| 370 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 371 | presentParameters.Flags = 0; |
| 372 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 373 | presentParameters.MultiSampleQuality = 0; |
| 374 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 375 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 376 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 377 | presentParameters.Windowed = TRUE; |
| 378 | |
| 379 | return presentParameters; |
| 380 | } |
| 381 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 382 | int Renderer9::generateConfigs(ConfigDesc **configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 383 | { |
| 384 | D3DDISPLAYMODE currentDisplayMode; |
| 385 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 386 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 387 | int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]); |
| 388 | int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 389 | (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; |
| 390 | int numConfigs = 0; |
| 391 | |
| 392 | for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) |
| 393 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 394 | D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 395 | |
| 396 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 397 | |
| 398 | if (SUCCEEDED(result)) |
| 399 | { |
| 400 | for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) |
| 401 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 402 | D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 403 | HRESULT result = D3D_OK; |
| 404 | |
| 405 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 406 | { |
| 407 | result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 408 | } |
| 409 | |
| 410 | if (SUCCEEDED(result)) |
| 411 | { |
| 412 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 413 | { |
| 414 | result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
| 415 | } |
| 416 | |
| 417 | if (SUCCEEDED(result)) |
| 418 | { |
| 419 | ConfigDesc newConfig; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 420 | newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat); |
| 421 | newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 422 | newConfig.multiSample = 0; // FIXME: enumerate multi-sampling |
| 423 | newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat); |
| 424 | |
| 425 | (*configDescList)[numConfigs++] = newConfig; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return numConfigs; |
| 433 | } |
| 434 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 435 | void Renderer9::deleteConfigs(ConfigDesc *configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 436 | { |
| 437 | delete [] (configDescList); |
| 438 | } |
| 439 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 440 | void Renderer9::startScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 441 | { |
| 442 | if (!mSceneStarted) |
| 443 | { |
| 444 | long result = mDevice->BeginScene(); |
| 445 | if (SUCCEEDED(result)) { |
| 446 | // This is defensive checking against the device being |
| 447 | // lost at unexpected times. |
| 448 | mSceneStarted = true; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 453 | void Renderer9::endScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 454 | { |
| 455 | if (mSceneStarted) |
| 456 | { |
| 457 | // EndScene can fail if the device was lost, for example due |
| 458 | // to a TDR during a draw call. |
| 459 | mDevice->EndScene(); |
| 460 | mSceneStarted = false; |
| 461 | } |
| 462 | } |
| 463 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 464 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 465 | void Renderer9::sync(bool block) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 466 | { |
| 467 | HRESULT result; |
| 468 | |
| 469 | IDirect3DQuery9* query = allocateEventQuery(); |
| 470 | if (!query) |
| 471 | { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | result = query->Issue(D3DISSUE_END); |
| 476 | ASSERT(SUCCEEDED(result)); |
| 477 | |
| 478 | do |
| 479 | { |
| 480 | result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); |
| 481 | |
| 482 | if(block && result == S_FALSE) |
| 483 | { |
| 484 | // Keep polling, but allow other threads to do something useful first |
| 485 | Sleep(0); |
| 486 | // explicitly check for device loss |
| 487 | // some drivers seem to return S_FALSE even if the device is lost |
| 488 | // instead of D3DERR_DEVICELOST like they should |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 489 | if (testDeviceLost(false)) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 490 | { |
| 491 | result = D3DERR_DEVICELOST; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | while(block && result == S_FALSE); |
| 496 | |
| 497 | freeEventQuery(query); |
| 498 | |
| 499 | if (isDeviceLostError(result)) |
| 500 | { |
| 501 | mDisplay->notifyDeviceLost(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 506 | IDirect3DQuery9* Renderer9::allocateEventQuery() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 507 | { |
| 508 | IDirect3DQuery9 *query = NULL; |
| 509 | |
| 510 | if (mEventQueryPool.empty()) |
| 511 | { |
| 512 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 513 | ASSERT(SUCCEEDED(result)); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | query = mEventQueryPool.back(); |
| 518 | mEventQueryPool.pop_back(); |
| 519 | } |
| 520 | |
| 521 | return query; |
| 522 | } |
| 523 | |
| 524 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 525 | void Renderer9::freeEventQuery(IDirect3DQuery9* query) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 526 | { |
| 527 | if (mEventQueryPool.size() > 1000) |
| 528 | { |
| 529 | query->Release(); |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | mEventQueryPool.push_back(query); |
| 534 | } |
| 535 | } |
| 536 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 537 | IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 538 | { |
| 539 | return mVertexShaderCache.create(function, length); |
| 540 | } |
| 541 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 542 | IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 543 | { |
| 544 | return mPixelShaderCache.create(function, length); |
| 545 | } |
| 546 | |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 547 | HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) |
| 548 | { |
| 549 | D3DPOOL Pool = getBufferPool(Usage); |
| 550 | return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); |
| 551 | } |
| 552 | |
| 553 | HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) |
| 554 | { |
| 555 | D3DPOOL Pool = getBufferPool(Usage); |
| 556 | return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); |
| 557 | } |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 558 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 559 | void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 560 | { |
| 561 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 562 | int d3dSampler = index + d3dSamplerOffset; |
| 563 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 564 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); |
| 565 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 566 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 567 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy)); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 568 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 569 | gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 570 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 571 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 572 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 573 | if (mSupportsTextureFilterAnisotropy) |
| 574 | { |
| 575 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 576 | } |
| 577 | } |
| 578 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 579 | void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 580 | { |
| 581 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 582 | int d3dSampler = index + d3dSamplerOffset; |
| 583 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
| 584 | |
| 585 | if (texture) |
| 586 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 587 | TextureStorage *texStorage = texture->getNativeTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 588 | if (texStorage) |
| 589 | { |
| 590 | d3dTexture = texStorage->getBaseTexture(); |
| 591 | } |
| 592 | // If we get NULL back from getBaseTexture here, something went wrong |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 593 | // in the texture class and we're unexpectedly missing the d3d texture |
| 594 | ASSERT(d3dTexture != NULL); |
| 595 | } |
| 596 | |
| 597 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 598 | } |
| 599 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 600 | void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize) |
| 601 | { |
| 602 | bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; |
| 603 | bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize; |
| 604 | |
| 605 | if (rasterStateChanged) |
| 606 | { |
| 607 | // Set the cull mode |
| 608 | if (rasterState.cullFace) |
| 609 | { |
| 610 | mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 615 | } |
| 616 | |
| 617 | mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE); |
| 618 | |
| 619 | mCurRasterState = rasterState; |
| 620 | } |
| 621 | |
| 622 | if (rasterStateChanged || depthSizeChanged) |
| 623 | { |
| 624 | if (rasterState.polygonOffsetFill) |
| 625 | { |
| 626 | if (depthSize > 0) |
| 627 | { |
| 628 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); |
| 629 | |
| 630 | float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 631 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); |
| 632 | } |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 637 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 638 | } |
| 639 | |
| 640 | mCurDepthSize = depthSize; |
| 641 | } |
| 642 | |
| 643 | mForceSetRasterState = false; |
| 644 | } |
| 645 | |
| 646 | void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) |
| 647 | { |
| 648 | bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; |
| 649 | bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; |
| 650 | bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; |
| 651 | |
| 652 | if (blendStateChanged || blendColorChanged) |
| 653 | { |
| 654 | if (blendState.blend) |
| 655 | { |
| 656 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 657 | |
| 658 | if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 659 | blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 660 | { |
| 661 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), |
| 666 | gl::unorm<8>(blendColor.alpha), |
| 667 | gl::unorm<8>(blendColor.alpha), |
| 668 | gl::unorm<8>(blendColor.alpha))); |
| 669 | } |
| 670 | |
| 671 | mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); |
| 672 | mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); |
| 673 | mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); |
| 674 | |
| 675 | if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || |
| 676 | blendState.destBlendRGB != blendState.destBlendAlpha || |
| 677 | blendState.blendEquationRGB != blendState.blendEquationAlpha) |
| 678 | { |
| 679 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 680 | |
| 681 | mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); |
| 682 | mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); |
| 683 | mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); |
| 684 | } |
| 685 | else |
| 686 | { |
| 687 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 688 | } |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 693 | } |
| 694 | |
| 695 | if (blendState.sampleAlphaToCoverage) |
| 696 | { |
| 697 | FIXME("Sample alpha to coverage is unimplemented."); |
| 698 | } |
| 699 | |
| 700 | // Set the color mask |
| 701 | bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; |
| 702 | // Apparently some ATI cards have a bug where a draw with a zero color |
| 703 | // write mask can cause later draws to have incorrect results. Instead, |
| 704 | // set a nonzero color write mask but modify the blend state so that no |
| 705 | // drawing is done. |
| 706 | // http://code.google.com/p/angleproject/issues/detail?id=169 |
| 707 | |
| 708 | DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, |
| 709 | blendState.colorMaskBlue, blendState.colorMaskAlpha); |
| 710 | if (colorMask == 0 && !zeroColorMaskAllowed) |
| 711 | { |
| 712 | // Enable green channel, but set blending so nothing will be drawn. |
| 713 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); |
| 714 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 715 | |
| 716 | mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); |
| 717 | mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); |
| 718 | mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); |
| 723 | } |
| 724 | |
| 725 | mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); |
| 726 | |
| 727 | mCurBlendState = blendState; |
| 728 | mCurBlendColor = blendColor; |
| 729 | } |
| 730 | |
| 731 | if (sampleMaskChanged) |
| 732 | { |
| 733 | // Set the multisample mask |
| 734 | mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 735 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); |
| 736 | |
| 737 | mCurSampleMask = sampleMask; |
| 738 | } |
| 739 | |
| 740 | mForceSetBlendState = false; |
| 741 | } |
| 742 | |
| 743 | void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, bool frontFaceCCW, |
| 744 | unsigned int stencilSize) |
| 745 | { |
| 746 | bool depthStencilStateChanged = mForceSetDepthStencilState || |
| 747 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; |
| 748 | bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; |
| 749 | bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize; |
| 750 | |
| 751 | if (depthStencilStateChanged) |
| 752 | { |
| 753 | if (depthStencilState.depthTest) |
| 754 | { |
| 755 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 756 | mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 761 | } |
| 762 | |
| 763 | mCurDepthStencilState = depthStencilState; |
| 764 | } |
| 765 | |
| 766 | if (depthStencilStateChanged || frontFaceCCWChanged || stencilSizeChanged) |
| 767 | { |
| 768 | if (depthStencilState.stencilTest && stencilSize > 0) |
| 769 | { |
| 770 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 771 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 772 | |
| 773 | // FIXME: Unsupported by D3D9 |
| 774 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 775 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 776 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 777 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
| 778 | depthStencilState.stencilRef != depthStencilState.stencilBackRef || |
| 779 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 780 | { |
| 781 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 782 | return error(GL_INVALID_OPERATION); |
| 783 | } |
| 784 | |
| 785 | // get the maximum size of the stencil ref |
| 786 | GLuint maxStencil = (1 << stencilSize) - 1; |
| 787 | |
| 788 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 789 | depthStencilState.stencilWritemask); |
| 790 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 791 | gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); |
| 792 | |
| 793 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
| 794 | (depthStencilState.stencilRef < (GLint)maxStencil) ? depthStencilState.stencilRef : maxStencil); |
| 795 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 796 | depthStencilState.stencilMask); |
| 797 | |
| 798 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 799 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); |
| 800 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 801 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); |
| 802 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 803 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); |
| 804 | |
| 805 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 806 | depthStencilState.stencilBackWritemask); |
| 807 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 808 | gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); |
| 809 | |
| 810 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
| 811 | (depthStencilState.stencilBackRef < (GLint)maxStencil) ? depthStencilState.stencilBackRef : maxStencil); |
| 812 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 813 | depthStencilState.stencilBackMask); |
| 814 | |
| 815 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 816 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); |
| 817 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 818 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); |
| 819 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 820 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 825 | } |
| 826 | |
| 827 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); |
| 828 | |
| 829 | mCurFrontFaceCCW = frontFaceCCW; |
| 830 | mCurStencilSize = stencilSize; |
| 831 | } |
| 832 | |
| 833 | mForceSetDepthStencilState = false; |
| 834 | } |
| 835 | |
| 836 | void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth, |
| 837 | unsigned int renderTargetHeight) |
| 838 | { |
| 839 | bool renderTargetSizedChanged = mForceSetScissor || |
| 840 | renderTargetWidth != mCurRenderTargetWidth || |
| 841 | renderTargetHeight != mCurRenderTargetHeight; |
| 842 | bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0; |
| 843 | |
| 844 | if (renderTargetSizedChanged || scissorChanged) |
| 845 | { |
| 846 | RECT rect; |
| 847 | rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth)); |
| 848 | rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth)); |
| 849 | rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth)); |
| 850 | rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth)); |
| 851 | mDevice->SetScissorRect(&rect); |
| 852 | |
| 853 | mCurScissor = scissor; |
| 854 | mCurRenderTargetWidth = renderTargetWidth; |
| 855 | mCurRenderTargetHeight = renderTargetHeight; |
| 856 | } |
| 857 | |
| 858 | mForceSetScissor = false; |
| 859 | } |
| 860 | |
| 861 | void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer) |
| 862 | { |
| 863 | mForceSetScissor = true; |
| 864 | |
| 865 | // TODO |
| 866 | } |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 867 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 868 | void Renderer9::releaseDeviceResources() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 869 | { |
| 870 | while (!mEventQueryPool.empty()) |
| 871 | { |
| 872 | mEventQueryPool.back()->Release(); |
| 873 | mEventQueryPool.pop_back(); |
| 874 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 875 | |
| 876 | mVertexShaderCache.clear(); |
| 877 | mPixelShaderCache.clear(); |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 881 | void Renderer9::markDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 882 | { |
| 883 | mDeviceLost = true; |
| 884 | } |
| 885 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 886 | bool Renderer9::isDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 887 | { |
| 888 | return mDeviceLost; |
| 889 | } |
| 890 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 891 | // set notify to true to broadcast a message to all contexts of the device loss |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 892 | bool Renderer9::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 893 | { |
| 894 | bool isLost = false; |
| 895 | |
| 896 | if (mDeviceEx) |
| 897 | { |
| 898 | isLost = FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 899 | } |
| 900 | else if (mDevice) |
| 901 | { |
| 902 | isLost = FAILED(mDevice->TestCooperativeLevel()); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | // No device yet, so no reset required |
| 907 | } |
| 908 | |
| 909 | if (isLost) |
| 910 | { |
| 911 | // ensure we note the device loss -- |
| 912 | // we'll probably get this done again by markDeviceLost |
| 913 | // but best to remember it! |
| 914 | // Note that we don't want to clear the device loss status here |
| 915 | // -- this needs to be done by resetDevice |
| 916 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 917 | if (notify) |
| 918 | { |
| 919 | mDisplay->notifyDeviceLost(); |
| 920 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | return isLost; |
| 924 | } |
| 925 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 926 | bool Renderer9::testDeviceResettable() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 927 | { |
| 928 | HRESULT status = D3D_OK; |
| 929 | |
| 930 | if (mDeviceEx) |
| 931 | { |
| 932 | status = mDeviceEx->CheckDeviceState(NULL); |
| 933 | } |
| 934 | else if (mDevice) |
| 935 | { |
| 936 | status = mDevice->TestCooperativeLevel(); |
| 937 | } |
| 938 | |
| 939 | switch (status) |
| 940 | { |
| 941 | case D3DERR_DEVICENOTRESET: |
| 942 | case D3DERR_DEVICEHUNG: |
| 943 | return true; |
| 944 | default: |
| 945 | return false; |
| 946 | } |
| 947 | } |
| 948 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 949 | bool Renderer9::resetDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 950 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 951 | releaseDeviceResources(); |
| 952 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 953 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 954 | |
| 955 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 956 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 957 | int attempts = 3; |
| 958 | |
| 959 | while (lost && attempts > 0) |
| 960 | { |
| 961 | if (mDeviceEx) |
| 962 | { |
| 963 | Sleep(500); // Give the graphics driver some CPU time |
| 964 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 965 | } |
| 966 | else |
| 967 | { |
| 968 | result = mDevice->TestCooperativeLevel(); |
| 969 | while (result == D3DERR_DEVICELOST) |
| 970 | { |
| 971 | Sleep(100); // Give the graphics driver some CPU time |
| 972 | result = mDevice->TestCooperativeLevel(); |
| 973 | } |
| 974 | |
| 975 | if (result == D3DERR_DEVICENOTRESET) |
| 976 | { |
| 977 | result = mDevice->Reset(&presentParameters); |
| 978 | } |
| 979 | } |
| 980 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 981 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 982 | attempts --; |
| 983 | } |
| 984 | |
| 985 | if (FAILED(result)) |
| 986 | { |
| 987 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | // reset device defaults |
| 992 | initializeDevice(); |
| 993 | mDeviceLost = false; |
| 994 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 995 | mForceSetDepthStencilState = true; |
| 996 | mForceSetRasterState = true; |
| 997 | mForceSetBlendState = true; |
| 998 | mForceSetScissor = true; |
| 999 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1000 | return true; |
| 1001 | } |
| 1002 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1003 | DWORD Renderer9::getAdapterVendor() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1004 | { |
| 1005 | return mAdapterIdentifier.VendorId; |
| 1006 | } |
| 1007 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1008 | const char *Renderer9::getAdapterDescription() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1009 | { |
| 1010 | return mAdapterIdentifier.Description; |
| 1011 | } |
| 1012 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1013 | GUID Renderer9::getAdapterIdentifier() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1014 | { |
| 1015 | return mAdapterIdentifier.DeviceIdentifier; |
| 1016 | } |
| 1017 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1018 | void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1019 | { |
| 1020 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 1021 | { |
| 1022 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 1023 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 1024 | |
| 1025 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 1026 | } |
| 1027 | } |
| 1028 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1029 | bool Renderer9::getDXT1TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1030 | { |
| 1031 | D3DDISPLAYMODE currentDisplayMode; |
| 1032 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1033 | |
| 1034 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 1035 | } |
| 1036 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1037 | bool Renderer9::getDXT3TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1038 | { |
| 1039 | D3DDISPLAYMODE currentDisplayMode; |
| 1040 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1041 | |
| 1042 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 1043 | } |
| 1044 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1045 | bool Renderer9::getDXT5TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1046 | { |
| 1047 | D3DDISPLAYMODE currentDisplayMode; |
| 1048 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1049 | |
| 1050 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 1051 | } |
| 1052 | |
| 1053 | // we use INTZ for depth textures in Direct3D9 |
| 1054 | // we also want NULL texture support to ensure the we can make depth-only FBOs |
| 1055 | // see http://aras-p.info/texts/D3D9GPUHacks.html |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1056 | bool Renderer9::getDepthTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1057 | { |
| 1058 | D3DDISPLAYMODE currentDisplayMode; |
| 1059 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1060 | |
| 1061 | bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1062 | D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)); |
| 1063 | bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1064 | D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); |
| 1065 | |
| 1066 | return intz && null; |
| 1067 | } |
| 1068 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1069 | bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1070 | { |
| 1071 | D3DDISPLAYMODE currentDisplayMode; |
| 1072 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1073 | |
| 1074 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1075 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1076 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1077 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1078 | |
| 1079 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1080 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 1081 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1082 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1083 | |
| 1084 | if (!*filtering && !*renderable) |
| 1085 | { |
| 1086 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1087 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1088 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1089 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1090 | } |
| 1091 | else |
| 1092 | { |
| 1093 | return true; |
| 1094 | } |
| 1095 | } |
| 1096 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1097 | bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1098 | { |
| 1099 | D3DDISPLAYMODE currentDisplayMode; |
| 1100 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1101 | |
| 1102 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1103 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1104 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1105 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1106 | |
| 1107 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1108 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1109 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1110 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1111 | |
| 1112 | if (!*filtering && !*renderable) |
| 1113 | { |
| 1114 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1115 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1116 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1117 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | return true; |
| 1122 | } |
| 1123 | } |
| 1124 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1125 | bool Renderer9::getLuminanceTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1126 | { |
| 1127 | D3DDISPLAYMODE currentDisplayMode; |
| 1128 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1129 | |
| 1130 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 1131 | } |
| 1132 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1133 | bool Renderer9::getLuminanceAlphaTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1134 | { |
| 1135 | D3DDISPLAYMODE currentDisplayMode; |
| 1136 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1137 | |
| 1138 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 1139 | } |
| 1140 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1141 | bool Renderer9::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1142 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1143 | return mSupportsTextureFilterAnisotropy; |
| 1144 | } |
| 1145 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1146 | float Renderer9::getTextureMaxAnisotropy() const |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1147 | { |
| 1148 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1149 | { |
| 1150 | return mDeviceCaps.MaxAnisotropy; |
| 1151 | } |
| 1152 | return 1.0f; |
| 1153 | } |
| 1154 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1155 | bool Renderer9::getEventQuerySupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1156 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1157 | IDirect3DQuery9 *query = allocateEventQuery(); |
| 1158 | if (query) |
| 1159 | { |
| 1160 | freeEventQuery(query); |
| 1161 | return true; |
| 1162 | } |
| 1163 | else |
| 1164 | { |
| 1165 | return false; |
| 1166 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1167 | return true; |
| 1168 | } |
| 1169 | |
| 1170 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 1171 | // We test this using D3D9 by checking support for the R16F format. |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1172 | bool Renderer9::getVertexTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1173 | { |
| 1174 | if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 1175 | { |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | D3DDISPLAYMODE currentDisplayMode; |
| 1180 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1181 | |
| 1182 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 1183 | |
| 1184 | return SUCCEEDED(result); |
| 1185 | } |
| 1186 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1187 | bool Renderer9::getNonPower2TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1188 | { |
| 1189 | return mSupportsNonPower2Textures; |
| 1190 | } |
| 1191 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1192 | bool Renderer9::getOcclusionQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1193 | { |
| 1194 | if (!mDevice) |
| 1195 | { |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | IDirect3DQuery9 *query = NULL; |
| 1200 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query); |
| 1201 | if (SUCCEEDED(result) && query) |
| 1202 | { |
| 1203 | query->Release(); |
| 1204 | return true; |
| 1205 | } |
| 1206 | else |
| 1207 | { |
| 1208 | return false; |
| 1209 | } |
| 1210 | } |
| 1211 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1212 | bool Renderer9::getInstancingSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1213 | { |
| 1214 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1215 | } |
| 1216 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1217 | bool Renderer9::getShareHandleSupport() const |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1218 | { |
| 1219 | // PIX doesn't seem to support using share handles, so disable them. |
| 1220 | // D3D9_REPLACE |
daniel@transgaming.com | 7cb796e | 2012-10-31 18:46:44 +0000 | [diff] [blame] | 1221 | return (mD3d9Ex != NULL) && !gl::perfActive(); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1222 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1223 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1224 | bool Renderer9::getShaderModel3Support() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1225 | { |
| 1226 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1227 | } |
| 1228 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1229 | float Renderer9::getMaxPointSize() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1230 | { |
| 1231 | return mDeviceCaps.MaxPointSize; |
| 1232 | } |
| 1233 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1234 | int Renderer9::getMaxTextureWidth() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1235 | { |
| 1236 | return (int)mDeviceCaps.MaxTextureWidth; |
| 1237 | } |
| 1238 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1239 | int Renderer9::getMaxTextureHeight() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1240 | { |
| 1241 | return (int)mDeviceCaps.MaxTextureHeight; |
| 1242 | } |
| 1243 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1244 | bool Renderer9::get32BitIndexSupport() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1245 | { |
| 1246 | return mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 1247 | } |
| 1248 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1249 | DWORD Renderer9::getCapsDeclTypes() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1250 | { |
| 1251 | return mDeviceCaps.DeclTypes; |
| 1252 | } |
| 1253 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1254 | int Renderer9::getMinSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1255 | { |
| 1256 | return mMinSwapInterval; |
| 1257 | } |
| 1258 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1259 | int Renderer9::getMaxSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1260 | { |
| 1261 | return mMaxSwapInterval; |
| 1262 | } |
| 1263 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1264 | int Renderer9::getMaxSupportedSamples() const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1265 | { |
| 1266 | return mMaxSupportedSamples; |
| 1267 | } |
| 1268 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1269 | int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1270 | { |
| 1271 | if (requested == 0) |
| 1272 | { |
| 1273 | return requested; |
| 1274 | } |
| 1275 | |
| 1276 | std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); |
| 1277 | if (itr == mMultiSampleSupport.end()) |
| 1278 | { |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 1279 | if (format == D3DFMT_UNKNOWN) |
| 1280 | return 0; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) |
| 1285 | { |
| 1286 | if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
| 1287 | { |
| 1288 | return i; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return -1; |
| 1293 | } |
| 1294 | |
daniel@transgaming.com | a957168 | 2012-11-28 19:33:08 +0000 | [diff] [blame] | 1295 | D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) |
| 1296 | { |
| 1297 | switch (internalformat) |
| 1298 | { |
| 1299 | case GL_DEPTH_COMPONENT16: |
| 1300 | case GL_DEPTH_COMPONENT32_OES: |
| 1301 | case GL_DEPTH24_STENCIL8_OES: |
| 1302 | return D3DFMT_INTZ; |
| 1303 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 1304 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 1305 | return D3DFMT_DXT1; |
| 1306 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 1307 | return D3DFMT_DXT3; |
| 1308 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 1309 | return D3DFMT_DXT5; |
| 1310 | case GL_RGBA32F_EXT: |
| 1311 | case GL_RGB32F_EXT: |
| 1312 | case GL_ALPHA32F_EXT: |
| 1313 | case GL_LUMINANCE32F_EXT: |
| 1314 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 1315 | return D3DFMT_A32B32G32R32F; |
| 1316 | case GL_RGBA16F_EXT: |
| 1317 | case GL_RGB16F_EXT: |
| 1318 | case GL_ALPHA16F_EXT: |
| 1319 | case GL_LUMINANCE16F_EXT: |
| 1320 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 1321 | return D3DFMT_A16B16G16R16F; |
| 1322 | case GL_LUMINANCE8_EXT: |
| 1323 | if (getLuminanceTextureSupport()) |
| 1324 | { |
| 1325 | return D3DFMT_L8; |
| 1326 | } |
| 1327 | break; |
| 1328 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 1329 | if (getLuminanceAlphaTextureSupport()) |
| 1330 | { |
| 1331 | return D3DFMT_A8L8; |
| 1332 | } |
| 1333 | break; |
| 1334 | case GL_RGB8_OES: |
| 1335 | case GL_RGB565: |
| 1336 | return D3DFMT_X8R8G8B8; |
| 1337 | } |
| 1338 | |
| 1339 | return D3DFMT_A8R8G8B8; |
| 1340 | } |
| 1341 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1342 | bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1343 | { |
| 1344 | bool result = false; |
| 1345 | |
| 1346 | if (source && dest) |
| 1347 | { |
| 1348 | int levels = source->levelCount(); |
| 1349 | for (int i = 0; i < levels; ++i) |
| 1350 | { |
| 1351 | IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false); |
| 1352 | IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false); |
| 1353 | |
| 1354 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 1355 | |
| 1356 | if (srcSurf) srcSurf->Release(); |
| 1357 | if (dstSurf) dstSurf->Release(); |
| 1358 | |
| 1359 | if (!result) |
| 1360 | return false; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | return result; |
| 1365 | } |
| 1366 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1367 | bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1368 | { |
| 1369 | bool result = false; |
| 1370 | |
| 1371 | if (source && dest) |
| 1372 | { |
| 1373 | int levels = source->levelCount(); |
| 1374 | for (int f = 0; f < 6; f++) |
| 1375 | { |
| 1376 | for (int i = 0; i < levels; i++) |
| 1377 | { |
| 1378 | IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); |
| 1379 | IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); |
| 1380 | |
| 1381 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 1382 | |
| 1383 | if (srcSurf) srcSurf->Release(); |
| 1384 | if (dstSurf) dstSurf->Release(); |
| 1385 | |
| 1386 | if (!result) |
| 1387 | return false; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return result; |
| 1393 | } |
| 1394 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1395 | D3DPOOL Renderer9::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1396 | { |
| 1397 | if (mD3d9Ex != NULL) |
| 1398 | { |
| 1399 | return D3DPOOL_DEFAULT; |
| 1400 | } |
| 1401 | else |
| 1402 | { |
| 1403 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 1404 | { |
| 1405 | return D3DPOOL_MANAGED; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | return D3DPOOL_DEFAULT; |
| 1410 | } |
| 1411 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1412 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1413 | TextureStorage2D *storage, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1414 | { |
| 1415 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level); |
| 1416 | } |
| 1417 | |
| 1418 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1419 | TextureStorageCubeMap *storage, GLenum target, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1420 | { |
| 1421 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level); |
| 1422 | } |
| 1423 | |
| 1424 | bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 1425 | { |
| 1426 | return mBlit->boxFilter(source, dest); |
| 1427 | } |
| 1428 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1429 | D3DPOOL Renderer9::getTexturePool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1430 | { |
| 1431 | if (mD3d9Ex != NULL) |
| 1432 | { |
| 1433 | return D3DPOOL_DEFAULT; |
| 1434 | } |
| 1435 | else |
| 1436 | { |
| 1437 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 1438 | { |
| 1439 | return D3DPOOL_MANAGED; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | return D3DPOOL_DEFAULT; |
| 1444 | } |
| 1445 | |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1446 | bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) |
| 1447 | { |
| 1448 | if (source && dest) |
| 1449 | { |
| 1450 | HRESULT result = D3DERR_OUTOFVIDEOMEMORY; |
| 1451 | IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE |
| 1452 | |
| 1453 | if (fromManaged) |
| 1454 | { |
| 1455 | D3DSURFACE_DESC desc; |
| 1456 | source->GetDesc(&desc); |
| 1457 | |
| 1458 | IDirect3DSurface9 *surf = 0; |
| 1459 | result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); |
| 1460 | |
| 1461 | if (SUCCEEDED(result)) |
| 1462 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1463 | Image::CopyLockableSurfaces(surf, source); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1464 | result = device->UpdateSurface(surf, NULL, dest, NULL); |
| 1465 | surf->Release(); |
| 1466 | } |
| 1467 | } |
| 1468 | else |
| 1469 | { |
| 1470 | endScene(); |
| 1471 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 1472 | } |
| 1473 | |
| 1474 | if (FAILED(result)) |
| 1475 | { |
| 1476 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 1477 | return false; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | return true; |
| 1482 | } |
| 1483 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1484 | } |