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