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