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