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