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