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