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