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