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