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