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