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" |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 13 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 14 | #include "libGLESv2/Framebuffer.h" |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 15 | #include "libGLESv2/Program.h" |
| 16 | #include "libGLESv2/ProgramBinary.h" |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 17 | #include "libGLESv2/IndexDataManager.h" |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 18 | #include "libGLESv2/VertexDataManager.h" |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/Renderer9.h" |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame] | 20 | #include "libGLESv2/renderer/renderer9_utils.h" |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 21 | #include "libGLESv2/renderer/SwapChain9.h" |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 22 | #include "libGLESv2/renderer/TextureStorage.h" |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 23 | #include "libGLESv2/renderer/Image.h" |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 24 | #include "libGLESv2/renderer/Blit.h" |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 25 | #include "libGLESv2/renderer/RenderTarget9.h" |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 26 | |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 27 | #include "libEGL/Config.h" |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 28 | #include "libEGL/Display.h" |
| 29 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 30 | // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros |
| 31 | #define REF_RAST 0 |
| 32 | |
| 33 | // The "Debug This Pixel..." feature in PIX often fails when using the |
| 34 | // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 |
| 35 | // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. |
| 36 | #if !defined(ANGLE_ENABLE_D3D9EX) |
| 37 | // Enables use of the IDirect3D9Ex interface, when available |
| 38 | #define ANGLE_ENABLE_D3D9EX 1 |
| 39 | #endif // !defined(ANGLE_ENABLE_D3D9EX) |
| 40 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 41 | namespace rx |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 42 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 43 | static const D3DFORMAT RenderTargetFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 44 | { |
| 45 | D3DFMT_A1R5G5B5, |
| 46 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 47 | D3DFMT_A8R8G8B8, |
| 48 | D3DFMT_R5G6B5, |
| 49 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
| 50 | D3DFMT_X8R8G8B8 |
| 51 | }; |
| 52 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 53 | static const D3DFORMAT DepthStencilFormats[] = |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 54 | { |
| 55 | D3DFMT_UNKNOWN, |
| 56 | // D3DFMT_D16_LOCKABLE, |
| 57 | D3DFMT_D32, |
| 58 | // D3DFMT_D15S1, |
| 59 | D3DFMT_D24S8, |
| 60 | D3DFMT_D24X8, |
| 61 | // D3DFMT_D24X4S4, |
| 62 | D3DFMT_D16, |
| 63 | // D3DFMT_D32F_LOCKABLE, |
| 64 | // D3DFMT_D24FS8 |
| 65 | }; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 66 | |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 67 | 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] | 68 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 69 | mD3d9Module = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 70 | |
daniel@transgaming.com | 7d738a2 | 2012-11-28 19:43:08 +0000 | [diff] [blame] | 71 | mD3dCompilerModule = NULL; |
| 72 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 73 | mD3d9 = NULL; |
| 74 | mD3d9Ex = NULL; |
| 75 | mDevice = NULL; |
| 76 | mDeviceEx = NULL; |
| 77 | mDeviceWindow = NULL; |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 78 | mBlit = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 79 | |
| 80 | mAdapter = D3DADAPTER_DEFAULT; |
| 81 | |
| 82 | #if REF_RAST == 1 || defined(FORCE_REF_RAST) |
| 83 | mDeviceType = D3DDEVTYPE_REF; |
| 84 | #else |
| 85 | mDeviceType = D3DDEVTYPE_HAL; |
| 86 | #endif |
| 87 | |
| 88 | mDeviceLost = false; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 89 | |
| 90 | mMaxSupportedSamples = 0; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 91 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 92 | mAppliedIBSerial = 0; |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 93 | |
| 94 | mMaskedClearSavedState = NULL; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 95 | |
| 96 | mVertexDataManager = NULL; |
| 97 | mIndexDataManager = NULL; |
| 98 | mLineLoopIB = NULL; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 101 | Renderer9::~Renderer9() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 102 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 103 | releaseDeviceResources(); |
| 104 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 105 | if (mDevice) |
| 106 | { |
| 107 | // 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] | 108 | if (testDeviceLost(false)) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 109 | { |
| 110 | resetDevice(); |
| 111 | } |
| 112 | |
| 113 | mDevice->Release(); |
| 114 | mDevice = NULL; |
| 115 | } |
| 116 | |
| 117 | if (mDeviceEx) |
| 118 | { |
| 119 | mDeviceEx->Release(); |
| 120 | mDeviceEx = NULL; |
| 121 | } |
| 122 | |
| 123 | if (mD3d9) |
| 124 | { |
| 125 | mD3d9->Release(); |
| 126 | mD3d9 = NULL; |
| 127 | } |
| 128 | |
| 129 | if (mDeviceWindow) |
| 130 | { |
| 131 | DestroyWindow(mDeviceWindow); |
| 132 | mDeviceWindow = NULL; |
| 133 | } |
| 134 | |
| 135 | if (mD3d9Ex) |
| 136 | { |
| 137 | mD3d9Ex->Release(); |
| 138 | mD3d9Ex = NULL; |
| 139 | } |
| 140 | |
| 141 | if (mD3d9Module) |
| 142 | { |
| 143 | mD3d9Module = NULL; |
| 144 | } |
| 145 | |
daniel@transgaming.com | 7d738a2 | 2012-11-28 19:43:08 +0000 | [diff] [blame] | 146 | if (mD3dCompilerModule) |
| 147 | { |
| 148 | FreeLibrary(mD3dCompilerModule); |
| 149 | mD3dCompilerModule = NULL; |
| 150 | } |
| 151 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 152 | while (!mMultiSampleSupport.empty()) |
| 153 | { |
| 154 | delete [] mMultiSampleSupport.begin()->second; |
| 155 | mMultiSampleSupport.erase(mMultiSampleSupport.begin()); |
| 156 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 157 | } |
| 158 | |
daniel@transgaming.com | b64ed28 | 2012-11-28 20:54:02 +0000 | [diff] [blame] | 159 | Renderer9 *Renderer9::makeRenderer9(Renderer *renderer) |
| 160 | { |
| 161 | ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL); |
| 162 | return static_cast<rx::Renderer9*>(renderer); |
| 163 | } |
| 164 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 165 | EGLint Renderer9::initialize() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 166 | { |
daniel@transgaming.com | 95ffbc1 | 2012-10-31 19:55:27 +0000 | [diff] [blame] | 167 | if (mSoftwareDevice) |
| 168 | { |
| 169 | mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); |
| 174 | } |
| 175 | |
| 176 | if (mD3d9Module == NULL) |
| 177 | { |
| 178 | ERR("No D3D9 module found - aborting!\n"); |
| 179 | return EGL_NOT_INITIALIZED; |
| 180 | } |
| 181 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 182 | typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); |
| 183 | Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); |
| 184 | |
| 185 | // Use Direct3D9Ex if available. Among other things, this version is less |
| 186 | // inclined to report a lost context, for example when the user switches |
| 187 | // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. |
| 188 | if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) |
| 189 | { |
| 190 | ASSERT(mD3d9Ex); |
| 191 | mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); |
| 192 | ASSERT(mD3d9); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 197 | } |
| 198 | |
| 199 | if (!mD3d9) |
| 200 | { |
| 201 | ERR("Could not create D3D9 device - aborting!\n"); |
| 202 | return EGL_NOT_INITIALIZED; |
| 203 | } |
daniel@transgaming.com | 7d738a2 | 2012-11-28 19:43:08 +0000 | [diff] [blame] | 204 | |
| 205 | #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) |
| 206 | // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. |
| 207 | static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; |
| 208 | |
| 209 | for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i) |
| 210 | { |
| 211 | if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule)) |
| 212 | { |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | #else |
| 217 | // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. |
| 218 | mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL); |
| 219 | #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES |
| 220 | |
| 221 | if (!mD3dCompilerModule) |
| 222 | { |
| 223 | terminate(); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | mD3DCompileFunc = reinterpret_cast<D3DCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile")); |
| 228 | ASSERT(mD3DCompileFunc); |
| 229 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 230 | if (mDc != NULL) |
| 231 | { |
| 232 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
| 233 | } |
| 234 | |
| 235 | HRESULT result; |
| 236 | |
| 237 | // Give up on getting device caps after about one second. |
| 238 | for (int i = 0; i < 10; ++i) |
| 239 | { |
| 240 | result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); |
| 241 | if (SUCCEEDED(result)) |
| 242 | { |
| 243 | break; |
| 244 | } |
| 245 | else if (result == D3DERR_NOTAVAILABLE) |
| 246 | { |
| 247 | Sleep(100); // Give the driver some time to initialize/recover |
| 248 | } |
| 249 | else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from |
| 250 | { |
| 251 | ERR("failed to get device caps (0x%x)\n", result); |
| 252 | return EGL_NOT_INITIALIZED; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
| 257 | { |
| 258 | ERR("Renderer does not support PS 2.0. aborting!\n"); |
| 259 | return EGL_NOT_INITIALIZED; |
| 260 | } |
| 261 | |
| 262 | // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. |
| 263 | // This is required by Texture2D::convertToRenderTarget. |
| 264 | if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) |
| 265 | { |
| 266 | ERR("Renderer does not support stretctrect from textures!\n"); |
| 267 | return EGL_NOT_INITIALIZED; |
| 268 | } |
| 269 | |
| 270 | mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); |
| 271 | |
| 272 | // ATI cards on XP have problems with non-power-of-two textures. |
| 273 | mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && |
| 274 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && |
| 275 | !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && |
| 276 | !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); |
| 277 | |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 278 | // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec |
| 279 | mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); |
| 280 | |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 281 | mMinSwapInterval = 4; |
| 282 | mMaxSwapInterval = 0; |
| 283 | |
| 284 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) |
| 285 | { |
| 286 | mMinSwapInterval = std::min(mMinSwapInterval, 0); |
| 287 | mMaxSwapInterval = std::max(mMaxSwapInterval, 0); |
| 288 | } |
| 289 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) |
| 290 | { |
| 291 | mMinSwapInterval = std::min(mMinSwapInterval, 1); |
| 292 | mMaxSwapInterval = std::max(mMaxSwapInterval, 1); |
| 293 | } |
| 294 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) |
| 295 | { |
| 296 | mMinSwapInterval = std::min(mMinSwapInterval, 2); |
| 297 | mMaxSwapInterval = std::max(mMaxSwapInterval, 2); |
| 298 | } |
| 299 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) |
| 300 | { |
| 301 | mMinSwapInterval = std::min(mMinSwapInterval, 3); |
| 302 | mMaxSwapInterval = std::max(mMaxSwapInterval, 3); |
| 303 | } |
| 304 | if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) |
| 305 | { |
| 306 | mMinSwapInterval = std::min(mMinSwapInterval, 4); |
| 307 | mMaxSwapInterval = std::max(mMaxSwapInterval, 4); |
| 308 | } |
| 309 | |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 310 | int max = 0; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 311 | for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 312 | { |
| 313 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 314 | getMultiSampleSupport(RenderTargetFormats[i], multisampleArray); |
| 315 | mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray; |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 316 | |
| 317 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 318 | { |
| 319 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 320 | { |
| 321 | max = j; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 326 | for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 327 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 328 | if (DepthStencilFormats[i] == D3DFMT_UNKNOWN) |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 329 | continue; |
| 330 | |
| 331 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 332 | getMultiSampleSupport(DepthStencilFormats[i], multisampleArray); |
| 333 | mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 334 | |
| 335 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 336 | { |
| 337 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 338 | { |
| 339 | max = j; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | mMaxSupportedSamples = max; |
| 345 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 346 | static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); |
| 347 | static const TCHAR className[] = TEXT("STATIC"); |
| 348 | |
| 349 | mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); |
| 350 | |
| 351 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 352 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 353 | |
| 354 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
| 355 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) |
| 356 | { |
| 357 | return EGL_BAD_ALLOC; |
| 358 | } |
| 359 | |
| 360 | if (FAILED(result)) |
| 361 | { |
| 362 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
| 363 | |
| 364 | if (FAILED(result)) |
| 365 | { |
| 366 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); |
| 367 | return EGL_BAD_ALLOC; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (mD3d9Ex) |
| 372 | { |
| 373 | result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); |
| 374 | ASSERT(SUCCEEDED(result)); |
| 375 | } |
| 376 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 377 | mVertexShaderCache.initialize(mDevice); |
| 378 | mPixelShaderCache.initialize(mDevice); |
| 379 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 380 | initializeDevice(); |
| 381 | |
| 382 | return EGL_SUCCESS; |
| 383 | } |
| 384 | |
| 385 | // do any one-time device initialization |
| 386 | // NOTE: this is also needed after a device lost/reset |
| 387 | // 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] | 388 | void Renderer9::initializeDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 389 | { |
| 390 | // Permanent non-default states |
| 391 | mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); |
| 392 | mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); |
| 393 | |
| 394 | if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) |
| 395 | { |
| 396 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f |
| 401 | } |
| 402 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 403 | markAllStateDirty(); |
| 404 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 405 | mSceneStarted = false; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 406 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 407 | ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager); |
daniel@transgaming.com | e569fc5 | 2012-11-28 20:56:02 +0000 | [diff] [blame] | 408 | mBlit = new Blit(this); |
| 409 | mVertexDataManager = new gl::VertexDataManager(this); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 410 | mIndexDataManager = new gl::IndexDataManager(this); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 411 | } |
| 412 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 413 | D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 414 | { |
| 415 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 416 | |
| 417 | // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. |
| 418 | presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; |
| 419 | presentParameters.BackBufferCount = 1; |
| 420 | presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; |
| 421 | presentParameters.BackBufferWidth = 1; |
| 422 | presentParameters.BackBufferHeight = 1; |
| 423 | presentParameters.EnableAutoDepthStencil = FALSE; |
| 424 | presentParameters.Flags = 0; |
| 425 | presentParameters.hDeviceWindow = mDeviceWindow; |
| 426 | presentParameters.MultiSampleQuality = 0; |
| 427 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; |
| 428 | presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 429 | presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 430 | presentParameters.Windowed = TRUE; |
| 431 | |
| 432 | return presentParameters; |
| 433 | } |
| 434 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 435 | int Renderer9::generateConfigs(ConfigDesc **configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 436 | { |
| 437 | D3DDISPLAYMODE currentDisplayMode; |
| 438 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 439 | |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 440 | unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]); |
| 441 | unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 442 | (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; |
| 443 | int numConfigs = 0; |
| 444 | |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 445 | for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 446 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 447 | D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 448 | |
| 449 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 450 | |
| 451 | if (SUCCEEDED(result)) |
| 452 | { |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 453 | for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 454 | { |
daniel@transgaming.com | 222ee08 | 2012-11-28 19:31:49 +0000 | [diff] [blame] | 455 | D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 456 | HRESULT result = D3D_OK; |
| 457 | |
| 458 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 459 | { |
| 460 | result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 461 | } |
| 462 | |
| 463 | if (SUCCEEDED(result)) |
| 464 | { |
| 465 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 466 | { |
| 467 | result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
| 468 | } |
| 469 | |
| 470 | if (SUCCEEDED(result)) |
| 471 | { |
| 472 | ConfigDesc newConfig; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 473 | newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat); |
| 474 | newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat); |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 475 | newConfig.multiSample = 0; // FIXME: enumerate multi-sampling |
| 476 | newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat); |
| 477 | |
| 478 | (*configDescList)[numConfigs++] = newConfig; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return numConfigs; |
| 486 | } |
| 487 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 488 | void Renderer9::deleteConfigs(ConfigDesc *configDescList) |
daniel@transgaming.com | 3281f97 | 2012-10-31 18:38:51 +0000 | [diff] [blame] | 489 | { |
| 490 | delete [] (configDescList); |
| 491 | } |
| 492 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 493 | void Renderer9::startScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 494 | { |
| 495 | if (!mSceneStarted) |
| 496 | { |
| 497 | long result = mDevice->BeginScene(); |
| 498 | if (SUCCEEDED(result)) { |
| 499 | // This is defensive checking against the device being |
| 500 | // lost at unexpected times. |
| 501 | mSceneStarted = true; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 506 | void Renderer9::endScene() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 507 | { |
| 508 | if (mSceneStarted) |
| 509 | { |
| 510 | // EndScene can fail if the device was lost, for example due |
| 511 | // to a TDR during a draw call. |
| 512 | mDevice->EndScene(); |
| 513 | mSceneStarted = false; |
| 514 | } |
| 515 | } |
| 516 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 517 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 518 | void Renderer9::sync(bool block) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 519 | { |
| 520 | HRESULT result; |
| 521 | |
| 522 | IDirect3DQuery9* query = allocateEventQuery(); |
| 523 | if (!query) |
| 524 | { |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | result = query->Issue(D3DISSUE_END); |
| 529 | ASSERT(SUCCEEDED(result)); |
| 530 | |
| 531 | do |
| 532 | { |
| 533 | result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); |
| 534 | |
| 535 | if(block && result == S_FALSE) |
| 536 | { |
| 537 | // Keep polling, but allow other threads to do something useful first |
| 538 | Sleep(0); |
| 539 | // explicitly check for device loss |
| 540 | // some drivers seem to return S_FALSE even if the device is lost |
| 541 | // instead of D3DERR_DEVICELOST like they should |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 542 | if (testDeviceLost(false)) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 543 | { |
| 544 | result = D3DERR_DEVICELOST; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | while(block && result == S_FALSE); |
| 549 | |
| 550 | freeEventQuery(query); |
| 551 | |
| 552 | if (isDeviceLostError(result)) |
| 553 | { |
| 554 | mDisplay->notifyDeviceLost(); |
| 555 | } |
| 556 | } |
| 557 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 558 | SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) |
| 559 | { |
daniel@transgaming.com | a27e05b | 2012-11-28 19:39:42 +0000 | [diff] [blame] | 560 | return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat); |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 561 | } |
| 562 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 563 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 564 | IDirect3DQuery9* Renderer9::allocateEventQuery() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 565 | { |
| 566 | IDirect3DQuery9 *query = NULL; |
| 567 | |
| 568 | if (mEventQueryPool.empty()) |
| 569 | { |
| 570 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); |
| 571 | ASSERT(SUCCEEDED(result)); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | query = mEventQueryPool.back(); |
| 576 | mEventQueryPool.pop_back(); |
| 577 | } |
| 578 | |
| 579 | return query; |
| 580 | } |
| 581 | |
| 582 | // D3D9_REPLACE |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 583 | void Renderer9::freeEventQuery(IDirect3DQuery9* query) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 584 | { |
| 585 | if (mEventQueryPool.size() > 1000) |
| 586 | { |
| 587 | query->Release(); |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | mEventQueryPool.push_back(query); |
| 592 | } |
| 593 | } |
| 594 | |
daniel@transgaming.com | 7d738a2 | 2012-11-28 19:43:08 +0000 | [diff] [blame] | 595 | |
| 596 | HRESULT Renderer9::compileShaderSource(const char* hlsl, const char* sourceName, const char* profile, DWORD flags, ID3DBlob** binary, ID3DBlob** errorMessage) |
| 597 | { |
| 598 | return mD3DCompileFunc(hlsl, strlen(hlsl), sourceName, NULL, NULL, "main", profile, flags, 0, binary, errorMessage); |
| 599 | } |
| 600 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 601 | IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 602 | { |
| 603 | return mVertexShaderCache.create(function, length); |
| 604 | } |
| 605 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 606 | IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 607 | { |
| 608 | return mPixelShaderCache.create(function, length); |
| 609 | } |
| 610 | |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 611 | HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) |
| 612 | { |
| 613 | D3DPOOL Pool = getBufferPool(Usage); |
| 614 | return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); |
| 615 | } |
| 616 | |
| 617 | HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) |
| 618 | { |
| 619 | D3DPOOL Pool = getBufferPool(Usage); |
| 620 | return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); |
| 621 | } |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 622 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 623 | 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] | 624 | { |
| 625 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 626 | int d3dSampler = index + d3dSamplerOffset; |
| 627 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 628 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); |
| 629 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 630 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 631 | 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] | 632 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 633 | gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 634 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 635 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 636 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 637 | if (mSupportsTextureFilterAnisotropy) |
| 638 | { |
| 639 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 640 | } |
| 641 | } |
| 642 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 643 | void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 644 | { |
| 645 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 646 | int d3dSampler = index + d3dSamplerOffset; |
| 647 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
| 648 | |
| 649 | if (texture) |
| 650 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 651 | TextureStorage *texStorage = texture->getNativeTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 652 | if (texStorage) |
| 653 | { |
| 654 | d3dTexture = texStorage->getBaseTexture(); |
| 655 | } |
| 656 | // If we get NULL back from getBaseTexture here, something went wrong |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 657 | // in the texture class and we're unexpectedly missing the d3d texture |
| 658 | ASSERT(d3dTexture != NULL); |
| 659 | } |
| 660 | |
| 661 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 662 | } |
| 663 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 664 | void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize) |
| 665 | { |
| 666 | bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; |
| 667 | bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize; |
| 668 | |
| 669 | if (rasterStateChanged) |
| 670 | { |
| 671 | // Set the cull mode |
| 672 | if (rasterState.cullFace) |
| 673 | { |
| 674 | mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 679 | } |
| 680 | |
| 681 | mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE); |
| 682 | |
| 683 | mCurRasterState = rasterState; |
| 684 | } |
| 685 | |
| 686 | if (rasterStateChanged || depthSizeChanged) |
| 687 | { |
| 688 | if (rasterState.polygonOffsetFill) |
| 689 | { |
| 690 | if (depthSize > 0) |
| 691 | { |
| 692 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); |
| 693 | |
| 694 | float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 695 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); |
| 696 | } |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 701 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 702 | } |
| 703 | |
| 704 | mCurDepthSize = depthSize; |
| 705 | } |
| 706 | |
| 707 | mForceSetRasterState = false; |
| 708 | } |
| 709 | |
| 710 | void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) |
| 711 | { |
| 712 | bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; |
| 713 | bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; |
| 714 | bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; |
| 715 | |
| 716 | if (blendStateChanged || blendColorChanged) |
| 717 | { |
| 718 | if (blendState.blend) |
| 719 | { |
| 720 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 721 | |
| 722 | if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 723 | blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 724 | { |
| 725 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), |
| 730 | gl::unorm<8>(blendColor.alpha), |
| 731 | gl::unorm<8>(blendColor.alpha), |
| 732 | gl::unorm<8>(blendColor.alpha))); |
| 733 | } |
| 734 | |
| 735 | mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); |
| 736 | mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); |
| 737 | mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); |
| 738 | |
| 739 | if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || |
| 740 | blendState.destBlendRGB != blendState.destBlendAlpha || |
| 741 | blendState.blendEquationRGB != blendState.blendEquationAlpha) |
| 742 | { |
| 743 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 744 | |
| 745 | mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); |
| 746 | mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); |
| 747 | mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 752 | } |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 757 | } |
| 758 | |
| 759 | if (blendState.sampleAlphaToCoverage) |
| 760 | { |
| 761 | FIXME("Sample alpha to coverage is unimplemented."); |
| 762 | } |
| 763 | |
| 764 | // Set the color mask |
| 765 | bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; |
| 766 | // Apparently some ATI cards have a bug where a draw with a zero color |
| 767 | // write mask can cause later draws to have incorrect results. Instead, |
| 768 | // set a nonzero color write mask but modify the blend state so that no |
| 769 | // drawing is done. |
| 770 | // http://code.google.com/p/angleproject/issues/detail?id=169 |
| 771 | |
| 772 | DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, |
| 773 | blendState.colorMaskBlue, blendState.colorMaskAlpha); |
| 774 | if (colorMask == 0 && !zeroColorMaskAllowed) |
| 775 | { |
| 776 | // Enable green channel, but set blending so nothing will be drawn. |
| 777 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); |
| 778 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 779 | |
| 780 | mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); |
| 781 | mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); |
| 782 | mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); |
| 787 | } |
| 788 | |
| 789 | mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); |
| 790 | |
| 791 | mCurBlendState = blendState; |
| 792 | mCurBlendColor = blendColor; |
| 793 | } |
| 794 | |
| 795 | if (sampleMaskChanged) |
| 796 | { |
| 797 | // Set the multisample mask |
| 798 | mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 799 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); |
| 800 | |
| 801 | mCurSampleMask = sampleMask; |
| 802 | } |
| 803 | |
| 804 | mForceSetBlendState = false; |
| 805 | } |
| 806 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 807 | void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
| 808 | int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 809 | { |
| 810 | bool depthStencilStateChanged = mForceSetDepthStencilState || |
| 811 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 812 | bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || |
| 813 | stencilBackRef != mCurStencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 814 | bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; |
| 815 | bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize; |
| 816 | |
| 817 | if (depthStencilStateChanged) |
| 818 | { |
| 819 | if (depthStencilState.depthTest) |
| 820 | { |
| 821 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 822 | mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 827 | } |
| 828 | |
| 829 | mCurDepthStencilState = depthStencilState; |
| 830 | } |
| 831 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 832 | if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 833 | { |
| 834 | if (depthStencilState.stencilTest && stencilSize > 0) |
| 835 | { |
| 836 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 837 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 838 | |
| 839 | // FIXME: Unsupported by D3D9 |
| 840 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 841 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 842 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 843 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 844 | stencilRef != stencilBackRef || |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 845 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 846 | { |
| 847 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 848 | return error(GL_INVALID_OPERATION); |
| 849 | } |
| 850 | |
| 851 | // get the maximum size of the stencil ref |
| 852 | GLuint maxStencil = (1 << stencilSize) - 1; |
| 853 | |
| 854 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 855 | depthStencilState.stencilWritemask); |
| 856 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 857 | gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); |
| 858 | |
| 859 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 860 | (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 861 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 862 | depthStencilState.stencilMask); |
| 863 | |
| 864 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 865 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); |
| 866 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 867 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); |
| 868 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 869 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); |
| 870 | |
| 871 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 872 | depthStencilState.stencilBackWritemask); |
| 873 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 874 | gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); |
| 875 | |
| 876 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 877 | (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 878 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 879 | depthStencilState.stencilBackMask); |
| 880 | |
| 881 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 882 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); |
| 883 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 884 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); |
| 885 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 886 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 891 | } |
| 892 | |
| 893 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); |
| 894 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 895 | mCurStencilRef = stencilRef; |
| 896 | mCurStencilBackRef = stencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 897 | mCurFrontFaceCCW = frontFaceCCW; |
| 898 | mCurStencilSize = stencilSize; |
| 899 | } |
| 900 | |
| 901 | mForceSetDepthStencilState = false; |
| 902 | } |
| 903 | |
| 904 | void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth, |
| 905 | unsigned int renderTargetHeight) |
| 906 | { |
| 907 | bool renderTargetSizedChanged = mForceSetScissor || |
| 908 | renderTargetWidth != mCurRenderTargetWidth || |
| 909 | renderTargetHeight != mCurRenderTargetHeight; |
| 910 | bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0; |
| 911 | |
| 912 | if (renderTargetSizedChanged || scissorChanged) |
| 913 | { |
| 914 | RECT rect; |
| 915 | rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth)); |
daniel@transgaming.com | 1dd557a | 2012-11-28 19:44:00 +0000 | [diff] [blame] | 916 | rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetHeight)); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 917 | rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth)); |
daniel@transgaming.com | 1dd557a | 2012-11-28 19:44:00 +0000 | [diff] [blame] | 918 | rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetHeight)); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 919 | mDevice->SetScissorRect(&rect); |
| 920 | |
| 921 | mCurScissor = scissor; |
| 922 | mCurRenderTargetWidth = renderTargetWidth; |
| 923 | mCurRenderTargetHeight = renderTargetHeight; |
| 924 | } |
| 925 | |
| 926 | mForceSetScissor = false; |
| 927 | } |
| 928 | |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 929 | bool Renderer9::setViewport(const gl::Rectangle& viewport, float zNear, float zFar, |
| 930 | unsigned int renderTargetWidth, unsigned int renderTargetHeight, |
| 931 | gl::ProgramBinary *currentProgram, bool forceSetUniforms) |
| 932 | { |
| 933 | bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || |
| 934 | zNear != mCurNear || zFar != mCurFar; |
| 935 | |
| 936 | D3DVIEWPORT9 dxViewport; |
| 937 | dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(renderTargetWidth)); |
| 938 | dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(renderTargetHeight)); |
| 939 | dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(renderTargetWidth) - static_cast<int>(dxViewport.X)); |
| 940 | dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(renderTargetHeight) - static_cast<int>(dxViewport.Y)); |
| 941 | dxViewport.MinZ = zNear; |
| 942 | dxViewport.MaxZ = zFar; |
| 943 | |
| 944 | if (dxViewport.Width <= 0 || dxViewport.Height <= 0) |
| 945 | { |
| 946 | return false; // Nothing to render |
| 947 | } |
| 948 | |
| 949 | if (viewportChanged) |
| 950 | { |
| 951 | mDevice->SetViewport(&dxViewport); |
| 952 | |
| 953 | mCurViewport = viewport; |
| 954 | mCurNear = zNear; |
| 955 | mCurFar = zFar; |
| 956 | } |
| 957 | |
| 958 | if (currentProgram && (viewportChanged || forceSetUniforms)) |
| 959 | { |
| 960 | GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation(); |
| 961 | GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height }; |
| 962 | currentProgram->setUniform2fv(halfPixelSize, 1, xy); |
| 963 | |
| 964 | // These values are used for computing gl_FragCoord in Program::linkVaryings(). |
| 965 | GLint coord = currentProgram->getDxCoordLocation(); |
| 966 | GLfloat whxy[4] = { viewport.width * 0.5f, |
| 967 | viewport.height * 0.5f, |
| 968 | viewport.x + (viewport.width * 0.5f), |
| 969 | viewport.y + (viewport.height * 0.5f) }; |
| 970 | currentProgram->setUniform4fv(coord, 1, whxy); |
| 971 | |
| 972 | GLint depth = currentProgram->getDxDepthLocation(); |
| 973 | GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f }; |
| 974 | currentProgram->setUniform2fv(depth, 1, dz); |
| 975 | |
| 976 | GLint depthRange = currentProgram->getDxDepthRangeLocation(); |
| 977 | GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear }; |
| 978 | currentProgram->setUniform3fv(depthRange, 1, nearFarDiff); |
| 979 | } |
| 980 | |
| 981 | mForceSetViewport = false; |
| 982 | return true; |
| 983 | } |
| 984 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 985 | bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count) |
| 986 | { |
| 987 | switch (mode) |
| 988 | { |
| 989 | case GL_POINTS: |
| 990 | mPrimitiveType = D3DPT_POINTLIST; |
| 991 | mPrimitiveCount = count; |
| 992 | break; |
| 993 | case GL_LINES: |
| 994 | mPrimitiveType = D3DPT_LINELIST; |
| 995 | mPrimitiveCount = count / 2; |
| 996 | break; |
| 997 | case GL_LINE_LOOP: |
| 998 | mPrimitiveType = D3DPT_LINESTRIP; |
| 999 | mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately |
| 1000 | break; |
| 1001 | case GL_LINE_STRIP: |
| 1002 | mPrimitiveType = D3DPT_LINESTRIP; |
| 1003 | mPrimitiveCount = count - 1; |
| 1004 | break; |
| 1005 | case GL_TRIANGLES: |
| 1006 | mPrimitiveType = D3DPT_TRIANGLELIST; |
| 1007 | mPrimitiveCount = count / 3; |
| 1008 | break; |
| 1009 | case GL_TRIANGLE_STRIP: |
| 1010 | mPrimitiveType = D3DPT_TRIANGLESTRIP; |
| 1011 | mPrimitiveCount = count - 2; |
| 1012 | break; |
| 1013 | case GL_TRIANGLE_FAN: |
| 1014 | mPrimitiveType = D3DPT_TRIANGLEFAN; |
| 1015 | mPrimitiveCount = count - 2; |
| 1016 | break; |
| 1017 | default: |
| 1018 | return error(GL_INVALID_ENUM, false); |
| 1019 | } |
| 1020 | |
| 1021 | return mPrimitiveCount > 0; |
| 1022 | } |
| 1023 | |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1024 | bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1025 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1026 | // if there is no color attachment we must synthesize a NULL colorattachment |
| 1027 | // to keep the D3D runtime happy. This should only be possible if depth texturing. |
| 1028 | gl::Renderbuffer *renderbufferObject = NULL; |
| 1029 | if (framebuffer->getColorbufferType() != GL_NONE) |
| 1030 | { |
| 1031 | renderbufferObject = framebuffer->getColorbuffer(); |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | renderbufferObject = framebuffer->getNullColorbuffer(); |
| 1036 | } |
| 1037 | if (!renderbufferObject) |
| 1038 | { |
| 1039 | ERR("unable to locate renderbuffer for FBO."); |
| 1040 | return false; |
| 1041 | } |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1042 | |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1043 | bool renderTargetChanged = false; |
| 1044 | unsigned int renderTargetSerial = renderbufferObject->getSerial(); |
| 1045 | if (renderTargetSerial != mAppliedRenderTargetSerial) |
| 1046 | { |
| 1047 | // Apply the render target on the device |
| 1048 | IDirect3DSurface9 *renderTargetSurface = NULL; |
| 1049 | |
| 1050 | RenderTarget *renderTarget = renderbufferObject->getRenderTarget(); |
| 1051 | if (renderTarget) |
| 1052 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1053 | renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | if (!renderTargetSurface) |
| 1057 | { |
| 1058 | ERR("render target pointer unexpectedly null."); |
| 1059 | return false; // Context must be lost |
| 1060 | } |
| 1061 | |
| 1062 | mDevice->SetRenderTarget(0, renderTargetSurface); |
| 1063 | renderTargetSurface->Release(); |
| 1064 | |
| 1065 | mAppliedRenderTargetSerial = renderTargetSerial; |
| 1066 | renderTargetChanged = true; |
| 1067 | } |
| 1068 | |
| 1069 | gl::Renderbuffer *depthStencil = NULL; |
| 1070 | unsigned int depthbufferSerial = 0; |
| 1071 | unsigned int stencilbufferSerial = 0; |
| 1072 | if (framebuffer->getDepthbufferType() != GL_NONE) |
| 1073 | { |
| 1074 | depthStencil = framebuffer->getDepthbuffer(); |
| 1075 | if (!depthStencil) |
| 1076 | { |
| 1077 | ERR("Depth stencil pointer unexpectedly null."); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | depthbufferSerial = depthStencil->getSerial(); |
| 1082 | } |
| 1083 | else if (framebuffer->getStencilbufferType() != GL_NONE) |
| 1084 | { |
| 1085 | depthStencil = framebuffer->getStencilbuffer(); |
| 1086 | if (!depthStencil) |
| 1087 | { |
| 1088 | ERR("Depth stencil pointer unexpectedly null."); |
| 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | stencilbufferSerial = depthStencil->getSerial(); |
| 1093 | } |
| 1094 | |
| 1095 | if (depthbufferSerial != mAppliedDepthbufferSerial || |
| 1096 | stencilbufferSerial != mAppliedStencilbufferSerial || |
| 1097 | !mDepthStencilInitialized) |
| 1098 | { |
| 1099 | // Apply the depth stencil on the device |
| 1100 | if (depthStencil) |
| 1101 | { |
| 1102 | IDirect3DSurface9 *depthStencilSurface = NULL; |
| 1103 | RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil(); |
| 1104 | |
| 1105 | if (depthStencilRenderTarget) |
| 1106 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1107 | depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | if (!depthStencilSurface) |
| 1111 | { |
| 1112 | ERR("depth stencil pointer unexpectedly null."); |
| 1113 | return false; // Context must be lost |
| 1114 | } |
| 1115 | |
| 1116 | mDevice->SetDepthStencilSurface(depthStencilSurface); |
| 1117 | depthStencilSurface->Release(); |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | mDevice->SetDepthStencilSurface(NULL); |
| 1122 | } |
| 1123 | |
| 1124 | mAppliedDepthbufferSerial = depthbufferSerial; |
| 1125 | mAppliedStencilbufferSerial = stencilbufferSerial; |
| 1126 | mDepthStencilInitialized = true; |
| 1127 | } |
| 1128 | |
| 1129 | if (renderTargetChanged || !mRenderTargetDescInitialized) |
| 1130 | { |
| 1131 | mForceSetScissor = true; |
| 1132 | mForceSetViewport = true; |
| 1133 | |
| 1134 | mRenderTargetDesc.width = renderbufferObject->getWidth(); |
| 1135 | mRenderTargetDesc.height = renderbufferObject->getHeight(); |
| 1136 | mRenderTargetDesc.format = renderbufferObject->getActualFormat(); |
| 1137 | mRenderTargetDescInitialized = true; |
| 1138 | } |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1139 | |
| 1140 | return true; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1141 | } |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 1142 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 1143 | GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances) |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1144 | { |
| 1145 | gl::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; |
| 1146 | GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); |
| 1147 | if (err != GL_NO_ERROR) |
| 1148 | { |
| 1149 | return err; |
| 1150 | } |
| 1151 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 1152 | return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw); |
| 1153 | } |
| 1154 | |
| 1155 | // Applies the indices and element array bindings to the Direct3D 9 device |
| 1156 | GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, gl::TranslatedIndexData *indexInfo) |
| 1157 | { |
| 1158 | IDirect3DIndexBuffer9 *indexBuffer; |
| 1159 | unsigned int serial; |
| 1160 | GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial); |
| 1161 | |
| 1162 | mIndexInfo.minIndex = indexInfo->minIndex; |
| 1163 | mIndexInfo.maxIndex = indexInfo->maxIndex; |
| 1164 | mIndexInfo.startIndex = indexInfo->startIndex; |
| 1165 | |
| 1166 | if (err == GL_NO_ERROR) |
| 1167 | { |
| 1168 | if (serial != mAppliedIBSerial) |
| 1169 | { |
| 1170 | mDevice->SetIndices(indexBuffer); |
| 1171 | mAppliedIBSerial = serial; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | return err; |
| 1176 | } |
| 1177 | |
| 1178 | void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances) |
| 1179 | { |
| 1180 | startScene(); |
| 1181 | |
| 1182 | if (mode == GL_LINE_LOOP) |
| 1183 | { |
| 1184 | drawLineLoop(count, GL_NONE, NULL, 0, NULL); |
| 1185 | } |
| 1186 | else if (instances > 0) |
| 1187 | { |
| 1188 | gl::StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count); |
| 1189 | if (countingIB) |
| 1190 | { |
| 1191 | if (mAppliedIBSerial != countingIB->getSerial()) |
| 1192 | { |
| 1193 | mDevice->SetIndices(countingIB->getBuffer()); |
| 1194 | mAppliedIBSerial = countingIB->getSerial(); |
| 1195 | } |
| 1196 | |
| 1197 | for (int i = 0; i < mRepeatDraw; i++) |
| 1198 | { |
| 1199 | mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount); |
| 1200 | } |
| 1201 | } |
| 1202 | else |
| 1203 | { |
| 1204 | ERR("Could not create a counting index buffer for glDrawArraysInstanced."); |
| 1205 | return error(GL_OUT_OF_MEMORY); |
| 1206 | } |
| 1207 | } |
| 1208 | else // Regular case |
| 1209 | { |
| 1210 | mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer) |
| 1215 | { |
| 1216 | startScene(); |
| 1217 | |
| 1218 | if (mode == GL_LINE_LOOP) |
| 1219 | { |
| 1220 | drawLineLoop(count, type, indices, mIndexInfo.minIndex, elementArrayBuffer); |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | for (int i = 0; i < mRepeatDraw; i++) |
| 1225 | { |
| 1226 | GLsizei vertexCount = mIndexInfo.maxIndex - mIndexInfo.minIndex + 1; |
| 1227 | mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)mIndexInfo.minIndex, mIndexInfo.minIndex, vertexCount, mIndexInfo.startIndex, mPrimitiveCount); |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) |
| 1233 | { |
| 1234 | // Get the raw indices for an indexed draw |
| 1235 | if (type != GL_NONE && elementArrayBuffer) |
| 1236 | { |
| 1237 | gl::Buffer *indexBuffer = elementArrayBuffer; |
| 1238 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 1239 | indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset; |
| 1240 | } |
| 1241 | |
| 1242 | UINT startIndex = 0; |
| 1243 | bool succeeded = false; |
| 1244 | |
| 1245 | if (get32BitIndexSupport()) |
| 1246 | { |
| 1247 | const int spaceNeeded = (count + 1) * sizeof(unsigned int); |
| 1248 | |
| 1249 | if (!mLineLoopIB) |
| 1250 | { |
| 1251 | mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
| 1252 | } |
| 1253 | |
| 1254 | if (mLineLoopIB) |
| 1255 | { |
| 1256 | mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT); |
| 1257 | |
| 1258 | UINT offset = 0; |
| 1259 | unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset)); |
| 1260 | startIndex = offset / 4; |
| 1261 | |
| 1262 | if (data) |
| 1263 | { |
| 1264 | switch (type) |
| 1265 | { |
| 1266 | case GL_NONE: // Non-indexed draw |
| 1267 | for (int i = 0; i < count; i++) |
| 1268 | { |
| 1269 | data[i] = i; |
| 1270 | } |
| 1271 | data[count] = 0; |
| 1272 | break; |
| 1273 | case GL_UNSIGNED_BYTE: |
| 1274 | for (int i = 0; i < count; i++) |
| 1275 | { |
| 1276 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1277 | } |
| 1278 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1279 | break; |
| 1280 | case GL_UNSIGNED_SHORT: |
| 1281 | for (int i = 0; i < count; i++) |
| 1282 | { |
| 1283 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1284 | } |
| 1285 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1286 | break; |
| 1287 | case GL_UNSIGNED_INT: |
| 1288 | for (int i = 0; i < count; i++) |
| 1289 | { |
| 1290 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1291 | } |
| 1292 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1293 | break; |
| 1294 | default: UNREACHABLE(); |
| 1295 | } |
| 1296 | |
| 1297 | mLineLoopIB->unmap(); |
| 1298 | succeeded = true; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | else |
| 1303 | { |
| 1304 | const int spaceNeeded = (count + 1) * sizeof(unsigned short); |
| 1305 | |
| 1306 | if (!mLineLoopIB) |
| 1307 | { |
| 1308 | mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
| 1309 | } |
| 1310 | |
| 1311 | if (mLineLoopIB) |
| 1312 | { |
| 1313 | mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
| 1314 | |
| 1315 | UINT offset = 0; |
| 1316 | unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset)); |
| 1317 | startIndex = offset / 2; |
| 1318 | |
| 1319 | if (data) |
| 1320 | { |
| 1321 | switch (type) |
| 1322 | { |
| 1323 | case GL_NONE: // Non-indexed draw |
| 1324 | for (int i = 0; i < count; i++) |
| 1325 | { |
| 1326 | data[i] = i; |
| 1327 | } |
| 1328 | data[count] = 0; |
| 1329 | break; |
| 1330 | case GL_UNSIGNED_BYTE: |
| 1331 | for (int i = 0; i < count; i++) |
| 1332 | { |
| 1333 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1334 | } |
| 1335 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1336 | break; |
| 1337 | case GL_UNSIGNED_SHORT: |
| 1338 | for (int i = 0; i < count; i++) |
| 1339 | { |
| 1340 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1341 | } |
| 1342 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1343 | break; |
| 1344 | case GL_UNSIGNED_INT: |
| 1345 | for (int i = 0; i < count; i++) |
| 1346 | { |
| 1347 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1348 | } |
| 1349 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1350 | break; |
| 1351 | default: UNREACHABLE(); |
| 1352 | } |
| 1353 | |
| 1354 | mLineLoopIB->unmap(); |
| 1355 | succeeded = true; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | if (succeeded) |
| 1361 | { |
| 1362 | if (mAppliedIBSerial != mLineLoopIB->getSerial()) |
| 1363 | { |
| 1364 | mDevice->SetIndices(mLineLoopIB->getBuffer()); |
| 1365 | mAppliedIBSerial = mLineLoopIB->getSerial(); |
| 1366 | } |
| 1367 | |
| 1368 | mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count); |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | ERR("Could not create a looping index buffer for GL_LINE_LOOP."); |
| 1373 | return error(GL_OUT_OF_MEMORY); |
| 1374 | } |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1377 | void Renderer9::applyShaders(gl::ProgramBinary *programBinary) |
| 1378 | { |
| 1379 | IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader(); |
| 1380 | IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader(); |
| 1381 | |
| 1382 | mDevice->SetPixelShader(pixelShader); |
| 1383 | mDevice->SetVertexShader(vertexShader); |
| 1384 | programBinary->dirtyAllUniforms(); |
| 1385 | } |
| 1386 | |
daniel@transgaming.com | 084a257 | 2012-11-28 20:55:17 +0000 | [diff] [blame] | 1387 | void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1388 | { |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1389 | D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha), |
| 1390 | gl::unorm<8>(clearParams.colorClearValue.red), |
| 1391 | gl::unorm<8>(clearParams.colorClearValue.green), |
| 1392 | gl::unorm<8>(clearParams.colorClearValue.blue)); |
| 1393 | float depth = gl::clamp01(clearParams.depthClearValue); |
| 1394 | int stencil = clearParams.stencilClearValue & 0x000000FF; |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1395 | |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1396 | unsigned int stencilUnmasked = 0x0; |
| 1397 | if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil()) |
| 1398 | { |
| 1399 | unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); |
| 1400 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1401 | } |
| 1402 | |
| 1403 | bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; |
| 1404 | |
| 1405 | const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && |
| 1406 | (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; |
| 1407 | const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && |
| 1408 | !(clearParams.colorMaskRed && clearParams.colorMaskGreen && |
| 1409 | clearParams.colorMaskBlue && alphaUnmasked); |
| 1410 | |
| 1411 | if (needMaskedColorClear || needMaskedStencilClear) |
| 1412 | { |
| 1413 | // State which is altered in all paths from this point to the clear call is saved. |
| 1414 | // State which is altered in only some paths will be flagged dirty in the case that |
| 1415 | // that path is taken. |
| 1416 | HRESULT hr; |
| 1417 | if (mMaskedClearSavedState == NULL) |
| 1418 | { |
| 1419 | hr = mDevice->BeginStateBlock(); |
| 1420 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1421 | |
| 1422 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1423 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1424 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1425 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1426 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1427 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1428 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1429 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1430 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1431 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1432 | mDevice->SetPixelShader(NULL); |
| 1433 | mDevice->SetVertexShader(NULL); |
| 1434 | mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
| 1435 | mDevice->SetStreamSource(0, NULL, 0, 0); |
| 1436 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1437 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1438 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1439 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1440 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1441 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1442 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1443 | |
| 1444 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1445 | { |
| 1446 | mDevice->SetStreamSourceFreq(i, 1); |
| 1447 | } |
| 1448 | |
| 1449 | hr = mDevice->EndStateBlock(&mMaskedClearSavedState); |
| 1450 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1451 | } |
| 1452 | |
| 1453 | ASSERT(mMaskedClearSavedState != NULL); |
| 1454 | |
| 1455 | if (mMaskedClearSavedState != NULL) |
| 1456 | { |
| 1457 | hr = mMaskedClearSavedState->Capture(); |
| 1458 | ASSERT(SUCCEEDED(hr)); |
| 1459 | } |
| 1460 | |
| 1461 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1462 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1463 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1464 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1465 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1466 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1467 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1468 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1469 | |
| 1470 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1471 | { |
| 1472 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, |
| 1473 | gl_d3d9::ConvertColorMask(clearParams.colorMaskRed, |
| 1474 | clearParams.colorMaskGreen, |
| 1475 | clearParams.colorMaskBlue, |
| 1476 | clearParams.colorMaskAlpha)); |
| 1477 | } |
| 1478 | else |
| 1479 | { |
| 1480 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1481 | } |
| 1482 | |
| 1483 | if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT)) |
| 1484 | { |
| 1485 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1486 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 1487 | mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 1488 | mDevice->SetRenderState(D3DRS_STENCILREF, stencil); |
| 1489 | mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask); |
| 1490 | mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
| 1491 | mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 1492 | mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1497 | } |
| 1498 | |
| 1499 | mDevice->SetPixelShader(NULL); |
| 1500 | mDevice->SetVertexShader(NULL); |
| 1501 | mDevice->SetFVF(D3DFVF_XYZRHW); |
| 1502 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1503 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1504 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1505 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1506 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1507 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1508 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1509 | |
| 1510 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1511 | { |
| 1512 | mDevice->SetStreamSourceFreq(i, 1); |
| 1513 | } |
| 1514 | |
| 1515 | float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges |
| 1516 | quad[0][0] = -0.5f; |
| 1517 | quad[0][1] = mRenderTargetDesc.height - 0.5f; |
| 1518 | quad[0][2] = 0.0f; |
| 1519 | quad[0][3] = 1.0f; |
| 1520 | |
| 1521 | quad[1][0] = mRenderTargetDesc.width - 0.5f; |
| 1522 | quad[1][1] = mRenderTargetDesc.height - 0.5f; |
| 1523 | quad[1][2] = 0.0f; |
| 1524 | quad[1][3] = 1.0f; |
| 1525 | |
| 1526 | quad[2][0] = -0.5f; |
| 1527 | quad[2][1] = -0.5f; |
| 1528 | quad[2][2] = 0.0f; |
| 1529 | quad[2][3] = 1.0f; |
| 1530 | |
| 1531 | quad[3][0] = mRenderTargetDesc.width - 0.5f; |
| 1532 | quad[3][1] = -0.5f; |
| 1533 | quad[3][2] = 0.0f; |
| 1534 | quad[3][3] = 1.0f; |
| 1535 | |
| 1536 | startScene(); |
| 1537 | mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); |
| 1538 | |
| 1539 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1540 | { |
| 1541 | mDevice->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 1542 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 1543 | mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 1544 | } |
| 1545 | |
| 1546 | if (mMaskedClearSavedState != NULL) |
| 1547 | { |
| 1548 | mMaskedClearSavedState->Apply(); |
| 1549 | } |
| 1550 | } |
| 1551 | else if (clearParams.mask) |
| 1552 | { |
| 1553 | DWORD dxClearFlags = 0; |
| 1554 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1555 | { |
| 1556 | dxClearFlags |= D3DCLEAR_TARGET; |
| 1557 | } |
| 1558 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1559 | { |
| 1560 | dxClearFlags |= D3DCLEAR_ZBUFFER; |
| 1561 | } |
| 1562 | if (clearParams.mask & GL_STENCIL_BUFFER_BIT) |
| 1563 | { |
| 1564 | dxClearFlags |= D3DCLEAR_STENCIL; |
| 1565 | } |
| 1566 | |
| 1567 | mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil); |
| 1568 | } |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1571 | void Renderer9::markAllStateDirty() |
| 1572 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1573 | mAppliedRenderTargetSerial = 0; |
| 1574 | mAppliedDepthbufferSerial = 0; |
| 1575 | mAppliedStencilbufferSerial = 0; |
| 1576 | mDepthStencilInitialized = false; |
| 1577 | mRenderTargetDescInitialized = false; |
| 1578 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1579 | mForceSetDepthStencilState = true; |
| 1580 | mForceSetRasterState = true; |
| 1581 | mForceSetBlendState = true; |
| 1582 | mForceSetScissor = true; |
| 1583 | mForceSetViewport = true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1584 | |
| 1585 | mVertexDeclarationCache.markStateDirty(); |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1588 | void Renderer9::releaseDeviceResources() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1589 | { |
| 1590 | while (!mEventQueryPool.empty()) |
| 1591 | { |
| 1592 | mEventQueryPool.back()->Release(); |
| 1593 | mEventQueryPool.pop_back(); |
| 1594 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1595 | |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1596 | if (mMaskedClearSavedState) |
| 1597 | { |
| 1598 | mMaskedClearSavedState->Release(); |
| 1599 | mMaskedClearSavedState = NULL; |
| 1600 | } |
| 1601 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1602 | mVertexShaderCache.clear(); |
| 1603 | mPixelShaderCache.clear(); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1604 | |
daniel@transgaming.com | e569fc5 | 2012-11-28 20:56:02 +0000 | [diff] [blame] | 1605 | delete mBlit; |
| 1606 | mBlit = NULL; |
| 1607 | |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1608 | delete mVertexDataManager; |
| 1609 | mVertexDataManager = NULL; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame^] | 1610 | |
| 1611 | delete mIndexDataManager; |
| 1612 | mIndexDataManager = NULL; |
| 1613 | |
| 1614 | delete mLineLoopIB; |
| 1615 | mLineLoopIB = NULL; |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1619 | void Renderer9::markDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1620 | { |
| 1621 | mDeviceLost = true; |
| 1622 | } |
| 1623 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1624 | bool Renderer9::isDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1625 | { |
| 1626 | return mDeviceLost; |
| 1627 | } |
| 1628 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1629 | // 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] | 1630 | bool Renderer9::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1631 | { |
| 1632 | bool isLost = false; |
| 1633 | |
| 1634 | if (mDeviceEx) |
| 1635 | { |
| 1636 | isLost = FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 1637 | } |
| 1638 | else if (mDevice) |
| 1639 | { |
| 1640 | isLost = FAILED(mDevice->TestCooperativeLevel()); |
| 1641 | } |
| 1642 | else |
| 1643 | { |
| 1644 | // No device yet, so no reset required |
| 1645 | } |
| 1646 | |
| 1647 | if (isLost) |
| 1648 | { |
| 1649 | // ensure we note the device loss -- |
| 1650 | // we'll probably get this done again by markDeviceLost |
| 1651 | // but best to remember it! |
| 1652 | // Note that we don't want to clear the device loss status here |
| 1653 | // -- this needs to be done by resetDevice |
| 1654 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1655 | if (notify) |
| 1656 | { |
| 1657 | mDisplay->notifyDeviceLost(); |
| 1658 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | return isLost; |
| 1662 | } |
| 1663 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1664 | bool Renderer9::testDeviceResettable() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1665 | { |
| 1666 | HRESULT status = D3D_OK; |
| 1667 | |
| 1668 | if (mDeviceEx) |
| 1669 | { |
| 1670 | status = mDeviceEx->CheckDeviceState(NULL); |
| 1671 | } |
| 1672 | else if (mDevice) |
| 1673 | { |
| 1674 | status = mDevice->TestCooperativeLevel(); |
| 1675 | } |
| 1676 | |
| 1677 | switch (status) |
| 1678 | { |
| 1679 | case D3DERR_DEVICENOTRESET: |
| 1680 | case D3DERR_DEVICEHUNG: |
| 1681 | return true; |
| 1682 | default: |
| 1683 | return false; |
| 1684 | } |
| 1685 | } |
| 1686 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1687 | bool Renderer9::resetDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1688 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1689 | releaseDeviceResources(); |
| 1690 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1691 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 1692 | |
| 1693 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1694 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1695 | int attempts = 3; |
| 1696 | |
| 1697 | while (lost && attempts > 0) |
| 1698 | { |
| 1699 | if (mDeviceEx) |
| 1700 | { |
| 1701 | Sleep(500); // Give the graphics driver some CPU time |
| 1702 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 1703 | } |
| 1704 | else |
| 1705 | { |
| 1706 | result = mDevice->TestCooperativeLevel(); |
| 1707 | while (result == D3DERR_DEVICELOST) |
| 1708 | { |
| 1709 | Sleep(100); // Give the graphics driver some CPU time |
| 1710 | result = mDevice->TestCooperativeLevel(); |
| 1711 | } |
| 1712 | |
| 1713 | if (result == D3DERR_DEVICENOTRESET) |
| 1714 | { |
| 1715 | result = mDevice->Reset(&presentParameters); |
| 1716 | } |
| 1717 | } |
| 1718 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1719 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1720 | attempts --; |
| 1721 | } |
| 1722 | |
| 1723 | if (FAILED(result)) |
| 1724 | { |
| 1725 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 1726 | return false; |
| 1727 | } |
| 1728 | |
| 1729 | // reset device defaults |
| 1730 | initializeDevice(); |
| 1731 | mDeviceLost = false; |
| 1732 | |
| 1733 | return true; |
| 1734 | } |
| 1735 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1736 | DWORD Renderer9::getAdapterVendor() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1737 | { |
| 1738 | return mAdapterIdentifier.VendorId; |
| 1739 | } |
| 1740 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1741 | const char *Renderer9::getAdapterDescription() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1742 | { |
| 1743 | return mAdapterIdentifier.Description; |
| 1744 | } |
| 1745 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1746 | GUID Renderer9::getAdapterIdentifier() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1747 | { |
| 1748 | return mAdapterIdentifier.DeviceIdentifier; |
| 1749 | } |
| 1750 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1751 | void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1752 | { |
| 1753 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 1754 | { |
| 1755 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 1756 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 1757 | |
| 1758 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 1759 | } |
| 1760 | } |
| 1761 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1762 | bool Renderer9::getDXT1TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1763 | { |
| 1764 | D3DDISPLAYMODE currentDisplayMode; |
| 1765 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1766 | |
| 1767 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 1768 | } |
| 1769 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1770 | bool Renderer9::getDXT3TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1771 | { |
| 1772 | D3DDISPLAYMODE currentDisplayMode; |
| 1773 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1774 | |
| 1775 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 1776 | } |
| 1777 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1778 | bool Renderer9::getDXT5TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1779 | { |
| 1780 | D3DDISPLAYMODE currentDisplayMode; |
| 1781 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1782 | |
| 1783 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 1784 | } |
| 1785 | |
| 1786 | // we use INTZ for depth textures in Direct3D9 |
| 1787 | // we also want NULL texture support to ensure the we can make depth-only FBOs |
| 1788 | // see http://aras-p.info/texts/D3D9GPUHacks.html |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1789 | bool Renderer9::getDepthTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1790 | { |
| 1791 | D3DDISPLAYMODE currentDisplayMode; |
| 1792 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1793 | |
| 1794 | bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1795 | D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)); |
| 1796 | bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1797 | D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); |
| 1798 | |
| 1799 | return intz && null; |
| 1800 | } |
| 1801 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1802 | bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1803 | { |
| 1804 | D3DDISPLAYMODE currentDisplayMode; |
| 1805 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1806 | |
| 1807 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1808 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1809 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1810 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1811 | |
| 1812 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1813 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 1814 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1815 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1816 | |
| 1817 | if (!*filtering && !*renderable) |
| 1818 | { |
| 1819 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1820 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1821 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1822 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | return true; |
| 1827 | } |
| 1828 | } |
| 1829 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1830 | bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1831 | { |
| 1832 | D3DDISPLAYMODE currentDisplayMode; |
| 1833 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1834 | |
| 1835 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1836 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1837 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1838 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1839 | |
| 1840 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1841 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1842 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1843 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1844 | |
| 1845 | if (!*filtering && !*renderable) |
| 1846 | { |
| 1847 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1848 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1849 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1850 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1851 | } |
| 1852 | else |
| 1853 | { |
| 1854 | return true; |
| 1855 | } |
| 1856 | } |
| 1857 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1858 | bool Renderer9::getLuminanceTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1859 | { |
| 1860 | D3DDISPLAYMODE currentDisplayMode; |
| 1861 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1862 | |
| 1863 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 1864 | } |
| 1865 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1866 | bool Renderer9::getLuminanceAlphaTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1867 | { |
| 1868 | D3DDISPLAYMODE currentDisplayMode; |
| 1869 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1870 | |
| 1871 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 1872 | } |
| 1873 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1874 | bool Renderer9::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1875 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1876 | return mSupportsTextureFilterAnisotropy; |
| 1877 | } |
| 1878 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1879 | float Renderer9::getTextureMaxAnisotropy() const |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1880 | { |
| 1881 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1882 | { |
| 1883 | return mDeviceCaps.MaxAnisotropy; |
| 1884 | } |
| 1885 | return 1.0f; |
| 1886 | } |
| 1887 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1888 | bool Renderer9::getEventQuerySupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1889 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1890 | IDirect3DQuery9 *query = allocateEventQuery(); |
| 1891 | if (query) |
| 1892 | { |
| 1893 | freeEventQuery(query); |
| 1894 | return true; |
| 1895 | } |
| 1896 | else |
| 1897 | { |
| 1898 | return false; |
| 1899 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1900 | return true; |
| 1901 | } |
| 1902 | |
| 1903 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 1904 | // 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] | 1905 | bool Renderer9::getVertexTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1906 | { |
| 1907 | if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 1908 | { |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
| 1912 | D3DDISPLAYMODE currentDisplayMode; |
| 1913 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1914 | |
| 1915 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 1916 | |
| 1917 | return SUCCEEDED(result); |
| 1918 | } |
| 1919 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1920 | bool Renderer9::getNonPower2TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1921 | { |
| 1922 | return mSupportsNonPower2Textures; |
| 1923 | } |
| 1924 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1925 | bool Renderer9::getOcclusionQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1926 | { |
| 1927 | if (!mDevice) |
| 1928 | { |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
| 1932 | IDirect3DQuery9 *query = NULL; |
| 1933 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query); |
| 1934 | if (SUCCEEDED(result) && query) |
| 1935 | { |
| 1936 | query->Release(); |
| 1937 | return true; |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | return false; |
| 1942 | } |
| 1943 | } |
| 1944 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1945 | bool Renderer9::getInstancingSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1946 | { |
| 1947 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1948 | } |
| 1949 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1950 | bool Renderer9::getShareHandleSupport() const |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1951 | { |
| 1952 | // PIX doesn't seem to support using share handles, so disable them. |
| 1953 | // D3D9_REPLACE |
daniel@transgaming.com | 7cb796e | 2012-10-31 18:46:44 +0000 | [diff] [blame] | 1954 | return (mD3d9Ex != NULL) && !gl::perfActive(); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1955 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1956 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1957 | bool Renderer9::getShaderModel3Support() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1958 | { |
| 1959 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1960 | } |
| 1961 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1962 | float Renderer9::getMaxPointSize() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1963 | { |
| 1964 | return mDeviceCaps.MaxPointSize; |
| 1965 | } |
| 1966 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1967 | int Renderer9::getMaxTextureWidth() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1968 | { |
| 1969 | return (int)mDeviceCaps.MaxTextureWidth; |
| 1970 | } |
| 1971 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1972 | int Renderer9::getMaxTextureHeight() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1973 | { |
| 1974 | return (int)mDeviceCaps.MaxTextureHeight; |
| 1975 | } |
| 1976 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1977 | bool Renderer9::get32BitIndexSupport() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1978 | { |
| 1979 | return mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 1980 | } |
| 1981 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1982 | DWORD Renderer9::getCapsDeclTypes() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1983 | { |
| 1984 | return mDeviceCaps.DeclTypes; |
| 1985 | } |
| 1986 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1987 | int Renderer9::getMinSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1988 | { |
| 1989 | return mMinSwapInterval; |
| 1990 | } |
| 1991 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1992 | int Renderer9::getMaxSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1993 | { |
| 1994 | return mMaxSwapInterval; |
| 1995 | } |
| 1996 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1997 | int Renderer9::getMaxSupportedSamples() const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1998 | { |
| 1999 | return mMaxSupportedSamples; |
| 2000 | } |
| 2001 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2002 | int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2003 | { |
| 2004 | if (requested == 0) |
| 2005 | { |
| 2006 | return requested; |
| 2007 | } |
| 2008 | |
| 2009 | std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); |
| 2010 | if (itr == mMultiSampleSupport.end()) |
| 2011 | { |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 2012 | if (format == D3DFMT_UNKNOWN) |
| 2013 | return 0; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2014 | return -1; |
| 2015 | } |
| 2016 | |
| 2017 | for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) |
| 2018 | { |
| 2019 | if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
| 2020 | { |
| 2021 | return i; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
daniel@transgaming.com | a957168 | 2012-11-28 19:33:08 +0000 | [diff] [blame] | 2028 | D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) |
| 2029 | { |
| 2030 | switch (internalformat) |
| 2031 | { |
| 2032 | case GL_DEPTH_COMPONENT16: |
| 2033 | case GL_DEPTH_COMPONENT32_OES: |
| 2034 | case GL_DEPTH24_STENCIL8_OES: |
| 2035 | return D3DFMT_INTZ; |
| 2036 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 2037 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 2038 | return D3DFMT_DXT1; |
| 2039 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 2040 | return D3DFMT_DXT3; |
| 2041 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 2042 | return D3DFMT_DXT5; |
| 2043 | case GL_RGBA32F_EXT: |
| 2044 | case GL_RGB32F_EXT: |
| 2045 | case GL_ALPHA32F_EXT: |
| 2046 | case GL_LUMINANCE32F_EXT: |
| 2047 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 2048 | return D3DFMT_A32B32G32R32F; |
| 2049 | case GL_RGBA16F_EXT: |
| 2050 | case GL_RGB16F_EXT: |
| 2051 | case GL_ALPHA16F_EXT: |
| 2052 | case GL_LUMINANCE16F_EXT: |
| 2053 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 2054 | return D3DFMT_A16B16G16R16F; |
| 2055 | case GL_LUMINANCE8_EXT: |
| 2056 | if (getLuminanceTextureSupport()) |
| 2057 | { |
| 2058 | return D3DFMT_L8; |
| 2059 | } |
| 2060 | break; |
| 2061 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 2062 | if (getLuminanceAlphaTextureSupport()) |
| 2063 | { |
| 2064 | return D3DFMT_A8L8; |
| 2065 | } |
| 2066 | break; |
| 2067 | case GL_RGB8_OES: |
| 2068 | case GL_RGB565: |
| 2069 | return D3DFMT_X8R8G8B8; |
| 2070 | } |
| 2071 | |
| 2072 | return D3DFMT_A8R8G8B8; |
| 2073 | } |
| 2074 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2075 | bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2076 | { |
| 2077 | bool result = false; |
| 2078 | |
| 2079 | if (source && dest) |
| 2080 | { |
| 2081 | int levels = source->levelCount(); |
| 2082 | for (int i = 0; i < levels; ++i) |
| 2083 | { |
| 2084 | IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false); |
| 2085 | IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false); |
| 2086 | |
| 2087 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 2088 | |
| 2089 | if (srcSurf) srcSurf->Release(); |
| 2090 | if (dstSurf) dstSurf->Release(); |
| 2091 | |
| 2092 | if (!result) |
| 2093 | return false; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | return result; |
| 2098 | } |
| 2099 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2100 | bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2101 | { |
| 2102 | bool result = false; |
| 2103 | |
| 2104 | if (source && dest) |
| 2105 | { |
| 2106 | int levels = source->levelCount(); |
| 2107 | for (int f = 0; f < 6; f++) |
| 2108 | { |
| 2109 | for (int i = 0; i < levels; i++) |
| 2110 | { |
| 2111 | IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); |
| 2112 | IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); |
| 2113 | |
| 2114 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 2115 | |
| 2116 | if (srcSurf) srcSurf->Release(); |
| 2117 | if (dstSurf) dstSurf->Release(); |
| 2118 | |
| 2119 | if (!result) |
| 2120 | return false; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | return result; |
| 2126 | } |
| 2127 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2128 | D3DPOOL Renderer9::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2129 | { |
| 2130 | if (mD3d9Ex != NULL) |
| 2131 | { |
| 2132 | return D3DPOOL_DEFAULT; |
| 2133 | } |
| 2134 | else |
| 2135 | { |
| 2136 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 2137 | { |
| 2138 | return D3DPOOL_MANAGED; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | return D3DPOOL_DEFAULT; |
| 2143 | } |
| 2144 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2145 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 2146 | GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2147 | { |
| 2148 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level); |
| 2149 | } |
| 2150 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2151 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 2152 | GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2153 | { |
| 2154 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level); |
| 2155 | } |
| 2156 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2157 | bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect, |
| 2158 | bool blitRenderTarget, bool blitDepthStencil) |
| 2159 | { |
| 2160 | endScene(); |
| 2161 | |
| 2162 | if (blitRenderTarget) |
| 2163 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2164 | gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(); |
| 2165 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(); |
| 2166 | RenderTarget9 *readRenderTarget = NULL; |
| 2167 | RenderTarget9 *drawRenderTarget = NULL; |
| 2168 | IDirect3DSurface9* readSurface = NULL; |
| 2169 | IDirect3DSurface9* drawSurface = NULL; |
| 2170 | |
| 2171 | if (readBuffer) |
| 2172 | { |
| 2173 | readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); |
| 2174 | } |
| 2175 | if (drawBuffer) |
| 2176 | { |
| 2177 | drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); |
| 2178 | } |
| 2179 | |
| 2180 | if (readRenderTarget) |
| 2181 | { |
| 2182 | readSurface = readRenderTarget->getSurface(); |
| 2183 | } |
| 2184 | if (drawRenderTarget) |
| 2185 | { |
| 2186 | drawSurface = drawRenderTarget->getSurface(); |
| 2187 | } |
| 2188 | |
| 2189 | if (!readSurface || !drawSurface) |
| 2190 | { |
| 2191 | ERR("Failed to retrieve the render target."); |
| 2192 | return error(GL_OUT_OF_MEMORY, false); |
| 2193 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2194 | |
| 2195 | RECT srcRect, dstRect; |
| 2196 | RECT *srcRectPtr = NULL; |
| 2197 | RECT *dstRectPtr = NULL; |
| 2198 | |
| 2199 | if (readRect) |
| 2200 | { |
| 2201 | srcRect.left = readRect->x; |
| 2202 | srcRect.right = readRect->x + readRect->width; |
| 2203 | srcRect.top = readRect->y; |
| 2204 | srcRect.bottom = readRect->y + readRect->height; |
| 2205 | srcRectPtr = &srcRect; |
| 2206 | } |
| 2207 | |
| 2208 | if (drawRect) |
| 2209 | { |
| 2210 | dstRect.left = drawRect->x; |
| 2211 | dstRect.right = drawRect->x + drawRect->width; |
| 2212 | dstRect.top = drawRect->y; |
| 2213 | dstRect.bottom = drawRect->y + drawRect->height; |
| 2214 | dstRectPtr = &dstRect; |
| 2215 | } |
| 2216 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2217 | HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2218 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2219 | readSurface->Release(); |
| 2220 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2221 | |
| 2222 | if (FAILED(result)) |
| 2223 | { |
| 2224 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2225 | return false; |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | if (blitDepthStencil) |
| 2230 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2231 | gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); |
| 2232 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); |
| 2233 | RenderTarget9 *readDepthStencil = NULL; |
| 2234 | RenderTarget9 *drawDepthStencil = NULL; |
| 2235 | IDirect3DSurface9* readSurface = NULL; |
| 2236 | IDirect3DSurface9* drawSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2237 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2238 | if (readBuffer) |
| 2239 | { |
| 2240 | readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); |
| 2241 | } |
| 2242 | if (drawBuffer) |
| 2243 | { |
| 2244 | drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); |
| 2245 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2246 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2247 | if (readDepthStencil) |
| 2248 | { |
| 2249 | readSurface = readDepthStencil->getSurface(); |
| 2250 | } |
| 2251 | if (drawDepthStencil) |
| 2252 | { |
| 2253 | drawSurface = drawDepthStencil->getSurface(); |
| 2254 | } |
| 2255 | |
| 2256 | if (!readSurface || !drawSurface) |
| 2257 | { |
| 2258 | ERR("Failed to retrieve the render target."); |
| 2259 | return error(GL_OUT_OF_MEMORY, false); |
| 2260 | } |
| 2261 | |
| 2262 | HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); |
| 2263 | |
| 2264 | readSurface->Release(); |
| 2265 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (FAILED(result)) |
| 2268 | { |
| 2269 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2270 | return false; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
| 2277 | void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2278 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2279 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2280 | RenderTarget9 *renderTarget = NULL; |
| 2281 | IDirect3DSurface9 *surface = NULL; |
| 2282 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2283 | |
| 2284 | if (colorbuffer) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2285 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2286 | renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); |
| 2287 | } |
| 2288 | |
| 2289 | if (renderTarget) |
| 2290 | { |
| 2291 | surface = renderTarget->getSurface(); |
| 2292 | } |
| 2293 | |
| 2294 | if (!surface) |
| 2295 | { |
| 2296 | // context must be lost |
| 2297 | return; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | D3DSURFACE_DESC desc; |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2301 | surface->GetDesc(&desc); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2302 | |
| 2303 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 2304 | { |
| 2305 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2306 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2307 | return error(GL_OUT_OF_MEMORY); |
| 2308 | } |
| 2309 | |
| 2310 | HRESULT result; |
| 2311 | IDirect3DSurface9 *systemSurface = NULL; |
| 2312 | bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && |
| 2313 | x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && |
| 2314 | desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; |
| 2315 | if (directToPixels) |
| 2316 | { |
| 2317 | // Use the pixels ptr as a shared handle to write directly into client's memory |
| 2318 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 2319 | D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); |
| 2320 | if (FAILED(result)) |
| 2321 | { |
| 2322 | // Try again without the shared handle |
| 2323 | directToPixels = false; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | if (!directToPixels) |
| 2328 | { |
| 2329 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 2330 | D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 2331 | if (FAILED(result)) |
| 2332 | { |
| 2333 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2334 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2335 | return error(GL_OUT_OF_MEMORY); |
| 2336 | } |
| 2337 | } |
| 2338 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2339 | result = mDevice->GetRenderTargetData(surface, systemSurface); |
| 2340 | surface->Release(); |
| 2341 | surface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (FAILED(result)) |
| 2344 | { |
| 2345 | systemSurface->Release(); |
| 2346 | |
| 2347 | // It turns out that D3D will sometimes produce more error |
| 2348 | // codes than those documented. |
daniel@transgaming.com | 414c916 | 2012-11-28 20:54:57 +0000 | [diff] [blame] | 2349 | if (checkDeviceLost(result)) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2350 | return error(GL_OUT_OF_MEMORY); |
| 2351 | else |
| 2352 | { |
| 2353 | UNREACHABLE(); |
| 2354 | return; |
| 2355 | } |
| 2356 | |
| 2357 | } |
| 2358 | |
| 2359 | if (directToPixels) |
| 2360 | { |
| 2361 | systemSurface->Release(); |
| 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | RECT rect; |
| 2366 | rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width)); |
| 2367 | rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height)); |
| 2368 | rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width)); |
| 2369 | rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height)); |
| 2370 | |
| 2371 | D3DLOCKED_RECT lock; |
| 2372 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 2373 | |
| 2374 | if (FAILED(result)) |
| 2375 | { |
| 2376 | UNREACHABLE(); |
| 2377 | systemSurface->Release(); |
| 2378 | |
| 2379 | return; // No sensible error to generate |
| 2380 | } |
| 2381 | |
| 2382 | unsigned char *dest = (unsigned char*)pixels; |
| 2383 | unsigned short *dest16 = (unsigned short*)pixels; |
| 2384 | |
| 2385 | unsigned char *source; |
| 2386 | int inputPitch; |
| 2387 | if (packReverseRowOrder) |
| 2388 | { |
| 2389 | source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); |
| 2390 | inputPitch = -lock.Pitch; |
| 2391 | } |
| 2392 | else |
| 2393 | { |
| 2394 | source = (unsigned char*)lock.pBits; |
| 2395 | inputPitch = lock.Pitch; |
| 2396 | } |
| 2397 | |
| 2398 | unsigned int fastPixelSize = 0; |
| 2399 | |
| 2400 | if (desc.Format == D3DFMT_A8R8G8B8 && |
| 2401 | format == GL_BGRA_EXT && |
| 2402 | type == GL_UNSIGNED_BYTE) |
| 2403 | { |
| 2404 | fastPixelSize = 4; |
| 2405 | } |
| 2406 | else if ((desc.Format == D3DFMT_A4R4G4B4 && |
| 2407 | format == GL_BGRA_EXT && |
| 2408 | type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) || |
| 2409 | (desc.Format == D3DFMT_A1R5G5B5 && |
| 2410 | format == GL_BGRA_EXT && |
| 2411 | type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)) |
| 2412 | { |
| 2413 | fastPixelSize = 2; |
| 2414 | } |
| 2415 | else if (desc.Format == D3DFMT_A16B16G16R16F && |
| 2416 | format == GL_RGBA && |
| 2417 | type == GL_HALF_FLOAT_OES) |
| 2418 | { |
| 2419 | fastPixelSize = 8; |
| 2420 | } |
| 2421 | else if (desc.Format == D3DFMT_A32B32G32R32F && |
| 2422 | format == GL_RGBA && |
| 2423 | type == GL_FLOAT) |
| 2424 | { |
| 2425 | fastPixelSize = 16; |
| 2426 | } |
| 2427 | |
| 2428 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 2429 | { |
| 2430 | if (fastPixelSize != 0) |
| 2431 | { |
| 2432 | // Fast path for formats which require no translation: |
| 2433 | // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE |
| 2434 | // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT |
| 2435 | // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT |
| 2436 | // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES |
| 2437 | // D3DFMT_A32B32G32R32F to RGBA/FLOAT |
| 2438 | // |
| 2439 | // Note that buffers with no alpha go through the slow path below. |
| 2440 | memcpy(dest + j * outputPitch, |
| 2441 | source + j * inputPitch, |
| 2442 | (rect.right - rect.left) * fastPixelSize); |
| 2443 | continue; |
| 2444 | } |
| 2445 | |
| 2446 | for (int i = 0; i < rect.right - rect.left; i++) |
| 2447 | { |
| 2448 | float r; |
| 2449 | float g; |
| 2450 | float b; |
| 2451 | float a; |
| 2452 | |
| 2453 | switch (desc.Format) |
| 2454 | { |
| 2455 | case D3DFMT_R5G6B5: |
| 2456 | { |
| 2457 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 2458 | |
| 2459 | a = 1.0f; |
| 2460 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 2461 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 2462 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 2463 | } |
| 2464 | break; |
| 2465 | case D3DFMT_A1R5G5B5: |
| 2466 | { |
| 2467 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 2468 | |
| 2469 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 2470 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 2471 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 2472 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 2473 | } |
| 2474 | break; |
| 2475 | case D3DFMT_A8R8G8B8: |
| 2476 | { |
| 2477 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2478 | |
| 2479 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 2480 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2481 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2482 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2483 | } |
| 2484 | break; |
| 2485 | case D3DFMT_X8R8G8B8: |
| 2486 | { |
| 2487 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2488 | |
| 2489 | a = 1.0f; |
| 2490 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2491 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2492 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2493 | } |
| 2494 | break; |
| 2495 | case D3DFMT_A2R10G10B10: |
| 2496 | { |
| 2497 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2498 | |
| 2499 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 2500 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 2501 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 2502 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 2503 | } |
| 2504 | break; |
| 2505 | case D3DFMT_A32B32G32R32F: |
| 2506 | { |
| 2507 | // float formats in D3D are stored rgba, rather than the other way round |
| 2508 | r = *((float*)(source + 16 * i + j * inputPitch) + 0); |
| 2509 | g = *((float*)(source + 16 * i + j * inputPitch) + 1); |
| 2510 | b = *((float*)(source + 16 * i + j * inputPitch) + 2); |
| 2511 | a = *((float*)(source + 16 * i + j * inputPitch) + 3); |
| 2512 | } |
| 2513 | break; |
| 2514 | case D3DFMT_A16B16G16R16F: |
| 2515 | { |
| 2516 | // float formats in D3D are stored rgba, rather than the other way round |
| 2517 | r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0)); |
| 2518 | g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1)); |
| 2519 | b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2)); |
| 2520 | a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3)); |
| 2521 | } |
| 2522 | break; |
| 2523 | default: |
| 2524 | UNIMPLEMENTED(); // FIXME |
| 2525 | UNREACHABLE(); |
| 2526 | return; |
| 2527 | } |
| 2528 | |
| 2529 | switch (format) |
| 2530 | { |
| 2531 | case GL_RGBA: |
| 2532 | switch (type) |
| 2533 | { |
| 2534 | case GL_UNSIGNED_BYTE: |
| 2535 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 2536 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2537 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 2538 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 2539 | break; |
| 2540 | default: UNREACHABLE(); |
| 2541 | } |
| 2542 | break; |
| 2543 | case GL_BGRA_EXT: |
| 2544 | switch (type) |
| 2545 | { |
| 2546 | case GL_UNSIGNED_BYTE: |
| 2547 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); |
| 2548 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2549 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); |
| 2550 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 2551 | break; |
| 2552 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 2553 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2554 | // this type is packed as follows: |
| 2555 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2556 | // -------------------------------------------------------------------------------- |
| 2557 | // | 4th | 3rd | 2nd | 1st component | |
| 2558 | // -------------------------------------------------------------------------------- |
| 2559 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2560 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2561 | ((unsigned short)(15 * a + 0.5f) << 12)| |
| 2562 | ((unsigned short)(15 * r + 0.5f) << 8) | |
| 2563 | ((unsigned short)(15 * g + 0.5f) << 4) | |
| 2564 | ((unsigned short)(15 * b + 0.5f) << 0); |
| 2565 | break; |
| 2566 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 2567 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2568 | // this type is packed as follows: |
| 2569 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2570 | // -------------------------------------------------------------------------------- |
| 2571 | // | 4th | 3rd | 2nd | 1st component | |
| 2572 | // -------------------------------------------------------------------------------- |
| 2573 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2574 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2575 | ((unsigned short)( a + 0.5f) << 15) | |
| 2576 | ((unsigned short)(31 * r + 0.5f) << 10) | |
| 2577 | ((unsigned short)(31 * g + 0.5f) << 5) | |
| 2578 | ((unsigned short)(31 * b + 0.5f) << 0); |
| 2579 | break; |
| 2580 | default: UNREACHABLE(); |
| 2581 | } |
| 2582 | break; |
| 2583 | case GL_RGB: |
| 2584 | switch (type) |
| 2585 | { |
| 2586 | case GL_UNSIGNED_SHORT_5_6_5: |
| 2587 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2588 | ((unsigned short)(31 * b + 0.5f) << 0) | |
| 2589 | ((unsigned short)(63 * g + 0.5f) << 5) | |
| 2590 | ((unsigned short)(31 * r + 0.5f) << 11); |
| 2591 | break; |
| 2592 | case GL_UNSIGNED_BYTE: |
| 2593 | dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 2594 | dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2595 | dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 2596 | break; |
| 2597 | default: UNREACHABLE(); |
| 2598 | } |
| 2599 | break; |
| 2600 | default: UNREACHABLE(); |
| 2601 | } |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | systemSurface->UnlockRect(); |
| 2606 | |
| 2607 | systemSurface->Release(); |
| 2608 | } |
| 2609 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2610 | RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth) |
| 2611 | { |
| 2612 | SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); |
| 2613 | IDirect3DSurface9 *surface = NULL; |
| 2614 | if (depth) |
| 2615 | { |
| 2616 | surface = swapChain9->getDepthStencil(); |
| 2617 | } |
| 2618 | else |
| 2619 | { |
| 2620 | surface = swapChain9->getRenderTarget(); |
| 2621 | } |
| 2622 | |
| 2623 | RenderTarget9 *renderTarget = new RenderTarget9(this, surface); |
| 2624 | |
| 2625 | return renderTarget; |
| 2626 | } |
| 2627 | |
| 2628 | RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) |
| 2629 | { |
| 2630 | RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples); |
| 2631 | return renderTarget; |
| 2632 | } |
| 2633 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2634 | bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 2635 | { |
| 2636 | return mBlit->boxFilter(source, dest); |
| 2637 | } |
| 2638 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2639 | D3DPOOL Renderer9::getTexturePool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2640 | { |
| 2641 | if (mD3d9Ex != NULL) |
| 2642 | { |
| 2643 | return D3DPOOL_DEFAULT; |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 2648 | { |
| 2649 | return D3DPOOL_MANAGED; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | return D3DPOOL_DEFAULT; |
| 2654 | } |
| 2655 | |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2656 | bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) |
| 2657 | { |
| 2658 | if (source && dest) |
| 2659 | { |
| 2660 | HRESULT result = D3DERR_OUTOFVIDEOMEMORY; |
| 2661 | IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE |
| 2662 | |
| 2663 | if (fromManaged) |
| 2664 | { |
| 2665 | D3DSURFACE_DESC desc; |
| 2666 | source->GetDesc(&desc); |
| 2667 | |
| 2668 | IDirect3DSurface9 *surf = 0; |
| 2669 | result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); |
| 2670 | |
| 2671 | if (SUCCEEDED(result)) |
| 2672 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2673 | Image::CopyLockableSurfaces(surf, source); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2674 | result = device->UpdateSurface(surf, NULL, dest, NULL); |
| 2675 | surf->Release(); |
| 2676 | } |
| 2677 | } |
| 2678 | else |
| 2679 | { |
| 2680 | endScene(); |
| 2681 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 2682 | } |
| 2683 | |
| 2684 | if (FAILED(result)) |
| 2685 | { |
| 2686 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 2687 | return false; |
| 2688 | } |
| 2689 | } |
| 2690 | |
| 2691 | return true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2694 | } |