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