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 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 762 | 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] | 763 | { |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 764 | bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates; |
| 765 | gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates; |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 766 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 767 | if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0) |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 768 | { |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 769 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 770 | int d3dSampler = index + d3dSamplerOffset; |
| 771 | |
| 772 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); |
| 773 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); |
| 774 | |
| 775 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy)); |
| 776 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
| 777 | gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); |
| 778 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 779 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
| 780 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); |
| 781 | if (mSupportsTextureFilterAnisotropy) |
| 782 | { |
| 783 | mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); |
| 784 | } |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 785 | } |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 786 | |
| 787 | forceSetSamplers[index] = false; |
| 788 | appliedSamplers[index] = samplerState; |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 789 | } |
| 790 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 791 | void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 792 | { |
| 793 | int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; |
| 794 | int d3dSampler = index + d3dSamplerOffset; |
| 795 | IDirect3DBaseTexture9 *d3dTexture = NULL; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 796 | unsigned int serial = 0; |
| 797 | bool forceSetTexture = false; |
| 798 | |
| 799 | unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials; |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 800 | |
| 801 | if (texture) |
| 802 | { |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 803 | TextureStorageInterface *texStorage = texture->getNativeTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 804 | if (texStorage) |
| 805 | { |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 806 | TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance()); |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 807 | d3dTexture = storage9->getBaseTexture(); |
daniel@transgaming.com | 9d4346f | 2012-10-31 19:52:04 +0000 | [diff] [blame] | 808 | } |
| 809 | // If we get NULL back from getBaseTexture here, something went wrong |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 810 | // in the texture class and we're unexpectedly missing the d3d texture |
| 811 | ASSERT(d3dTexture != NULL); |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 812 | |
| 813 | serial = texture->getTextureSerial(); |
| 814 | forceSetTexture = texture->hasDirtyImages(); |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 815 | } |
| 816 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 817 | if (forceSetTexture || appliedSerials[index] != serial) |
| 818 | { |
| 819 | mDevice->SetTexture(d3dSampler, d3dTexture); |
| 820 | } |
| 821 | |
| 822 | appliedSerials[index] = serial; |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 823 | } |
| 824 | |
shannonwoods@chromium.org | 1bddfb9 | 2013-05-30 00:11:29 +0000 | [diff] [blame] | 825 | bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[]) |
| 826 | { |
| 827 | // No effect in ES2/D3D9 |
| 828 | return true; |
| 829 | } |
| 830 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 831 | void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 832 | { |
| 833 | bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 834 | |
| 835 | if (rasterStateChanged) |
| 836 | { |
| 837 | // Set the cull mode |
| 838 | if (rasterState.cullFace) |
| 839 | { |
| 840 | mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 845 | } |
| 846 | |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 847 | if (rasterState.polygonOffsetFill) |
| 848 | { |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 849 | if (mCurDepthSize > 0) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 850 | { |
| 851 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); |
| 852 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 853 | float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize)); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 854 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); |
| 855 | } |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 860 | mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 861 | } |
| 862 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 863 | mCurRasterState = rasterState; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | mForceSetRasterState = false; |
| 867 | } |
| 868 | |
Geoff Lang | 2a64ee4 | 2013-05-31 11:22:40 -0400 | [diff] [blame] | 869 | 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] | 870 | { |
| 871 | bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; |
Geoff Lang | 2a64ee4 | 2013-05-31 11:22:40 -0400 | [diff] [blame] | 872 | bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 873 | bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; |
| 874 | |
| 875 | if (blendStateChanged || blendColorChanged) |
| 876 | { |
| 877 | if (blendState.blend) |
| 878 | { |
| 879 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 880 | |
| 881 | if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 882 | blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 883 | { |
| 884 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); |
| 885 | } |
| 886 | else |
| 887 | { |
| 888 | mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), |
| 889 | gl::unorm<8>(blendColor.alpha), |
| 890 | gl::unorm<8>(blendColor.alpha), |
| 891 | gl::unorm<8>(blendColor.alpha))); |
| 892 | } |
| 893 | |
| 894 | mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); |
| 895 | mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); |
| 896 | mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); |
| 897 | |
| 898 | if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || |
| 899 | blendState.destBlendRGB != blendState.destBlendAlpha || |
| 900 | blendState.blendEquationRGB != blendState.blendEquationAlpha) |
| 901 | { |
| 902 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 903 | |
| 904 | mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); |
| 905 | mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); |
| 906 | mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); |
| 907 | } |
| 908 | else |
| 909 | { |
| 910 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 911 | } |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 916 | } |
| 917 | |
| 918 | if (blendState.sampleAlphaToCoverage) |
| 919 | { |
| 920 | FIXME("Sample alpha to coverage is unimplemented."); |
| 921 | } |
| 922 | |
| 923 | // Set the color mask |
| 924 | bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; |
| 925 | // Apparently some ATI cards have a bug where a draw with a zero color |
| 926 | // write mask can cause later draws to have incorrect results. Instead, |
| 927 | // set a nonzero color write mask but modify the blend state so that no |
| 928 | // drawing is done. |
| 929 | // http://code.google.com/p/angleproject/issues/detail?id=169 |
| 930 | |
| 931 | DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, |
| 932 | blendState.colorMaskBlue, blendState.colorMaskAlpha); |
| 933 | if (colorMask == 0 && !zeroColorMaskAllowed) |
| 934 | { |
| 935 | // Enable green channel, but set blending so nothing will be drawn. |
| 936 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); |
| 937 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 938 | |
| 939 | mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); |
| 940 | mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); |
| 941 | mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 942 | } |
| 943 | else |
| 944 | { |
| 945 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); |
| 946 | } |
| 947 | |
| 948 | mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); |
| 949 | |
| 950 | mCurBlendState = blendState; |
| 951 | mCurBlendColor = blendColor; |
| 952 | } |
| 953 | |
| 954 | if (sampleMaskChanged) |
| 955 | { |
| 956 | // Set the multisample mask |
| 957 | mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 958 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); |
| 959 | |
| 960 | mCurSampleMask = sampleMask; |
| 961 | } |
| 962 | |
| 963 | mForceSetBlendState = false; |
| 964 | } |
| 965 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 966 | void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 967 | int stencilBackRef, bool frontFaceCCW) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 968 | { |
| 969 | bool depthStencilStateChanged = mForceSetDepthStencilState || |
| 970 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 971 | bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || |
| 972 | stencilBackRef != mCurStencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 973 | bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 974 | |
| 975 | if (depthStencilStateChanged) |
| 976 | { |
| 977 | if (depthStencilState.depthTest) |
| 978 | { |
| 979 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 980 | mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 985 | } |
| 986 | |
| 987 | mCurDepthStencilState = depthStencilState; |
| 988 | } |
| 989 | |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 990 | if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 991 | { |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 992 | if (depthStencilState.stencilTest && mCurStencilSize > 0) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 993 | { |
| 994 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 995 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 996 | |
| 997 | // FIXME: Unsupported by D3D9 |
| 998 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 999 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 1000 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 1001 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 1002 | stencilRef != stencilBackRef || |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1003 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 1004 | { |
| 1005 | 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] | 1006 | return gl::error(GL_INVALID_OPERATION); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | // get the maximum size of the stencil ref |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 1010 | unsigned int maxStencil = (1 << mCurStencilSize) - 1; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1011 | |
| 1012 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 1013 | depthStencilState.stencilWritemask); |
| 1014 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 1015 | gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); |
| 1016 | |
| 1017 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 1018 | (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1019 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 1020 | depthStencilState.stencilMask); |
| 1021 | |
| 1022 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 1023 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); |
| 1024 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 1025 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); |
| 1026 | mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 1027 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); |
| 1028 | |
| 1029 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, |
| 1030 | depthStencilState.stencilBackWritemask); |
| 1031 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
| 1032 | gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); |
| 1033 | |
| 1034 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 1035 | (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1036 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, |
| 1037 | depthStencilState.stencilBackMask); |
| 1038 | |
| 1039 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
| 1040 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); |
| 1041 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
| 1042 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); |
| 1043 | mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
| 1044 | gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1049 | } |
| 1050 | |
| 1051 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); |
| 1052 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 1053 | mCurStencilRef = stencilRef; |
| 1054 | mCurStencilBackRef = stencilBackRef; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1055 | mCurFrontFaceCCW = frontFaceCCW; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | mForceSetDepthStencilState = false; |
| 1059 | } |
| 1060 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 1061 | void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1062 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 1063 | bool scissorChanged = mForceSetScissor || |
| 1064 | memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || |
| 1065 | enabled != mScissorEnabled; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1066 | |
daniel@transgaming.com | 04f1b33 | 2012-11-28 21:00:40 +0000 | [diff] [blame] | 1067 | if (scissorChanged) |
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 | if (enabled) |
| 1070 | { |
| 1071 | RECT rect; |
| 1072 | rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 1073 | rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height)); |
| 1074 | rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 1075 | rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height)); |
| 1076 | mDevice->SetScissorRect(&rect); |
| 1077 | } |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1078 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 1079 | mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE); |
| 1080 | |
| 1081 | mScissorEnabled = enabled; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1082 | mCurScissor = scissor; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | mForceSetScissor = false; |
| 1086 | } |
| 1087 | |
daniel@transgaming.com | 1298518 | 2012-12-20 20:56:31 +0000 | [diff] [blame] | 1088 | 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] | 1089 | bool ignoreViewport) |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1090 | { |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 1091 | gl::Rectangle actualViewport = viewport; |
| 1092 | float actualZNear = gl::clamp01(zNear); |
| 1093 | float actualZFar = gl::clamp01(zFar); |
| 1094 | if (ignoreViewport) |
| 1095 | { |
| 1096 | actualViewport.x = 0; |
| 1097 | actualViewport.y = 0; |
| 1098 | actualViewport.width = mRenderTargetDesc.width; |
| 1099 | actualViewport.height = mRenderTargetDesc.height; |
| 1100 | actualZNear = 0.0f; |
| 1101 | actualZFar = 1.0f; |
| 1102 | } |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1103 | |
| 1104 | D3DVIEWPORT9 dxViewport; |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 1105 | dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width)); |
| 1106 | dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height)); |
| 1107 | dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X)); |
| 1108 | dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y)); |
| 1109 | dxViewport.MinZ = actualZNear; |
| 1110 | dxViewport.MaxZ = actualZFar; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (dxViewport.Width <= 0 || dxViewport.Height <= 0) |
| 1113 | { |
| 1114 | return false; // Nothing to render |
| 1115 | } |
| 1116 | |
Geoff Lang | de14d60 | 2013-08-14 12:28:33 -0400 | [diff] [blame] | 1117 | float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f); |
| 1118 | |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1119 | bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || |
Geoff Lang | de14d60 | 2013-08-14 12:28:33 -0400 | [diff] [blame] | 1120 | actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1121 | if (viewportChanged) |
| 1122 | { |
| 1123 | mDevice->SetViewport(&dxViewport); |
| 1124 | |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 1125 | mCurViewport = actualViewport; |
| 1126 | mCurNear = actualZNear; |
| 1127 | mCurFar = actualZFar; |
Geoff Lang | de14d60 | 2013-08-14 12:28:33 -0400 | [diff] [blame] | 1128 | mCurDepthFront = depthFront; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1129 | |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1130 | dx_VertexConstants vc = {0}; |
| 1131 | dx_PixelConstants pc = {0}; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1132 | |
shannon.woods@transgaming.com | 42832a6 | 2013-02-28 23:18:38 +0000 | [diff] [blame] | 1133 | vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width; |
| 1134 | vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height; |
| 1135 | vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width; |
| 1136 | vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1137 | |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 1138 | pc.viewCoords[0] = actualViewport.width * 0.5f; |
| 1139 | pc.viewCoords[1] = actualViewport.height * 0.5f; |
| 1140 | pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f); |
| 1141 | pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f); |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1142 | |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1143 | pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f; |
| 1144 | pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f; |
Geoff Lang | de14d60 | 2013-08-14 12:28:33 -0400 | [diff] [blame] | 1145 | pc.depthFront[2] = depthFront; |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1146 | |
| 1147 | vc.depthRange[0] = actualZNear; |
| 1148 | vc.depthRange[1] = actualZFar; |
| 1149 | vc.depthRange[2] = actualZFar - actualZNear; |
| 1150 | |
| 1151 | pc.depthRange[0] = actualZNear; |
| 1152 | pc.depthRange[1] = actualZFar; |
| 1153 | pc.depthRange[2] = actualZFar - actualZNear; |
| 1154 | |
| 1155 | if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0) |
| 1156 | { |
| 1157 | mVertexConstants = vc; |
| 1158 | mDxUniformsDirty = true; |
| 1159 | } |
| 1160 | |
| 1161 | if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0) |
| 1162 | { |
| 1163 | mPixelConstants = pc; |
| 1164 | mDxUniformsDirty = true; |
| 1165 | } |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | mForceSetViewport = false; |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1172 | bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count) |
| 1173 | { |
| 1174 | switch (mode) |
| 1175 | { |
| 1176 | case GL_POINTS: |
| 1177 | mPrimitiveType = D3DPT_POINTLIST; |
| 1178 | mPrimitiveCount = count; |
| 1179 | break; |
| 1180 | case GL_LINES: |
| 1181 | mPrimitiveType = D3DPT_LINELIST; |
| 1182 | mPrimitiveCount = count / 2; |
| 1183 | break; |
| 1184 | case GL_LINE_LOOP: |
| 1185 | mPrimitiveType = D3DPT_LINESTRIP; |
| 1186 | mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately |
| 1187 | break; |
| 1188 | case GL_LINE_STRIP: |
| 1189 | mPrimitiveType = D3DPT_LINESTRIP; |
| 1190 | mPrimitiveCount = count - 1; |
| 1191 | break; |
| 1192 | case GL_TRIANGLES: |
| 1193 | mPrimitiveType = D3DPT_TRIANGLELIST; |
| 1194 | mPrimitiveCount = count / 3; |
| 1195 | break; |
| 1196 | case GL_TRIANGLE_STRIP: |
| 1197 | mPrimitiveType = D3DPT_TRIANGLESTRIP; |
| 1198 | mPrimitiveCount = count - 2; |
| 1199 | break; |
| 1200 | case GL_TRIANGLE_FAN: |
| 1201 | mPrimitiveType = D3DPT_TRIANGLEFAN; |
| 1202 | mPrimitiveCount = count - 2; |
| 1203 | break; |
| 1204 | default: |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1205 | return gl::error(GL_INVALID_ENUM, false); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | return mPrimitiveCount > 0; |
| 1209 | } |
| 1210 | |
daniel@transgaming.com | e4e1a33 | 2012-12-20 20:52:09 +0000 | [diff] [blame] | 1211 | |
| 1212 | gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer) |
| 1213 | { |
| 1214 | if (!depthbuffer) |
| 1215 | { |
| 1216 | ERR("Unexpected null depthbuffer for depth-only FBO."); |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
| 1220 | GLsizei width = depthbuffer->getWidth(); |
| 1221 | GLsizei height = depthbuffer->getHeight(); |
| 1222 | |
| 1223 | // search cached nullcolorbuffers |
| 1224 | for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) |
| 1225 | { |
| 1226 | if (mNullColorbufferCache[i].buffer != NULL && |
| 1227 | mNullColorbufferCache[i].width == width && |
| 1228 | mNullColorbufferCache[i].height == height) |
| 1229 | { |
| 1230 | mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU; |
| 1231 | return mNullColorbufferCache[i].buffer; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0)); |
| 1236 | |
| 1237 | // add nullbuffer to the cache |
| 1238 | NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0]; |
| 1239 | for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) |
| 1240 | { |
| 1241 | if (mNullColorbufferCache[i].lruCount < oldest->lruCount) |
| 1242 | { |
| 1243 | oldest = &mNullColorbufferCache[i]; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | delete oldest->buffer; |
| 1248 | oldest->buffer = nullbuffer; |
| 1249 | oldest->lruCount = ++mMaxNullColorbufferLRU; |
| 1250 | oldest->width = width; |
| 1251 | oldest->height = height; |
| 1252 | |
| 1253 | return nullbuffer; |
| 1254 | } |
| 1255 | |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1256 | bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1257 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1258 | // if there is no color attachment we must synthesize a NULL colorattachment |
| 1259 | // to keep the D3D runtime happy. This should only be possible if depth texturing. |
| 1260 | gl::Renderbuffer *renderbufferObject = NULL; |
shannon.woods%transgaming.com@gtempaccount.com | 89ae113 | 2013-04-13 03:28:43 +0000 | [diff] [blame] | 1261 | if (framebuffer->getColorbufferType(0) != GL_NONE) |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1262 | { |
shannon.woods%transgaming.com@gtempaccount.com | 89ae113 | 2013-04-13 03:28:43 +0000 | [diff] [blame] | 1263 | renderbufferObject = framebuffer->getColorbuffer(0); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1264 | } |
| 1265 | else |
| 1266 | { |
daniel@transgaming.com | e4e1a33 | 2012-12-20 20:52:09 +0000 | [diff] [blame] | 1267 | renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer()); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1268 | } |
| 1269 | if (!renderbufferObject) |
| 1270 | { |
| 1271 | ERR("unable to locate renderbuffer for FBO."); |
| 1272 | return false; |
| 1273 | } |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1274 | |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1275 | bool renderTargetChanged = false; |
| 1276 | unsigned int renderTargetSerial = renderbufferObject->getSerial(); |
| 1277 | if (renderTargetSerial != mAppliedRenderTargetSerial) |
| 1278 | { |
| 1279 | // Apply the render target on the device |
| 1280 | IDirect3DSurface9 *renderTargetSurface = NULL; |
| 1281 | |
| 1282 | RenderTarget *renderTarget = renderbufferObject->getRenderTarget(); |
| 1283 | if (renderTarget) |
| 1284 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1285 | renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | if (!renderTargetSurface) |
| 1289 | { |
| 1290 | ERR("render target pointer unexpectedly null."); |
| 1291 | return false; // Context must be lost |
| 1292 | } |
| 1293 | |
| 1294 | mDevice->SetRenderTarget(0, renderTargetSurface); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 1295 | SafeRelease(renderTargetSurface); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1296 | |
| 1297 | mAppliedRenderTargetSerial = renderTargetSerial; |
| 1298 | renderTargetChanged = true; |
| 1299 | } |
| 1300 | |
| 1301 | gl::Renderbuffer *depthStencil = NULL; |
| 1302 | unsigned int depthbufferSerial = 0; |
| 1303 | unsigned int stencilbufferSerial = 0; |
| 1304 | if (framebuffer->getDepthbufferType() != GL_NONE) |
| 1305 | { |
| 1306 | depthStencil = framebuffer->getDepthbuffer(); |
| 1307 | if (!depthStencil) |
| 1308 | { |
| 1309 | ERR("Depth stencil pointer unexpectedly null."); |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | depthbufferSerial = depthStencil->getSerial(); |
| 1314 | } |
| 1315 | else if (framebuffer->getStencilbufferType() != GL_NONE) |
| 1316 | { |
| 1317 | depthStencil = framebuffer->getStencilbuffer(); |
| 1318 | if (!depthStencil) |
| 1319 | { |
| 1320 | ERR("Depth stencil pointer unexpectedly null."); |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
| 1324 | stencilbufferSerial = depthStencil->getSerial(); |
| 1325 | } |
| 1326 | |
| 1327 | if (depthbufferSerial != mAppliedDepthbufferSerial || |
| 1328 | stencilbufferSerial != mAppliedStencilbufferSerial || |
| 1329 | !mDepthStencilInitialized) |
| 1330 | { |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 1331 | unsigned int depthSize = 0; |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 1332 | unsigned int stencilSize = 0; |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 1333 | |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1334 | // Apply the depth stencil on the device |
| 1335 | if (depthStencil) |
| 1336 | { |
| 1337 | IDirect3DSurface9 *depthStencilSurface = NULL; |
| 1338 | RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil(); |
| 1339 | |
| 1340 | if (depthStencilRenderTarget) |
| 1341 | { |
daniel@transgaming.com | 965bcd2 | 2012-11-28 20:54:14 +0000 | [diff] [blame] | 1342 | depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | if (!depthStencilSurface) |
| 1346 | { |
| 1347 | ERR("depth stencil pointer unexpectedly null."); |
| 1348 | return false; // Context must be lost |
| 1349 | } |
| 1350 | |
| 1351 | mDevice->SetDepthStencilSurface(depthStencilSurface); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 1352 | SafeRelease(depthStencilSurface); |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 1353 | |
| 1354 | depthSize = depthStencil->getDepthSize(); |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 1355 | stencilSize = depthStencil->getStencilSize(); |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1356 | } |
| 1357 | else |
| 1358 | { |
| 1359 | mDevice->SetDepthStencilSurface(NULL); |
| 1360 | } |
| 1361 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 1362 | if (!mDepthStencilInitialized || depthSize != mCurDepthSize) |
| 1363 | { |
| 1364 | mCurDepthSize = depthSize; |
| 1365 | mForceSetRasterState = true; |
| 1366 | } |
| 1367 | |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 1368 | if (!mDepthStencilInitialized || stencilSize != mCurStencilSize) |
| 1369 | { |
| 1370 | mCurStencilSize = stencilSize; |
| 1371 | mForceSetDepthStencilState = true; |
| 1372 | } |
| 1373 | |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 1374 | mAppliedDepthbufferSerial = depthbufferSerial; |
| 1375 | mAppliedStencilbufferSerial = stencilbufferSerial; |
| 1376 | mDepthStencilInitialized = true; |
| 1377 | } |
| 1378 | |
| 1379 | if (renderTargetChanged || !mRenderTargetDescInitialized) |
| 1380 | { |
| 1381 | mForceSetScissor = true; |
| 1382 | mForceSetViewport = true; |
| 1383 | |
| 1384 | mRenderTargetDesc.width = renderbufferObject->getWidth(); |
| 1385 | mRenderTargetDesc.height = renderbufferObject->getHeight(); |
| 1386 | mRenderTargetDesc.format = renderbufferObject->getActualFormat(); |
| 1387 | mRenderTargetDescInitialized = true; |
| 1388 | } |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 1389 | |
| 1390 | return true; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 1391 | } |
daniel@transgaming.com | a734f27 | 2012-10-31 18:07:48 +0000 | [diff] [blame] | 1392 | |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 1393 | 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] | 1394 | GLint first, GLsizei count, GLsizei instances) |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1395 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 1396 | TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; |
Jamie Madill | a857c36 | 2013-07-02 11:57:02 -0400 | [diff] [blame] | 1397 | GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1398 | if (err != GL_NO_ERROR) |
| 1399 | { |
| 1400 | return err; |
| 1401 | } |
| 1402 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1403 | return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw); |
| 1404 | } |
| 1405 | |
| 1406 | // 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] | 1407 | 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] | 1408 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1409 | GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1410 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1411 | if (err == GL_NO_ERROR) |
| 1412 | { |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1413 | // Directly binding the storage buffer is not supported for d3d9 |
| 1414 | ASSERT(indexInfo->storage == NULL); |
| 1415 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1416 | if (indexInfo->serial != mAppliedIBSerial) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1417 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1418 | IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer); |
| 1419 | |
| 1420 | mDevice->SetIndices(indexBuffer->getBuffer()); |
| 1421 | mAppliedIBSerial = indexInfo->serial; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | return err; |
| 1426 | } |
| 1427 | |
| 1428 | void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances) |
| 1429 | { |
| 1430 | startScene(); |
| 1431 | |
| 1432 | if (mode == GL_LINE_LOOP) |
| 1433 | { |
| 1434 | drawLineLoop(count, GL_NONE, NULL, 0, NULL); |
| 1435 | } |
| 1436 | else if (instances > 0) |
| 1437 | { |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 1438 | StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1439 | if (countingIB) |
| 1440 | { |
| 1441 | if (mAppliedIBSerial != countingIB->getSerial()) |
| 1442 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1443 | IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer()); |
| 1444 | |
| 1445 | mDevice->SetIndices(indexBuffer->getBuffer()); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1446 | mAppliedIBSerial = countingIB->getSerial(); |
| 1447 | } |
| 1448 | |
| 1449 | for (int i = 0; i < mRepeatDraw; i++) |
| 1450 | { |
| 1451 | mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount); |
| 1452 | } |
| 1453 | } |
| 1454 | else |
| 1455 | { |
| 1456 | ERR("Could not create a counting index buffer for glDrawArraysInstanced."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1457 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | else // Regular case |
| 1461 | { |
| 1462 | mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount); |
| 1463 | } |
| 1464 | } |
| 1465 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 1466 | 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] | 1467 | { |
| 1468 | startScene(); |
| 1469 | |
shannon.woods@transgaming.com | e1602ae | 2013-02-28 23:17:38 +0000 | [diff] [blame] | 1470 | if (mode == GL_POINTS) |
| 1471 | { |
| 1472 | drawIndexedPoints(count, type, indices, elementArrayBuffer); |
| 1473 | } |
| 1474 | else if (mode == GL_LINE_LOOP) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1475 | { |
daniel@transgaming.com | 97400dd | 2012-11-28 20:57:00 +0000 | [diff] [blame] | 1476 | drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1477 | } |
| 1478 | else |
| 1479 | { |
| 1480 | for (int i = 0; i < mRepeatDraw; i++) |
| 1481 | { |
daniel@transgaming.com | 97400dd | 2012-11-28 20:57:00 +0000 | [diff] [blame] | 1482 | GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; |
| 1483 | 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] | 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) |
| 1489 | { |
| 1490 | // Get the raw indices for an indexed draw |
| 1491 | if (type != GL_NONE && elementArrayBuffer) |
| 1492 | { |
| 1493 | gl::Buffer *indexBuffer = elementArrayBuffer; |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1494 | BufferStorage *storage = indexBuffer->getStorage(); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1495 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1496 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 1499 | unsigned int startIndex = 0; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1500 | |
| 1501 | if (get32BitIndexSupport()) |
| 1502 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1503 | if (!mLineLoopIB) |
| 1504 | { |
| 1505 | mLineLoopIB = new StreamingIndexBufferInterface(this); |
| 1506 | if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 1507 | { |
| 1508 | delete mLineLoopIB; |
| 1509 | mLineLoopIB = NULL; |
| 1510 | |
| 1511 | 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] | 1512 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | |
Geoff Lang | 57e713e | 2013-07-31 17:01:58 -0400 | [diff] [blame] | 1516 | // Checked by Renderer9::applyPrimitiveType |
| 1517 | ASSERT(count >= 0); |
| 1518 | |
| 1519 | 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] | 1520 | { |
| 1521 | ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required."); |
| 1522 | return gl::error(GL_OUT_OF_MEMORY); |
| 1523 | } |
| 1524 | |
Geoff Lang | 57e713e | 2013-07-31 17:01:58 -0400 | [diff] [blame] | 1525 | 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] | 1526 | if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1527 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1528 | ERR("Could not 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] | 1529 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1532 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 1533 | unsigned int offset = 0; |
| 1534 | if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset)) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1535 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1536 | ERR("Could not map index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1537 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1538 | } |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1539 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 1540 | startIndex = static_cast<unsigned int>(offset) / 4; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1541 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 1542 | |
| 1543 | switch (type) |
| 1544 | { |
| 1545 | case GL_NONE: // Non-indexed draw |
| 1546 | for (int i = 0; i < count; i++) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1547 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1548 | data[i] = i; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1549 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1550 | data[count] = 0; |
| 1551 | break; |
| 1552 | case GL_UNSIGNED_BYTE: |
| 1553 | for (int i = 0; i < count; i++) |
| 1554 | { |
| 1555 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1556 | } |
| 1557 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1558 | break; |
| 1559 | case GL_UNSIGNED_SHORT: |
| 1560 | for (int i = 0; i < count; i++) |
| 1561 | { |
| 1562 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1563 | } |
| 1564 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1565 | break; |
| 1566 | case GL_UNSIGNED_INT: |
| 1567 | for (int i = 0; i < count; i++) |
| 1568 | { |
| 1569 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1570 | } |
| 1571 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1572 | break; |
| 1573 | default: UNREACHABLE(); |
| 1574 | } |
| 1575 | |
| 1576 | if (!mLineLoopIB->unmapBuffer()) |
| 1577 | { |
| 1578 | ERR("Could not unmap index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1579 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | else |
| 1583 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1584 | if (!mLineLoopIB) |
| 1585 | { |
| 1586 | mLineLoopIB = new StreamingIndexBufferInterface(this); |
| 1587 | if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) |
| 1588 | { |
| 1589 | delete mLineLoopIB; |
| 1590 | mLineLoopIB = NULL; |
| 1591 | |
| 1592 | 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] | 1593 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Geoff Lang | 57e713e | 2013-07-31 17:01:58 -0400 | [diff] [blame] | 1597 | // Checked by Renderer9::applyPrimitiveType |
| 1598 | ASSERT(count >= 0); |
| 1599 | |
| 1600 | 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] | 1601 | { |
| 1602 | ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required."); |
| 1603 | return gl::error(GL_OUT_OF_MEMORY); |
| 1604 | } |
| 1605 | |
Geoff Lang | 57e713e | 2013-07-31 17:01:58 -0400 | [diff] [blame] | 1606 | 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] | 1607 | if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT)) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1608 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1609 | 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] | 1610 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1613 | void* mappedMemory = NULL; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 1614 | unsigned int offset; |
| 1615 | if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset)) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1616 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1617 | ERR("Could not map index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1618 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1619 | } |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1620 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 1621 | startIndex = static_cast<unsigned int>(offset) / 2; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1622 | unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); |
| 1623 | |
| 1624 | switch (type) |
| 1625 | { |
| 1626 | case GL_NONE: // Non-indexed draw |
| 1627 | for (int i = 0; i < count; i++) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1628 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1629 | data[i] = i; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1630 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1631 | data[count] = 0; |
| 1632 | break; |
| 1633 | case GL_UNSIGNED_BYTE: |
| 1634 | for (int i = 0; i < count; i++) |
| 1635 | { |
| 1636 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1637 | } |
| 1638 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1639 | break; |
| 1640 | case GL_UNSIGNED_SHORT: |
| 1641 | for (int i = 0; i < count; i++) |
| 1642 | { |
| 1643 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1644 | } |
| 1645 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1646 | break; |
| 1647 | case GL_UNSIGNED_INT: |
| 1648 | for (int i = 0; i < count; i++) |
| 1649 | { |
| 1650 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1651 | } |
| 1652 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1653 | break; |
| 1654 | default: UNREACHABLE(); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1657 | if (!mLineLoopIB->unmapBuffer()) |
| 1658 | { |
| 1659 | ERR("Could not unmap index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1660 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1661 | } |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1662 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1663 | |
| 1664 | if (mAppliedIBSerial != mLineLoopIB->getSerial()) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1665 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1666 | IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer()); |
| 1667 | |
| 1668 | mDevice->SetIndices(indexBuffer->getBuffer()); |
| 1669 | mAppliedIBSerial = mLineLoopIB->getSerial(); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1670 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 1671 | |
| 1672 | mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
shannon.woods@transgaming.com | e1602ae | 2013-02-28 23:17:38 +0000 | [diff] [blame] | 1675 | template <typename T> |
| 1676 | static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices) |
| 1677 | { |
| 1678 | for (int i = 0; i < count; i++) |
| 1679 | { |
| 1680 | unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]); |
| 1681 | device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer) |
| 1686 | { |
| 1687 | // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call |
| 1688 | // for each individual point. This call is not expected to happen often. |
| 1689 | |
| 1690 | if (elementArrayBuffer) |
| 1691 | { |
| 1692 | BufferStorage *storage = elementArrayBuffer->getStorage(); |
| 1693 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 1694 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
| 1695 | } |
| 1696 | |
| 1697 | switch (type) |
| 1698 | { |
| 1699 | case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break; |
| 1700 | case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break; |
| 1701 | case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break; |
| 1702 | default: UNREACHABLE(); |
| 1703 | } |
| 1704 | } |
| 1705 | |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1706 | void Renderer9::applyShaders(gl::ProgramBinary *programBinary) |
| 1707 | { |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1708 | unsigned int programBinarySerial = programBinary->getSerial(); |
| 1709 | if (programBinarySerial != mAppliedProgramBinarySerial) |
| 1710 | { |
| 1711 | ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); |
| 1712 | ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); |
daniel@transgaming.com | 9589241 | 2012-11-28 20:59:09 +0000 | [diff] [blame] | 1713 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1714 | IDirect3DVertexShader9 *vertexShader = NULL; |
| 1715 | if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader(); |
daniel@transgaming.com | 9589241 | 2012-11-28 20:59:09 +0000 | [diff] [blame] | 1716 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1717 | IDirect3DPixelShader9 *pixelShader = NULL; |
| 1718 | if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader(); |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1719 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1720 | mDevice->SetPixelShader(pixelShader); |
| 1721 | mDevice->SetVertexShader(vertexShader); |
| 1722 | programBinary->dirtyAllUniforms(); |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1723 | mDxUniformsDirty = true; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1724 | |
| 1725 | mAppliedProgramBinarySerial = programBinarySerial; |
| 1726 | } |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1729 | void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1730 | { |
| 1731 | for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub) |
| 1732 | { |
| 1733 | gl::Uniform *targetUniform = *ub; |
| 1734 | |
| 1735 | if (targetUniform->dirty) |
| 1736 | { |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1737 | GLfloat *f = (GLfloat*)targetUniform->data; |
| 1738 | GLint *i = (GLint*)targetUniform->data; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1739 | |
| 1740 | switch (targetUniform->type) |
| 1741 | { |
| 1742 | case GL_SAMPLER_2D: |
| 1743 | case GL_SAMPLER_CUBE: |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | case GL_BOOL: |
| 1746 | case GL_BOOL_VEC2: |
| 1747 | case GL_BOOL_VEC3: |
| 1748 | case GL_BOOL_VEC4: |
| 1749 | applyUniformnbv(targetUniform, i); |
| 1750 | break; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1751 | case GL_FLOAT: |
| 1752 | case GL_FLOAT_VEC2: |
| 1753 | case GL_FLOAT_VEC3: |
| 1754 | case GL_FLOAT_VEC4: |
| 1755 | case GL_FLOAT_MAT2: |
| 1756 | case GL_FLOAT_MAT3: |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1757 | case GL_FLOAT_MAT4: |
| 1758 | applyUniformnfv(targetUniform, f); |
| 1759 | break; |
| 1760 | case GL_INT: |
| 1761 | case GL_INT_VEC2: |
| 1762 | case GL_INT_VEC3: |
| 1763 | case GL_INT_VEC4: |
| 1764 | applyUniformniv(targetUniform, i); |
| 1765 | break; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1766 | default: |
| 1767 | UNREACHABLE(); |
| 1768 | } |
| 1769 | |
| 1770 | targetUniform->dirty = false; |
| 1771 | } |
| 1772 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Driver uniforms |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 1775 | if (mDxUniformsDirty) |
| 1776 | { |
| 1777 | mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4])); |
| 1778 | mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4])); |
| 1779 | mDxUniformsDirty = false; |
| 1780 | } |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
daniel@transgaming.com | f956186 | 2012-12-20 21:12:07 +0000 | [diff] [blame] | 1783 | void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1784 | { |
shannonwoods@chromium.org | 38676dc | 2013-05-30 00:06:52 +0000 | [diff] [blame] | 1785 | if (targetUniform->isReferencedByFragmentShader()) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1786 | { |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1787 | mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount); |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
shannonwoods@chromium.org | 38676dc | 2013-05-30 00:06:52 +0000 | [diff] [blame] | 1790 | if (targetUniform->isReferencedByVertexShader()) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1791 | { |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1792 | mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount); |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1793 | } |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1796 | void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1797 | { |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 1798 | ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); |
| 1799 | GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1800 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1801 | for (unsigned int i = 0; i < targetUniform->registerCount; i++) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1802 | { |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1803 | vector[i][0] = (GLfloat)v[4 * i + 0]; |
| 1804 | vector[i][1] = (GLfloat)v[4 * i + 1]; |
| 1805 | vector[i][2] = (GLfloat)v[4 * i + 2]; |
| 1806 | vector[i][3] = (GLfloat)v[4 * i + 3]; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1809 | applyUniformnfv(targetUniform, (GLfloat*)vector); |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1812 | void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1813 | { |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 1814 | ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); |
| 1815 | GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1816 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1817 | for (unsigned int i = 0; i < targetUniform->registerCount; i++) |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1818 | { |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1819 | vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f; |
| 1820 | vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f; |
| 1821 | vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f; |
| 1822 | 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] | 1823 | } |
| 1824 | |
shannon.woods@transgaming.com | 2494c97 | 2013-02-28 23:10:03 +0000 | [diff] [blame] | 1825 | applyUniformnfv(targetUniform, (GLfloat*)vector); |
daniel@transgaming.com | b6e5510 | 2012-12-20 21:08:14 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
daniel@transgaming.com | 084a257 | 2012-11-28 20:55:17 +0000 | [diff] [blame] | 1828 | void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1829 | { |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1830 | if (clearParams.colorClearType != GL_FLOAT) |
| 1831 | { |
| 1832 | // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0 |
| 1833 | UNREACHABLE(); |
| 1834 | return; |
| 1835 | } |
| 1836 | |
| 1837 | bool clearColor = clearParams.clearColor[0]; |
| 1838 | for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++) |
| 1839 | { |
| 1840 | if (clearParams.clearColor[i] != clearColor) |
| 1841 | { |
| 1842 | // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0 |
| 1843 | UNREACHABLE(); |
| 1844 | return; |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorFClearValue.alpha), |
| 1849 | gl::unorm<8>(clearParams.colorFClearValue.red), |
| 1850 | gl::unorm<8>(clearParams.colorFClearValue.green), |
| 1851 | gl::unorm<8>(clearParams.colorFClearValue.blue)); |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1852 | float depth = gl::clamp01(clearParams.depthClearValue); |
| 1853 | int stencil = clearParams.stencilClearValue & 0x000000FF; |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1854 | |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1855 | unsigned int stencilUnmasked = 0x0; |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1856 | if (clearParams.clearStencil && frameBuffer->hasStencil()) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1857 | { |
shannonwoods@chromium.org | 755012f | 2013-05-30 00:09:32 +0000 | [diff] [blame] | 1858 | unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(), |
| 1859 | getCurrentClientVersion()); |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1860 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1861 | } |
| 1862 | |
shannonwoods@chromium.org | 755012f | 2013-05-30 00:09:32 +0000 | [diff] [blame] | 1863 | bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 || |
| 1864 | clearParams.colorMaskAlpha; |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1865 | |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1866 | const bool needMaskedStencilClear = clearParams.clearStencil && |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1867 | (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1868 | const bool needMaskedColorClear = clearColor && !(clearParams.colorMaskRed && clearParams.colorMaskGreen && |
| 1869 | clearParams.colorMaskBlue && alphaUnmasked); |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (needMaskedColorClear || needMaskedStencilClear) |
| 1872 | { |
| 1873 | // State which is altered in all paths from this point to the clear call is saved. |
| 1874 | // State which is altered in only some paths will be flagged dirty in the case that |
| 1875 | // that path is taken. |
| 1876 | HRESULT hr; |
| 1877 | if (mMaskedClearSavedState == NULL) |
| 1878 | { |
| 1879 | hr = mDevice->BeginStateBlock(); |
| 1880 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1881 | |
| 1882 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1883 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1884 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1885 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1886 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1887 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1888 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1889 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1890 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1891 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1892 | mDevice->SetPixelShader(NULL); |
| 1893 | mDevice->SetVertexShader(NULL); |
| 1894 | mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
| 1895 | mDevice->SetStreamSource(0, NULL, 0, 0); |
| 1896 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1897 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1898 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1899 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1900 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1901 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1902 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1903 | |
| 1904 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1905 | { |
| 1906 | mDevice->SetStreamSourceFreq(i, 1); |
| 1907 | } |
| 1908 | |
| 1909 | hr = mDevice->EndStateBlock(&mMaskedClearSavedState); |
| 1910 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 1911 | } |
| 1912 | |
| 1913 | ASSERT(mMaskedClearSavedState != NULL); |
| 1914 | |
| 1915 | if (mMaskedClearSavedState != NULL) |
| 1916 | { |
| 1917 | hr = mMaskedClearSavedState->Capture(); |
| 1918 | ASSERT(SUCCEEDED(hr)); |
| 1919 | } |
| 1920 | |
| 1921 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1922 | mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1923 | mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1924 | mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1925 | mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1926 | mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1927 | mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1928 | mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1929 | |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1930 | if (clearColor) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1931 | { |
| 1932 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, |
| 1933 | gl_d3d9::ConvertColorMask(clearParams.colorMaskRed, |
| 1934 | clearParams.colorMaskGreen, |
| 1935 | clearParams.colorMaskBlue, |
| 1936 | clearParams.colorMaskAlpha)); |
| 1937 | } |
| 1938 | else |
| 1939 | { |
| 1940 | mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1941 | } |
| 1942 | |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1943 | if (stencilUnmasked != 0x0 && clearParams.clearStencil) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 1944 | { |
| 1945 | mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1946 | mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 1947 | mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 1948 | mDevice->SetRenderState(D3DRS_STENCILREF, stencil); |
| 1949 | mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask); |
| 1950 | mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
| 1951 | mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 1952 | mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
| 1953 | } |
| 1954 | else |
| 1955 | { |
| 1956 | mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1957 | } |
| 1958 | |
| 1959 | mDevice->SetPixelShader(NULL); |
| 1960 | mDevice->SetVertexShader(NULL); |
| 1961 | mDevice->SetFVF(D3DFVF_XYZRHW); |
| 1962 | mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1963 | mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 1964 | mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 1965 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 1966 | mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 1967 | mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 1968 | mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1969 | |
| 1970 | for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 1971 | { |
| 1972 | mDevice->SetStreamSourceFreq(i, 1); |
| 1973 | } |
| 1974 | |
| 1975 | float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges |
| 1976 | quad[0][0] = -0.5f; |
| 1977 | quad[0][1] = mRenderTargetDesc.height - 0.5f; |
| 1978 | quad[0][2] = 0.0f; |
| 1979 | quad[0][3] = 1.0f; |
| 1980 | |
| 1981 | quad[1][0] = mRenderTargetDesc.width - 0.5f; |
| 1982 | quad[1][1] = mRenderTargetDesc.height - 0.5f; |
| 1983 | quad[1][2] = 0.0f; |
| 1984 | quad[1][3] = 1.0f; |
| 1985 | |
| 1986 | quad[2][0] = -0.5f; |
| 1987 | quad[2][1] = -0.5f; |
| 1988 | quad[2][2] = 0.0f; |
| 1989 | quad[2][3] = 1.0f; |
| 1990 | |
| 1991 | quad[3][0] = mRenderTargetDesc.width - 0.5f; |
| 1992 | quad[3][1] = -0.5f; |
| 1993 | quad[3][2] = 0.0f; |
| 1994 | quad[3][3] = 1.0f; |
| 1995 | |
| 1996 | startScene(); |
| 1997 | mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); |
| 1998 | |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 1999 | if (clearParams.clearDepth) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2000 | { |
| 2001 | mDevice->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 2002 | mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 2003 | mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 2004 | } |
| 2005 | |
| 2006 | if (mMaskedClearSavedState != NULL) |
| 2007 | { |
| 2008 | mMaskedClearSavedState->Apply(); |
| 2009 | } |
| 2010 | } |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 2011 | else if (clearColor || clearParams.clearDepth || clearParams.clearStencil) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2012 | { |
| 2013 | DWORD dxClearFlags = 0; |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 2014 | if (clearColor) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2015 | { |
| 2016 | dxClearFlags |= D3DCLEAR_TARGET; |
| 2017 | } |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 2018 | if (clearParams.clearDepth) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2019 | { |
| 2020 | dxClearFlags |= D3DCLEAR_ZBUFFER; |
| 2021 | } |
Geoff Lang | da507fe | 2013-08-20 12:01:42 -0400 | [diff] [blame] | 2022 | if (clearParams.clearStencil) |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2023 | { |
| 2024 | dxClearFlags |= D3DCLEAR_STENCIL; |
| 2025 | } |
| 2026 | |
| 2027 | mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil); |
| 2028 | } |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 2031 | void Renderer9::markAllStateDirty() |
| 2032 | { |
daniel@transgaming.com | 220e79a | 2012-11-28 19:42:11 +0000 | [diff] [blame] | 2033 | mAppliedRenderTargetSerial = 0; |
| 2034 | mAppliedDepthbufferSerial = 0; |
| 2035 | mAppliedStencilbufferSerial = 0; |
| 2036 | mDepthStencilInitialized = false; |
| 2037 | mRenderTargetDescInitialized = false; |
| 2038 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 2039 | mForceSetDepthStencilState = true; |
| 2040 | mForceSetRasterState = true; |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 2041 | mForceSetScissor = true; |
| 2042 | mForceSetViewport = true; |
daniel@transgaming.com | 9a06737 | 2012-12-20 20:55:24 +0000 | [diff] [blame] | 2043 | mForceSetBlendState = true; |
| 2044 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2045 | 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] | 2046 | { |
| 2047 | mForceSetVertexSamplerStates[i] = true; |
| 2048 | mCurVertexTextureSerials[i] = 0; |
| 2049 | } |
| 2050 | for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) |
| 2051 | { |
| 2052 | mForceSetPixelSamplerStates[i] = true; |
| 2053 | mCurPixelTextureSerials[i] = 0; |
| 2054 | } |
| 2055 | |
daniel@transgaming.com | 9a06737 | 2012-12-20 20:55:24 +0000 | [diff] [blame] | 2056 | mAppliedIBSerial = 0; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 2057 | mAppliedProgramBinarySerial = 0; |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 2058 | mDxUniformsDirty = true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 2059 | |
| 2060 | mVertexDeclarationCache.markStateDirty(); |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2063 | void Renderer9::releaseDeviceResources() |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 2064 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2065 | for (size_t i = 0; i < mEventQueryPool.size(); i++) |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 2066 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2067 | SafeRelease(mEventQueryPool[i]); |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 2068 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2069 | mEventQueryPool.clear(); |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 2070 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2071 | SafeRelease(mMaskedClearSavedState); |
daniel@transgaming.com | 0393e5b | 2012-11-28 20:55:33 +0000 | [diff] [blame] | 2072 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 2073 | mVertexShaderCache.clear(); |
| 2074 | mPixelShaderCache.clear(); |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 2075 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2076 | SafeDelete(mBlit); |
| 2077 | SafeDelete(mVertexDataManager); |
| 2078 | SafeDelete(mIndexDataManager); |
| 2079 | SafeDelete(mLineLoopIB); |
daniel@transgaming.com | e4e1a33 | 2012-12-20 20:52:09 +0000 | [diff] [blame] | 2080 | |
| 2081 | for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) |
| 2082 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2083 | SafeDelete(mNullColorbufferCache[i].buffer); |
daniel@transgaming.com | e4e1a33 | 2012-12-20 20:52:09 +0000 | [diff] [blame] | 2084 | } |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 2087 | void Renderer9::notifyDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2088 | { |
| 2089 | mDeviceLost = true; |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 2090 | mDisplay->notifyDeviceLost(); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2093 | bool Renderer9::isDeviceLost() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2094 | { |
| 2095 | return mDeviceLost; |
| 2096 | } |
| 2097 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 2098 | // 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] | 2099 | bool Renderer9::testDeviceLost(bool notify) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2100 | { |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2101 | HRESULT status = getDeviceStatusCode(); |
| 2102 | bool isLost = (FAILED(status) || d3d9::isDeviceLostError(status)); |
shannon.woods@transgaming.com | a4ba59c | 2013-02-28 23:14:38 +0000 | [diff] [blame] | 2103 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2104 | if (isLost) |
| 2105 | { |
| 2106 | // ensure we note the device loss -- |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2107 | // we'll probably get this done again by notifyDeviceLost |
| 2108 | // but best to remember it! |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2109 | // Note that we don't want to clear the device loss status here |
| 2110 | // -- this needs to be done by resetDevice |
| 2111 | mDeviceLost = true; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 2112 | if (notify) |
| 2113 | { |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 2114 | notifyDeviceLost(); |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 2115 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | return isLost; |
| 2119 | } |
| 2120 | |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2121 | HRESULT Renderer9::getDeviceStatusCode() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2122 | { |
| 2123 | HRESULT status = D3D_OK; |
| 2124 | |
| 2125 | if (mDeviceEx) |
| 2126 | { |
| 2127 | status = mDeviceEx->CheckDeviceState(NULL); |
| 2128 | } |
| 2129 | else if (mDevice) |
| 2130 | { |
| 2131 | status = mDevice->TestCooperativeLevel(); |
| 2132 | } |
| 2133 | |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2134 | return status; |
| 2135 | } |
| 2136 | |
| 2137 | bool Renderer9::testDeviceResettable() |
| 2138 | { |
shannon.woods@transgaming.com | a4ba59c | 2013-02-28 23:14:38 +0000 | [diff] [blame] | 2139 | // 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] | 2140 | // DEVICEREMOVED indicates the device has been stopped and must be recreated |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2141 | switch (getDeviceStatusCode()) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2142 | { |
| 2143 | case D3DERR_DEVICENOTRESET: |
| 2144 | case D3DERR_DEVICEHUNG: |
| 2145 | return true; |
shannon.woods@transgaming.com | a4ba59c | 2013-02-28 23:14:38 +0000 | [diff] [blame] | 2146 | case D3DERR_DEVICELOST: |
| 2147 | return (mDeviceEx != NULL); |
| 2148 | case D3DERR_DEVICEREMOVED: |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2149 | ASSERT(mDeviceEx != NULL); |
| 2150 | return true; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2151 | default: |
| 2152 | return false; |
| 2153 | } |
| 2154 | } |
| 2155 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2156 | bool Renderer9::resetDevice() |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2157 | { |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame] | 2158 | releaseDeviceResources(); |
| 2159 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2160 | D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); |
| 2161 | |
| 2162 | HRESULT result = D3D_OK; |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 2163 | bool lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2164 | int attempts = 3; |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2165 | bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2166 | |
| 2167 | while (lost && attempts > 0) |
| 2168 | { |
| 2169 | if (mDeviceEx) |
| 2170 | { |
| 2171 | Sleep(500); // Give the graphics driver some CPU time |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2172 | |
| 2173 | if (removedDevice) |
| 2174 | { |
| 2175 | result = resetRemovedDevice(); |
| 2176 | } |
| 2177 | else |
| 2178 | { |
| 2179 | result = mDeviceEx->ResetEx(&presentParameters, NULL); |
| 2180 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else |
| 2183 | { |
| 2184 | result = mDevice->TestCooperativeLevel(); |
| 2185 | while (result == D3DERR_DEVICELOST) |
| 2186 | { |
| 2187 | Sleep(100); // Give the graphics driver some CPU time |
| 2188 | result = mDevice->TestCooperativeLevel(); |
| 2189 | } |
| 2190 | |
| 2191 | if (result == D3DERR_DEVICENOTRESET) |
| 2192 | { |
| 2193 | result = mDevice->Reset(&presentParameters); |
| 2194 | } |
| 2195 | } |
| 2196 | |
daniel@transgaming.com | f688c0d | 2012-10-31 17:52:57 +0000 | [diff] [blame] | 2197 | lost = testDeviceLost(false); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2198 | attempts --; |
| 2199 | } |
| 2200 | |
| 2201 | if (FAILED(result)) |
| 2202 | { |
| 2203 | ERR("Reset/ResetEx failed multiple times: 0x%08X", result); |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2207 | // If the device was removed, we already finished re-initialization in resetRemovedDevice |
| 2208 | if (!removedDevice) |
| 2209 | { |
| 2210 | // reset device defaults |
| 2211 | initializeDevice(); |
| 2212 | } |
| 2213 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2214 | mDeviceLost = false; |
| 2215 | |
| 2216 | return true; |
| 2217 | } |
| 2218 | |
Jamie Madill | b7935e5 | 2013-11-20 15:47:47 -0500 | [diff] [blame^] | 2219 | HRESULT Renderer9::resetRemovedDevice() |
| 2220 | { |
| 2221 | // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx: |
| 2222 | // The hardware adapter has been removed. Application must destroy the device, do enumeration of |
| 2223 | // adapters and create another Direct3D device. If application continues rendering without |
| 2224 | // calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only. |
| 2225 | ASSERT(mDeviceEx != NULL); |
| 2226 | deinitialize(); |
| 2227 | return (initialize() == EGL_SUCCESS ? S_OK : S_FALSE); |
| 2228 | } |
| 2229 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2230 | DWORD Renderer9::getAdapterVendor() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 2231 | { |
| 2232 | return mAdapterIdentifier.VendorId; |
| 2233 | } |
| 2234 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2235 | std::string Renderer9::getRendererDescription() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 2236 | { |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2237 | std::ostringstream rendererString; |
| 2238 | |
| 2239 | rendererString << mAdapterIdentifier.Description; |
| 2240 | if (getShareHandleSupport()) |
| 2241 | { |
| 2242 | rendererString << " Direct3D9Ex"; |
| 2243 | } |
| 2244 | else |
| 2245 | { |
| 2246 | rendererString << " Direct3D9"; |
| 2247 | } |
| 2248 | |
| 2249 | rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion); |
| 2250 | rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion); |
| 2251 | |
| 2252 | return rendererString.str(); |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2255 | GUID Renderer9::getAdapterIdentifier() const |
daniel@transgaming.com | 4ca789e | 2012-10-31 18:46:40 +0000 | [diff] [blame] | 2256 | { |
| 2257 | return mAdapterIdentifier.DeviceIdentifier; |
| 2258 | } |
| 2259 | |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2260 | Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2261 | { |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2262 | MultisampleSupportInfo support = { 0 }; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2263 | |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2264 | for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++) |
| 2265 | { |
| 2266 | HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE, |
| 2267 | (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); |
| 2268 | |
| 2269 | if (SUCCEEDED(result)) |
| 2270 | { |
| 2271 | support.supportedSamples[multiSampleIndex] = true; |
| 2272 | if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE) |
| 2273 | { |
| 2274 | support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex); |
| 2275 | } |
| 2276 | } |
| 2277 | else |
| 2278 | { |
| 2279 | support.supportedSamples[multiSampleIndex] = false; |
| 2280 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2281 | } |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2282 | |
| 2283 | return support; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 2286 | bool Renderer9::getBGRATextureSupport() const |
| 2287 | { |
| 2288 | // DirectX 9 always supports BGRA |
| 2289 | return true; |
| 2290 | } |
| 2291 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2292 | bool Renderer9::getDXT1TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2293 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2294 | return mDXT1TextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2297 | bool Renderer9::getDXT3TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2298 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2299 | return mDXT3TextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2302 | bool Renderer9::getDXT5TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2303 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2304 | return mDXT5TextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2307 | bool Renderer9::getDepthTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2308 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2309 | return mDepthTextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2312 | bool Renderer9::getFloat32TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2313 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2314 | return mFloat32TextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2317 | bool Renderer9::getFloat32TextureFilteringSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2318 | { |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2319 | return mFloat32FilterSupport; |
| 2320 | } |
| 2321 | |
| 2322 | bool Renderer9::getFloat32TextureRenderingSupport() const |
| 2323 | { |
| 2324 | return mFloat32RenderSupport; |
| 2325 | } |
| 2326 | |
| 2327 | bool Renderer9::getFloat16TextureSupport() const |
| 2328 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2329 | return mFloat16TextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2332 | bool Renderer9::getFloat16TextureFilteringSupport() const |
| 2333 | { |
| 2334 | return mFloat16FilterSupport; |
| 2335 | } |
| 2336 | |
| 2337 | bool Renderer9::getFloat16TextureRenderingSupport() const |
| 2338 | { |
| 2339 | return mFloat16RenderSupport; |
| 2340 | } |
| 2341 | |
Geoff Lang | d42cf4e | 2013-06-05 16:09:17 -0400 | [diff] [blame] | 2342 | bool Renderer9::getRGB565TextureSupport() const |
| 2343 | { |
| 2344 | return mRGB565TextureSupport; |
| 2345 | } |
| 2346 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2347 | bool Renderer9::getLuminanceTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2348 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2349 | return mLuminanceTextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2352 | bool Renderer9::getLuminanceAlphaTextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2353 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2354 | return mLuminanceAlphaTextureSupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Geoff Lang | 632192d | 2013-10-04 13:40:46 -0400 | [diff] [blame] | 2357 | bool Renderer9::getRGTextureSupport() const |
| 2358 | { |
| 2359 | return mRGTextureSupport; |
| 2360 | } |
| 2361 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2362 | bool Renderer9::getTextureFilterAnisotropySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2363 | { |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 2364 | return mSupportsTextureFilterAnisotropy; |
| 2365 | } |
| 2366 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2367 | float Renderer9::getTextureMaxAnisotropy() const |
daniel@transgaming.com | ba0570e | 2012-10-31 18:07:39 +0000 | [diff] [blame] | 2368 | { |
| 2369 | if (mSupportsTextureFilterAnisotropy) |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2370 | { |
shannon.woods%transgaming.com@gtempaccount.com | 6b73191 | 2013-04-13 03:35:19 +0000 | [diff] [blame] | 2371 | return static_cast<float>(mDeviceCaps.MaxAnisotropy); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2372 | } |
| 2373 | return 1.0f; |
| 2374 | } |
| 2375 | |
shannonwoods@chromium.org | 89200d9 | 2013-05-30 00:07:50 +0000 | [diff] [blame] | 2376 | bool Renderer9::getEventQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2377 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2378 | return mEventQuerySupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2381 | unsigned int Renderer9::getMaxVertexTextureImageUnits() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2382 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2383 | META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); |
| 2384 | return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
shannon.woods@transgaming.com | 76cd88c | 2013-01-25 21:54:36 +0000 | [diff] [blame] | 2387 | unsigned int Renderer9::getMaxCombinedTextureImageUnits() const |
| 2388 | { |
| 2389 | return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); |
| 2390 | } |
| 2391 | |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 2392 | unsigned int Renderer9::getReservedVertexUniformVectors() const |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2393 | { |
shannon.woods@transgaming.com | 42832a6 | 2013-02-28 23:18:38 +0000 | [diff] [blame] | 2394 | return 2; // dx_ViewAdjust and dx_DepthRange. |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 2397 | unsigned int Renderer9::getReservedFragmentUniformVectors() const |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2398 | { |
shannon.woods@transgaming.com | 42832a6 | 2013-02-28 23:18:38 +0000 | [diff] [blame] | 2399 | return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange. |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 2402 | unsigned int Renderer9::getMaxVertexUniformVectors() const |
shannon.woods@transgaming.com | 28d268e | 2013-01-25 21:54:26 +0000 | [diff] [blame] | 2403 | { |
shannon.woods@transgaming.com | d8136cb | 2013-02-28 23:14:44 +0000 | [diff] [blame] | 2404 | return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors(); |
| 2405 | } |
| 2406 | |
| 2407 | unsigned int Renderer9::getMaxFragmentUniformVectors() const |
| 2408 | { |
| 2409 | const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2; |
| 2410 | |
| 2411 | return maxPixelConstantVectors - getReservedFragmentUniformVectors(); |
| 2412 | } |
| 2413 | |
| 2414 | unsigned int Renderer9::getMaxVaryingVectors() const |
| 2415 | { |
| 2416 | 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] | 2417 | } |
| 2418 | |
shannon.woods%transgaming.com@gtempaccount.com | 3f72ce3 | 2013-04-13 03:36:43 +0000 | [diff] [blame] | 2419 | unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const |
| 2420 | { |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const |
| 2425 | { |
| 2426 | return 0; |
| 2427 | } |
| 2428 | |
shannonwoods@chromium.org | 2b54422 | 2013-05-30 00:11:12 +0000 | [diff] [blame] | 2429 | unsigned int Renderer9::getReservedVertexUniformBuffers() const |
| 2430 | { |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | unsigned int Renderer9::getReservedFragmentUniformBuffers() const |
| 2435 | { |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
shannon.woods%transgaming.com@gtempaccount.com | 3f72ce3 | 2013-04-13 03:36:43 +0000 | [diff] [blame] | 2439 | unsigned int Renderer9::getMaxTransformFeedbackBuffers() const |
| 2440 | { |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
shannonwoods@chromium.org | 33e798f | 2013-05-30 00:05:05 +0000 | [diff] [blame] | 2444 | unsigned int Renderer9::getMaxUniformBufferSize() const |
| 2445 | { |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2449 | bool Renderer9::getNonPower2TextureSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2450 | { |
| 2451 | return mSupportsNonPower2Textures; |
| 2452 | } |
| 2453 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2454 | bool Renderer9::getOcclusionQuerySupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2455 | { |
daniel@transgaming.com | 669c995 | 2013-01-11 04:08:16 +0000 | [diff] [blame] | 2456 | return mOcclusionQuerySupport; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2459 | bool Renderer9::getInstancingSupport() const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2460 | { |
| 2461 | return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); |
| 2462 | } |
| 2463 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2464 | bool Renderer9::getShareHandleSupport() const |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 2465 | { |
| 2466 | // 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] | 2467 | return (mD3d9Ex != NULL) && !gl::perfActive(); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 2468 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2469 | |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 2470 | bool Renderer9::getDerivativeInstructionSupport() const |
| 2471 | { |
| 2472 | return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0; |
| 2473 | } |
| 2474 | |
shannon.woods@transgaming.com | 8d2f086 | 2013-02-28 23:09:19 +0000 | [diff] [blame] | 2475 | bool Renderer9::getPostSubBufferSupport() const |
| 2476 | { |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2480 | int Renderer9::getMajorShaderModel() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2481 | { |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2482 | return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion); |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2485 | float Renderer9::getMaxPointSize() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2486 | { |
shannon.woods@transgaming.com | bd8c10c | 2013-01-25 21:15:03 +0000 | [diff] [blame] | 2487 | // Point size clamped at 1.0f for SM2 |
| 2488 | return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f; |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2491 | int Renderer9::getMaxViewportDimension() const |
| 2492 | { |
| 2493 | int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()), |
shannon.woods%transgaming.com@gtempaccount.com | c1fdf6b | 2013-04-13 03:44:41 +0000 | [diff] [blame] | 2494 | (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE); |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2495 | return maxTextureDimension; |
| 2496 | } |
| 2497 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2498 | int Renderer9::getMaxTextureWidth() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2499 | { |
| 2500 | return (int)mDeviceCaps.MaxTextureWidth; |
| 2501 | } |
| 2502 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2503 | int Renderer9::getMaxTextureHeight() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2504 | { |
| 2505 | return (int)mDeviceCaps.MaxTextureHeight; |
| 2506 | } |
| 2507 | |
shannon.woods%transgaming.com@gtempaccount.com | c1fdf6b | 2013-04-13 03:44:41 +0000 | [diff] [blame] | 2508 | int Renderer9::getMaxTextureDepth() const |
| 2509 | { |
| 2510 | // 3D textures are not available in the D3D9 backend. |
| 2511 | return 1; |
| 2512 | } |
| 2513 | |
shannon.woods%transgaming.com@gtempaccount.com | a98a811 | 2013-04-13 03:45:57 +0000 | [diff] [blame] | 2514 | int Renderer9::getMaxTextureArrayLayers() const |
| 2515 | { |
| 2516 | // 2D array textures are not available in the D3D9 backend. |
| 2517 | return 1; |
| 2518 | } |
| 2519 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2520 | bool Renderer9::get32BitIndexSupport() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2521 | { |
| 2522 | return mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 2523 | } |
| 2524 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2525 | DWORD Renderer9::getCapsDeclTypes() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2526 | { |
| 2527 | return mDeviceCaps.DeclTypes; |
| 2528 | } |
| 2529 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2530 | int Renderer9::getMinSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2531 | { |
| 2532 | return mMinSwapInterval; |
| 2533 | } |
| 2534 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2535 | int Renderer9::getMaxSwapInterval() const |
daniel@transgaming.com | 5f4c136 | 2012-10-31 18:29:00 +0000 | [diff] [blame] | 2536 | { |
| 2537 | return mMaxSwapInterval; |
| 2538 | } |
| 2539 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2540 | int Renderer9::getMaxSupportedSamples() const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2541 | { |
| 2542 | return mMaxSupportedSamples; |
| 2543 | } |
| 2544 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 2545 | GLsizei Renderer9::getMaxSupportedFormatSamples(GLenum internalFormat) const |
Geoff Lang | 0e120e3 | 2013-05-29 10:23:55 -0400 | [diff] [blame] | 2546 | { |
Shannon Woods | ddb785c | 2013-07-08 10:32:13 -0400 | [diff] [blame] | 2547 | D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this); |
Geoff Lang | 0e120e3 | 2013-05-29 10:23:55 -0400 | [diff] [blame] | 2548 | MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format); |
| 2549 | return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0; |
| 2550 | } |
| 2551 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 2552 | GLsizei Renderer9::getNumSampleCounts(GLenum internalFormat) const |
Shannon Woods | 52f1e7e | 2013-07-08 10:32:17 -0400 | [diff] [blame] | 2553 | { |
| 2554 | D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this); |
| 2555 | MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format); |
| 2556 | |
| 2557 | unsigned int numCounts = 0; |
| 2558 | if (iter != mMultiSampleSupport.end()) |
| 2559 | { |
| 2560 | const MultisampleSupportInfo& info = iter->second; |
| 2561 | for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++) |
| 2562 | { |
| 2563 | if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i]) |
| 2564 | { |
| 2565 | numCounts++; |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | return numCounts; |
| 2571 | } |
| 2572 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 2573 | void Renderer9::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const |
Shannon Woods | 52f1e7e | 2013-07-08 10:32:17 -0400 | [diff] [blame] | 2574 | { |
| 2575 | D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this); |
| 2576 | MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format); |
| 2577 | |
| 2578 | if (iter != mMultiSampleSupport.end()) |
| 2579 | { |
| 2580 | const MultisampleSupportInfo& info = iter->second; |
| 2581 | int bufPos = 0; |
| 2582 | for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--) |
| 2583 | { |
| 2584 | if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i]) |
| 2585 | { |
| 2586 | params[bufPos++] = i; |
| 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | } |
| 2591 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2592 | int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2593 | { |
| 2594 | if (requested == 0) |
| 2595 | { |
| 2596 | return requested; |
| 2597 | } |
| 2598 | |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2599 | MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format); |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2600 | if (itr == mMultiSampleSupport.end()) |
| 2601 | { |
daniel@transgaming.com | 9295562 | 2012-10-31 18:38:41 +0000 | [diff] [blame] | 2602 | if (format == D3DFMT_UNKNOWN) |
| 2603 | return 0; |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2604 | return -1; |
| 2605 | } |
| 2606 | |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2607 | for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i) |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2608 | { |
Geoff Lang | 61e49a5 | 2013-05-29 10:22:58 -0400 | [diff] [blame] | 2609 | if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
daniel@transgaming.com | b783398 | 2012-10-31 18:31:46 +0000 | [diff] [blame] | 2610 | { |
| 2611 | return i; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | return -1; |
| 2616 | } |
| 2617 | |
shannon.woods%transgaming.com@gtempaccount.com | b290ac1 | 2013-04-13 03:28:15 +0000 | [diff] [blame] | 2618 | unsigned int Renderer9::getMaxRenderTargets() const |
| 2619 | { |
| 2620 | // we do not support MRT in d3d9 |
| 2621 | return 1; |
| 2622 | } |
| 2623 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 2624 | D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLenum internalformat) |
daniel@transgaming.com | a957168 | 2012-11-28 19:33:08 +0000 | [diff] [blame] | 2625 | { |
| 2626 | switch (internalformat) |
| 2627 | { |
| 2628 | case GL_DEPTH_COMPONENT16: |
| 2629 | case GL_DEPTH_COMPONENT32_OES: |
| 2630 | case GL_DEPTH24_STENCIL8_OES: |
| 2631 | return D3DFMT_INTZ; |
| 2632 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 2633 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 2634 | return D3DFMT_DXT1; |
| 2635 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 2636 | return D3DFMT_DXT3; |
| 2637 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 2638 | return D3DFMT_DXT5; |
| 2639 | case GL_RGBA32F_EXT: |
| 2640 | case GL_RGB32F_EXT: |
| 2641 | case GL_ALPHA32F_EXT: |
| 2642 | case GL_LUMINANCE32F_EXT: |
| 2643 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 2644 | return D3DFMT_A32B32G32R32F; |
| 2645 | case GL_RGBA16F_EXT: |
| 2646 | case GL_RGB16F_EXT: |
| 2647 | case GL_ALPHA16F_EXT: |
| 2648 | case GL_LUMINANCE16F_EXT: |
| 2649 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 2650 | return D3DFMT_A16B16G16R16F; |
| 2651 | case GL_LUMINANCE8_EXT: |
| 2652 | if (getLuminanceTextureSupport()) |
| 2653 | { |
| 2654 | return D3DFMT_L8; |
| 2655 | } |
| 2656 | break; |
| 2657 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 2658 | if (getLuminanceAlphaTextureSupport()) |
| 2659 | { |
| 2660 | return D3DFMT_A8L8; |
| 2661 | } |
| 2662 | break; |
| 2663 | case GL_RGB8_OES: |
| 2664 | case GL_RGB565: |
| 2665 | return D3DFMT_X8R8G8B8; |
| 2666 | } |
| 2667 | |
| 2668 | return D3DFMT_A8R8G8B8; |
| 2669 | } |
| 2670 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2671 | bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2672 | { |
| 2673 | bool result = false; |
| 2674 | |
| 2675 | if (source && dest) |
| 2676 | { |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2677 | TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance()); |
| 2678 | TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance()); |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 2679 | |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 2680 | int levels = source9->getMaxLevel() - source9->getBaseLevel(); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2681 | for (int i = 0; i < levels; ++i) |
| 2682 | { |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 2683 | IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false); |
| 2684 | IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2685 | |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 2686 | result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2687 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2688 | SafeRelease(srcSurf); |
| 2689 | SafeRelease(dstSurf); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2690 | |
| 2691 | if (!result) |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2692 | { |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2693 | return false; |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2694 | } |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | return result; |
| 2699 | } |
| 2700 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2701 | bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2702 | { |
| 2703 | bool result = false; |
| 2704 | |
| 2705 | if (source && dest) |
| 2706 | { |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2707 | TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance()); |
| 2708 | TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance()); |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 2709 | int levels = source9->getMaxLevel() - source9->getBaseLevel(); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2710 | for (int f = 0; f < 6; f++) |
| 2711 | { |
| 2712 | for (int i = 0; i < levels; i++) |
| 2713 | { |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 2714 | IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); |
| 2715 | 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] | 2716 | |
daniel@transgaming.com | 34da397 | 2012-12-20 21:10:29 +0000 | [diff] [blame] | 2717 | result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2718 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2719 | SafeRelease(srcSurf); |
| 2720 | SafeRelease(dstSurf); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2721 | |
| 2722 | if (!result) |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2723 | { |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2724 | return false; |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2725 | } |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | return result; |
| 2731 | } |
| 2732 | |
shannon.woods%transgaming.com@gtempaccount.com | 414e82a | 2013-04-13 03:44:05 +0000 | [diff] [blame] | 2733 | bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source) |
| 2734 | { |
| 2735 | // 3D textures are not available in the D3D9 backend. |
| 2736 | UNREACHABLE(); |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
shannon.woods%transgaming.com@gtempaccount.com | 6c86bd5 | 2013-04-13 03:45:45 +0000 | [diff] [blame] | 2740 | bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source) |
| 2741 | { |
| 2742 | // 2D array textures are not supported by the D3D9 backend. |
| 2743 | UNREACHABLE(); |
| 2744 | return false; |
| 2745 | } |
| 2746 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 2747 | D3DPOOL Renderer9::getBufferPool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 2748 | { |
| 2749 | if (mD3d9Ex != NULL) |
| 2750 | { |
| 2751 | return D3DPOOL_DEFAULT; |
| 2752 | } |
| 2753 | else |
| 2754 | { |
| 2755 | if (!(usage & D3DUSAGE_DYNAMIC)) |
| 2756 | { |
| 2757 | return D3DPOOL_MANAGED; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | return D3DPOOL_DEFAULT; |
| 2762 | } |
| 2763 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2764 | 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] | 2765 | GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2766 | { |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2767 | RECT rect; |
| 2768 | rect.left = sourceRect.x; |
| 2769 | rect.top = sourceRect.y; |
| 2770 | rect.right = sourceRect.x + sourceRect.width; |
| 2771 | rect.bottom = sourceRect.y + sourceRect.height; |
| 2772 | |
| 2773 | return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level); |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2776 | 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] | 2777 | GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2778 | { |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2779 | RECT rect; |
| 2780 | rect.left = sourceRect.x; |
| 2781 | rect.top = sourceRect.y; |
| 2782 | rect.right = sourceRect.x + sourceRect.width; |
| 2783 | rect.bottom = sourceRect.y + sourceRect.height; |
| 2784 | |
| 2785 | return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level); |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
shannon.woods%transgaming.com@gtempaccount.com | 414e82a | 2013-04-13 03:44:05 +0000 | [diff] [blame] | 2788 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, |
| 2789 | GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level) |
| 2790 | { |
| 2791 | // 3D textures are not available in the D3D9 backend. |
| 2792 | UNREACHABLE(); |
| 2793 | return false; |
| 2794 | } |
| 2795 | |
shannon.woods%transgaming.com@gtempaccount.com | 6c86bd5 | 2013-04-13 03:45:45 +0000 | [diff] [blame] | 2796 | bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, |
| 2797 | GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level) |
| 2798 | { |
| 2799 | // 2D array textures are not available in the D3D9 backend. |
| 2800 | UNREACHABLE(); |
| 2801 | return false; |
| 2802 | } |
| 2803 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2804 | 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] | 2805 | 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] | 2806 | { |
Geoff Lang | 758d5b2 | 2013-06-11 11:42:50 -0400 | [diff] [blame] | 2807 | ASSERT(filter == GL_NEAREST); |
| 2808 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2809 | endScene(); |
| 2810 | |
| 2811 | if (blitRenderTarget) |
| 2812 | { |
shannon.woods%transgaming.com@gtempaccount.com | 89ae113 | 2013-04-13 03:28:43 +0000 | [diff] [blame] | 2813 | gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0); |
| 2814 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2815 | RenderTarget9 *readRenderTarget = NULL; |
| 2816 | RenderTarget9 *drawRenderTarget = NULL; |
| 2817 | IDirect3DSurface9* readSurface = NULL; |
| 2818 | IDirect3DSurface9* drawSurface = NULL; |
| 2819 | |
| 2820 | if (readBuffer) |
| 2821 | { |
| 2822 | readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); |
| 2823 | } |
| 2824 | if (drawBuffer) |
| 2825 | { |
| 2826 | drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); |
| 2827 | } |
| 2828 | |
| 2829 | if (readRenderTarget) |
| 2830 | { |
| 2831 | readSurface = readRenderTarget->getSurface(); |
| 2832 | } |
| 2833 | if (drawRenderTarget) |
| 2834 | { |
| 2835 | drawSurface = drawRenderTarget->getSurface(); |
| 2836 | } |
| 2837 | |
| 2838 | if (!readSurface || !drawSurface) |
| 2839 | { |
| 2840 | ERR("Failed to retrieve the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2841 | return gl::error(GL_OUT_OF_MEMORY, false); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2842 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2843 | |
Geoff Lang | 125deab | 2013-08-09 13:34:16 -0400 | [diff] [blame] | 2844 | gl::Extents srcSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1); |
| 2845 | gl::Extents dstSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1); |
| 2846 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2847 | RECT srcRect; |
| 2848 | srcRect.left = readRect.x; |
| 2849 | srcRect.right = readRect.x + readRect.width; |
| 2850 | srcRect.top = readRect.y; |
| 2851 | srcRect.bottom = readRect.y + readRect.height; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2852 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2853 | RECT dstRect; |
| 2854 | dstRect.left = drawRect.x; |
| 2855 | dstRect.right = drawRect.x + drawRect.width; |
| 2856 | dstRect.top = drawRect.y; |
| 2857 | dstRect.bottom = drawRect.y + drawRect.height; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2858 | |
Geoff Lang | 125deab | 2013-08-09 13:34:16 -0400 | [diff] [blame] | 2859 | // Clip the rectangles to the scissor rectangle |
| 2860 | if (scissor) |
| 2861 | { |
| 2862 | if (dstRect.left < scissor->x) |
| 2863 | { |
| 2864 | srcRect.left += (scissor->x - dstRect.left); |
| 2865 | dstRect.left = scissor->x; |
| 2866 | } |
| 2867 | if (dstRect.top < scissor->y) |
| 2868 | { |
| 2869 | srcRect.top += (scissor->y - dstRect.top); |
| 2870 | dstRect.top = scissor->y; |
| 2871 | } |
| 2872 | if (dstRect.right > scissor->x + scissor->width) |
| 2873 | { |
| 2874 | srcRect.right -= (dstRect.right - (scissor->x + scissor->width)); |
| 2875 | dstRect.right = scissor->x + scissor->width; |
| 2876 | } |
| 2877 | if (dstRect.bottom > scissor->y + scissor->height) |
| 2878 | { |
| 2879 | srcRect.bottom -= (dstRect.bottom - (scissor->y + scissor->height)); |
| 2880 | dstRect.bottom = scissor->y + scissor->height; |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | // Clip the rectangles to the destination size |
| 2885 | if (dstRect.left < 0) |
| 2886 | { |
| 2887 | srcRect.left += -dstRect.left; |
| 2888 | dstRect.left = 0; |
| 2889 | } |
| 2890 | if (dstRect.right > dstSize.width) |
| 2891 | { |
| 2892 | srcRect.right -= (dstRect.right - dstSize.width); |
| 2893 | dstRect.right = dstSize.width; |
| 2894 | } |
| 2895 | if (dstRect.top < 0) |
| 2896 | { |
| 2897 | srcRect.top += -dstRect.top; |
| 2898 | dstRect.top = 0; |
| 2899 | } |
| 2900 | if (dstRect.bottom > dstSize.height) |
| 2901 | { |
| 2902 | srcRect.bottom -= (dstRect.bottom - dstSize.height); |
| 2903 | dstRect.bottom = dstSize.height; |
| 2904 | } |
| 2905 | |
| 2906 | // Clip the rectangles to the source size |
| 2907 | if (srcRect.left < 0) |
| 2908 | { |
| 2909 | dstRect.left += -srcRect.left; |
| 2910 | srcRect.left = 0; |
| 2911 | } |
| 2912 | if (srcRect.right > srcSize.width) |
| 2913 | { |
| 2914 | dstRect.right -= (srcRect.right - srcSize.width); |
| 2915 | srcRect.right = srcSize.width; |
| 2916 | } |
| 2917 | if (srcRect.top < 0) |
| 2918 | { |
| 2919 | dstRect.top += -srcRect.top; |
| 2920 | srcRect.top = 0; |
| 2921 | } |
| 2922 | if (srcRect.bottom > srcSize.height) |
| 2923 | { |
| 2924 | dstRect.bottom -= (srcRect.bottom - srcSize.height); |
| 2925 | srcRect.bottom = srcSize.height; |
| 2926 | } |
| 2927 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2928 | HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2929 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2930 | SafeRelease(readSurface); |
| 2931 | SafeRelease(drawSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2932 | |
| 2933 | if (FAILED(result)) |
| 2934 | { |
| 2935 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2936 | return false; |
| 2937 | } |
| 2938 | } |
| 2939 | |
Geoff Lang | 685806d | 2013-06-12 11:16:36 -0400 | [diff] [blame] | 2940 | if (blitDepth || blitStencil) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2941 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2942 | gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); |
| 2943 | gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); |
| 2944 | RenderTarget9 *readDepthStencil = NULL; |
| 2945 | RenderTarget9 *drawDepthStencil = NULL; |
| 2946 | IDirect3DSurface9* readSurface = NULL; |
| 2947 | IDirect3DSurface9* drawSurface = NULL; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2948 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2949 | if (readBuffer) |
| 2950 | { |
| 2951 | readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); |
| 2952 | } |
| 2953 | if (drawBuffer) |
| 2954 | { |
| 2955 | drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); |
| 2956 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2957 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2958 | if (readDepthStencil) |
| 2959 | { |
| 2960 | readSurface = readDepthStencil->getSurface(); |
| 2961 | } |
| 2962 | if (drawDepthStencil) |
| 2963 | { |
| 2964 | drawSurface = drawDepthStencil->getSurface(); |
| 2965 | } |
| 2966 | |
| 2967 | if (!readSurface || !drawSurface) |
| 2968 | { |
| 2969 | ERR("Failed to retrieve the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2970 | return gl::error(GL_OUT_OF_MEMORY, false); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); |
| 2974 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 2975 | SafeRelease(readSurface); |
| 2976 | SafeRelease(drawSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2977 | |
| 2978 | if (FAILED(result)) |
| 2979 | { |
| 2980 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 2981 | return false; |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | return true; |
| 2986 | } |
| 2987 | |
| 2988 | void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2989 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2990 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2991 | RenderTarget9 *renderTarget = NULL; |
| 2992 | IDirect3DSurface9 *surface = NULL; |
shannon.woods%transgaming.com@gtempaccount.com | 89ae113 | 2013-04-13 03:28:43 +0000 | [diff] [blame] | 2993 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0); |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2994 | |
| 2995 | if (colorbuffer) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2996 | { |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 2997 | renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); |
| 2998 | } |
| 2999 | |
| 3000 | if (renderTarget) |
| 3001 | { |
| 3002 | surface = renderTarget->getSurface(); |
| 3003 | } |
| 3004 | |
| 3005 | if (!surface) |
| 3006 | { |
| 3007 | // context must be lost |
| 3008 | return; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | D3DSURFACE_DESC desc; |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 3012 | surface->GetDesc(&desc); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3013 | |
| 3014 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 3015 | { |
| 3016 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3017 | SafeRelease(surface); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 3018 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | HRESULT result; |
| 3022 | IDirect3DSurface9 *systemSurface = NULL; |
| 3023 | bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && |
| 3024 | x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && |
| 3025 | desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; |
| 3026 | if (directToPixels) |
| 3027 | { |
| 3028 | // Use the pixels ptr as a shared handle to write directly into client's memory |
| 3029 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 3030 | D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); |
| 3031 | if (FAILED(result)) |
| 3032 | { |
| 3033 | // Try again without the shared handle |
| 3034 | directToPixels = false; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | if (!directToPixels) |
| 3039 | { |
| 3040 | result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, |
| 3041 | D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 3042 | if (FAILED(result)) |
| 3043 | { |
| 3044 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3045 | SafeRelease(surface); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 3046 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
daniel@transgaming.com | d186dc7 | 2012-11-28 19:40:16 +0000 | [diff] [blame] | 3050 | result = mDevice->GetRenderTargetData(surface, systemSurface); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3051 | SafeRelease(surface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (FAILED(result)) |
| 3054 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3055 | SafeRelease(systemSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3056 | |
| 3057 | // It turns out that D3D will sometimes produce more error |
| 3058 | // codes than those documented. |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 3059 | if (d3d9::isDeviceLostError(result)) |
| 3060 | { |
| 3061 | notifyDeviceLost(); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 3062 | return gl::error(GL_OUT_OF_MEMORY); |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 3063 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3064 | else |
| 3065 | { |
| 3066 | UNREACHABLE(); |
| 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | } |
| 3071 | |
| 3072 | if (directToPixels) |
| 3073 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3074 | SafeRelease(systemSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3075 | return; |
| 3076 | } |
| 3077 | |
| 3078 | RECT rect; |
| 3079 | rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width)); |
| 3080 | rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height)); |
| 3081 | rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width)); |
| 3082 | rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height)); |
| 3083 | |
| 3084 | D3DLOCKED_RECT lock; |
| 3085 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 3086 | |
| 3087 | if (FAILED(result)) |
| 3088 | { |
| 3089 | UNREACHABLE(); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3090 | SafeRelease(systemSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3091 | |
| 3092 | return; // No sensible error to generate |
| 3093 | } |
| 3094 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3095 | unsigned char *source; |
| 3096 | int inputPitch; |
| 3097 | if (packReverseRowOrder) |
| 3098 | { |
| 3099 | source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); |
| 3100 | inputPitch = -lock.Pitch; |
| 3101 | } |
| 3102 | else |
| 3103 | { |
| 3104 | source = (unsigned char*)lock.pBits; |
| 3105 | inputPitch = lock.Pitch; |
| 3106 | } |
| 3107 | |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3108 | GLuint clientVersion = getCurrentClientVersion(); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3109 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 3110 | GLenum sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format); |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3111 | GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion); |
| 3112 | GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3113 | |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3114 | GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion); |
| 3115 | |
| 3116 | if (sourceFormat == format && sourceType == type) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3117 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3118 | // Direct copy possible |
| 3119 | unsigned char *dest = static_cast<unsigned char*>(pixels); |
| 3120 | for (int y = 0; y < rect.bottom - rect.top; y++) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3121 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3122 | 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] | 3123 | } |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3124 | } |
| 3125 | else |
| 3126 | { |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 3127 | GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion); |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3128 | GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion); |
| 3129 | GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion); |
| 3130 | |
| 3131 | ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion()); |
| 3132 | if (fastCopyFunc) |
shannon.woods%transgaming.com@gtempaccount.com | 676dc8f | 2013-04-13 03:35:13 +0000 | [diff] [blame] | 3133 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3134 | // Fast copy is possible through some special function |
| 3135 | 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] | 3136 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3137 | for (int x = 0; x < rect.right - rect.left; x++) |
| 3138 | { |
| 3139 | void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize; |
| 3140 | void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize; |
| 3141 | |
| 3142 | fastCopyFunc(src, dest); |
| 3143 | } |
shannon.woods%transgaming.com@gtempaccount.com | 676dc8f | 2013-04-13 03:35:13 +0000 | [diff] [blame] | 3144 | } |
shannon.woods%transgaming.com@gtempaccount.com | 676dc8f | 2013-04-13 03:35:13 +0000 | [diff] [blame] | 3145 | } |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3146 | else |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3147 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3148 | ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format); |
| 3149 | ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3150 | |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3151 | gl::ColorF temp; |
| 3152 | |
| 3153 | for (int y = 0; y < rect.bottom - rect.top; y++) |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3154 | { |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3155 | for (int x = 0; x < rect.right - rect.left; x++) |
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 | void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize; |
| 3158 | void *src = source + y * inputPitch + x * sourcePixelSize; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3159 | |
Geoff Lang | 697ad3e | 2013-06-04 10:11:28 -0400 | [diff] [blame] | 3160 | // readFunc and writeFunc will be using the same type of color, CopyTexImage |
| 3161 | // will not allow the copy otherwise. |
| 3162 | readFunc(src, &temp); |
| 3163 | writeFunc(&temp, dest); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3164 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | systemSurface->UnlockRect(); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3170 | SafeRelease(systemSurface); |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 3173 | RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth) |
| 3174 | { |
| 3175 | SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); |
| 3176 | IDirect3DSurface9 *surface = NULL; |
| 3177 | if (depth) |
| 3178 | { |
| 3179 | surface = swapChain9->getDepthStencil(); |
| 3180 | } |
| 3181 | else |
| 3182 | { |
| 3183 | surface = swapChain9->getRenderTarget(); |
| 3184 | } |
| 3185 | |
| 3186 | RenderTarget9 *renderTarget = new RenderTarget9(this, surface); |
| 3187 | |
| 3188 | return renderTarget; |
| 3189 | } |
| 3190 | |
Geoff Lang | a2d97f1 | 2013-06-11 11:44:02 -0400 | [diff] [blame] | 3191 | RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples) |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 3192 | { |
| 3193 | RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples); |
| 3194 | return renderTarget; |
| 3195 | } |
| 3196 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3197 | 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] | 3198 | { |
| 3199 | ShaderExecutable9 *executable = NULL; |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3200 | |
| 3201 | switch (type) |
| 3202 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3203 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3204 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 3205 | IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length); |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3206 | if (vshader) |
| 3207 | { |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 3208 | executable = new ShaderExecutable9(function, length, vshader); |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3209 | } |
| 3210 | } |
| 3211 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3212 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3213 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 3214 | IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length); |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3215 | if (pshader) |
| 3216 | { |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 3217 | executable = new ShaderExecutable9(function, length, pshader); |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | break; |
| 3221 | default: |
| 3222 | UNREACHABLE(); |
| 3223 | break; |
| 3224 | } |
| 3225 | |
| 3226 | return executable; |
| 3227 | } |
| 3228 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3229 | ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3230 | { |
| 3231 | const char *profile = NULL; |
| 3232 | |
| 3233 | switch (type) |
| 3234 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3235 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3236 | profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0"; |
| 3237 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 3238 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3239 | profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0"; |
| 3240 | break; |
| 3241 | default: |
| 3242 | UNREACHABLE(); |
| 3243 | return NULL; |
| 3244 | } |
| 3245 | |
shannon.woods@transgaming.com | d3d4208 | 2013-02-28 23:14:31 +0000 | [diff] [blame] | 3246 | ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true); |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3247 | if (!binary) |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3248 | { |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3249 | return NULL; |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3250 | } |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3251 | |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 3252 | ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3253 | SafeRelease(binary); |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 3254 | |
| 3255 | return executable; |
| 3256 | } |
| 3257 | |
daniel@transgaming.com | de8a7ff | 2012-11-28 19:34:13 +0000 | [diff] [blame] | 3258 | bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 3259 | { |
| 3260 | return mBlit->boxFilter(source, dest); |
| 3261 | } |
| 3262 | |
daniel@transgaming.com | 2507f41 | 2012-10-31 18:46:48 +0000 | [diff] [blame] | 3263 | D3DPOOL Renderer9::getTexturePool(DWORD usage) const |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 3264 | { |
| 3265 | if (mD3d9Ex != NULL) |
| 3266 | { |
| 3267 | return D3DPOOL_DEFAULT; |
| 3268 | } |
| 3269 | else |
| 3270 | { |
| 3271 | if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) |
| 3272 | { |
| 3273 | return D3DPOOL_MANAGED; |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | return D3DPOOL_DEFAULT; |
| 3278 | } |
| 3279 | |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 3280 | bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) |
| 3281 | { |
| 3282 | if (source && dest) |
| 3283 | { |
| 3284 | HRESULT result = D3DERR_OUTOFVIDEOMEMORY; |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 3285 | |
| 3286 | if (fromManaged) |
| 3287 | { |
| 3288 | D3DSURFACE_DESC desc; |
| 3289 | source->GetDesc(&desc); |
| 3290 | |
| 3291 | IDirect3DSurface9 *surf = 0; |
daniel@transgaming.com | 204677a | 2013-01-11 21:16:09 +0000 | [diff] [blame] | 3292 | 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] | 3293 | |
| 3294 | if (SUCCEEDED(result)) |
| 3295 | { |
daniel@transgaming.com | 4ba2406 | 2012-12-20 20:54:24 +0000 | [diff] [blame] | 3296 | Image9::copyLockableSurfaces(surf, source); |
daniel@transgaming.com | 204677a | 2013-01-11 21:16:09 +0000 | [diff] [blame] | 3297 | result = mDevice->UpdateSurface(surf, NULL, dest, NULL); |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 3298 | SafeRelease(surf); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | else |
| 3302 | { |
| 3303 | endScene(); |
daniel@transgaming.com | 204677a | 2013-01-11 21:16:09 +0000 | [diff] [blame] | 3304 | result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); |
daniel@transgaming.com | 1d80eee | 2012-11-28 19:33:31 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | if (FAILED(result)) |
| 3308 | { |
| 3309 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 3310 | return false; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | return true; |
daniel@transgaming.com | 67094ee | 2012-11-28 20:53:04 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 3317 | Image *Renderer9::createImage() |
| 3318 | { |
daniel@transgaming.com | 4ba2406 | 2012-12-20 20:54:24 +0000 | [diff] [blame] | 3319 | return new Image9(); |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 3322 | void Renderer9::generateMipmap(Image *dest, Image *src) |
| 3323 | { |
daniel@transgaming.com | 4ba2406 | 2012-12-20 20:54:24 +0000 | [diff] [blame] | 3324 | Image9 *src9 = Image9::makeImage9(src); |
| 3325 | Image9 *dst9 = Image9::makeImage9(dest); |
| 3326 | Image9::generateMipmap(dst9, src9); |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 3329 | TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain) |
| 3330 | { |
| 3331 | SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); |
| 3332 | return new TextureStorage9_2D(this, swapChain9); |
| 3333 | } |
| 3334 | |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3335 | 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] | 3336 | { |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3337 | return new TextureStorage9_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3340 | 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] | 3341 | { |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3342 | return new TextureStorage9_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3345 | 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] | 3346 | { |
| 3347 | // 3D textures are not supported by the D3D9 backend. |
| 3348 | UNREACHABLE(); |
| 3349 | |
| 3350 | return NULL; |
| 3351 | } |
| 3352 | |
Jamie Madill | 4cfff5f | 2013-10-24 17:49:46 -0400 | [diff] [blame] | 3353 | 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] | 3354 | { |
| 3355 | // 2D array textures are not supported by the D3D9 backend. |
| 3356 | UNREACHABLE(); |
| 3357 | |
| 3358 | return NULL; |
| 3359 | } |
| 3360 | |
shannonwoods@chromium.org | 6e4f2a6 | 2013-05-30 00:15:19 +0000 | [diff] [blame] | 3361 | bool Renderer9::getLUID(LUID *adapterLuid) const |
| 3362 | { |
| 3363 | adapterLuid->HighPart = 0; |
| 3364 | adapterLuid->LowPart = 0; |
| 3365 | |
| 3366 | if (mD3d9Ex) |
| 3367 | { |
| 3368 | mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid); |
| 3369 | return true; |
| 3370 | } |
| 3371 | |
| 3372 | return false; |
| 3373 | } |
| 3374 | |
Geoff Lang | 005df41 | 2013-10-16 14:12:50 -0400 | [diff] [blame] | 3375 | GLenum Renderer9::getNativeTextureFormat(GLenum internalFormat) const |
Jamie Madill | c8c102b | 2013-10-10 15:10:24 -0400 | [diff] [blame] | 3376 | { |
| 3377 | return d3d9_gl::GetInternalFormat(gl_d3d9::GetTextureFormat(internalFormat, this)); |
| 3378 | } |
| 3379 | |
shannon.woods%transgaming.com@gtempaccount.com | 2058d64 | 2013-04-13 03:42:50 +0000 | [diff] [blame] | 3380 | } |