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