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