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