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 | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 600 | IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 601 | { |
| 602 | return mVertexShaderCache.create(function, length); |
| 603 | } |
| 604 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 605 | IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 606 | { |
| 607 | return mPixelShaderCache.create(function, length); |
| 608 | } |
| 609 | |
daniel@transgaming.com | 113f0eb | 2012-10-31 18:46:58 +0000 | [diff] [blame] | 610 | HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) |
| 611 | { |
| 612 | D3DPOOL Pool = getBufferPool(Usage); |
| 613 | return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); |
| 614 | } |
| 615 | |
| 616 | HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) |
| 617 | { |
| 618 | D3DPOOL Pool = getBufferPool(Usage); |
| 619 | return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); |
| 620 | } |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 621 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 622 | 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] | 623 | { |
| 624 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 625 | int d3dSampler = index + d3dSamplerOffset; |
| 626 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 627 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); |
| 628 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 629 | |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 630 | 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] | 631 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
daniel@transgaming.com | 682a37c | 2012-11-28 19:34:44 +0000 | [diff] [blame] | 632 | gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 633 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 634 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 635 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 636 | if (mSupportsTextureFilterAnisotropy) |
| 637 | { |
| 638 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 639 | } |
| 640 | } |
| 641 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 642 | void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 643 | { |
| 644 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 645 | int d3dSampler = index + d3dSamplerOffset; |
| 646 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
| 647 | |
| 648 | if (texture) |
| 649 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 650 | TextureStorage *texStorage = texture->getNativeTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 651 | if (texStorage) |
| 652 | { |
| 653 | d3dTexture = texStorage->getBaseTexture(); |
| 654 | } |
| 655 | // If we get NULL back from getBaseTexture here, something went wrong |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 656 | // in the texture class and we're unexpectedly missing the d3d texture |
| 657 | ASSERT(d3dTexture != NULL); |
| 658 | } |
| 659 | |
| 660 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 661 | } |
| 662 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 663 | void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize) |
| 664 | { |
| 665 | bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; |
| 666 | bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize; |
| 667 | |
| 668 | if (rasterStateChanged) |
| 669 | { |
| 670 | // Set the cull mode |
| 671 | if (rasterState.cullFace) |
| 672 | { |
| 673 | mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 678 | } |
| 679 | |
| 680 | mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE); |
| 681 | |
| 682 | mCurRasterState = rasterState; |
| 683 | } |
| 684 | |
| 685 | if (rasterStateChanged || depthSizeChanged) |
| 686 | { |
| 687 | if (rasterState.polygonOffsetFill) |
| 688 | { |
| 689 | if (depthSize > 0) |
| 690 | { |
| 691 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); |
| 692 | |
| 693 | float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 694 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); |
| 695 | } |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 700 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 701 | } |
| 702 | |
| 703 | mCurDepthSize = depthSize; |
| 704 | } |
| 705 | |
| 706 | mForceSetRasterState = false; |
| 707 | } |
| 708 | |
| 709 | void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) |
| 710 | { |
| 711 | bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; |
| 712 | bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; |
| 713 | bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; |
| 714 | |
| 715 | if (blendStateChanged || blendColorChanged) |
| 716 | { |
| 717 | if (blendState.blend) |
| 718 | { |
| 719 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 720 | |
| 721 | if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 722 | blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 723 | { |
| 724 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), |
| 729 | gl::unorm<8>(blendColor.alpha), |
| 730 | gl::unorm<8>(blendColor.alpha), |
| 731 | gl::unorm<8>(blendColor.alpha))); |
| 732 | } |
| 733 | |
| 734 | mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); |
| 735 | mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); |
| 736 | mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); |
| 737 | |
| 738 | if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || |
| 739 | blendState.destBlendRGB != blendState.destBlendAlpha || |
| 740 | blendState.blendEquationRGB != blendState.blendEquationAlpha) |
| 741 | { |
| 742 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 743 | |
| 744 | mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); |
| 745 | mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); |
| 746 | mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 751 | } |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 756 | } |
| 757 | |
| 758 | if (blendState.sampleAlphaToCoverage) |
| 759 | { |
| 760 | FIXME("Sample alpha to coverage is unimplemented."); |
| 761 | } |
| 762 | |
| 763 | // Set the color mask |
| 764 | bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; |
| 765 | // Apparently some ATI cards have a bug where a draw with a zero color |
| 766 | // write mask can cause later draws to have incorrect results. Instead, |
| 767 | // set a nonzero color write mask but modify the blend state so that no |
| 768 | // drawing is done. |
| 769 | // http://code.google.com/p/angleproject/issues/detail?id=169 |
| 770 | |
| 771 | DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, |
| 772 | blendState.colorMaskBlue, blendState.colorMaskAlpha); |
| 773 | if (colorMask == 0 && !zeroColorMaskAllowed) |
| 774 | { |
| 775 | // Enable green channel, but set blending so nothing will be drawn. |
| 776 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); |
| 777 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 778 | |
| 779 | mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); |
| 780 | mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); |
| 781 | mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); |
| 786 | } |
| 787 | |
| 788 | mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); |
| 789 | |
| 790 | mCurBlendState = blendState; |
| 791 | mCurBlendColor = blendColor; |
| 792 | } |
| 793 | |
| 794 | if (sampleMaskChanged) |
| 795 | { |
| 796 | // Set the multisample mask |
| 797 | mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 798 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); |
| 799 | |
| 800 | mCurSampleMask = sampleMask; |
| 801 | } |
| 802 | |
| 803 | mForceSetBlendState = false; |
| 804 | } |
| 805 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 806 | void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
| 807 | int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 808 | { |
| 809 | bool depthStencilStateChanged = mForceSetDepthStencilState || |
| 810 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 811 | bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || |
| 812 | stencilBackRef != mCurStencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 813 | bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; |
| 814 | bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize; |
| 815 | |
| 816 | if (depthStencilStateChanged) |
| 817 | { |
| 818 | if (depthStencilState.depthTest) |
| 819 | { |
| 820 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 821 | mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 826 | } |
| 827 | |
| 828 | mCurDepthStencilState = depthStencilState; |
| 829 | } |
| 830 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 831 | if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 832 | { |
| 833 | if (depthStencilState.stencilTest && stencilSize > 0) |
| 834 | { |
| 835 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 836 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 837 | |
| 838 | // FIXME: Unsupported by D3D9 |
| 839 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 840 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 841 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 842 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 843 | stencilRef != stencilBackRef || |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 844 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 845 | { |
| 846 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 847 | return error(GL_INVALID_OPERATION); |
| 848 | } |
| 849 | |
| 850 | // get the maximum size of the stencil ref |
| 851 | GLuint maxStencil = (1 << stencilSize) - 1; |
| 852 | |
| 853 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 854 | depthStencilState.stencilWritemask); |
| 855 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 856 | gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); |
| 857 | |
| 858 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 859 | (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 860 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 861 | depthStencilState.stencilMask); |
| 862 | |
| 863 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 864 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); |
| 865 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 866 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); |
| 867 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 868 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); |
| 869 | |
| 870 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 871 | depthStencilState.stencilBackWritemask); |
| 872 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 873 | gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); |
| 874 | |
| 875 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 876 | (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 877 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 878 | depthStencilState.stencilBackMask); |
| 879 | |
| 880 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 881 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); |
| 882 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 883 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); |
| 884 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 885 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 890 | } |
| 891 | |
| 892 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); |
| 893 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 894 | mCurStencilRef = stencilRef; |
| 895 | mCurStencilBackRef = stencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 896 | mCurFrontFaceCCW = frontFaceCCW; |
| 897 | mCurStencilSize = stencilSize; |
| 898 | } |
| 899 | |
| 900 | mForceSetDepthStencilState = false; |
| 901 | } |
| 902 | |
daniel@transgaming.com | 04f1b33 | 2012-11-28 21:00:40 +0000 | [diff] [blame] | 903 | void Renderer9::setScissorRectangle(const gl::Rectangle &scissor) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 904 | { |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 905 | bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0; |
| 906 | |
daniel@transgaming.com | 04f1b33 | 2012-11-28 21:00:40 +0000 | [diff] [blame] | 907 | if (scissorChanged) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 908 | { |
| 909 | RECT rect; |
daniel@transgaming.com | 04f1b33 | 2012-11-28 21:00:40 +0000 | [diff] [blame] | 910 | rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 911 | rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height)); |
| 912 | rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 913 | rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height)); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 914 | mDevice->SetScissorRect(&rect); |
| 915 | |
| 916 | mCurScissor = scissor; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | mForceSetScissor = false; |
| 920 | } |
| 921 | |
daniel@transgaming.com | e2f7b6b | 2012-11-28 21:00:51 +0000 | [diff] [blame^] | 922 | bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 923 | gl::ProgramBinary *currentProgram, bool forceSetUniforms) |
| 924 | { |
| 925 | bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || |
| 926 | zNear != mCurNear || zFar != mCurFar; |
| 927 | |
| 928 | D3DVIEWPORT9 dxViewport; |
daniel@transgaming.com | e2f7b6b | 2012-11-28 21:00:51 +0000 | [diff] [blame^] | 929 | dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 930 | dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(mRenderTargetDesc.height)); |
| 931 | dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X)); |
| 932 | dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y)); |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 933 | dxViewport.MinZ = zNear; |
| 934 | dxViewport.MaxZ = zFar; |
| 935 | |
| 936 | if (dxViewport.Width <= 0 || dxViewport.Height <= 0) |
| 937 | { |
| 938 | return false; // Nothing to render |
| 939 | } |
| 940 | |
| 941 | if (viewportChanged) |
| 942 | { |
| 943 | mDevice->SetViewport(&dxViewport); |
| 944 | |
| 945 | mCurViewport = viewport; |
| 946 | mCurNear = zNear; |
| 947 | mCurFar = zFar; |
| 948 | } |
| 949 | |
| 950 | if (currentProgram && (viewportChanged || forceSetUniforms)) |
| 951 | { |
| 952 | GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation(); |
| 953 | GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height }; |
| 954 | currentProgram->setUniform2fv(halfPixelSize, 1, xy); |
| 955 | |
| 956 | // These values are used for computing gl_FragCoord in Program::linkVaryings(). |
| 957 | GLint coord = currentProgram->getDxCoordLocation(); |
| 958 | GLfloat whxy[4] = { viewport.width * 0.5f, |
| 959 | viewport.height * 0.5f, |
| 960 | viewport.x + (viewport.width * 0.5f), |
| 961 | viewport.y + (viewport.height * 0.5f) }; |
| 962 | currentProgram->setUniform4fv(coord, 1, whxy); |
| 963 | |
| 964 | GLint depth = currentProgram->getDxDepthLocation(); |
| 965 | GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f }; |
| 966 | currentProgram->setUniform2fv(depth, 1, dz); |
| 967 | |
| 968 | GLint depthRange = currentProgram->getDxDepthRangeLocation(); |
| 969 | GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear }; |
| 970 | currentProgram->setUniform3fv(depthRange, 1, nearFarDiff); |
| 971 | } |
| 972 | |
| 973 | mForceSetViewport = false; |
| 974 | return true; |
| 975 | } |
| 976 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 977 | bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count) |
| 978 | { |
| 979 | switch (mode) |
| 980 | { |
| 981 | case GL_POINTS: |
| 982 | mPrimitiveType = D3DPT_POINTLIST; |
| 983 | mPrimitiveCount = count; |
| 984 | break; |
| 985 | case GL_LINES: |
| 986 | mPrimitiveType = D3DPT_LINELIST; |
| 987 | mPrimitiveCount = count / 2; |
| 988 | break; |
| 989 | case GL_LINE_LOOP: |
| 990 | mPrimitiveType = D3DPT_LINESTRIP; |
| 991 | mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately |
| 992 | break; |
| 993 | case GL_LINE_STRIP: |
| 994 | mPrimitiveType = D3DPT_LINESTRIP; |
| 995 | mPrimitiveCount = count - 1; |
| 996 | break; |
| 997 | case GL_TRIANGLES: |
| 998 | mPrimitiveType = D3DPT_TRIANGLELIST; |
| 999 | mPrimitiveCount = count / 3; |
| 1000 | break; |
| 1001 | case GL_TRIANGLE_STRIP: |
| 1002 | mPrimitiveType = D3DPT_TRIANGLESTRIP; |
| 1003 | mPrimitiveCount = count - 2; |
| 1004 | break; |
| 1005 | case GL_TRIANGLE_FAN: |
| 1006 | mPrimitiveType = D3DPT_TRIANGLEFAN; |
| 1007 | mPrimitiveCount = count - 2; |
| 1008 | break; |
| 1009 | default: |
| 1010 | return error(GL_INVALID_ENUM, false); |
| 1011 | } |
| 1012 | |
| 1013 | return mPrimitiveCount > 0; |
| 1014 | } |
| 1015 | |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1016 | bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1017 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1018 | // if there is no color attachment we must synthesize a NULL colorattachment |
| 1019 | // to keep the D3D runtime happy. This should only be possible if depth texturing. |
| 1020 | gl::Renderbuffer *renderbufferObject = NULL; |
| 1021 | if (framebuffer->getColorbufferType() != GL_NONE) |
| 1022 | { |
| 1023 | renderbufferObject = framebuffer->getColorbuffer(); |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | renderbufferObject = framebuffer->getNullColorbuffer(); |
| 1028 | } |
| 1029 | if (!renderbufferObject) |
| 1030 | { |
| 1031 | ERR("unable to locate renderbuffer for FBO."); |
| 1032 | return false; |
| 1033 | } |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1034 | |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1035 | bool renderTargetChanged = false; |
| 1036 | unsigned int renderTargetSerial = renderbufferObject->getSerial(); |
| 1037 | if (renderTargetSerial != mAppliedRenderTargetSerial) |
| 1038 | { |
| 1039 | // Apply the render target on the device |
| 1040 | IDirect3DSurface9 *renderTargetSurface = NULL; |
| 1041 | |
| 1042 | RenderTarget *renderTarget = renderbufferObject->getRenderTarget(); |
| 1043 | if (renderTarget) |
| 1044 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1045 | renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | if (!renderTargetSurface) |
| 1049 | { |
| 1050 | ERR("render target pointer unexpectedly null."); |
| 1051 | return false; // Context must be lost |
| 1052 | } |
| 1053 | |
| 1054 | mDevice->SetRenderTarget(0, renderTargetSurface); |
| 1055 | renderTargetSurface->Release(); |
| 1056 | |
| 1057 | mAppliedRenderTargetSerial = renderTargetSerial; |
| 1058 | renderTargetChanged = true; |
| 1059 | } |
| 1060 | |
| 1061 | gl::Renderbuffer *depthStencil = NULL; |
| 1062 | unsigned int depthbufferSerial = 0; |
| 1063 | unsigned int stencilbufferSerial = 0; |
| 1064 | if (framebuffer->getDepthbufferType() != GL_NONE) |
| 1065 | { |
| 1066 | depthStencil = framebuffer->getDepthbuffer(); |
| 1067 | if (!depthStencil) |
| 1068 | { |
| 1069 | ERR("Depth stencil pointer unexpectedly null."); |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | depthbufferSerial = depthStencil->getSerial(); |
| 1074 | } |
| 1075 | else if (framebuffer->getStencilbufferType() != GL_NONE) |
| 1076 | { |
| 1077 | depthStencil = framebuffer->getStencilbuffer(); |
| 1078 | if (!depthStencil) |
| 1079 | { |
| 1080 | ERR("Depth stencil pointer unexpectedly null."); |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | stencilbufferSerial = depthStencil->getSerial(); |
| 1085 | } |
| 1086 | |
| 1087 | if (depthbufferSerial != mAppliedDepthbufferSerial || |
| 1088 | stencilbufferSerial != mAppliedStencilbufferSerial || |
| 1089 | !mDepthStencilInitialized) |
| 1090 | { |
| 1091 | // Apply the depth stencil on the device |
| 1092 | if (depthStencil) |
| 1093 | { |
| 1094 | IDirect3DSurface9 *depthStencilSurface = NULL; |
| 1095 | RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil(); |
| 1096 | |
| 1097 | if (depthStencilRenderTarget) |
| 1098 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1099 | depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | if (!depthStencilSurface) |
| 1103 | { |
| 1104 | ERR("depth stencil pointer unexpectedly null."); |
| 1105 | return false; // Context must be lost |
| 1106 | } |
| 1107 | |
| 1108 | mDevice->SetDepthStencilSurface(depthStencilSurface); |
| 1109 | depthStencilSurface->Release(); |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | mDevice->SetDepthStencilSurface(NULL); |
| 1114 | } |
| 1115 | |
| 1116 | mAppliedDepthbufferSerial = depthbufferSerial; |
| 1117 | mAppliedStencilbufferSerial = stencilbufferSerial; |
| 1118 | mDepthStencilInitialized = true; |
| 1119 | } |
| 1120 | |
| 1121 | if (renderTargetChanged || !mRenderTargetDescInitialized) |
| 1122 | { |
| 1123 | mForceSetScissor = true; |
| 1124 | mForceSetViewport = true; |
| 1125 | |
| 1126 | mRenderTargetDesc.width = renderbufferObject->getWidth(); |
| 1127 | mRenderTargetDesc.height = renderbufferObject->getHeight(); |
| 1128 | mRenderTargetDesc.format = renderbufferObject->getActualFormat(); |
| 1129 | mRenderTargetDescInitialized = true; |
| 1130 | } |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1131 | |
| 1132 | return true; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1133 | } |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 1134 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1135 | 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] | 1136 | { |
| 1137 | gl::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; |
| 1138 | GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); |
| 1139 | if (err != GL_NO_ERROR) |
| 1140 | { |
| 1141 | return err; |
| 1142 | } |
| 1143 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1144 | return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw); |
| 1145 | } |
| 1146 | |
| 1147 | // Applies the indices and element array bindings to the Direct3D 9 device |
| 1148 | GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, gl::TranslatedIndexData *indexInfo) |
| 1149 | { |
| 1150 | IDirect3DIndexBuffer9 *indexBuffer; |
| 1151 | unsigned int serial; |
| 1152 | GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial); |
| 1153 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1154 | if (err == GL_NO_ERROR) |
| 1155 | { |
| 1156 | if (serial != mAppliedIBSerial) |
| 1157 | { |
| 1158 | mDevice->SetIndices(indexBuffer); |
| 1159 | mAppliedIBSerial = serial; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
| 1166 | void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances) |
| 1167 | { |
| 1168 | startScene(); |
| 1169 | |
| 1170 | if (mode == GL_LINE_LOOP) |
| 1171 | { |
| 1172 | drawLineLoop(count, GL_NONE, NULL, 0, NULL); |
| 1173 | } |
| 1174 | else if (instances > 0) |
| 1175 | { |
| 1176 | gl::StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count); |
| 1177 | if (countingIB) |
| 1178 | { |
| 1179 | if (mAppliedIBSerial != countingIB->getSerial()) |
| 1180 | { |
| 1181 | mDevice->SetIndices(countingIB->getBuffer()); |
| 1182 | mAppliedIBSerial = countingIB->getSerial(); |
| 1183 | } |
| 1184 | |
| 1185 | for (int i = 0; i < mRepeatDraw; i++) |
| 1186 | { |
| 1187 | mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount); |
| 1188 | } |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | ERR("Could not create a counting index buffer for glDrawArraysInstanced."); |
| 1193 | return error(GL_OUT_OF_MEMORY); |
| 1194 | } |
| 1195 | } |
| 1196 | else // Regular case |
| 1197 | { |
| 1198 | mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount); |
| 1199 | } |
| 1200 | } |
| 1201 | |
daniel@transgaming.com | 97400dd | 2012-11-28 20:57:00 +0000 | [diff] [blame] | 1202 | 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] | 1203 | { |
| 1204 | startScene(); |
| 1205 | |
| 1206 | if (mode == GL_LINE_LOOP) |
| 1207 | { |
daniel@transgaming.com | 97400dd | 2012-11-28 20:57:00 +0000 | [diff] [blame] | 1208 | drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | for (int i = 0; i < mRepeatDraw; i++) |
| 1213 | { |
daniel@transgaming.com | 97400dd | 2012-11-28 20:57:00 +0000 | [diff] [blame] | 1214 | GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; |
| 1215 | 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] | 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) |
| 1221 | { |
| 1222 | // Get the raw indices for an indexed draw |
| 1223 | if (type != GL_NONE && elementArrayBuffer) |
| 1224 | { |
| 1225 | gl::Buffer *indexBuffer = elementArrayBuffer; |
| 1226 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 1227 | indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset; |
| 1228 | } |
| 1229 | |
| 1230 | UINT startIndex = 0; |
| 1231 | bool succeeded = false; |
| 1232 | |
| 1233 | if (get32BitIndexSupport()) |
| 1234 | { |
| 1235 | const int spaceNeeded = (count + 1) * sizeof(unsigned int); |
| 1236 | |
| 1237 | if (!mLineLoopIB) |
| 1238 | { |
| 1239 | mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
| 1240 | } |
| 1241 | |
| 1242 | if (mLineLoopIB) |
| 1243 | { |
| 1244 | mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT); |
| 1245 | |
| 1246 | UINT offset = 0; |
| 1247 | unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset)); |
| 1248 | startIndex = offset / 4; |
| 1249 | |
| 1250 | if (data) |
| 1251 | { |
| 1252 | switch (type) |
| 1253 | { |
| 1254 | case GL_NONE: // Non-indexed draw |
| 1255 | for (int i = 0; i < count; i++) |
| 1256 | { |
| 1257 | data[i] = i; |
| 1258 | } |
| 1259 | data[count] = 0; |
| 1260 | break; |
| 1261 | case GL_UNSIGNED_BYTE: |
| 1262 | for (int i = 0; i < count; i++) |
| 1263 | { |
| 1264 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1265 | } |
| 1266 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1267 | break; |
| 1268 | case GL_UNSIGNED_SHORT: |
| 1269 | for (int i = 0; i < count; i++) |
| 1270 | { |
| 1271 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1272 | } |
| 1273 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1274 | break; |
| 1275 | case GL_UNSIGNED_INT: |
| 1276 | for (int i = 0; i < count; i++) |
| 1277 | { |
| 1278 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1279 | } |
| 1280 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1281 | break; |
| 1282 | default: UNREACHABLE(); |
| 1283 | } |
| 1284 | |
| 1285 | mLineLoopIB->unmap(); |
| 1286 | succeeded = true; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | const int spaceNeeded = (count + 1) * sizeof(unsigned short); |
| 1293 | |
| 1294 | if (!mLineLoopIB) |
| 1295 | { |
| 1296 | mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
| 1297 | } |
| 1298 | |
| 1299 | if (mLineLoopIB) |
| 1300 | { |
| 1301 | mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
| 1302 | |
| 1303 | UINT offset = 0; |
| 1304 | unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset)); |
| 1305 | startIndex = offset / 2; |
| 1306 | |
| 1307 | if (data) |
| 1308 | { |
| 1309 | switch (type) |
| 1310 | { |
| 1311 | case GL_NONE: // Non-indexed draw |
| 1312 | for (int i = 0; i < count; i++) |
| 1313 | { |
| 1314 | data[i] = i; |
| 1315 | } |
| 1316 | data[count] = 0; |
| 1317 | break; |
| 1318 | case GL_UNSIGNED_BYTE: |
| 1319 | for (int i = 0; i < count; i++) |
| 1320 | { |
| 1321 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1322 | } |
| 1323 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1324 | break; |
| 1325 | case GL_UNSIGNED_SHORT: |
| 1326 | for (int i = 0; i < count; i++) |
| 1327 | { |
| 1328 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1329 | } |
| 1330 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1331 | break; |
| 1332 | case GL_UNSIGNED_INT: |
| 1333 | for (int i = 0; i < count; i++) |
| 1334 | { |
| 1335 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1336 | } |
| 1337 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1338 | break; |
| 1339 | default: UNREACHABLE(); |
| 1340 | } |
| 1341 | |
| 1342 | mLineLoopIB->unmap(); |
| 1343 | succeeded = true; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | if (succeeded) |
| 1349 | { |
| 1350 | if (mAppliedIBSerial != mLineLoopIB->getSerial()) |
| 1351 | { |
| 1352 | mDevice->SetIndices(mLineLoopIB->getBuffer()); |
| 1353 | mAppliedIBSerial = mLineLoopIB->getSerial(); |
| 1354 | } |
| 1355 | |
| 1356 | mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count); |
| 1357 | } |
| 1358 | else |
| 1359 | { |
| 1360 | ERR("Could not create a looping index buffer for GL_LINE_LOOP."); |
| 1361 | return error(GL_OUT_OF_MEMORY); |
| 1362 | } |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1365 | void Renderer9::applyShaders(gl::ProgramBinary *programBinary) |
| 1366 | { |
daniel@transgaming.com | 9589241 | 2012-11-28 20:59:09 +0000 | [diff] [blame] | 1367 | ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); |
| 1368 | ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); |
| 1369 | |
| 1370 | IDirect3DVertexShader9 *vertexShader = NULL; |
| 1371 | if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader(); |
| 1372 | |
| 1373 | IDirect3DPixelShader9 *pixelShader = NULL; |
| 1374 | if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader(); |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1375 | |
| 1376 | mDevice->SetPixelShader(pixelShader); |
| 1377 | mDevice->SetVertexShader(vertexShader); |
| 1378 | programBinary->dirtyAllUniforms(); |
| 1379 | } |
| 1380 | |
daniel@transgaming.com | 084a257 | 2012-11-28 20:55:17 +0000 | [diff] [blame] | 1381 | void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1382 | { |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1383 | D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha), |
| 1384 | gl::unorm<8>(clearParams.colorClearValue.red), |
| 1385 | gl::unorm<8>(clearParams.colorClearValue.green), |
| 1386 | gl::unorm<8>(clearParams.colorClearValue.blue)); |
| 1387 | float depth = gl::clamp01(clearParams.depthClearValue); |
| 1388 | int stencil = clearParams.stencilClearValue & 0x000000FF; |
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 | unsigned int stencilUnmasked = 0x0; |
| 1391 | if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil()) |
| 1392 | { |
| 1393 | unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); |
| 1394 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1395 | } |
| 1396 | |
| 1397 | bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; |
| 1398 | |
| 1399 | const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && |
| 1400 | (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; |
| 1401 | const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && |
| 1402 | !(clearParams.colorMaskRed && clearParams.colorMaskGreen && |
| 1403 | clearParams.colorMaskBlue && alphaUnmasked); |
| 1404 | |
| 1405 | if (needMaskedColorClear || needMaskedStencilClear) |
| 1406 | { |
| 1407 | // State which is altered in all paths from this point to the clear call is saved. |
| 1408 | // State which is altered in only some paths will be flagged dirty in the case that |
| 1409 | // that path is taken. |
| 1410 | HRESULT hr; |
| 1411 | if (mMaskedClearSavedState == NULL) |
| 1412 | { |
| 1413 | hr = mDevice->BeginStateBlock(); |
| 1414 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1415 | |
| 1416 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1417 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1418 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1419 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1420 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1421 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1422 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1423 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1424 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1425 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1426 | mDevice->SetPixelShader(NULL); |
| 1427 | mDevice->SetVertexShader(NULL); |
| 1428 | mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
| 1429 | mDevice->SetStreamSource(0, NULL, 0, 0); |
| 1430 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1431 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1432 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1433 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1434 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1435 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1436 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1437 | |
| 1438 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1439 | { |
| 1440 | mDevice->SetStreamSourceFreq(i, 1); |
| 1441 | } |
| 1442 | |
| 1443 | hr = mDevice->EndStateBlock(&mMaskedClearSavedState); |
| 1444 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1445 | } |
| 1446 | |
| 1447 | ASSERT(mMaskedClearSavedState != NULL); |
| 1448 | |
| 1449 | if (mMaskedClearSavedState != NULL) |
| 1450 | { |
| 1451 | hr = mMaskedClearSavedState->Capture(); |
| 1452 | ASSERT(SUCCEEDED(hr)); |
| 1453 | } |
| 1454 | |
| 1455 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1456 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1457 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1458 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1459 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1460 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1461 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1462 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1463 | |
| 1464 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1465 | { |
| 1466 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, |
| 1467 | gl_d3d9::ConvertColorMask(clearParams.colorMaskRed, |
| 1468 | clearParams.colorMaskGreen, |
| 1469 | clearParams.colorMaskBlue, |
| 1470 | clearParams.colorMaskAlpha)); |
| 1471 | } |
| 1472 | else |
| 1473 | { |
| 1474 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1475 | } |
| 1476 | |
| 1477 | if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT)) |
| 1478 | { |
| 1479 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1480 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 1481 | mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 1482 | mDevice->SetRenderState(D3DRS_STENCILREF, stencil); |
| 1483 | mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask); |
| 1484 | mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
| 1485 | mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 1486 | mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
| 1487 | } |
| 1488 | else |
| 1489 | { |
| 1490 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1491 | } |
| 1492 | |
| 1493 | mDevice->SetPixelShader(NULL); |
| 1494 | mDevice->SetVertexShader(NULL); |
| 1495 | mDevice->SetFVF(D3DFVF_XYZRHW); |
| 1496 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1497 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1498 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1499 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1500 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1501 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1502 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1503 | |
| 1504 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1505 | { |
| 1506 | mDevice->SetStreamSourceFreq(i, 1); |
| 1507 | } |
| 1508 | |
| 1509 | float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges |
| 1510 | quad[0][0] = -0.5f; |
| 1511 | quad[0][1] = mRenderTargetDesc.height - 0.5f; |
| 1512 | quad[0][2] = 0.0f; |
| 1513 | quad[0][3] = 1.0f; |
| 1514 | |
| 1515 | quad[1][0] = mRenderTargetDesc.width - 0.5f; |
| 1516 | quad[1][1] = mRenderTargetDesc.height - 0.5f; |
| 1517 | quad[1][2] = 0.0f; |
| 1518 | quad[1][3] = 1.0f; |
| 1519 | |
| 1520 | quad[2][0] = -0.5f; |
| 1521 | quad[2][1] = -0.5f; |
| 1522 | quad[2][2] = 0.0f; |
| 1523 | quad[2][3] = 1.0f; |
| 1524 | |
| 1525 | quad[3][0] = mRenderTargetDesc.width - 0.5f; |
| 1526 | quad[3][1] = -0.5f; |
| 1527 | quad[3][2] = 0.0f; |
| 1528 | quad[3][3] = 1.0f; |
| 1529 | |
| 1530 | startScene(); |
| 1531 | mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); |
| 1532 | |
| 1533 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1534 | { |
| 1535 | mDevice->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 1536 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 1537 | mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 1538 | } |
| 1539 | |
| 1540 | if (mMaskedClearSavedState != NULL) |
| 1541 | { |
| 1542 | mMaskedClearSavedState->Apply(); |
| 1543 | } |
| 1544 | } |
| 1545 | else if (clearParams.mask) |
| 1546 | { |
| 1547 | DWORD dxClearFlags = 0; |
| 1548 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1549 | { |
| 1550 | dxClearFlags |= D3DCLEAR_TARGET; |
| 1551 | } |
| 1552 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1553 | { |
| 1554 | dxClearFlags |= D3DCLEAR_ZBUFFER; |
| 1555 | } |
| 1556 | if (clearParams.mask & GL_STENCIL_BUFFER_BIT) |
| 1557 | { |
| 1558 | dxClearFlags |= D3DCLEAR_STENCIL; |
| 1559 | } |
| 1560 | |
| 1561 | mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil); |
| 1562 | } |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1565 | void Renderer9::markAllStateDirty() |
| 1566 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1567 | mAppliedRenderTargetSerial = 0; |
| 1568 | mAppliedDepthbufferSerial = 0; |
| 1569 | mAppliedStencilbufferSerial = 0; |
| 1570 | mDepthStencilInitialized = false; |
| 1571 | mRenderTargetDescInitialized = false; |
| 1572 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1573 | mForceSetDepthStencilState = true; |
| 1574 | mForceSetRasterState = true; |
| 1575 | mForceSetBlendState = true; |
| 1576 | mForceSetScissor = true; |
| 1577 | mForceSetViewport = true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1578 | |
| 1579 | mVertexDeclarationCache.markStateDirty(); |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1582 | void Renderer9::releaseDeviceResources() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1583 | { |
| 1584 | while (!mEventQueryPool.empty()) |
| 1585 | { |
| 1586 | mEventQueryPool.back()->Release(); |
| 1587 | mEventQueryPool.pop_back(); |
| 1588 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1589 | |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1590 | if (mMaskedClearSavedState) |
| 1591 | { |
| 1592 | mMaskedClearSavedState->Release(); |
| 1593 | mMaskedClearSavedState = NULL; |
| 1594 | } |
| 1595 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 1596 | mVertexShaderCache.clear(); |
| 1597 | mPixelShaderCache.clear(); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1598 | |
daniel@transgaming.com | e569fc5 | 2012-11-28 20:56:02 +0000 | [diff] [blame] | 1599 | delete mBlit; |
| 1600 | mBlit = NULL; |
| 1601 | |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1602 | delete mVertexDataManager; |
| 1603 | mVertexDataManager = NULL; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1604 | |
| 1605 | delete mIndexDataManager; |
| 1606 | mIndexDataManager = NULL; |
| 1607 | |
| 1608 | delete mLineLoopIB; |
| 1609 | mLineLoopIB = NULL; |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1613 | void Renderer9::markDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1614 | { |
| 1615 | mDeviceLost = true; |
| 1616 | } |
| 1617 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1618 | bool Renderer9::isDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1619 | { |
| 1620 | return mDeviceLost; |
| 1621 | } |
| 1622 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1623 | // 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] | 1624 | bool Renderer9::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1625 | { |
| 1626 | bool isLost = false; |
| 1627 | |
| 1628 | if (mDeviceEx) |
| 1629 | { |
| 1630 | isLost = FAILED(mDeviceEx->CheckDeviceState(NULL)); |
| 1631 | } |
| 1632 | else if (mDevice) |
| 1633 | { |
| 1634 | isLost = FAILED(mDevice->TestCooperativeLevel()); |
| 1635 | } |
| 1636 | else |
| 1637 | { |
| 1638 | // No device yet, so no reset required |
| 1639 | } |
| 1640 | |
| 1641 | if (isLost) |
| 1642 | { |
| 1643 | // ensure we note the device loss -- |
| 1644 | // we'll probably get this done again by markDeviceLost |
| 1645 | // but best to remember it! |
| 1646 | // Note that we don't want to clear the device loss status here |
| 1647 | // -- this needs to be done by resetDevice |
| 1648 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1649 | if (notify) |
| 1650 | { |
| 1651 | mDisplay->notifyDeviceLost(); |
| 1652 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | return isLost; |
| 1656 | } |
| 1657 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1658 | bool Renderer9::testDeviceResettable() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1659 | { |
| 1660 | HRESULT status = D3D_OK; |
| 1661 | |
| 1662 | if (mDeviceEx) |
| 1663 | { |
| 1664 | status = mDeviceEx->CheckDeviceState(NULL); |
| 1665 | } |
| 1666 | else if (mDevice) |
| 1667 | { |
| 1668 | status = mDevice->TestCooperativeLevel(); |
| 1669 | } |
| 1670 | |
| 1671 | switch (status) |
| 1672 | { |
| 1673 | case D3DERR_DEVICENOTRESET: |
| 1674 | case D3DERR_DEVICEHUNG: |
| 1675 | return true; |
| 1676 | default: |
| 1677 | return false; |
| 1678 | } |
| 1679 | } |
| 1680 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1681 | bool Renderer9::resetDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1682 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 1683 | releaseDeviceResources(); |
| 1684 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1685 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 1686 | |
| 1687 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1688 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1689 | int attempts = 3; |
| 1690 | |
| 1691 | while (lost && attempts > 0) |
| 1692 | { |
| 1693 | if (mDeviceEx) |
| 1694 | { |
| 1695 | Sleep(500); // Give the graphics driver some CPU time |
| 1696 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 1697 | } |
| 1698 | else |
| 1699 | { |
| 1700 | result = mDevice->TestCooperativeLevel(); |
| 1701 | while (result == D3DERR_DEVICELOST) |
| 1702 | { |
| 1703 | Sleep(100); // Give the graphics driver some CPU time |
| 1704 | result = mDevice->TestCooperativeLevel(); |
| 1705 | } |
| 1706 | |
| 1707 | if (result == D3DERR_DEVICENOTRESET) |
| 1708 | { |
| 1709 | result = mDevice->Reset(&presentParameters); |
| 1710 | } |
| 1711 | } |
| 1712 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 1713 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1714 | attempts --; |
| 1715 | } |
| 1716 | |
| 1717 | if (FAILED(result)) |
| 1718 | { |
| 1719 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | // reset device defaults |
| 1724 | initializeDevice(); |
| 1725 | mDeviceLost = false; |
| 1726 | |
| 1727 | return true; |
| 1728 | } |
| 1729 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1730 | DWORD Renderer9::getAdapterVendor() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1731 | { |
| 1732 | return mAdapterIdentifier.VendorId; |
| 1733 | } |
| 1734 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1735 | const char *Renderer9::getAdapterDescription() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1736 | { |
| 1737 | return mAdapterIdentifier.Description; |
| 1738 | } |
| 1739 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1740 | GUID Renderer9::getAdapterIdentifier() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 1741 | { |
| 1742 | return mAdapterIdentifier.DeviceIdentifier; |
| 1743 | } |
| 1744 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1745 | void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1746 | { |
| 1747 | for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) |
| 1748 | { |
| 1749 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, |
| 1750 | TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 1751 | |
| 1752 | multiSampleArray[multiSampleIndex] = SUCCEEDED(result); |
| 1753 | } |
| 1754 | } |
| 1755 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1756 | bool Renderer9::getDXT1TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1757 | { |
| 1758 | D3DDISPLAYMODE currentDisplayMode; |
| 1759 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1760 | |
| 1761 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); |
| 1762 | } |
| 1763 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1764 | bool Renderer9::getDXT3TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1765 | { |
| 1766 | D3DDISPLAYMODE currentDisplayMode; |
| 1767 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1768 | |
| 1769 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); |
| 1770 | } |
| 1771 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1772 | bool Renderer9::getDXT5TextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1773 | { |
| 1774 | D3DDISPLAYMODE currentDisplayMode; |
| 1775 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1776 | |
| 1777 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); |
| 1778 | } |
| 1779 | |
| 1780 | // we use INTZ for depth textures in Direct3D9 |
| 1781 | // we also want NULL texture support to ensure the we can make depth-only FBOs |
| 1782 | // see http://aras-p.info/texts/D3D9GPUHacks.html |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1783 | bool Renderer9::getDepthTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1784 | { |
| 1785 | D3DDISPLAYMODE currentDisplayMode; |
| 1786 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1787 | |
| 1788 | bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1789 | D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)); |
| 1790 | bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, |
| 1791 | D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); |
| 1792 | |
| 1793 | return intz && null; |
| 1794 | } |
| 1795 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1796 | bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1797 | { |
| 1798 | D3DDISPLAYMODE currentDisplayMode; |
| 1799 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1800 | |
| 1801 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1802 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1803 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1804 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1805 | |
| 1806 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1807 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& |
| 1808 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1809 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1810 | |
| 1811 | if (!*filtering && !*renderable) |
| 1812 | { |
| 1813 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1814 | D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && |
| 1815 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1816 | D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | return true; |
| 1821 | } |
| 1822 | } |
| 1823 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1824 | bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1825 | { |
| 1826 | D3DDISPLAYMODE currentDisplayMode; |
| 1827 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1828 | |
| 1829 | *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1830 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1831 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, |
| 1832 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1833 | |
| 1834 | *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1835 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1836 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, |
| 1837 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1838 | |
| 1839 | if (!*filtering && !*renderable) |
| 1840 | { |
| 1841 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1842 | D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && |
| 1843 | SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, |
| 1844 | D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); |
| 1845 | } |
| 1846 | else |
| 1847 | { |
| 1848 | return true; |
| 1849 | } |
| 1850 | } |
| 1851 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1852 | bool Renderer9::getLuminanceTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1853 | { |
| 1854 | D3DDISPLAYMODE currentDisplayMode; |
| 1855 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1856 | |
| 1857 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); |
| 1858 | } |
| 1859 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1860 | bool Renderer9::getLuminanceAlphaTextureSupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1861 | { |
| 1862 | D3DDISPLAYMODE currentDisplayMode; |
| 1863 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1864 | |
| 1865 | return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); |
| 1866 | } |
| 1867 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1868 | bool Renderer9::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1869 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1870 | return mSupportsTextureFilterAnisotropy; |
| 1871 | } |
| 1872 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1873 | float Renderer9::getTextureMaxAnisotropy() const |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 1874 | { |
| 1875 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1876 | { |
| 1877 | return mDeviceCaps.MaxAnisotropy; |
| 1878 | } |
| 1879 | return 1.0f; |
| 1880 | } |
| 1881 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1882 | bool Renderer9::getEventQuerySupport() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1883 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1884 | IDirect3DQuery9 *query = allocateEventQuery(); |
| 1885 | if (query) |
| 1886 | { |
| 1887 | freeEventQuery(query); |
| 1888 | return true; |
| 1889 | } |
| 1890 | else |
| 1891 | { |
| 1892 | return false; |
| 1893 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1894 | return true; |
| 1895 | } |
| 1896 | |
| 1897 | // Only Direct3D 10 ready devices support all the necessary vertex texture formats. |
| 1898 | // 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] | 1899 | bool Renderer9::getVertexTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1900 | { |
| 1901 | if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) |
| 1902 | { |
| 1903 | return false; |
| 1904 | } |
| 1905 | |
| 1906 | D3DDISPLAYMODE currentDisplayMode; |
| 1907 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 1908 | |
| 1909 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F); |
| 1910 | |
| 1911 | return SUCCEEDED(result); |
| 1912 | } |
| 1913 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1914 | bool Renderer9::getNonPower2TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1915 | { |
| 1916 | return mSupportsNonPower2Textures; |
| 1917 | } |
| 1918 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1919 | bool Renderer9::getOcclusionQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1920 | { |
| 1921 | if (!mDevice) |
| 1922 | { |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
| 1926 | IDirect3DQuery9 *query = NULL; |
| 1927 | HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query); |
| 1928 | if (SUCCEEDED(result) && query) |
| 1929 | { |
| 1930 | query->Release(); |
| 1931 | return true; |
| 1932 | } |
| 1933 | else |
| 1934 | { |
| 1935 | return false; |
| 1936 | } |
| 1937 | } |
| 1938 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1939 | bool Renderer9::getInstancingSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1940 | { |
| 1941 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 1942 | } |
| 1943 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1944 | bool Renderer9::getShareHandleSupport() const |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1945 | { |
| 1946 | // PIX doesn't seem to support using share handles, so disable them. |
| 1947 | // D3D9_REPLACE |
daniel@transgaming.com | 7cb796e | 2012-10-31 18:46:44 +0000 | [diff] [blame] | 1948 | return (mD3d9Ex != NULL) && !gl::perfActive(); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 1949 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 1950 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1951 | int Renderer9::getMajorShaderModel() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1952 | { |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1953 | return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion); |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1956 | float Renderer9::getMaxPointSize() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1957 | { |
| 1958 | return mDeviceCaps.MaxPointSize; |
| 1959 | } |
| 1960 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1961 | int Renderer9::getMaxTextureWidth() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1962 | { |
| 1963 | return (int)mDeviceCaps.MaxTextureWidth; |
| 1964 | } |
| 1965 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1966 | int Renderer9::getMaxTextureHeight() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1967 | { |
| 1968 | return (int)mDeviceCaps.MaxTextureHeight; |
| 1969 | } |
| 1970 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1971 | bool Renderer9::get32BitIndexSupport() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1972 | { |
| 1973 | return mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 1974 | } |
| 1975 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1976 | DWORD Renderer9::getCapsDeclTypes() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1977 | { |
| 1978 | return mDeviceCaps.DeclTypes; |
| 1979 | } |
| 1980 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1981 | int Renderer9::getMinSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1982 | { |
| 1983 | return mMinSwapInterval; |
| 1984 | } |
| 1985 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1986 | int Renderer9::getMaxSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 1987 | { |
| 1988 | return mMaxSwapInterval; |
| 1989 | } |
| 1990 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1991 | int Renderer9::getMaxSupportedSamples() const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1992 | { |
| 1993 | return mMaxSupportedSamples; |
| 1994 | } |
| 1995 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 1996 | int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 1997 | { |
| 1998 | if (requested == 0) |
| 1999 | { |
| 2000 | return requested; |
| 2001 | } |
| 2002 | |
| 2003 | std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); |
| 2004 | if (itr == mMultiSampleSupport.end()) |
| 2005 | { |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 2006 | if (format == D3DFMT_UNKNOWN) |
| 2007 | return 0; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2008 | return -1; |
| 2009 | } |
| 2010 | |
| 2011 | for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) |
| 2012 | { |
| 2013 | if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
| 2014 | { |
| 2015 | return i; |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | return -1; |
| 2020 | } |
| 2021 | |
daniel@transgaming.com | a957168 | 2012-11-28 19:33:08 +0000 | [diff] [blame] | 2022 | D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) |
| 2023 | { |
| 2024 | switch (internalformat) |
| 2025 | { |
| 2026 | case GL_DEPTH_COMPONENT16: |
| 2027 | case GL_DEPTH_COMPONENT32_OES: |
| 2028 | case GL_DEPTH24_STENCIL8_OES: |
| 2029 | return D3DFMT_INTZ; |
| 2030 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 2031 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 2032 | return D3DFMT_DXT1; |
| 2033 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 2034 | return D3DFMT_DXT3; |
| 2035 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 2036 | return D3DFMT_DXT5; |
| 2037 | case GL_RGBA32F_EXT: |
| 2038 | case GL_RGB32F_EXT: |
| 2039 | case GL_ALPHA32F_EXT: |
| 2040 | case GL_LUMINANCE32F_EXT: |
| 2041 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 2042 | return D3DFMT_A32B32G32R32F; |
| 2043 | case GL_RGBA16F_EXT: |
| 2044 | case GL_RGB16F_EXT: |
| 2045 | case GL_ALPHA16F_EXT: |
| 2046 | case GL_LUMINANCE16F_EXT: |
| 2047 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 2048 | return D3DFMT_A16B16G16R16F; |
| 2049 | case GL_LUMINANCE8_EXT: |
| 2050 | if (getLuminanceTextureSupport()) |
| 2051 | { |
| 2052 | return D3DFMT_L8; |
| 2053 | } |
| 2054 | break; |
| 2055 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 2056 | if (getLuminanceAlphaTextureSupport()) |
| 2057 | { |
| 2058 | return D3DFMT_A8L8; |
| 2059 | } |
| 2060 | break; |
| 2061 | case GL_RGB8_OES: |
| 2062 | case GL_RGB565: |
| 2063 | return D3DFMT_X8R8G8B8; |
| 2064 | } |
| 2065 | |
| 2066 | return D3DFMT_A8R8G8B8; |
| 2067 | } |
| 2068 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2069 | bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2070 | { |
| 2071 | bool result = false; |
| 2072 | |
| 2073 | if (source && dest) |
| 2074 | { |
| 2075 | int levels = source->levelCount(); |
| 2076 | for (int i = 0; i < levels; ++i) |
| 2077 | { |
| 2078 | IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false); |
| 2079 | IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false); |
| 2080 | |
| 2081 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 2082 | |
| 2083 | if (srcSurf) srcSurf->Release(); |
| 2084 | if (dstSurf) dstSurf->Release(); |
| 2085 | |
| 2086 | if (!result) |
| 2087 | return false; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | return result; |
| 2092 | } |
| 2093 | |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2094 | bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2095 | { |
| 2096 | bool result = false; |
| 2097 | |
| 2098 | if (source && dest) |
| 2099 | { |
| 2100 | int levels = source->levelCount(); |
| 2101 | for (int f = 0; f < 6; f++) |
| 2102 | { |
| 2103 | for (int i = 0; i < levels; i++) |
| 2104 | { |
| 2105 | IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); |
| 2106 | IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); |
| 2107 | |
| 2108 | result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged()); |
| 2109 | |
| 2110 | if (srcSurf) srcSurf->Release(); |
| 2111 | if (dstSurf) dstSurf->Release(); |
| 2112 | |
| 2113 | if (!result) |
| 2114 | return false; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | return result; |
| 2120 | } |
| 2121 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2122 | D3DPOOL Renderer9::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2123 | { |
| 2124 | if (mD3d9Ex != NULL) |
| 2125 | { |
| 2126 | return D3DPOOL_DEFAULT; |
| 2127 | } |
| 2128 | else |
| 2129 | { |
| 2130 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 2131 | { |
| 2132 | return D3DPOOL_MANAGED; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | return D3DPOOL_DEFAULT; |
| 2137 | } |
| 2138 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2139 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 2140 | GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2141 | { |
| 2142 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level); |
| 2143 | } |
| 2144 | |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2145 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, |
| 2146 | GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2147 | { |
| 2148 | return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level); |
| 2149 | } |
| 2150 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2151 | bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect, |
| 2152 | bool blitRenderTarget, bool blitDepthStencil) |
| 2153 | { |
| 2154 | endScene(); |
| 2155 | |
| 2156 | if (blitRenderTarget) |
| 2157 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2158 | gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(); |
| 2159 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(); |
| 2160 | RenderTarget9 *readRenderTarget = NULL; |
| 2161 | RenderTarget9 *drawRenderTarget = NULL; |
| 2162 | IDirect3DSurface9* readSurface = NULL; |
| 2163 | IDirect3DSurface9* drawSurface = NULL; |
| 2164 | |
| 2165 | if (readBuffer) |
| 2166 | { |
| 2167 | readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); |
| 2168 | } |
| 2169 | if (drawBuffer) |
| 2170 | { |
| 2171 | drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); |
| 2172 | } |
| 2173 | |
| 2174 | if (readRenderTarget) |
| 2175 | { |
| 2176 | readSurface = readRenderTarget->getSurface(); |
| 2177 | } |
| 2178 | if (drawRenderTarget) |
| 2179 | { |
| 2180 | drawSurface = drawRenderTarget->getSurface(); |
| 2181 | } |
| 2182 | |
| 2183 | if (!readSurface || !drawSurface) |
| 2184 | { |
| 2185 | ERR("Failed to retrieve the render target."); |
| 2186 | return error(GL_OUT_OF_MEMORY, false); |
| 2187 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2188 | |
| 2189 | RECT srcRect, dstRect; |
| 2190 | RECT *srcRectPtr = NULL; |
| 2191 | RECT *dstRectPtr = NULL; |
| 2192 | |
| 2193 | if (readRect) |
| 2194 | { |
| 2195 | srcRect.left = readRect->x; |
| 2196 | srcRect.right = readRect->x + readRect->width; |
| 2197 | srcRect.top = readRect->y; |
| 2198 | srcRect.bottom = readRect->y + readRect->height; |
| 2199 | srcRectPtr = &srcRect; |
| 2200 | } |
| 2201 | |
| 2202 | if (drawRect) |
| 2203 | { |
| 2204 | dstRect.left = drawRect->x; |
| 2205 | dstRect.right = drawRect->x + drawRect->width; |
| 2206 | dstRect.top = drawRect->y; |
| 2207 | dstRect.bottom = drawRect->y + drawRect->height; |
| 2208 | dstRectPtr = &dstRect; |
| 2209 | } |
| 2210 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2211 | HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2212 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2213 | readSurface->Release(); |
| 2214 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2215 | |
| 2216 | if (FAILED(result)) |
| 2217 | { |
| 2218 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2219 | return false; |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | if (blitDepthStencil) |
| 2224 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2225 | gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); |
| 2226 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); |
| 2227 | RenderTarget9 *readDepthStencil = NULL; |
| 2228 | RenderTarget9 *drawDepthStencil = NULL; |
| 2229 | IDirect3DSurface9* readSurface = NULL; |
| 2230 | IDirect3DSurface9* drawSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2231 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2232 | if (readBuffer) |
| 2233 | { |
| 2234 | readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); |
| 2235 | } |
| 2236 | if (drawBuffer) |
| 2237 | { |
| 2238 | drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); |
| 2239 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2240 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2241 | if (readDepthStencil) |
| 2242 | { |
| 2243 | readSurface = readDepthStencil->getSurface(); |
| 2244 | } |
| 2245 | if (drawDepthStencil) |
| 2246 | { |
| 2247 | drawSurface = drawDepthStencil->getSurface(); |
| 2248 | } |
| 2249 | |
| 2250 | if (!readSurface || !drawSurface) |
| 2251 | { |
| 2252 | ERR("Failed to retrieve the render target."); |
| 2253 | return error(GL_OUT_OF_MEMORY, false); |
| 2254 | } |
| 2255 | |
| 2256 | HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); |
| 2257 | |
| 2258 | readSurface->Release(); |
| 2259 | drawSurface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (FAILED(result)) |
| 2262 | { |
| 2263 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2264 | return false; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | return true; |
| 2269 | } |
| 2270 | |
| 2271 | void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2272 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2273 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2274 | RenderTarget9 *renderTarget = NULL; |
| 2275 | IDirect3DSurface9 *surface = NULL; |
| 2276 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2277 | |
| 2278 | if (colorbuffer) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2279 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2280 | renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); |
| 2281 | } |
| 2282 | |
| 2283 | if (renderTarget) |
| 2284 | { |
| 2285 | surface = renderTarget->getSurface(); |
| 2286 | } |
| 2287 | |
| 2288 | if (!surface) |
| 2289 | { |
| 2290 | // context must be lost |
| 2291 | return; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | D3DSURFACE_DESC desc; |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2295 | surface->GetDesc(&desc); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2296 | |
| 2297 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 2298 | { |
| 2299 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2300 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2301 | return error(GL_OUT_OF_MEMORY); |
| 2302 | } |
| 2303 | |
| 2304 | HRESULT result; |
| 2305 | IDirect3DSurface9 *systemSurface = NULL; |
| 2306 | bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && |
| 2307 | x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && |
| 2308 | desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; |
| 2309 | if (directToPixels) |
| 2310 | { |
| 2311 | // Use the pixels ptr as a shared handle to write directly into client's memory |
| 2312 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 2313 | D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); |
| 2314 | if (FAILED(result)) |
| 2315 | { |
| 2316 | // Try again without the shared handle |
| 2317 | directToPixels = false; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | if (!directToPixels) |
| 2322 | { |
| 2323 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 2324 | D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 2325 | if (FAILED(result)) |
| 2326 | { |
| 2327 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2328 | surface->Release(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2329 | return error(GL_OUT_OF_MEMORY); |
| 2330 | } |
| 2331 | } |
| 2332 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2333 | result = mDevice->GetRenderTargetData(surface, systemSurface); |
| 2334 | surface->Release(); |
| 2335 | surface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2336 | |
| 2337 | if (FAILED(result)) |
| 2338 | { |
| 2339 | systemSurface->Release(); |
| 2340 | |
| 2341 | // It turns out that D3D will sometimes produce more error |
| 2342 | // codes than those documented. |
daniel@transgaming.com | 414c916 | 2012-11-28 20:54:57 +0000 | [diff] [blame] | 2343 | if (checkDeviceLost(result)) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2344 | return error(GL_OUT_OF_MEMORY); |
| 2345 | else |
| 2346 | { |
| 2347 | UNREACHABLE(); |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | } |
| 2352 | |
| 2353 | if (directToPixels) |
| 2354 | { |
| 2355 | systemSurface->Release(); |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | RECT rect; |
| 2360 | rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width)); |
| 2361 | rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height)); |
| 2362 | rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width)); |
| 2363 | rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height)); |
| 2364 | |
| 2365 | D3DLOCKED_RECT lock; |
| 2366 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 2367 | |
| 2368 | if (FAILED(result)) |
| 2369 | { |
| 2370 | UNREACHABLE(); |
| 2371 | systemSurface->Release(); |
| 2372 | |
| 2373 | return; // No sensible error to generate |
| 2374 | } |
| 2375 | |
| 2376 | unsigned char *dest = (unsigned char*)pixels; |
| 2377 | unsigned short *dest16 = (unsigned short*)pixels; |
| 2378 | |
| 2379 | unsigned char *source; |
| 2380 | int inputPitch; |
| 2381 | if (packReverseRowOrder) |
| 2382 | { |
| 2383 | source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); |
| 2384 | inputPitch = -lock.Pitch; |
| 2385 | } |
| 2386 | else |
| 2387 | { |
| 2388 | source = (unsigned char*)lock.pBits; |
| 2389 | inputPitch = lock.Pitch; |
| 2390 | } |
| 2391 | |
| 2392 | unsigned int fastPixelSize = 0; |
| 2393 | |
| 2394 | if (desc.Format == D3DFMT_A8R8G8B8 && |
| 2395 | format == GL_BGRA_EXT && |
| 2396 | type == GL_UNSIGNED_BYTE) |
| 2397 | { |
| 2398 | fastPixelSize = 4; |
| 2399 | } |
| 2400 | else if ((desc.Format == D3DFMT_A4R4G4B4 && |
| 2401 | format == GL_BGRA_EXT && |
| 2402 | type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) || |
| 2403 | (desc.Format == D3DFMT_A1R5G5B5 && |
| 2404 | format == GL_BGRA_EXT && |
| 2405 | type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)) |
| 2406 | { |
| 2407 | fastPixelSize = 2; |
| 2408 | } |
| 2409 | else if (desc.Format == D3DFMT_A16B16G16R16F && |
| 2410 | format == GL_RGBA && |
| 2411 | type == GL_HALF_FLOAT_OES) |
| 2412 | { |
| 2413 | fastPixelSize = 8; |
| 2414 | } |
| 2415 | else if (desc.Format == D3DFMT_A32B32G32R32F && |
| 2416 | format == GL_RGBA && |
| 2417 | type == GL_FLOAT) |
| 2418 | { |
| 2419 | fastPixelSize = 16; |
| 2420 | } |
| 2421 | |
| 2422 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 2423 | { |
| 2424 | if (fastPixelSize != 0) |
| 2425 | { |
| 2426 | // Fast path for formats which require no translation: |
| 2427 | // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE |
| 2428 | // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT |
| 2429 | // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT |
| 2430 | // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES |
| 2431 | // D3DFMT_A32B32G32R32F to RGBA/FLOAT |
| 2432 | // |
| 2433 | // Note that buffers with no alpha go through the slow path below. |
| 2434 | memcpy(dest + j * outputPitch, |
| 2435 | source + j * inputPitch, |
| 2436 | (rect.right - rect.left) * fastPixelSize); |
| 2437 | continue; |
| 2438 | } |
| 2439 | |
| 2440 | for (int i = 0; i < rect.right - rect.left; i++) |
| 2441 | { |
| 2442 | float r; |
| 2443 | float g; |
| 2444 | float b; |
| 2445 | float a; |
| 2446 | |
| 2447 | switch (desc.Format) |
| 2448 | { |
| 2449 | case D3DFMT_R5G6B5: |
| 2450 | { |
| 2451 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 2452 | |
| 2453 | a = 1.0f; |
| 2454 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 2455 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 2456 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 2457 | } |
| 2458 | break; |
| 2459 | case D3DFMT_A1R5G5B5: |
| 2460 | { |
| 2461 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
| 2462 | |
| 2463 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 2464 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 2465 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 2466 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 2467 | } |
| 2468 | break; |
| 2469 | case D3DFMT_A8R8G8B8: |
| 2470 | { |
| 2471 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2472 | |
| 2473 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 2474 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2475 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2476 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2477 | } |
| 2478 | break; |
| 2479 | case D3DFMT_X8R8G8B8: |
| 2480 | { |
| 2481 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2482 | |
| 2483 | a = 1.0f; |
| 2484 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2485 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2486 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2487 | } |
| 2488 | break; |
| 2489 | case D3DFMT_A2R10G10B10: |
| 2490 | { |
| 2491 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
| 2492 | |
| 2493 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 2494 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 2495 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 2496 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 2497 | } |
| 2498 | break; |
| 2499 | case D3DFMT_A32B32G32R32F: |
| 2500 | { |
| 2501 | // float formats in D3D are stored rgba, rather than the other way round |
| 2502 | r = *((float*)(source + 16 * i + j * inputPitch) + 0); |
| 2503 | g = *((float*)(source + 16 * i + j * inputPitch) + 1); |
| 2504 | b = *((float*)(source + 16 * i + j * inputPitch) + 2); |
| 2505 | a = *((float*)(source + 16 * i + j * inputPitch) + 3); |
| 2506 | } |
| 2507 | break; |
| 2508 | case D3DFMT_A16B16G16R16F: |
| 2509 | { |
| 2510 | // float formats in D3D are stored rgba, rather than the other way round |
| 2511 | r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0)); |
| 2512 | g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1)); |
| 2513 | b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2)); |
| 2514 | a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3)); |
| 2515 | } |
| 2516 | break; |
| 2517 | default: |
| 2518 | UNIMPLEMENTED(); // FIXME |
| 2519 | UNREACHABLE(); |
| 2520 | return; |
| 2521 | } |
| 2522 | |
| 2523 | switch (format) |
| 2524 | { |
| 2525 | case GL_RGBA: |
| 2526 | switch (type) |
| 2527 | { |
| 2528 | case GL_UNSIGNED_BYTE: |
| 2529 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 2530 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2531 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 2532 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 2533 | break; |
| 2534 | default: UNREACHABLE(); |
| 2535 | } |
| 2536 | break; |
| 2537 | case GL_BGRA_EXT: |
| 2538 | switch (type) |
| 2539 | { |
| 2540 | case GL_UNSIGNED_BYTE: |
| 2541 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); |
| 2542 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2543 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); |
| 2544 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 2545 | break; |
| 2546 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 2547 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2548 | // this type is packed as follows: |
| 2549 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2550 | // -------------------------------------------------------------------------------- |
| 2551 | // | 4th | 3rd | 2nd | 1st component | |
| 2552 | // -------------------------------------------------------------------------------- |
| 2553 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2554 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2555 | ((unsigned short)(15 * a + 0.5f) << 12)| |
| 2556 | ((unsigned short)(15 * r + 0.5f) << 8) | |
| 2557 | ((unsigned short)(15 * g + 0.5f) << 4) | |
| 2558 | ((unsigned short)(15 * b + 0.5f) << 0); |
| 2559 | break; |
| 2560 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 2561 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2562 | // this type is packed as follows: |
| 2563 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2564 | // -------------------------------------------------------------------------------- |
| 2565 | // | 4th | 3rd | 2nd | 1st component | |
| 2566 | // -------------------------------------------------------------------------------- |
| 2567 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2568 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2569 | ((unsigned short)( a + 0.5f) << 15) | |
| 2570 | ((unsigned short)(31 * r + 0.5f) << 10) | |
| 2571 | ((unsigned short)(31 * g + 0.5f) << 5) | |
| 2572 | ((unsigned short)(31 * b + 0.5f) << 0); |
| 2573 | break; |
| 2574 | default: UNREACHABLE(); |
| 2575 | } |
| 2576 | break; |
| 2577 | case GL_RGB: |
| 2578 | switch (type) |
| 2579 | { |
| 2580 | case GL_UNSIGNED_SHORT_5_6_5: |
| 2581 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2582 | ((unsigned short)(31 * b + 0.5f) << 0) | |
| 2583 | ((unsigned short)(63 * g + 0.5f) << 5) | |
| 2584 | ((unsigned short)(31 * r + 0.5f) << 11); |
| 2585 | break; |
| 2586 | case GL_UNSIGNED_BYTE: |
| 2587 | dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 2588 | dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2589 | dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 2590 | break; |
| 2591 | default: UNREACHABLE(); |
| 2592 | } |
| 2593 | break; |
| 2594 | default: UNREACHABLE(); |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | systemSurface->UnlockRect(); |
| 2600 | |
| 2601 | systemSurface->Release(); |
| 2602 | } |
| 2603 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2604 | RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth) |
| 2605 | { |
| 2606 | SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); |
| 2607 | IDirect3DSurface9 *surface = NULL; |
| 2608 | if (depth) |
| 2609 | { |
| 2610 | surface = swapChain9->getDepthStencil(); |
| 2611 | } |
| 2612 | else |
| 2613 | { |
| 2614 | surface = swapChain9->getRenderTarget(); |
| 2615 | } |
| 2616 | |
| 2617 | RenderTarget9 *renderTarget = new RenderTarget9(this, surface); |
| 2618 | |
| 2619 | return renderTarget; |
| 2620 | } |
| 2621 | |
| 2622 | RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) |
| 2623 | { |
| 2624 | RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples); |
| 2625 | return renderTarget; |
| 2626 | } |
| 2627 | |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2628 | ShaderExecutable *Renderer9::loadExecutable(const DWORD *function, size_t length, GLenum type, void *data) |
| 2629 | { |
| 2630 | ShaderExecutable9 *executable = NULL; |
| 2631 | gl::D3DConstantTable *table = reinterpret_cast<gl::D3DConstantTable *>(data); |
| 2632 | |
| 2633 | switch (type) |
| 2634 | { |
| 2635 | case GL_VERTEX_SHADER: |
| 2636 | { |
| 2637 | IDirect3DVertexShader9 *vshader = createVertexShader(function, length); |
| 2638 | if (vshader) |
| 2639 | { |
| 2640 | executable = new ShaderExecutable9(vshader, table); |
| 2641 | } |
| 2642 | } |
| 2643 | break; |
| 2644 | case GL_FRAGMENT_SHADER: |
| 2645 | { |
| 2646 | IDirect3DPixelShader9 *pshader = createPixelShader(function, length); |
| 2647 | if (pshader) |
| 2648 | { |
| 2649 | executable = new ShaderExecutable9(pshader, table); |
| 2650 | } |
| 2651 | } |
| 2652 | break; |
| 2653 | default: |
| 2654 | UNREACHABLE(); |
| 2655 | break; |
| 2656 | } |
| 2657 | |
| 2658 | return executable; |
| 2659 | } |
| 2660 | |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2661 | ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type) |
| 2662 | { |
| 2663 | const char *profile = NULL; |
| 2664 | |
| 2665 | switch (type) |
| 2666 | { |
| 2667 | case GL_VERTEX_SHADER: |
| 2668 | profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0"; |
| 2669 | break; |
| 2670 | case GL_FRAGMENT_SHADER: |
| 2671 | profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0"; |
| 2672 | break; |
| 2673 | default: |
| 2674 | UNREACHABLE(); |
| 2675 | return NULL; |
| 2676 | } |
| 2677 | |
| 2678 | gl::D3DConstantTable *constantTable = NULL; |
| 2679 | ID3D10Blob *binary = compileToBinary(infoLog, shaderHLSL, profile, &constantTable); |
| 2680 | if (!binary) |
| 2681 | return NULL; |
| 2682 | |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2683 | ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable); |
daniel@transgaming.com | 536dd6e | 2012-11-28 21:00:18 +0000 | [diff] [blame] | 2684 | binary->Release(); |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2685 | |
| 2686 | return executable; |
| 2687 | } |
| 2688 | |
| 2689 | // Compiles the HLSL code of the attached shaders into executable binaries |
| 2690 | ID3D10Blob *Renderer9::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, gl::D3DConstantTable **constantTable) |
| 2691 | { |
| 2692 | if (!hlsl) |
| 2693 | { |
| 2694 | return NULL; |
| 2695 | } |
| 2696 | |
| 2697 | HRESULT result = S_OK; |
| 2698 | UINT flags = 0; |
| 2699 | std::string sourceText; |
| 2700 | if (gl::perfActive()) |
| 2701 | { |
| 2702 | flags |= D3DCOMPILE_DEBUG; |
| 2703 | #ifdef NDEBUG |
| 2704 | flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; |
| 2705 | #else |
| 2706 | flags |= D3DCOMPILE_SKIP_OPTIMIZATION; |
| 2707 | #endif |
| 2708 | |
| 2709 | std::string sourcePath = getTempPath(); |
| 2710 | sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); |
| 2711 | writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); |
| 2712 | } |
| 2713 | else |
| 2714 | { |
| 2715 | flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL; |
| 2716 | sourceText = hlsl; |
| 2717 | } |
| 2718 | |
| 2719 | // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. |
| 2720 | // Try the default flags first and if compilation fails, try some alternatives. |
| 2721 | const static UINT extraFlags[] = |
| 2722 | { |
| 2723 | 0, |
| 2724 | D3DCOMPILE_AVOID_FLOW_CONTROL, |
| 2725 | D3DCOMPILE_PREFER_FLOW_CONTROL |
| 2726 | }; |
| 2727 | |
| 2728 | const static char * const extraFlagNames[] = |
| 2729 | { |
| 2730 | "default", |
| 2731 | "avoid flow control", |
| 2732 | "prefer flow control" |
| 2733 | }; |
| 2734 | |
| 2735 | for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i) |
| 2736 | { |
| 2737 | ID3D10Blob *errorMessage = NULL; |
| 2738 | ID3D10Blob *binary = NULL; |
daniel@transgaming.com | f0516cf | 2012-11-28 20:59:18 +0000 | [diff] [blame] | 2739 | result = mD3DCompileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, |
| 2740 | "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage); |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2741 | if (errorMessage) |
| 2742 | { |
| 2743 | const char *message = (const char*)errorMessage->GetBufferPointer(); |
| 2744 | |
| 2745 | infoLog.appendSanitized(message); |
| 2746 | TRACE("\n%s", hlsl); |
| 2747 | TRACE("\n%s", message); |
| 2748 | |
| 2749 | errorMessage->Release(); |
| 2750 | errorMessage = NULL; |
| 2751 | } |
| 2752 | |
| 2753 | if (SUCCEEDED(result)) |
| 2754 | { |
| 2755 | gl::D3DConstantTable *table = new gl::D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize()); |
| 2756 | if (table->error()) |
| 2757 | { |
| 2758 | delete table; |
| 2759 | binary->Release(); |
| 2760 | return NULL; |
| 2761 | } |
| 2762 | |
| 2763 | *constantTable = table; |
| 2764 | return binary; |
| 2765 | } |
| 2766 | else |
| 2767 | { |
| 2768 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 2769 | { |
| 2770 | return error(GL_OUT_OF_MEMORY, (ID3D10Blob*) NULL); |
| 2771 | } |
| 2772 | |
| 2773 | infoLog.append("Warning: D3D shader compilation failed with "); |
| 2774 | infoLog.append(extraFlagNames[i]); |
| 2775 | infoLog.append(" flags."); |
| 2776 | if (i + 1 < sizeof(extraFlagNames) / sizeof(char*)) |
| 2777 | { |
| 2778 | infoLog.append(" Retrying with "); |
| 2779 | infoLog.append(extraFlagNames[i + 1]); |
| 2780 | infoLog.append(".\n"); |
| 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | return NULL; |
| 2786 | } |
| 2787 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2788 | bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 2789 | { |
| 2790 | return mBlit->boxFilter(source, dest); |
| 2791 | } |
| 2792 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2793 | D3DPOOL Renderer9::getTexturePool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2794 | { |
| 2795 | if (mD3d9Ex != NULL) |
| 2796 | { |
| 2797 | return D3DPOOL_DEFAULT; |
| 2798 | } |
| 2799 | else |
| 2800 | { |
| 2801 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 2802 | { |
| 2803 | return D3DPOOL_MANAGED; |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | return D3DPOOL_DEFAULT; |
| 2808 | } |
| 2809 | |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2810 | bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) |
| 2811 | { |
| 2812 | if (source && dest) |
| 2813 | { |
| 2814 | HRESULT result = D3DERR_OUTOFVIDEOMEMORY; |
| 2815 | IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE |
| 2816 | |
| 2817 | if (fromManaged) |
| 2818 | { |
| 2819 | D3DSURFACE_DESC desc; |
| 2820 | source->GetDesc(&desc); |
| 2821 | |
| 2822 | IDirect3DSurface9 *surf = 0; |
| 2823 | result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); |
| 2824 | |
| 2825 | if (SUCCEEDED(result)) |
| 2826 | { |
daniel@transgaming.com | 31b13e1 | 2012-11-28 19:34:30 +0000 | [diff] [blame] | 2827 | Image::CopyLockableSurfaces(surf, source); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2828 | result = device->UpdateSurface(surf, NULL, dest, NULL); |
| 2829 | surf->Release(); |
| 2830 | } |
| 2831 | } |
| 2832 | else |
| 2833 | { |
| 2834 | endScene(); |
| 2835 | result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
| 2836 | } |
| 2837 | |
| 2838 | if (FAILED(result)) |
| 2839 | { |
| 2840 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 2841 | return false; |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | return true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2848 | } |