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 | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 16 | #include "libGLESv2/renderer/SwapChain9.h" |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/TextureStorage.h" |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 18 | #include "libGLESv2/renderer/Image.h" |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/Blit.h" |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 20 | #include "libGLESv2/renderer/RenderTarget9.h" |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 22 | #include "libEGL/Config.h" |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 23 | #include "libEGL/Display.h" |
| 24 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 25 | // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 26 | #define REF_RAST 0 |
| 27 | |
| 28 | // The "Debug This Pixel..." feature in PIX often fails when using the |
| 29 | // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 |
| 30 | // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. |
| 31 | #if !defined(ANGLE_ENABLE_D3D9EX) |
| 32 | // Enables use of the IDirect3D9Ex interface, when available |
| 33 | #define ANGLE_ENABLE_D3D9EX 1 |
| 34 | #endif // !defined(ANGLE_ENABLE_D3D9EX) |
| 35 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 36 | namespace rx |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 37 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 38 | static const D3DFORMAT RenderTargetFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 39 | { |
| 40 | D3DFMT_A1R5G5B5, |
| 41 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 42 | D3DFMT_A8R8G8B8, |
| 43 | D3DFMT_R5G6B5, |
| 44 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
| 45 | D3DFMT_X8R8G8B8 |
| 46 | }; |
| 47 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 48 | static const D3DFORMAT DepthStencilFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 49 | { |
| 50 | D3DFMT_UNKNOWN, |
| 51 | // D3DFMT_D16_LOCKABLE, |
| 52 | D3DFMT_D32, |
| 53 | // D3DFMT_D15S1, |
| 54 | D3DFMT_D24S8, |
| 55 | D3DFMT_D24X8, |
| 56 | // D3DFMT_D24X4S4, |
| 57 | D3DFMT_D16, |
| 58 | // D3DFMT_D32F_LOCKABLE, |
| 59 | // D3DFMT_D24FS8 |
| 60 | }; |
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 | 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] | 63 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 64 | mD3d9Module = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 65 | |
| 66 | mD3d9 = NULL; |
| 67 | mD3d9Ex = NULL; |
| 68 | mDevice = NULL; |
| 69 | mDeviceEx = NULL; |
| 70 | mDeviceWindow = NULL; |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 71 | mBlit = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 72 | |
| 73 | mAdapter = D3DADAPTER_DEFAULT; |
| 74 | |
| 75 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 76 | mDeviceType = D3DDEVTYPE_REF; |
| 77 | #else |
| 78 | mDeviceType = D3DDEVTYPE_HAL; |
| 79 | #endif |
| 80 | |
| 81 | mDeviceLost = false; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 82 | |
| 83 | mMaxSupportedSamples = 0; |
daniel@transgaming.com | 8a8b24c | 2012-11-28 19:36:26 +0000 | [diff] [blame] | 84 | |
| 85 | mForceSetDepthStencilState = true; |
| 86 | mForceSetRasterState = true; |
| 87 | mForceSetBlendState = true; |
| 88 | mForceSetScissor = true; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 91 | Renderer9::~Renderer9() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 92 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 93 | releaseDeviceResources(); |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 94 | |
| 95 | delete mBlit; |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 96 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 97 | if (mDevice) |
| 98 | { |
| 99 | // 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] | 100 | if (testDeviceLost(false)) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 101 | { |
| 102 | resetDevice(); |
| 103 | } |
| 104 | |
| 105 | mDevice->Release(); |
| 106 | mDevice = NULL; |
| 107 | } |
| 108 | |
| 109 | if (mDeviceEx) |
| 110 | { |
| 111 | mDeviceEx->Release(); |
| 112 | mDeviceEx = NULL; |
| 113 | } |
| 114 | |
| 115 | if (mD3d9) |
| 116 | { |
| 117 | mD3d9->Release(); |
| 118 | mD3d9 = NULL; |
| 119 | } |
| 120 | |
| 121 | if (mDeviceWindow) |
| 122 | { |
| 123 | DestroyWindow(mDeviceWindow); |
| 124 | mDeviceWindow = NULL; |
| 125 | } |
| 126 | |
| 127 | if (mD3d9Ex) |
| 128 | { |
| 129 | mD3d9Ex->Release(); |
| 130 | mD3d9Ex = NULL; |
| 131 | } |
| 132 | |
| 133 | if (mD3d9Module) |
| 134 | { |
| 135 | mD3d9Module = NULL; |
| 136 | } |
| 137 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 138 | while (!mMultiSampleSupport.empty()) |
| 139 | { |
| 140 | delete [] mMultiSampleSupport.begin()->second; |
| 141 | mMultiSampleSupport.erase(mMultiSampleSupport.begin()); |
| 142 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 143 | } |
| 144 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 145 | EGLint Renderer9::initialize() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 146 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 147 | if (mSoftwareDevice) |
| 148 | { |
| 149 | mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); |
| 154 | } |
| 155 | |
| 156 | if (mD3d9Module == NULL) |
| 157 | { |
| 158 | ERR("No D3D9 module found - aborting!\n"); |
| 159 | return EGL_NOT_INITIALIZED; |
| 160 | } |
| 161 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 162 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 163 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 164 | |
| 165 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 166 | // inclined to report a lost context, for example when the user switches |
| 167 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
| 168 | if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
| 169 | { |
| 170 | ASSERT(mD3d9Ex); |
| 171 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
| 172 | ASSERT(mD3d9); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 177 | } |
| 178 | |
| 179 | if (!mD3d9) |
| 180 | { |
| 181 | ERR("Could not create D3D9 device - aborting!\n"); |
| 182 | return EGL_NOT_INITIALIZED; |
| 183 | } |
| 184 | if (mDc != NULL) |
| 185 | { |
| 186 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 187 | } |
| 188 | |
| 189 | HRESULT result; |
| 190 | |
| 191 | // Give up on getting device caps after about one second. |
| 192 | for (int i = 0; i < 10; ++i) |
| 193 | { |
| 194 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
| 195 | if (SUCCEEDED(result)) |
| 196 | { |
| 197 | break; |
| 198 | } |
| 199 | else if (result == D3DERR_NOTAVAILABLE) |
| 200 | { |
| 201 | Sleep(100); // Give the driver some time to initialize/recover |
| 202 | } |
| 203 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 204 | { |
| 205 | ERR("failed to get device caps (0x%x)\n", result); |
| 206 | return EGL_NOT_INITIALIZED; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 211 | { |
| 212 | ERR("Renderer does not support PS 2.0. aborting!\n"); |
| 213 | return EGL_NOT_INITIALIZED; |
| 214 | } |
| 215 | |
| 216 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 217 | // This is required by Texture2D::convertToRenderTarget. |
| 218 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 219 | { |
| 220 | ERR("Renderer does not support stretctrect from textures!\n"); |
| 221 | return EGL_NOT_INITIALIZED; |
| 222 | } |
| 223 | |
| 224 | mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); |
| 225 | |
| 226 | // ATI cards on XP have problems with non-power-of-two textures. |
| 227 | mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && |
| 228 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && |
| 229 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && |
| 230 | !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); |
| 231 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 232 | // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec |
| 233 | mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); |
| 234 | |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 235 | mMinSwapInterval = 4; |
| 236 | mMaxSwapInterval = 0; |
| 237 | |
| 238 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) |
| 239 | { |
| 240 | mMinSwapInterval = std::min(mMinSwapInterval, 0); |
| 241 | mMaxSwapInterval = std::max(mMaxSwapInterval, 0); |
| 242 | } |
| 243 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) |
| 244 | { |
| 245 | mMinSwapInterval = std::min(mMinSwapInterval, 1); |
| 246 | mMaxSwapInterval = std::max(mMaxSwapInterval, 1); |
| 247 | } |
| 248 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) |
| 249 | { |
| 250 | mMinSwapInterval = std::min(mMinSwapInterval, 2); |
| 251 | mMaxSwapInterval = std::max(mMaxSwapInterval, 2); |
| 252 | } |
| 253 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) |
| 254 | { |
| 255 | mMinSwapInterval = std::min(mMinSwapInterval, 3); |
| 256 | mMaxSwapInterval = std::max(mMaxSwapInterval, 3); |
| 257 | } |
| 258 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) |
| 259 | { |
| 260 | mMinSwapInterval = std::min(mMinSwapInterval, 4); |
| 261 | mMaxSwapInterval = std::max(mMaxSwapInterval, 4); |
| 262 | } |
| 263 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 264 | int max = 0; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 265 | for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 266 | { |
| 267 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 268 | getMultiSampleSupport(RenderTargetFormats[i], multisampleArray); |
| 269 | mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray; |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 270 | |
| 271 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 272 | { |
| 273 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 274 | { |
| 275 | max = j; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 280 | for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 281 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 282 | if (DepthStencilFormats[i] == D3DFMT_UNKNOWN) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 283 | continue; |
| 284 | |
| 285 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 286 | getMultiSampleSupport(DepthStencilFormats[i], multisampleArray); |
| 287 | mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 288 | |
| 289 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 290 | { |
| 291 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 292 | { |
| 293 | max = j; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | mMaxSupportedSamples = max; |
| 299 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 300 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 301 | static const TCHAR className[] = TEXT("STATIC"); |
| 302 | |
| 303 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 304 | |
| 305 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 306 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 307 | |
| 308 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 309 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 310 | { |
| 311 | return EGL_BAD_ALLOC; |
| 312 | } |
| 313 | |
| 314 | if (FAILED(result)) |
| 315 | { |
| 316 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 317 | |
| 318 | if (FAILED(result)) |
| 319 | { |
| 320 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 321 | return EGL_BAD_ALLOC; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (mD3d9Ex) |
| 326 | { |
| 327 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 328 | ASSERT(SUCCEEDED(result)); |
| 329 | } |
| 330 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 331 | mVertexShaderCache.initialize(mDevice); |
| 332 | mPixelShaderCache.initialize(mDevice); |
| 333 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 334 | initializeDevice(); |
| 335 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 336 | mBlit = new Blit(this); |
| 337 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 338 | return EGL_SUCCESS; |
| 339 | } |
| 340 | |
| 341 | // do any one-time device initialization |
| 342 | // NOTE: this is also needed after a device lost/reset |
| 343 | // 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] | 344 | void Renderer9::initializeDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 345 | { |
| 346 | // Permanent non-default states |
| 347 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 348 | mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); |
| 349 | |
| 350 | if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) |
| 351 | { |
| 352 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f |
| 357 | } |
| 358 | |
| 359 | mSceneStarted = false; |
| 360 | } |
| 361 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 362 | D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 363 | { |
| 364 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 365 | |
| 366 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 367 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 368 | presentParameters.BackBufferCount = 1; |
| 369 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 370 | presentParameters.BackBufferWidth = 1; |
| 371 | presentParameters.BackBufferHeight = 1; |
| 372 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 373 | presentParameters.Flags = 0; |
| 374 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 375 | presentParameters.MultiSampleQuality = 0; |
| 376 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 377 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 378 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 379 | presentParameters.Windowed = TRUE; |
| 380 | |
| 381 | return presentParameters; |
| 382 | } |
| 383 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 384 | int Renderer9::generateConfigs(ConfigDesc **configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 385 | { |
| 386 | D3DDISPLAYMODE currentDisplayMode; |
| 387 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 388 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 389 | int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]); |
| 390 | int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 391 | (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; |
| 392 | int numConfigs = 0; |
| 393 | |
| 394 | for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) |
| 395 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 396 | D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 397 | |
| 398 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 399 | |
| 400 | if (SUCCEEDED(result)) |
| 401 | { |
| 402 | for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) |
| 403 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 404 | D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 405 | HRESULT result = D3D_OK; |
| 406 | |
| 407 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 408 | { |
| 409 | result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 410 | } |
| 411 | |
| 412 | if (SUCCEEDED(result)) |
| 413 | { |
| 414 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 415 | { |
| 416 | result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
| 417 | } |
| 418 | |
| 419 | if (SUCCEEDED(result)) |
| 420 | { |
| 421 | ConfigDesc newConfig; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 422 | newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat); |
| 423 | newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 424 | newConfig.multiSample = 0; // FIXME: enumerate multi-sampling |
| 425 | newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat); |
| 426 | |
| 427 | (*configDescList)[numConfigs++] = newConfig; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return numConfigs; |
| 435 | } |
| 436 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 437 | void Renderer9::deleteConfigs(ConfigDesc *configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 438 | { |
| 439 | delete [] (configDescList); |
| 440 | } |
| 441 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 442 | void Renderer9::startScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 443 | { |
| 444 | if (!mSceneStarted) |
| 445 | { |
| 446 | long result = mDevice->BeginScene(); |
| 447 | if (SUCCEEDED(result)) { |
| 448 | // This is defensive checking against the device being |
| 449 | // lost at unexpected times. |
| 450 | mSceneStarted = true; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 455 | void Renderer9::endScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 456 | { |
| 457 | if (mSceneStarted) |
| 458 | { |
| 459 | // EndScene can fail if the device was lost, for example due |
| 460 | // to a TDR during a draw call. |
| 461 | mDevice->EndScene(); |
| 462 | mSceneStarted = false; |
| 463 | } |
| 464 | } |
| 465 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 466 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 467 | void Renderer9::sync(bool block) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 468 | { |
| 469 | HRESULT result; |
| 470 | |
| 471 | IDirect3DQuery9* query = allocateEventQuery(); |
| 472 | if (!query) |
| 473 | { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | result = query->Issue(D3DISSUE_END); |
| 478 | ASSERT(SUCCEEDED(result)); |
| 479 | |
| 480 | do |
| 481 | { |
| 482 | result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); |
| 483 | |
| 484 | if(block && result == S_FALSE) |
| 485 | { |
| 486 | // Keep polling, but allow other threads to do something useful first |
| 487 | Sleep(0); |
| 488 | // explicitly check for device loss |
| 489 | // some drivers seem to return S_FALSE even if the device is lost |
| 490 | // instead of D3DERR_DEVICELOST like they should |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 491 | if (testDeviceLost(false)) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 492 | { |
| 493 | result = D3DERR_DEVICELOST; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | while(block && result == S_FALSE); |
| 498 | |
| 499 | freeEventQuery(query); |
| 500 | |
| 501 | if (isDeviceLostError(result)) |
| 502 | { |
| 503 | mDisplay->notifyDeviceLost(); |
| 504 | } |
| 505 | } |
| 506 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 507 | SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) |
| 508 | { |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 509 | return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat); |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 510 | } |
| 511 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 512 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 513 | IDirect3DQuery9* Renderer9::allocateEventQuery() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 514 | { |
| 515 | IDirect3DQuery9 *query = NULL; |
| 516 | |
| 517 | if (mEventQueryPool.empty()) |
| 518 | { |
| 519 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 520 | ASSERT(SUCCEEDED(result)); |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | query = mEventQueryPool.back(); |
| 525 | mEventQueryPool.pop_back(); |
| 526 | } |
| 527 | |
| 528 | return query; |
| 529 | } |
| 530 | |
| 531 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 532 | void Renderer9::freeEventQuery(IDirect3DQuery9* query) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 533 | { |
| 534 | if (mEventQueryPool.size() > 1000) |
| 535 | { |
| 536 | query->Release(); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | mEventQueryPool.push_back(query); |
| 541 | } |
| 542 | } |
| 543 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 544 | IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 545 | { |
| 546 | return mVertexShaderCache.create(function, length); |
| 547 | } |
| 548 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 549 | IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 550 | { |
| 551 | return mPixelShaderCache.create(function, length); |
| 552 | } |
| 553 | |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 554 | HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) |
| 555 | { |
| 556 | D3DPOOL Pool = getBufferPool(Usage); |
| 557 | return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); |
| 558 | } |
| 559 | |
| 560 | HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) |
| 561 | { |
| 562 | D3DPOOL Pool = getBufferPool(Usage); |
| 563 | return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); |
| 564 | } |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 565 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 566 | 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] | 567 | { |
| 568 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 569 | int d3dSampler = index + d3dSamplerOffset; |
| 570 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 571 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); |
| 572 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 573 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 574 | 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] | 575 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 576 | gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 577 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 578 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 579 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 580 | if (mSupportsTextureFilterAnisotropy) |
| 581 | { |
| 582 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 583 | } |
| 584 | } |
| 585 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 586 | void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 587 | { |
| 588 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 589 | int d3dSampler = index + d3dSamplerOffset; |
| 590 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
| 591 | |
| 592 | if (texture) |
| 593 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 594 | TextureStorage *texStorage = texture->getNativeTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 595 | if (texStorage) |
| 596 | { |
| 597 | d3dTexture = texStorage->getBaseTexture(); |
| 598 | } |
| 599 | // If we get NULL back from getBaseTexture here, something went wrong |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 600 | // in the texture class and we're unexpectedly missing the d3d texture |
| 601 | ASSERT(d3dTexture != NULL); |
| 602 | } |
| 603 | |
| 604 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 605 | } |
| 606 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 607 | void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize) |
| 608 | { |
| 609 | bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; |
| 610 | bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize; |
| 611 | |
| 612 | if (rasterStateChanged) |
| 613 | { |
| 614 | // Set the cull mode |
| 615 | if (rasterState.cullFace) |
| 616 | { |
| 617 | mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 622 | } |
| 623 | |
| 624 | mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE); |
| 625 | |
| 626 | mCurRasterState = rasterState; |
| 627 | } |
| 628 | |
| 629 | if (rasterStateChanged || depthSizeChanged) |
| 630 | { |
| 631 | if (rasterState.polygonOffsetFill) |
| 632 | { |
| 633 | if (depthSize > 0) |
| 634 | { |
| 635 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); |
| 636 | |
| 637 | float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 638 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); |
| 639 | } |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 644 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 645 | } |
| 646 | |
| 647 | mCurDepthSize = depthSize; |
| 648 | } |
| 649 | |
| 650 | mForceSetRasterState = false; |
| 651 | } |
| 652 | |
| 653 | void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) |
| 654 | { |
| 655 | bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; |
| 656 | bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; |
| 657 | bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; |
| 658 | |
| 659 | if (blendStateChanged || blendColorChanged) |
| 660 | { |
| 661 | if (blendState.blend) |
| 662 | { |
| 663 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 664 | |
| 665 | if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 666 | blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 667 | { |
| 668 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); |
| 669 | } |
| 670 | else |
| 671 | { |
| 672 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), |
| 673 | gl::unorm<8>(blendColor.alpha), |
| 674 | gl::unorm<8>(blendColor.alpha), |
| 675 | gl::unorm<8>(blendColor.alpha))); |
| 676 | } |
| 677 | |
| 678 | mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); |
| 679 | mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); |
| 680 | mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); |
| 681 | |
| 682 | if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || |
| 683 | blendState.destBlendRGB != blendState.destBlendAlpha || |
| 684 | blendState.blendEquationRGB != blendState.blendEquationAlpha) |
| 685 | { |
| 686 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 687 | |
| 688 | mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); |
| 689 | mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); |
| 690 | mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); |
| 691 | } |
| 692 | else |
| 693 | { |
| 694 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 695 | } |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 700 | } |
| 701 | |
| 702 | if (blendState.sampleAlphaToCoverage) |
| 703 | { |
| 704 | FIXME("Sample alpha to coverage is unimplemented."); |
| 705 | } |
| 706 | |
| 707 | // Set the color mask |
| 708 | bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; |
| 709 | // Apparently some ATI cards have a bug where a draw with a zero color |
| 710 | // write mask can cause later draws to have incorrect results. Instead, |
| 711 | // set a nonzero color write mask but modify the blend state so that no |
| 712 | // drawing is done. |
| 713 | // http://code.google.com/p/angleproject/issues/detail?id=169 |
| 714 | |
| 715 | DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, |
| 716 | blendState.colorMaskBlue, blendState.colorMaskAlpha); |
| 717 | if (colorMask == 0 && !zeroColorMaskAllowed) |
| 718 | { |
| 719 | // Enable green channel, but set blending so nothing will be drawn. |
| 720 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); |
| 721 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 722 | |
| 723 | mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); |
| 724 | mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); |
| 725 | mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); |
| 730 | } |
| 731 | |
| 732 | mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); |
| 733 | |
| 734 | mCurBlendState = blendState; |
| 735 | mCurBlendColor = blendColor; |
| 736 | } |
| 737 | |
| 738 | if (sampleMaskChanged) |
| 739 | { |
| 740 | // Set the multisample mask |
| 741 | mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 742 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); |
| 743 | |
| 744 | mCurSampleMask = sampleMask; |
| 745 | } |
| 746 | |
| 747 | mForceSetBlendState = false; |
| 748 | } |
| 749 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 750 | void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
| 751 | int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 752 | { |
| 753 | bool depthStencilStateChanged = mForceSetDepthStencilState || |
| 754 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 755 | bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || |
| 756 | stencilBackRef != mCurStencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 757 | bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; |
| 758 | bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize; |
| 759 | |
| 760 | if (depthStencilStateChanged) |
| 761 | { |
| 762 | if (depthStencilState.depthTest) |
| 763 | { |
| 764 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 765 | mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 770 | } |
| 771 | |
| 772 | mCurDepthStencilState = depthStencilState; |
| 773 | } |
| 774 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 775 | if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 776 | { |
| 777 | if (depthStencilState.stencilTest && stencilSize > 0) |
| 778 | { |
| 779 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 780 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 781 | |
| 782 | // FIXME: Unsupported by D3D9 |
| 783 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 784 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 785 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 786 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 787 | stencilRef != stencilBackRef || |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 788 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 789 | { |
| 790 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 791 | return error(GL_INVALID_OPERATION); |
| 792 | } |
| 793 | |
| 794 | // get the maximum size of the stencil ref |
| 795 | GLuint maxStencil = (1 << stencilSize) - 1; |
| 796 | |
| 797 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 798 | depthStencilState.stencilWritemask); |
| 799 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 800 | gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); |
| 801 | |
| 802 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 803 | (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 804 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 805 | depthStencilState.stencilMask); |
| 806 | |
| 807 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 808 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); |
| 809 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 810 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); |
| 811 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 812 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); |
| 813 | |
| 814 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 815 | depthStencilState.stencilBackWritemask); |
| 816 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 817 | gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); |
| 818 | |
| 819 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 820 | (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 821 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 822 | depthStencilState.stencilBackMask); |
| 823 | |
| 824 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 825 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); |
| 826 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 827 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); |
| 828 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 829 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); |
| 830 | } |
| 831 | else |
| 832 | { |
| 833 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 834 | } |
| 835 | |
| 836 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); |
| 837 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 838 | mCurStencilRef = stencilRef; |
| 839 | mCurStencilBackRef = stencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 840 | mCurFrontFaceCCW = frontFaceCCW; |
| 841 | mCurStencilSize = stencilSize; |
| 842 | } |
| 843 | |
| 844 | mForceSetDepthStencilState = false; |
| 845 | } |
| 846 | |
| 847 | void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth, |
| 848 | unsigned int renderTargetHeight) |
| 849 | { |
| 850 | bool renderTargetSizedChanged = mForceSetScissor || |
| 851 | renderTargetWidth != mCurRenderTargetWidth || |
| 852 | renderTargetHeight != mCurRenderTargetHeight; |
| 853 | bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0; |
| 854 | |
| 855 | if (renderTargetSizedChanged || scissorChanged) |
| 856 | { |
| 857 | RECT rect; |
| 858 | rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth)); |
| 859 | rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth)); |
| 860 | rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth)); |
| 861 | rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth)); |
| 862 | mDevice->SetScissorRect(&rect); |
| 863 | |
| 864 | mCurScissor = scissor; |
| 865 | mCurRenderTargetWidth = renderTargetWidth; |
| 866 | mCurRenderTargetHeight = renderTargetHeight; |
| 867 | } |
| 868 | |
| 869 | mForceSetScissor = false; |
| 870 | } |
| 871 | |
| 872 | void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer) |
| 873 | { |
| 874 | mForceSetScissor = true; |
| 875 | |
| 876 | // TODO |
| 877 | } |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 878 | |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 879 | void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear, |
| 880 | gl::Framebuffer *frameBuffer) |
| 881 | { |
| 882 | mForceSetDepthStencilState = true; |
| 883 | |
| 884 | // TODO |
| 885 | } |
| 886 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 887 | void Renderer9::releaseDeviceResources() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 888 | { |
| 889 | while (!mEventQueryPool.empty()) |
| 890 | { |
| 891 | mEventQueryPool.back()->Release(); |
| 892 | mEventQueryPool.pop_back(); |
| 893 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 894 | |
| 895 | mVertexShaderCache.clear(); |
| 896 | mPixelShaderCache.clear(); |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 900 | void Renderer9::markDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 901 | { |
| 902 | mDeviceLost = true; |
| 903 | } |
| 904 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 905 | bool Renderer9::isDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 906 | { |
| 907 | return mDeviceLost; |
| 908 | } |
| 909 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 910 | // 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] | 911 | bool Renderer9::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 912 | { |
| 913 | bool isLost = false; |
| 914 | |
| 915 | if (mDeviceEx) |
| 916 | { |
| 917 | isLost = FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 918 | } |
| 919 | else if (mDevice) |
| 920 | { |
| 921 | isLost = FAILED(mDevice->TestCooperativeLevel()); |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | // No device yet, so no reset required |
| 926 | } |
| 927 | |
| 928 | if (isLost) |
| 929 | { |
| 930 | // ensure we note the device loss -- |
| 931 | // we'll probably get this done again by markDeviceLost |
| 932 | // but best to remember it! |
| 933 | // Note that we don't want to clear the device loss status here |
| 934 | // -- this needs to be done by resetDevice |
| 935 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 936 | if (notify) |
| 937 | { |
| 938 | mDisplay->notifyDeviceLost(); |
| 939 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | return isLost; |
| 943 | } |
| 944 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 945 | bool Renderer9::testDeviceResettable() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 946 | { |
| 947 | HRESULT status = D3D_OK; |
| 948 | |
| 949 | if (mDeviceEx) |
| 950 | { |
| 951 | status = mDeviceEx->CheckDeviceState(NULL); |
| 952 | } |
| 953 | else if (mDevice) |
| 954 | { |
| 955 | status = mDevice->TestCooperativeLevel(); |
| 956 | } |
| 957 | |
| 958 | switch (status) |
| 959 | { |
| 960 | case D3DERR_DEVICENOTRESET: |
| 961 | case D3DERR_DEVICEHUNG: |
| 962 | return true; |
| 963 | default: |
| 964 | return false; |
| 965 | } |
| 966 | } |
| 967 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 968 | bool Renderer9::resetDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 969 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 970 | releaseDeviceResources(); |
| 971 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 972 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 973 | |
| 974 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 975 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 976 | int attempts = 3; |
| 977 | |
| 978 | while (lost && attempts > 0) |
| 979 | { |
| 980 | if (mDeviceEx) |
| 981 | { |
| 982 | Sleep(500); // Give the graphics driver some CPU time |
| 983 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | result = mDevice->TestCooperativeLevel(); |
| 988 | while (result == D3DERR_DEVICELOST) |
| 989 | { |
| 990 | Sleep(100); // Give the graphics driver some CPU time |
| 991 | result = mDevice->TestCooperativeLevel(); |
| 992 | } |
| 993 | |
| 994 | if (result == D3DERR_DEVICENOTRESET) |
| 995 | { |
| 996 | result = mDevice->Reset(&presentParameters); |
| 997 | } |
| 998 | } |
| 999 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1000 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1001 | attempts --; |
| 1002 | } |
| 1003 | |
| 1004 | if (FAILED(result)) |
| 1005 | { |
| 1006 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
| 1010 | // reset device defaults |
| 1011 | initializeDevice(); |
| 1012 | mDeviceLost = false; |
| 1013 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1014 | mForceSetDepthStencilState = true; |
| 1015 | mForceSetRasterState = true; |
| 1016 | mForceSetBlendState = true; |
| 1017 | mForceSetScissor = true; |
| 1018 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1019 | return true; |
| 1020 | } |
| 1021 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1022 | DWORD Renderer9::getAdapterVendor() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1023 | { |
| 1024 | return mAdapterIdentifier.VendorId; |
| 1025 | } |
| 1026 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1027 | const char *Renderer9::getAdapterDescription() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1028 | { |
| 1029 | return mAdapterIdentifier.Description; |
| 1030 | } |
| 1031 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1032 | GUID Renderer9::getAdapterIdentifier() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1033 | { |
| 1034 | return mAdapterIdentifier.DeviceIdentifier; |
| 1035 | } |
| 1036 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1037 | void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1038 | { |
| 1039 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 1040 | { |
| 1041 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 1042 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 1043 | |
| 1044 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 1045 | } |
| 1046 | } |
| 1047 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1048 | bool Renderer9::getDXT1TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1049 | { |
| 1050 | D3DDISPLAYMODE currentDisplayMode; |
| 1051 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1052 | |
| 1053 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 1054 | } |
| 1055 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1056 | bool Renderer9::getDXT3TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1057 | { |
| 1058 | D3DDISPLAYMODE currentDisplayMode; |
| 1059 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1060 | |
| 1061 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 1062 | } |
| 1063 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1064 | bool Renderer9::getDXT5TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1065 | { |
| 1066 | D3DDISPLAYMODE currentDisplayMode; |
| 1067 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1068 | |
| 1069 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 1070 | } |
| 1071 | |
| 1072 | // we use INTZ for depth textures in Direct3D9 |
| 1073 | // we also want NULL texture support to ensure the we can make depth-only FBOs |
| 1074 | // see http://aras-p.info/texts/D3D9GPUHacks.html |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1075 | bool Renderer9::getDepthTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1076 | { |
| 1077 | D3DDISPLAYMODE currentDisplayMode; |
| 1078 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1079 | |
| 1080 | bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1081 | D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)); |
| 1082 | bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1083 | D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); |
| 1084 | |
| 1085 | return intz && null; |
| 1086 | } |
| 1087 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1088 | bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1089 | { |
| 1090 | D3DDISPLAYMODE currentDisplayMode; |
| 1091 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1092 | |
| 1093 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1094 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1095 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1096 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1097 | |
| 1098 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1099 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 1100 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1101 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1102 | |
| 1103 | if (!*filtering && !*renderable) |
| 1104 | { |
| 1105 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1106 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1107 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1108 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | return true; |
| 1113 | } |
| 1114 | } |
| 1115 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1116 | bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1117 | { |
| 1118 | D3DDISPLAYMODE currentDisplayMode; |
| 1119 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1120 | |
| 1121 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1122 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1123 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1124 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1125 | |
| 1126 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1127 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1128 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1129 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1130 | |
| 1131 | if (!*filtering && !*renderable) |
| 1132 | { |
| 1133 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1134 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1135 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1136 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1137 | } |
| 1138 | else |
| 1139 | { |
| 1140 | return true; |
| 1141 | } |
| 1142 | } |
| 1143 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1144 | bool Renderer9::getLuminanceTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1145 | { |
| 1146 | D3DDISPLAYMODE currentDisplayMode; |
| 1147 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1148 | |
| 1149 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 1150 | } |
| 1151 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1152 | bool Renderer9::getLuminanceAlphaTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1153 | { |
| 1154 | D3DDISPLAYMODE currentDisplayMode; |
| 1155 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1156 | |
| 1157 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 1158 | } |
| 1159 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1160 | bool Renderer9::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1161 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1162 | return mSupportsTextureFilterAnisotropy; |
| 1163 | } |
| 1164 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1165 | float Renderer9::getTextureMaxAnisotropy() const |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1166 | { |
| 1167 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1168 | { |
| 1169 | return mDeviceCaps.MaxAnisotropy; |
| 1170 | } |
| 1171 | return 1.0f; |
| 1172 | } |
| 1173 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1174 | bool Renderer9::getEventQuerySupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1175 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1176 | IDirect3DQuery9 *query = allocateEventQuery(); |
| 1177 | if (query) |
| 1178 | { |
| 1179 | freeEventQuery(query); |
| 1180 | return true; |
| 1181 | } |
| 1182 | else |
| 1183 | { |
| 1184 | return false; |
| 1185 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1186 | return true; |
| 1187 | } |
| 1188 | |
| 1189 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 1190 | // 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] | 1191 | bool Renderer9::getVertexTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1192 | { |
| 1193 | if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 1194 | { |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | D3DDISPLAYMODE currentDisplayMode; |
| 1199 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1200 | |
| 1201 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 1202 | |
| 1203 | return SUCCEEDED(result); |
| 1204 | } |
| 1205 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1206 | bool Renderer9::getNonPower2TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1207 | { |
| 1208 | return mSupportsNonPower2Textures; |
| 1209 | } |
| 1210 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1211 | bool Renderer9::getOcclusionQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1212 | { |
| 1213 | if (!mDevice) |
| 1214 | { |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | IDirect3DQuery9 *query = NULL; |
| 1219 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query); |
| 1220 | if (SUCCEEDED(result) && query) |
| 1221 | { |
| 1222 | query->Release(); |
| 1223 | return true; |
| 1224 | } |
| 1225 | else |
| 1226 | { |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1231 | bool Renderer9::getInstancingSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1232 | { |
| 1233 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1234 | } |
| 1235 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1236 | bool Renderer9::getShareHandleSupport() const |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1237 | { |
| 1238 | // PIX doesn't seem to support using share handles, so disable them. |
| 1239 | // D3D9_REPLACE |
daniel@transgaming.com | 7cb796e | 2012-10-31 18:46:44 +0000 | [diff] [blame] | 1240 | return (mD3d9Ex != NULL) && !gl::perfActive(); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1241 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1242 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1243 | bool Renderer9::getShaderModel3Support() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1244 | { |
| 1245 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1246 | } |
| 1247 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1248 | float Renderer9::getMaxPointSize() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1249 | { |
| 1250 | return mDeviceCaps.MaxPointSize; |
| 1251 | } |
| 1252 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1253 | int Renderer9::getMaxTextureWidth() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1254 | { |
| 1255 | return (int)mDeviceCaps.MaxTextureWidth; |
| 1256 | } |
| 1257 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1258 | int Renderer9::getMaxTextureHeight() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1259 | { |
| 1260 | return (int)mDeviceCaps.MaxTextureHeight; |
| 1261 | } |
| 1262 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1263 | bool Renderer9::get32BitIndexSupport() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1264 | { |
| 1265 | return mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 1266 | } |
| 1267 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1268 | DWORD Renderer9::getCapsDeclTypes() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1269 | { |
| 1270 | return mDeviceCaps.DeclTypes; |
| 1271 | } |
| 1272 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1273 | int Renderer9::getMinSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1274 | { |
| 1275 | return mMinSwapInterval; |
| 1276 | } |
| 1277 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1278 | int Renderer9::getMaxSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1279 | { |
| 1280 | return mMaxSwapInterval; |
| 1281 | } |
| 1282 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1283 | int Renderer9::getMaxSupportedSamples() const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1284 | { |
| 1285 | return mMaxSupportedSamples; |
| 1286 | } |
| 1287 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1288 | int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1289 | { |
| 1290 | if (requested == 0) |
| 1291 | { |
| 1292 | return requested; |
| 1293 | } |
| 1294 | |
| 1295 | std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); |
| 1296 | if (itr == mMultiSampleSupport.end()) |
| 1297 | { |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 1298 | if (format == D3DFMT_UNKNOWN) |
| 1299 | return 0; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1300 | return -1; |
| 1301 | } |
| 1302 | |
| 1303 | for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) |
| 1304 | { |
| 1305 | if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
| 1306 | { |
| 1307 | return i; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | return -1; |
| 1312 | } |
| 1313 | |
daniel@transgaming.com | a957168 | 2012-11-28 19:33:08 +0000 | [diff] [blame] | 1314 | D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) |
| 1315 | { |
| 1316 | switch (internalformat) |
| 1317 | { |
| 1318 | case GL_DEPTH_COMPONENT16: |
| 1319 | case GL_DEPTH_COMPONENT32_OES: |
| 1320 | case GL_DEPTH24_STENCIL8_OES: |
| 1321 | return D3DFMT_INTZ; |
| 1322 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 1323 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 1324 | return D3DFMT_DXT1; |
| 1325 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 1326 | return D3DFMT_DXT3; |
| 1327 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 1328 | return D3DFMT_DXT5; |
| 1329 | case GL_RGBA32F_EXT: |
| 1330 | case GL_RGB32F_EXT: |
| 1331 | case GL_ALPHA32F_EXT: |
| 1332 | case GL_LUMINANCE32F_EXT: |
| 1333 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 1334 | return D3DFMT_A32B32G32R32F; |
| 1335 | case GL_RGBA16F_EXT: |
| 1336 | case GL_RGB16F_EXT: |
| 1337 | case GL_ALPHA16F_EXT: |
| 1338 | case GL_LUMINANCE16F_EXT: |
| 1339 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 1340 | return D3DFMT_A16B16G16R16F; |
| 1341 | case GL_LUMINANCE8_EXT: |
| 1342 | if (getLuminanceTextureSupport()) |
| 1343 | { |
| 1344 | return D3DFMT_L8; |
| 1345 | } |
| 1346 | break; |
| 1347 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 1348 | if (getLuminanceAlphaTextureSupport()) |
| 1349 | { |
| 1350 | return D3DFMT_A8L8; |
| 1351 | } |
| 1352 | break; |
| 1353 | case GL_RGB8_OES: |
| 1354 | case GL_RGB565: |
| 1355 | return D3DFMT_X8R8G8B8; |
| 1356 | } |
| 1357 | |
| 1358 | return D3DFMT_A8R8G8B8; |
| 1359 | } |
| 1360 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1361 | bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1362 | { |
| 1363 | bool result = false; |
| 1364 | |
| 1365 | if (source && dest) |
| 1366 | { |
| 1367 | int levels = source->levelCount(); |
| 1368 | for (int i = 0; i < levels; ++i) |
| 1369 | { |
| 1370 | IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false); |
| 1371 | IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false); |
| 1372 | |
| 1373 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 1374 | |
| 1375 | if (srcSurf) srcSurf->Release(); |
| 1376 | if (dstSurf) dstSurf->Release(); |
| 1377 | |
| 1378 | if (!result) |
| 1379 | return false; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | return result; |
| 1384 | } |
| 1385 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1386 | bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1387 | { |
| 1388 | bool result = false; |
| 1389 | |
| 1390 | if (source && dest) |
| 1391 | { |
| 1392 | int levels = source->levelCount(); |
| 1393 | for (int f = 0; f < 6; f++) |
| 1394 | { |
| 1395 | for (int i = 0; i < levels; i++) |
| 1396 | { |
| 1397 | IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); |
| 1398 | IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); |
| 1399 | |
| 1400 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 1401 | |
| 1402 | if (srcSurf) srcSurf->Release(); |
| 1403 | if (dstSurf) dstSurf->Release(); |
| 1404 | |
| 1405 | if (!result) |
| 1406 | return false; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | return result; |
| 1412 | } |
| 1413 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1414 | D3DPOOL Renderer9::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1415 | { |
| 1416 | if (mD3d9Ex != NULL) |
| 1417 | { |
| 1418 | return D3DPOOL_DEFAULT; |
| 1419 | } |
| 1420 | else |
| 1421 | { |
| 1422 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 1423 | { |
| 1424 | return D3DPOOL_MANAGED; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | return D3DPOOL_DEFAULT; |
| 1429 | } |
| 1430 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 1431 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 1432 | GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1433 | { |
| 1434 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level); |
| 1435 | } |
| 1436 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 1437 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 1438 | GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1439 | { |
| 1440 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level); |
| 1441 | } |
| 1442 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1443 | bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect, |
| 1444 | bool blitRenderTarget, bool blitDepthStencil) |
| 1445 | { |
| 1446 | endScene(); |
| 1447 | |
| 1448 | if (blitRenderTarget) |
| 1449 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1450 | gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(); |
| 1451 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(); |
| 1452 | RenderTarget9 *readRenderTarget = NULL; |
| 1453 | RenderTarget9 *drawRenderTarget = NULL; |
| 1454 | IDirect3DSurface9* readSurface = NULL; |
| 1455 | IDirect3DSurface9* drawSurface = NULL; |
| 1456 | |
| 1457 | if (readBuffer) |
| 1458 | { |
| 1459 | readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); |
| 1460 | } |
| 1461 | if (drawBuffer) |
| 1462 | { |
| 1463 | drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); |
| 1464 | } |
| 1465 | |
| 1466 | if (readRenderTarget) |
| 1467 | { |
| 1468 | readSurface = readRenderTarget->getSurface(); |
| 1469 | } |
| 1470 | if (drawRenderTarget) |
| 1471 | { |
| 1472 | drawSurface = drawRenderTarget->getSurface(); |
| 1473 | } |
| 1474 | |
| 1475 | if (!readSurface || !drawSurface) |
| 1476 | { |
| 1477 | ERR("Failed to retrieve the render target."); |
| 1478 | return error(GL_OUT_OF_MEMORY, false); |
| 1479 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1480 | |
| 1481 | RECT srcRect, dstRect; |
| 1482 | RECT *srcRectPtr = NULL; |
| 1483 | RECT *dstRectPtr = NULL; |
| 1484 | |
| 1485 | if (readRect) |
| 1486 | { |
| 1487 | srcRect.left = readRect->x; |
| 1488 | srcRect.right = readRect->x + readRect->width; |
| 1489 | srcRect.top = readRect->y; |
| 1490 | srcRect.bottom = readRect->y + readRect->height; |
| 1491 | srcRectPtr = &srcRect; |
| 1492 | } |
| 1493 | |
| 1494 | if (drawRect) |
| 1495 | { |
| 1496 | dstRect.left = drawRect->x; |
| 1497 | dstRect.right = drawRect->x + drawRect->width; |
| 1498 | dstRect.top = drawRect->y; |
| 1499 | dstRect.bottom = drawRect->y + drawRect->height; |
| 1500 | dstRectPtr = &dstRect; |
| 1501 | } |
| 1502 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1503 | HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1504 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1505 | readSurface->Release(); |
| 1506 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (FAILED(result)) |
| 1509 | { |
| 1510 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 1511 | return false; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | if (blitDepthStencil) |
| 1516 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1517 | gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); |
| 1518 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); |
| 1519 | RenderTarget9 *readDepthStencil = NULL; |
| 1520 | RenderTarget9 *drawDepthStencil = NULL; |
| 1521 | IDirect3DSurface9* readSurface = NULL; |
| 1522 | IDirect3DSurface9* drawSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1523 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1524 | if (readBuffer) |
| 1525 | { |
| 1526 | readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); |
| 1527 | } |
| 1528 | if (drawBuffer) |
| 1529 | { |
| 1530 | drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); |
| 1531 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1532 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1533 | if (readDepthStencil) |
| 1534 | { |
| 1535 | readSurface = readDepthStencil->getSurface(); |
| 1536 | } |
| 1537 | if (drawDepthStencil) |
| 1538 | { |
| 1539 | drawSurface = drawDepthStencil->getSurface(); |
| 1540 | } |
| 1541 | |
| 1542 | if (!readSurface || !drawSurface) |
| 1543 | { |
| 1544 | ERR("Failed to retrieve the render target."); |
| 1545 | return error(GL_OUT_OF_MEMORY, false); |
| 1546 | } |
| 1547 | |
| 1548 | HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); |
| 1549 | |
| 1550 | readSurface->Release(); |
| 1551 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1552 | |
| 1553 | if (FAILED(result)) |
| 1554 | { |
| 1555 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 1556 | return false; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | return true; |
| 1561 | } |
| 1562 | |
| 1563 | void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 1564 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 1565 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1566 | RenderTarget9 *renderTarget = NULL; |
| 1567 | IDirect3DSurface9 *surface = NULL; |
| 1568 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 1569 | |
| 1570 | if (colorbuffer) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1571 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1572 | renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); |
| 1573 | } |
| 1574 | |
| 1575 | if (renderTarget) |
| 1576 | { |
| 1577 | surface = renderTarget->getSurface(); |
| 1578 | } |
| 1579 | |
| 1580 | if (!surface) |
| 1581 | { |
| 1582 | // context must be lost |
| 1583 | return; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | D3DSURFACE_DESC desc; |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1587 | surface->GetDesc(&desc); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1588 | |
| 1589 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 1590 | { |
| 1591 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1592 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1593 | return error(GL_OUT_OF_MEMORY); |
| 1594 | } |
| 1595 | |
| 1596 | HRESULT result; |
| 1597 | IDirect3DSurface9 *systemSurface = NULL; |
| 1598 | bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && |
| 1599 | x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && |
| 1600 | desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; |
| 1601 | if (directToPixels) |
| 1602 | { |
| 1603 | // Use the pixels ptr as a shared handle to write directly into client's memory |
| 1604 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 1605 | D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); |
| 1606 | if (FAILED(result)) |
| 1607 | { |
| 1608 | // Try again without the shared handle |
| 1609 | directToPixels = false; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | if (!directToPixels) |
| 1614 | { |
| 1615 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 1616 | D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 1617 | if (FAILED(result)) |
| 1618 | { |
| 1619 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1620 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1621 | return error(GL_OUT_OF_MEMORY); |
| 1622 | } |
| 1623 | } |
| 1624 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1625 | result = mDevice->GetRenderTargetData(surface, systemSurface); |
| 1626 | surface->Release(); |
| 1627 | surface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1628 | |
| 1629 | if (FAILED(result)) |
| 1630 | { |
| 1631 | systemSurface->Release(); |
| 1632 | |
| 1633 | // It turns out that D3D will sometimes produce more error |
| 1634 | // codes than those documented. |
| 1635 | if (gl::checkDeviceLost(result)) |
| 1636 | return error(GL_OUT_OF_MEMORY); |
| 1637 | else |
| 1638 | { |
| 1639 | UNREACHABLE(); |
| 1640 | return; |
| 1641 | } |
| 1642 | |
| 1643 | } |
| 1644 | |
| 1645 | if (directToPixels) |
| 1646 | { |
| 1647 | systemSurface->Release(); |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | RECT rect; |
| 1652 | rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width)); |
| 1653 | rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height)); |
| 1654 | rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width)); |
| 1655 | rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height)); |
| 1656 | |
| 1657 | D3DLOCKED_RECT lock; |
| 1658 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 1659 | |
| 1660 | if (FAILED(result)) |
| 1661 | { |
| 1662 | UNREACHABLE(); |
| 1663 | systemSurface->Release(); |
| 1664 | |
| 1665 | return; // No sensible error to generate |
| 1666 | } |
| 1667 | |
| 1668 | unsigned char *dest = (unsigned char*)pixels; |
| 1669 | unsigned short *dest16 = (unsigned short*)pixels; |
| 1670 | |
| 1671 | unsigned char *source; |
| 1672 | int inputPitch; |
| 1673 | if (packReverseRowOrder) |
| 1674 | { |
| 1675 | source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); |
| 1676 | inputPitch = -lock.Pitch; |
| 1677 | } |
| 1678 | else |
| 1679 | { |
| 1680 | source = (unsigned char*)lock.pBits; |
| 1681 | inputPitch = lock.Pitch; |
| 1682 | } |
| 1683 | |
| 1684 | unsigned int fastPixelSize = 0; |
| 1685 | |
| 1686 | if (desc.Format == D3DFMT_A8R8G8B8 && |
| 1687 | format == GL_BGRA_EXT && |
| 1688 | type == GL_UNSIGNED_BYTE) |
| 1689 | { |
| 1690 | fastPixelSize = 4; |
| 1691 | } |
| 1692 | else if ((desc.Format == D3DFMT_A4R4G4B4 && |
| 1693 | format == GL_BGRA_EXT && |
| 1694 | type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) || |
| 1695 | (desc.Format == D3DFMT_A1R5G5B5 && |
| 1696 | format == GL_BGRA_EXT && |
| 1697 | type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)) |
| 1698 | { |
| 1699 | fastPixelSize = 2; |
| 1700 | } |
| 1701 | else if (desc.Format == D3DFMT_A16B16G16R16F && |
| 1702 | format == GL_RGBA && |
| 1703 | type == GL_HALF_FLOAT_OES) |
| 1704 | { |
| 1705 | fastPixelSize = 8; |
| 1706 | } |
| 1707 | else if (desc.Format == D3DFMT_A32B32G32R32F && |
| 1708 | format == GL_RGBA && |
| 1709 | type == GL_FLOAT) |
| 1710 | { |
| 1711 | fastPixelSize = 16; |
| 1712 | } |
| 1713 | |
| 1714 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 1715 | { |
| 1716 | if (fastPixelSize != 0) |
| 1717 | { |
| 1718 | // Fast path for formats which require no translation: |
| 1719 | // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE |
| 1720 | // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT |
| 1721 | // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT |
| 1722 | // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES |
| 1723 | // D3DFMT_A32B32G32R32F to RGBA/FLOAT |
| 1724 | // |
| 1725 | // Note that buffers with no alpha go through the slow path below. |
| 1726 | memcpy(dest + j * outputPitch, |
| 1727 | source + j * inputPitch, |
| 1728 | (rect.right - rect.left) * fastPixelSize); |
| 1729 | continue; |
| 1730 | } |
| 1731 | |
| 1732 | for (int i = 0; i < rect.right - rect.left; i++) |
| 1733 | { |
| 1734 | float r; |
| 1735 | float g; |
| 1736 | float b; |
| 1737 | float a; |
| 1738 | |
| 1739 | switch (desc.Format) |
| 1740 | { |
| 1741 | case D3DFMT_R5G6B5: |
| 1742 | { |
| 1743 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 1744 | |
| 1745 | a = 1.0f; |
| 1746 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 1747 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 1748 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 1749 | } |
| 1750 | break; |
| 1751 | case D3DFMT_A1R5G5B5: |
| 1752 | { |
| 1753 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 1754 | |
| 1755 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 1756 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 1757 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 1758 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 1759 | } |
| 1760 | break; |
| 1761 | case D3DFMT_A8R8G8B8: |
| 1762 | { |
| 1763 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 1764 | |
| 1765 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 1766 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1767 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1768 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1769 | } |
| 1770 | break; |
| 1771 | case D3DFMT_X8R8G8B8: |
| 1772 | { |
| 1773 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 1774 | |
| 1775 | a = 1.0f; |
| 1776 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1777 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1778 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1779 | } |
| 1780 | break; |
| 1781 | case D3DFMT_A2R10G10B10: |
| 1782 | { |
| 1783 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 1784 | |
| 1785 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 1786 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 1787 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 1788 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 1789 | } |
| 1790 | break; |
| 1791 | case D3DFMT_A32B32G32R32F: |
| 1792 | { |
| 1793 | // float formats in D3D are stored rgba, rather than the other way round |
| 1794 | r = *((float*)(source + 16 * i + j * inputPitch) + 0); |
| 1795 | g = *((float*)(source + 16 * i + j * inputPitch) + 1); |
| 1796 | b = *((float*)(source + 16 * i + j * inputPitch) + 2); |
| 1797 | a = *((float*)(source + 16 * i + j * inputPitch) + 3); |
| 1798 | } |
| 1799 | break; |
| 1800 | case D3DFMT_A16B16G16R16F: |
| 1801 | { |
| 1802 | // float formats in D3D are stored rgba, rather than the other way round |
| 1803 | r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0)); |
| 1804 | g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1)); |
| 1805 | b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2)); |
| 1806 | a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3)); |
| 1807 | } |
| 1808 | break; |
| 1809 | default: |
| 1810 | UNIMPLEMENTED(); // FIXME |
| 1811 | UNREACHABLE(); |
| 1812 | return; |
| 1813 | } |
| 1814 | |
| 1815 | switch (format) |
| 1816 | { |
| 1817 | case GL_RGBA: |
| 1818 | switch (type) |
| 1819 | { |
| 1820 | case GL_UNSIGNED_BYTE: |
| 1821 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 1822 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 1823 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 1824 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 1825 | break; |
| 1826 | default: UNREACHABLE(); |
| 1827 | } |
| 1828 | break; |
| 1829 | case GL_BGRA_EXT: |
| 1830 | switch (type) |
| 1831 | { |
| 1832 | case GL_UNSIGNED_BYTE: |
| 1833 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); |
| 1834 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 1835 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); |
| 1836 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 1837 | break; |
| 1838 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 1839 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 1840 | // this type is packed as follows: |
| 1841 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 1842 | // -------------------------------------------------------------------------------- |
| 1843 | // | 4th | 3rd | 2nd | 1st component | |
| 1844 | // -------------------------------------------------------------------------------- |
| 1845 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 1846 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 1847 | ((unsigned short)(15 * a + 0.5f) << 12)| |
| 1848 | ((unsigned short)(15 * r + 0.5f) << 8) | |
| 1849 | ((unsigned short)(15 * g + 0.5f) << 4) | |
| 1850 | ((unsigned short)(15 * b + 0.5f) << 0); |
| 1851 | break; |
| 1852 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 1853 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 1854 | // this type is packed as follows: |
| 1855 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 1856 | // -------------------------------------------------------------------------------- |
| 1857 | // | 4th | 3rd | 2nd | 1st component | |
| 1858 | // -------------------------------------------------------------------------------- |
| 1859 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 1860 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 1861 | ((unsigned short)( a + 0.5f) << 15) | |
| 1862 | ((unsigned short)(31 * r + 0.5f) << 10) | |
| 1863 | ((unsigned short)(31 * g + 0.5f) << 5) | |
| 1864 | ((unsigned short)(31 * b + 0.5f) << 0); |
| 1865 | break; |
| 1866 | default: UNREACHABLE(); |
| 1867 | } |
| 1868 | break; |
| 1869 | case GL_RGB: |
| 1870 | switch (type) |
| 1871 | { |
| 1872 | case GL_UNSIGNED_SHORT_5_6_5: |
| 1873 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 1874 | ((unsigned short)(31 * b + 0.5f) << 0) | |
| 1875 | ((unsigned short)(63 * g + 0.5f) << 5) | |
| 1876 | ((unsigned short)(31 * r + 0.5f) << 11); |
| 1877 | break; |
| 1878 | case GL_UNSIGNED_BYTE: |
| 1879 | dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 1880 | dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 1881 | dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 1882 | break; |
| 1883 | default: UNREACHABLE(); |
| 1884 | } |
| 1885 | break; |
| 1886 | default: UNREACHABLE(); |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | systemSurface->UnlockRect(); |
| 1892 | |
| 1893 | systemSurface->Release(); |
| 1894 | } |
| 1895 | |
| 1896 | bool Renderer9::setRenderTarget(gl::Renderbuffer *renderbuffer) |
| 1897 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1898 | IDirect3DSurface9 *renderTargetSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1899 | |
| 1900 | if (renderbuffer) |
| 1901 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1902 | RenderTarget *renderTarget = renderbuffer->getRenderTarget(); |
| 1903 | if (renderTarget) |
| 1904 | { |
| 1905 | renderTargetSurface = renderTarget->getSurface(); |
| 1906 | } |
| 1907 | |
| 1908 | if (!renderTargetSurface) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1909 | { |
| 1910 | ERR("render target pointer unexpectedly null."); |
| 1911 | return false; // Context must be lost |
| 1912 | } |
| 1913 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1914 | mDevice->SetRenderTarget(0, renderTargetSurface); |
| 1915 | renderTargetSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | mDevice->SetRenderTarget(0, NULL); |
| 1920 | } |
| 1921 | |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
| 1925 | bool Renderer9::setDepthStencil(gl::Renderbuffer *renderbuffer) |
| 1926 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1927 | IDirect3DSurface9 *depthStencilSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (renderbuffer) |
| 1930 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1931 | RenderTarget *depthStencil = renderbuffer->getDepthStencil(); |
| 1932 | |
| 1933 | if (depthStencil) |
| 1934 | { |
| 1935 | depthStencilSurface = depthStencil->getSurface(); |
| 1936 | } |
| 1937 | |
| 1938 | if (!depthStencilSurface) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1939 | { |
| 1940 | ERR("depth stencil pointer unexpectedly null."); |
| 1941 | return false; // Context must be lost |
| 1942 | } |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 1943 | |
| 1944 | mDevice->SetDepthStencilSurface(depthStencilSurface); |
| 1945 | depthStencilSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 1946 | } |
| 1947 | else |
| 1948 | { |
| 1949 | mDevice->SetDepthStencilSurface(NULL); |
| 1950 | } |
| 1951 | return true; |
| 1952 | } |
| 1953 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 1954 | bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 1955 | { |
| 1956 | return mBlit->boxFilter(source, dest); |
| 1957 | } |
| 1958 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1959 | D3DPOOL Renderer9::getTexturePool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1960 | { |
| 1961 | if (mD3d9Ex != NULL) |
| 1962 | { |
| 1963 | return D3DPOOL_DEFAULT; |
| 1964 | } |
| 1965 | else |
| 1966 | { |
| 1967 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 1968 | { |
| 1969 | return D3DPOOL_MANAGED; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | return D3DPOOL_DEFAULT; |
| 1974 | } |
| 1975 | |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1976 | bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) |
| 1977 | { |
| 1978 | if (source && dest) |
| 1979 | { |
| 1980 | HRESULT result = D3DERR_OUTOFVIDEOMEMORY; |
| 1981 | IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE |
| 1982 | |
| 1983 | if (fromManaged) |
| 1984 | { |
| 1985 | D3DSURFACE_DESC desc; |
| 1986 | source->GetDesc(&desc); |
| 1987 | |
| 1988 | IDirect3DSurface9 *surf = 0; |
| 1989 | result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); |
| 1990 | |
| 1991 | if (SUCCEEDED(result)) |
| 1992 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 1993 | Image::CopyLockableSurfaces(surf, source); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 1994 | result = device->UpdateSurface(surf, NULL, dest, NULL); |
| 1995 | surf->Release(); |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | { |
| 2000 | endScene(); |
| 2001 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 2002 | } |
| 2003 | |
| 2004 | if (FAILED(result)) |
| 2005 | { |
| 2006 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 2007 | return false; |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | return true; |
| 2012 | } |
| 2013 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2014 | } |