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