michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer. michael@0: michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/Buffer.h" michael@0: #include "libGLESv2/Texture.h" michael@0: #include "libGLESv2/Framebuffer.h" michael@0: #include "libGLESv2/Renderbuffer.h" michael@0: #include "libGLESv2/ProgramBinary.h" michael@0: #include "libGLESv2/renderer/IndexDataManager.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: #include "libGLESv2/renderer/renderer9_utils.h" michael@0: #include "libGLESv2/renderer/ShaderExecutable9.h" michael@0: #include "libGLESv2/renderer/SwapChain9.h" michael@0: #include "libGLESv2/renderer/TextureStorage9.h" michael@0: #include "libGLESv2/renderer/Image9.h" michael@0: #include "libGLESv2/renderer/Blit.h" michael@0: #include "libGLESv2/renderer/RenderTarget9.h" michael@0: #include "libGLESv2/renderer/VertexBuffer9.h" michael@0: #include "libGLESv2/renderer/IndexBuffer9.h" michael@0: #include "libGLESv2/renderer/BufferStorage9.h" michael@0: #include "libGLESv2/renderer/Query9.h" michael@0: #include "libGLESv2/renderer/Fence9.h" michael@0: michael@0: #include "libEGL/Display.h" michael@0: michael@0: // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros michael@0: #define REF_RAST 0 michael@0: michael@0: // The "Debug This Pixel..." feature in PIX often fails when using the michael@0: // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 michael@0: // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. michael@0: #if !defined(ANGLE_ENABLE_D3D9EX) michael@0: // Enables use of the IDirect3D9Ex interface, when available michael@0: #define ANGLE_ENABLE_D3D9EX 1 michael@0: #endif // !defined(ANGLE_ENABLE_D3D9EX) michael@0: michael@0: namespace rx michael@0: { michael@0: static const D3DFORMAT RenderTargetFormats[] = michael@0: { michael@0: D3DFMT_A1R5G5B5, michael@0: // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. michael@0: D3DFMT_A8R8G8B8, michael@0: D3DFMT_R5G6B5, michael@0: // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format michael@0: D3DFMT_X8R8G8B8 michael@0: }; michael@0: michael@0: static const D3DFORMAT DepthStencilFormats[] = michael@0: { michael@0: D3DFMT_UNKNOWN, michael@0: // D3DFMT_D16_LOCKABLE, michael@0: D3DFMT_D32, michael@0: // D3DFMT_D15S1, michael@0: D3DFMT_D24S8, michael@0: D3DFMT_D24X8, michael@0: // D3DFMT_D24X4S4, michael@0: D3DFMT_D16, michael@0: // D3DFMT_D32F_LOCKABLE, michael@0: // D3DFMT_D24FS8 michael@0: }; michael@0: michael@0: enum michael@0: { michael@0: MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256, michael@0: MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32, michael@0: MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224, michael@0: MAX_VARYING_VECTORS_SM2 = 8, michael@0: MAX_VARYING_VECTORS_SM3 = 10, michael@0: michael@0: MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4 michael@0: }; michael@0: michael@0: Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice) michael@0: { michael@0: mD3d9Module = NULL; michael@0: michael@0: mD3d9 = NULL; michael@0: mD3d9Ex = NULL; michael@0: mDevice = NULL; michael@0: mDeviceEx = NULL; michael@0: mDeviceWindow = NULL; michael@0: mBlit = NULL; michael@0: michael@0: mAdapter = D3DADAPTER_DEFAULT; michael@0: michael@0: #if REF_RAST == 1 || defined(FORCE_REF_RAST) michael@0: mDeviceType = D3DDEVTYPE_REF; michael@0: #else michael@0: mDeviceType = D3DDEVTYPE_HAL; michael@0: #endif michael@0: michael@0: mDeviceLost = false; michael@0: michael@0: mMaxSupportedSamples = 0; michael@0: michael@0: mMaskedClearSavedState = NULL; michael@0: michael@0: mVertexDataManager = NULL; michael@0: mIndexDataManager = NULL; michael@0: mLineLoopIB = NULL; michael@0: michael@0: mMaxNullColorbufferLRU = 0; michael@0: for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) michael@0: { michael@0: mNullColorbufferCache[i].lruCount = 0; michael@0: mNullColorbufferCache[i].width = 0; michael@0: mNullColorbufferCache[i].height = 0; michael@0: mNullColorbufferCache[i].buffer = NULL; michael@0: } michael@0: } michael@0: michael@0: Renderer9::~Renderer9() michael@0: { michael@0: releaseDeviceResources(); michael@0: michael@0: if (mDevice) michael@0: { michael@0: // If the device is lost, reset it first to prevent leaving the driver in an unstable state michael@0: if (testDeviceLost(false)) michael@0: { michael@0: resetDevice(); michael@0: } michael@0: michael@0: mDevice->Release(); michael@0: mDevice = NULL; michael@0: } michael@0: michael@0: if (mDeviceEx) michael@0: { michael@0: mDeviceEx->Release(); michael@0: mDeviceEx = NULL; michael@0: } michael@0: michael@0: if (mD3d9) michael@0: { michael@0: mD3d9->Release(); michael@0: mD3d9 = NULL; michael@0: } michael@0: michael@0: if (mDeviceWindow) michael@0: { michael@0: DestroyWindow(mDeviceWindow); michael@0: mDeviceWindow = NULL; michael@0: } michael@0: michael@0: if (mD3d9Ex) michael@0: { michael@0: mD3d9Ex->Release(); michael@0: mD3d9Ex = NULL; michael@0: } michael@0: michael@0: if (mD3d9Module) michael@0: { michael@0: mD3d9Module = NULL; michael@0: } michael@0: michael@0: while (!mMultiSampleSupport.empty()) michael@0: { michael@0: delete [] mMultiSampleSupport.begin()->second; michael@0: mMultiSampleSupport.erase(mMultiSampleSupport.begin()); michael@0: } michael@0: } michael@0: michael@0: Renderer9 *Renderer9::makeRenderer9(Renderer *renderer) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer)); michael@0: return static_cast(renderer); michael@0: } michael@0: michael@0: EGLint Renderer9::initialize() michael@0: { michael@0: if (!initializeCompiler()) michael@0: { michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: michael@0: if (mSoftwareDevice) michael@0: { michael@0: mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); michael@0: } michael@0: else michael@0: { michael@0: mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); michael@0: } michael@0: michael@0: if (mD3d9Module == NULL) michael@0: { michael@0: ERR("No D3D9 module found - aborting!\n"); michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: michael@0: typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); michael@0: Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); michael@0: michael@0: // Use Direct3D9Ex if available. Among other things, this version is less michael@0: // inclined to report a lost context, for example when the user switches michael@0: // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. michael@0: if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) michael@0: { michael@0: ASSERT(mD3d9Ex); michael@0: mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast(&mD3d9)); michael@0: ASSERT(mD3d9); michael@0: } michael@0: else michael@0: { michael@0: mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); michael@0: } michael@0: michael@0: if (!mD3d9) michael@0: { michael@0: ERR("Could not create D3D9 device - aborting!\n"); michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: michael@0: if (mDc != NULL) michael@0: { michael@0: // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to michael@0: } michael@0: michael@0: HRESULT result; michael@0: michael@0: // Give up on getting device caps after about one second. michael@0: for (int i = 0; i < 10; ++i) michael@0: { michael@0: result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: break; michael@0: } michael@0: else if (result == D3DERR_NOTAVAILABLE) michael@0: { michael@0: Sleep(100); // Give the driver some time to initialize/recover michael@0: } michael@0: else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from michael@0: { michael@0: ERR("failed to get device caps (0x%x)\n", result); michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: } michael@0: michael@0: if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) michael@0: { michael@0: ERR("Renderer does not support PS 2.0. aborting!\n"); michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: michael@0: // 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: // This is required by Texture2D::convertToRenderTarget. michael@0: if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) michael@0: { michael@0: ERR("Renderer does not support stretctrect from textures!\n"); michael@0: return EGL_NOT_INITIALIZED; michael@0: } michael@0: michael@0: mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); michael@0: michael@0: // ATI cards on XP have problems with non-power-of-two textures. michael@0: mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && michael@0: !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && michael@0: !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && michael@0: !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); michael@0: michael@0: // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec michael@0: mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); michael@0: michael@0: mMinSwapInterval = 4; michael@0: mMaxSwapInterval = 0; michael@0: michael@0: if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) michael@0: { michael@0: mMinSwapInterval = std::min(mMinSwapInterval, 0); michael@0: mMaxSwapInterval = std::max(mMaxSwapInterval, 0); michael@0: } michael@0: if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) michael@0: { michael@0: mMinSwapInterval = std::min(mMinSwapInterval, 1); michael@0: mMaxSwapInterval = std::max(mMaxSwapInterval, 1); michael@0: } michael@0: if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) michael@0: { michael@0: mMinSwapInterval = std::min(mMinSwapInterval, 2); michael@0: mMaxSwapInterval = std::max(mMaxSwapInterval, 2); michael@0: } michael@0: if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) michael@0: { michael@0: mMinSwapInterval = std::min(mMinSwapInterval, 3); michael@0: mMaxSwapInterval = std::max(mMaxSwapInterval, 3); michael@0: } michael@0: if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) michael@0: { michael@0: mMinSwapInterval = std::min(mMinSwapInterval, 4); michael@0: mMaxSwapInterval = std::max(mMaxSwapInterval, 4); michael@0: } michael@0: michael@0: int max = 0; michael@0: for (unsigned int i = 0; i < ArraySize(RenderTargetFormats); ++i) michael@0: { michael@0: bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; michael@0: getMultiSampleSupport(RenderTargetFormats[i], multisampleArray); michael@0: mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray; michael@0: michael@0: for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) michael@0: { michael@0: if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) michael@0: { michael@0: max = j; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (unsigned int i = 0; i < ArraySize(DepthStencilFormats); ++i) michael@0: { michael@0: if (DepthStencilFormats[i] == D3DFMT_UNKNOWN) michael@0: continue; michael@0: michael@0: bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; michael@0: getMultiSampleSupport(DepthStencilFormats[i], multisampleArray); michael@0: mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray; michael@0: michael@0: for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) michael@0: { michael@0: if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) michael@0: { michael@0: max = j; michael@0: } michael@0: } michael@0: } michael@0: michael@0: mMaxSupportedSamples = max; michael@0: michael@0: static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); michael@0: static const TCHAR className[] = TEXT("STATIC"); michael@0: michael@0: mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); michael@0: michael@0: D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); michael@0: DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; michael@0: michael@0: result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); michael@0: if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) michael@0: { michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); michael@0: return EGL_BAD_ALLOC; michael@0: } michael@0: } michael@0: michael@0: if (mD3d9Ex) michael@0: { michael@0: result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); michael@0: ASSERT(SUCCEEDED(result)); michael@0: } michael@0: michael@0: mVertexShaderCache.initialize(mDevice); michael@0: mPixelShaderCache.initialize(mDevice); michael@0: michael@0: // Check occlusion query support michael@0: IDirect3DQuery9 *occlusionQuery = NULL; michael@0: if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery) michael@0: { michael@0: occlusionQuery->Release(); michael@0: mOcclusionQuerySupport = true; michael@0: } michael@0: else michael@0: { michael@0: mOcclusionQuerySupport = false; michael@0: } michael@0: michael@0: // Check event query support michael@0: IDirect3DQuery9 *eventQuery = NULL; michael@0: if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery) michael@0: { michael@0: eventQuery->Release(); michael@0: mEventQuerySupport = true; michael@0: } michael@0: else michael@0: { michael@0: mEventQuerySupport = false; michael@0: } michael@0: michael@0: D3DDISPLAYMODE currentDisplayMode; michael@0: mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); michael@0: michael@0: // Check vertex texture support michael@0: // Only Direct3D 10 ready devices support all the necessary vertex texture formats. michael@0: // We test this using D3D9 by checking support for the R16F format. michael@0: mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, michael@0: D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F)); michael@0: michael@0: // Check depth texture support michael@0: // we use INTZ for depth textures in Direct3D9 michael@0: // we also want NULL texture support to ensure the we can make depth-only FBOs michael@0: // see http://aras-p.info/texts/D3D9GPUHacks.html michael@0: mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, michael@0: D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, michael@0: D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); michael@0: michael@0: // Check 32 bit floating point texture support michael@0: mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); michael@0: michael@0: mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); michael@0: michael@0: if (!mFloat32FilterSupport && !mFloat32RenderSupport) michael@0: { michael@0: mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); michael@0: } michael@0: else michael@0: { michael@0: mFloat32TextureSupport = true; michael@0: } michael@0: michael@0: // Check 16 bit floating point texture support michael@0: mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); michael@0: michael@0: mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); michael@0: michael@0: if (!mFloat16FilterSupport && !mFloat16RenderSupport) michael@0: { michael@0: mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, michael@0: D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && michael@0: SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, michael@0: D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); michael@0: } michael@0: else michael@0: { michael@0: mFloat16TextureSupport = true; michael@0: } michael@0: michael@0: // Check DXT texture support michael@0: mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); michael@0: mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); michael@0: mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); michael@0: michael@0: // Check luminance[alpha] texture support michael@0: mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); michael@0: mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); michael@0: michael@0: initializeDevice(); michael@0: michael@0: return EGL_SUCCESS; michael@0: } michael@0: michael@0: // do any one-time device initialization michael@0: // NOTE: this is also needed after a device lost/reset michael@0: // to reset the scene status and ensure the default states are reset. michael@0: void Renderer9::initializeDevice() michael@0: { michael@0: // Permanent non-default states michael@0: mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); michael@0: mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); michael@0: michael@0: if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f michael@0: } michael@0: michael@0: markAllStateDirty(); michael@0: michael@0: mSceneStarted = false; michael@0: michael@0: ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager); michael@0: mBlit = new Blit(this); michael@0: mVertexDataManager = new rx::VertexDataManager(this); michael@0: mIndexDataManager = new rx::IndexDataManager(this); michael@0: } michael@0: michael@0: D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters() michael@0: { michael@0: D3DPRESENT_PARAMETERS presentParameters = {0}; michael@0: michael@0: // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. michael@0: presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; michael@0: presentParameters.BackBufferCount = 1; michael@0: presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; michael@0: presentParameters.BackBufferWidth = 1; michael@0: presentParameters.BackBufferHeight = 1; michael@0: presentParameters.EnableAutoDepthStencil = FALSE; michael@0: presentParameters.Flags = 0; michael@0: presentParameters.hDeviceWindow = mDeviceWindow; michael@0: presentParameters.MultiSampleQuality = 0; michael@0: presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; michael@0: presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; michael@0: presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; michael@0: presentParameters.Windowed = TRUE; michael@0: michael@0: return presentParameters; michael@0: } michael@0: michael@0: int Renderer9::generateConfigs(ConfigDesc **configDescList) michael@0: { michael@0: D3DDISPLAYMODE currentDisplayMode; michael@0: mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); michael@0: michael@0: unsigned int numRenderFormats = ArraySize(RenderTargetFormats); michael@0: unsigned int numDepthFormats = ArraySize(DepthStencilFormats); michael@0: (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; michael@0: int numConfigs = 0; michael@0: michael@0: for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) michael@0: { michael@0: D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; michael@0: michael@0: HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) michael@0: { michael@0: D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; michael@0: HRESULT result = D3D_OK; michael@0: michael@0: if(depthStencilFormat != D3DFMT_UNKNOWN) michael@0: { michael@0: result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); michael@0: } michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: if(depthStencilFormat != D3DFMT_UNKNOWN) michael@0: { michael@0: result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); michael@0: } michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: ConfigDesc newConfig; michael@0: newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat); michael@0: newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat); michael@0: newConfig.multiSample = 0; // FIXME: enumerate multi-sampling michael@0: newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat); michael@0: michael@0: (*configDescList)[numConfigs++] = newConfig; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return numConfigs; michael@0: } michael@0: michael@0: void Renderer9::deleteConfigs(ConfigDesc *configDescList) michael@0: { michael@0: delete [] (configDescList); michael@0: } michael@0: michael@0: void Renderer9::startScene() michael@0: { michael@0: if (!mSceneStarted) michael@0: { michael@0: long result = mDevice->BeginScene(); michael@0: if (SUCCEEDED(result)) { michael@0: // This is defensive checking against the device being michael@0: // lost at unexpected times. michael@0: mSceneStarted = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Renderer9::endScene() michael@0: { michael@0: if (mSceneStarted) michael@0: { michael@0: // EndScene can fail if the device was lost, for example due michael@0: // to a TDR during a draw call. michael@0: mDevice->EndScene(); michael@0: mSceneStarted = false; michael@0: } michael@0: } michael@0: michael@0: void Renderer9::sync(bool block) michael@0: { michael@0: HRESULT result; michael@0: michael@0: IDirect3DQuery9* query = allocateEventQuery(); michael@0: if (!query) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: result = query->Issue(D3DISSUE_END); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: do michael@0: { michael@0: result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); michael@0: michael@0: if(block && result == S_FALSE) michael@0: { michael@0: // Keep polling, but allow other threads to do something useful first michael@0: Sleep(0); michael@0: // explicitly check for device loss michael@0: // some drivers seem to return S_FALSE even if the device is lost michael@0: // instead of D3DERR_DEVICELOST like they should michael@0: if (testDeviceLost(false)) michael@0: { michael@0: result = D3DERR_DEVICELOST; michael@0: } michael@0: } michael@0: } michael@0: while(block && result == S_FALSE); michael@0: michael@0: freeEventQuery(query); michael@0: michael@0: if (d3d9::isDeviceLostError(result)) michael@0: { michael@0: notifyDeviceLost(); michael@0: } michael@0: } michael@0: michael@0: SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) michael@0: { michael@0: return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat); michael@0: } michael@0: michael@0: IDirect3DQuery9* Renderer9::allocateEventQuery() michael@0: { michael@0: IDirect3DQuery9 *query = NULL; michael@0: michael@0: if (mEventQueryPool.empty()) michael@0: { michael@0: HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); michael@0: ASSERT(SUCCEEDED(result)); michael@0: } michael@0: else michael@0: { michael@0: query = mEventQueryPool.back(); michael@0: mEventQueryPool.pop_back(); michael@0: } michael@0: michael@0: return query; michael@0: } michael@0: michael@0: void Renderer9::freeEventQuery(IDirect3DQuery9* query) michael@0: { michael@0: if (mEventQueryPool.size() > 1000) michael@0: { michael@0: query->Release(); michael@0: } michael@0: else michael@0: { michael@0: mEventQueryPool.push_back(query); michael@0: } michael@0: } michael@0: michael@0: IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) michael@0: { michael@0: return mVertexShaderCache.create(function, length); michael@0: } michael@0: michael@0: IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) michael@0: { michael@0: return mPixelShaderCache.create(function, length); michael@0: } michael@0: michael@0: HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) michael@0: { michael@0: D3DPOOL Pool = getBufferPool(Usage); michael@0: return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); michael@0: } michael@0: michael@0: VertexBuffer *Renderer9::createVertexBuffer() michael@0: { michael@0: return new VertexBuffer9(this); michael@0: } michael@0: michael@0: HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) michael@0: { michael@0: D3DPOOL Pool = getBufferPool(Usage); michael@0: return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); michael@0: } michael@0: michael@0: IndexBuffer *Renderer9::createIndexBuffer() michael@0: { michael@0: return new IndexBuffer9(this); michael@0: } michael@0: michael@0: BufferStorage *Renderer9::createBufferStorage() michael@0: { michael@0: return new BufferStorage9(); michael@0: } michael@0: michael@0: QueryImpl *Renderer9::createQuery(GLenum type) michael@0: { michael@0: return new Query9(this, type); michael@0: } michael@0: michael@0: FenceImpl *Renderer9::createFence() michael@0: { michael@0: return new Fence9(this); michael@0: } michael@0: michael@0: void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) michael@0: { michael@0: bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates; michael@0: gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates; michael@0: michael@0: if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0) michael@0: { michael@0: int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; michael@0: int d3dSampler = index + d3dSamplerOffset; michael@0: michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); michael@0: michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy)); michael@0: D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; michael@0: gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); michael@0: if (mSupportsTextureFilterAnisotropy) michael@0: { michael@0: mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); michael@0: } michael@0: } michael@0: michael@0: forceSetSamplers[index] = false; michael@0: appliedSamplers[index] = samplerState; michael@0: } michael@0: michael@0: void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) michael@0: { michael@0: int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; michael@0: int d3dSampler = index + d3dSamplerOffset; michael@0: IDirect3DBaseTexture9 *d3dTexture = NULL; michael@0: unsigned int serial = 0; michael@0: bool forceSetTexture = false; michael@0: michael@0: unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials; michael@0: michael@0: if (texture) michael@0: { michael@0: TextureStorageInterface *texStorage = texture->getNativeTexture(); michael@0: if (texStorage) michael@0: { michael@0: TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance()); michael@0: d3dTexture = storage9->getBaseTexture(); michael@0: } michael@0: // If we get NULL back from getBaseTexture here, something went wrong michael@0: // in the texture class and we're unexpectedly missing the d3d texture michael@0: ASSERT(d3dTexture != NULL); michael@0: michael@0: serial = texture->getTextureSerial(); michael@0: forceSetTexture = texture->hasDirtyImages(); michael@0: } michael@0: michael@0: if (forceSetTexture || appliedSerials[index] != serial) michael@0: { michael@0: mDevice->SetTexture(d3dSampler, d3dTexture); michael@0: } michael@0: michael@0: appliedSerials[index] = serial; michael@0: } michael@0: michael@0: void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState) michael@0: { michael@0: bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; michael@0: michael@0: if (rasterStateChanged) michael@0: { michael@0: // Set the cull mode michael@0: if (rasterState.cullFace) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); michael@0: } michael@0: michael@0: if (rasterState.polygonOffsetFill) michael@0: { michael@0: if (mCurDepthSize > 0) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); michael@0: michael@0: float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast(mCurDepthSize)); michael@0: mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); michael@0: mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); michael@0: } michael@0: michael@0: mCurRasterState = rasterState; michael@0: } michael@0: michael@0: mForceSetRasterState = false; michael@0: } michael@0: michael@0: void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) michael@0: { michael@0: bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; michael@0: bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; michael@0: bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; michael@0: michael@0: if (blendStateChanged || blendColorChanged) michael@0: { michael@0: if (blendState.blend) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); michael@0: michael@0: if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && michael@0: blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), michael@0: gl::unorm<8>(blendColor.alpha), michael@0: gl::unorm<8>(blendColor.alpha), michael@0: gl::unorm<8>(blendColor.alpha))); michael@0: } michael@0: michael@0: mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); michael@0: mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); michael@0: mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); michael@0: michael@0: if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || michael@0: blendState.destBlendRGB != blendState.destBlendAlpha || michael@0: blendState.blendEquationRGB != blendState.blendEquationAlpha) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); michael@0: michael@0: mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); michael@0: mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); michael@0: mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); michael@0: } michael@0: michael@0: if (blendState.sampleAlphaToCoverage) michael@0: { michael@0: FIXME("Sample alpha to coverage is unimplemented."); michael@0: } michael@0: michael@0: // Set the color mask michael@0: bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; michael@0: // Apparently some ATI cards have a bug where a draw with a zero color michael@0: // write mask can cause later draws to have incorrect results. Instead, michael@0: // set a nonzero color write mask but modify the blend state so that no michael@0: // drawing is done. michael@0: // http://code.google.com/p/angleproject/issues/detail?id=169 michael@0: michael@0: DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, michael@0: blendState.colorMaskBlue, blendState.colorMaskAlpha); michael@0: if (colorMask == 0 && !zeroColorMaskAllowed) michael@0: { michael@0: // Enable green channel, but set blending so nothing will be drawn. michael@0: mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); michael@0: mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); michael@0: michael@0: mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); michael@0: mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); michael@0: mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); michael@0: } michael@0: michael@0: mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); michael@0: michael@0: mCurBlendState = blendState; michael@0: mCurBlendColor = blendColor; michael@0: } michael@0: michael@0: if (sampleMaskChanged) michael@0: { michael@0: // Set the multisample mask michael@0: mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); michael@0: mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast(sampleMask)); michael@0: michael@0: mCurSampleMask = sampleMask; michael@0: } michael@0: michael@0: mForceSetBlendState = false; michael@0: } michael@0: michael@0: void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, michael@0: int stencilBackRef, bool frontFaceCCW) michael@0: { michael@0: bool depthStencilStateChanged = mForceSetDepthStencilState || michael@0: memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; michael@0: bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || michael@0: stencilBackRef != mCurStencilBackRef; michael@0: bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; michael@0: michael@0: if (depthStencilStateChanged) michael@0: { michael@0: if (depthStencilState.depthTest) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); michael@0: mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); michael@0: } michael@0: michael@0: mCurDepthStencilState = depthStencilState; michael@0: } michael@0: michael@0: if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged) michael@0: { michael@0: if (depthStencilState.stencilTest && mCurStencilSize > 0) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); michael@0: mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); michael@0: michael@0: // FIXME: Unsupported by D3D9 michael@0: const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; michael@0: const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; michael@0: const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; michael@0: if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || michael@0: stencilRef != stencilBackRef || michael@0: depthStencilState.stencilMask != depthStencilState.stencilBackMask) michael@0: { michael@0: ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: // get the maximum size of the stencil ref michael@0: unsigned int maxStencil = (1 << mCurStencilSize) - 1; michael@0: michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, michael@0: depthStencilState.stencilWritemask); michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, michael@0: gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); michael@0: michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, michael@0: (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, michael@0: depthStencilState.stencilMask); michael@0: michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); michael@0: mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); michael@0: michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, michael@0: depthStencilState.stencilBackWritemask); michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, michael@0: gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); michael@0: michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, michael@0: (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, michael@0: depthStencilState.stencilBackMask); michael@0: michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); michael@0: mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, michael@0: gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); michael@0: } michael@0: michael@0: mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); michael@0: michael@0: mCurStencilRef = stencilRef; michael@0: mCurStencilBackRef = stencilBackRef; michael@0: mCurFrontFaceCCW = frontFaceCCW; michael@0: } michael@0: michael@0: mForceSetDepthStencilState = false; michael@0: } michael@0: michael@0: void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) michael@0: { michael@0: bool scissorChanged = mForceSetScissor || michael@0: memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || michael@0: enabled != mScissorEnabled; michael@0: michael@0: if (scissorChanged) michael@0: { michael@0: if (enabled) michael@0: { michael@0: RECT rect; michael@0: rect.left = gl::clamp(scissor.x, 0, static_cast(mRenderTargetDesc.width)); michael@0: rect.top = gl::clamp(scissor.y, 0, static_cast(mRenderTargetDesc.height)); michael@0: rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast(mRenderTargetDesc.width)); michael@0: rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast(mRenderTargetDesc.height)); michael@0: mDevice->SetScissorRect(&rect); michael@0: } michael@0: michael@0: mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE); michael@0: michael@0: mScissorEnabled = enabled; michael@0: mCurScissor = scissor; michael@0: } michael@0: michael@0: mForceSetScissor = false; michael@0: } michael@0: michael@0: bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, michael@0: bool ignoreViewport) michael@0: { michael@0: gl::Rectangle actualViewport = viewport; michael@0: float actualZNear = gl::clamp01(zNear); michael@0: float actualZFar = gl::clamp01(zFar); michael@0: if (ignoreViewport) michael@0: { michael@0: actualViewport.x = 0; michael@0: actualViewport.y = 0; michael@0: actualViewport.width = mRenderTargetDesc.width; michael@0: actualViewport.height = mRenderTargetDesc.height; michael@0: actualZNear = 0.0f; michael@0: actualZFar = 1.0f; michael@0: } michael@0: michael@0: D3DVIEWPORT9 dxViewport; michael@0: dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast(mRenderTargetDesc.width)); michael@0: dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast(mRenderTargetDesc.height)); michael@0: dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast(mRenderTargetDesc.width) - static_cast(dxViewport.X)); michael@0: dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast(mRenderTargetDesc.height) - static_cast(dxViewport.Y)); michael@0: dxViewport.MinZ = actualZNear; michael@0: dxViewport.MaxZ = actualZFar; michael@0: michael@0: if (dxViewport.Width <= 0 || dxViewport.Height <= 0) michael@0: { michael@0: return false; // Nothing to render michael@0: } michael@0: michael@0: bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || michael@0: actualZNear != mCurNear || actualZFar != mCurFar; michael@0: if (viewportChanged) michael@0: { michael@0: mDevice->SetViewport(&dxViewport); michael@0: michael@0: mCurViewport = actualViewport; michael@0: mCurNear = actualZNear; michael@0: mCurFar = actualZFar; michael@0: michael@0: dx_VertexConstants vc = {0}; michael@0: dx_PixelConstants pc = {0}; michael@0: michael@0: vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width; michael@0: vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height; michael@0: vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width; michael@0: vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height; michael@0: michael@0: pc.viewCoords[0] = actualViewport.width * 0.5f; michael@0: pc.viewCoords[1] = actualViewport.height * 0.5f; michael@0: pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f); michael@0: pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f); michael@0: michael@0: pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f; michael@0: pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f; michael@0: pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);; michael@0: michael@0: vc.depthRange[0] = actualZNear; michael@0: vc.depthRange[1] = actualZFar; michael@0: vc.depthRange[2] = actualZFar - actualZNear; michael@0: michael@0: pc.depthRange[0] = actualZNear; michael@0: pc.depthRange[1] = actualZFar; michael@0: pc.depthRange[2] = actualZFar - actualZNear; michael@0: michael@0: if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0) michael@0: { michael@0: mVertexConstants = vc; michael@0: mDxUniformsDirty = true; michael@0: } michael@0: michael@0: if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0) michael@0: { michael@0: mPixelConstants = pc; michael@0: mDxUniformsDirty = true; michael@0: } michael@0: } michael@0: michael@0: mForceSetViewport = false; michael@0: return true; michael@0: } michael@0: michael@0: bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count) michael@0: { michael@0: switch (mode) michael@0: { michael@0: case GL_POINTS: michael@0: mPrimitiveType = D3DPT_POINTLIST; michael@0: mPrimitiveCount = count; michael@0: break; michael@0: case GL_LINES: michael@0: mPrimitiveType = D3DPT_LINELIST; michael@0: mPrimitiveCount = count / 2; michael@0: break; michael@0: case GL_LINE_LOOP: michael@0: mPrimitiveType = D3DPT_LINESTRIP; michael@0: mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately michael@0: break; michael@0: case GL_LINE_STRIP: michael@0: mPrimitiveType = D3DPT_LINESTRIP; michael@0: mPrimitiveCount = count - 1; michael@0: break; michael@0: case GL_TRIANGLES: michael@0: mPrimitiveType = D3DPT_TRIANGLELIST; michael@0: mPrimitiveCount = count / 3; michael@0: break; michael@0: case GL_TRIANGLE_STRIP: michael@0: mPrimitiveType = D3DPT_TRIANGLESTRIP; michael@0: mPrimitiveCount = count - 2; michael@0: break; michael@0: case GL_TRIANGLE_FAN: michael@0: mPrimitiveType = D3DPT_TRIANGLEFAN; michael@0: mPrimitiveCount = count - 2; michael@0: break; michael@0: default: michael@0: return gl::error(GL_INVALID_ENUM, false); michael@0: } michael@0: michael@0: return mPrimitiveCount > 0; michael@0: } michael@0: michael@0: michael@0: gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer) michael@0: { michael@0: if (!depthbuffer) michael@0: { michael@0: ERR("Unexpected null depthbuffer for depth-only FBO."); michael@0: return NULL; michael@0: } michael@0: michael@0: GLsizei width = depthbuffer->getWidth(); michael@0: GLsizei height = depthbuffer->getHeight(); michael@0: michael@0: // search cached nullcolorbuffers michael@0: for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) michael@0: { michael@0: if (mNullColorbufferCache[i].buffer != NULL && michael@0: mNullColorbufferCache[i].width == width && michael@0: mNullColorbufferCache[i].height == height) michael@0: { michael@0: mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU; michael@0: return mNullColorbufferCache[i].buffer; michael@0: } michael@0: } michael@0: michael@0: gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0)); michael@0: michael@0: // add nullbuffer to the cache michael@0: NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0]; michael@0: for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) michael@0: { michael@0: if (mNullColorbufferCache[i].lruCount < oldest->lruCount) michael@0: { michael@0: oldest = &mNullColorbufferCache[i]; michael@0: } michael@0: } michael@0: michael@0: delete oldest->buffer; michael@0: oldest->buffer = nullbuffer; michael@0: oldest->lruCount = ++mMaxNullColorbufferLRU; michael@0: oldest->width = width; michael@0: oldest->height = height; michael@0: michael@0: return nullbuffer; michael@0: } michael@0: michael@0: bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer) michael@0: { michael@0: // if there is no color attachment we must synthesize a NULL colorattachment michael@0: // to keep the D3D runtime happy. This should only be possible if depth texturing. michael@0: gl::Renderbuffer *renderbufferObject = NULL; michael@0: if (framebuffer->getColorbufferType(0) != GL_NONE) michael@0: { michael@0: renderbufferObject = framebuffer->getColorbuffer(0); michael@0: } michael@0: else michael@0: { michael@0: renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer()); michael@0: } michael@0: if (!renderbufferObject) michael@0: { michael@0: ERR("unable to locate renderbuffer for FBO."); michael@0: return false; michael@0: } michael@0: michael@0: bool renderTargetChanged = false; michael@0: unsigned int renderTargetSerial = renderbufferObject->getSerial(); michael@0: if (renderTargetSerial != mAppliedRenderTargetSerial) michael@0: { michael@0: // Apply the render target on the device michael@0: IDirect3DSurface9 *renderTargetSurface = NULL; michael@0: michael@0: RenderTarget *renderTarget = renderbufferObject->getRenderTarget(); michael@0: if (renderTarget) michael@0: { michael@0: renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface(); michael@0: } michael@0: michael@0: if (!renderTargetSurface) michael@0: { michael@0: ERR("render target pointer unexpectedly null."); michael@0: return false; // Context must be lost michael@0: } michael@0: michael@0: mDevice->SetRenderTarget(0, renderTargetSurface); michael@0: renderTargetSurface->Release(); michael@0: michael@0: mAppliedRenderTargetSerial = renderTargetSerial; michael@0: renderTargetChanged = true; michael@0: } michael@0: michael@0: gl::Renderbuffer *depthStencil = NULL; michael@0: unsigned int depthbufferSerial = 0; michael@0: unsigned int stencilbufferSerial = 0; michael@0: if (framebuffer->getDepthbufferType() != GL_NONE) michael@0: { michael@0: depthStencil = framebuffer->getDepthbuffer(); michael@0: if (!depthStencil) michael@0: { michael@0: ERR("Depth stencil pointer unexpectedly null."); michael@0: return false; michael@0: } michael@0: michael@0: depthbufferSerial = depthStencil->getSerial(); michael@0: } michael@0: else if (framebuffer->getStencilbufferType() != GL_NONE) michael@0: { michael@0: depthStencil = framebuffer->getStencilbuffer(); michael@0: if (!depthStencil) michael@0: { michael@0: ERR("Depth stencil pointer unexpectedly null."); michael@0: return false; michael@0: } michael@0: michael@0: stencilbufferSerial = depthStencil->getSerial(); michael@0: } michael@0: michael@0: if (depthbufferSerial != mAppliedDepthbufferSerial || michael@0: stencilbufferSerial != mAppliedStencilbufferSerial || michael@0: !mDepthStencilInitialized) michael@0: { michael@0: unsigned int depthSize = 0; michael@0: unsigned int stencilSize = 0; michael@0: michael@0: // Apply the depth stencil on the device michael@0: if (depthStencil) michael@0: { michael@0: IDirect3DSurface9 *depthStencilSurface = NULL; michael@0: RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil(); michael@0: michael@0: if (depthStencilRenderTarget) michael@0: { michael@0: depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface(); michael@0: } michael@0: michael@0: if (!depthStencilSurface) michael@0: { michael@0: ERR("depth stencil pointer unexpectedly null."); michael@0: return false; // Context must be lost michael@0: } michael@0: michael@0: mDevice->SetDepthStencilSurface(depthStencilSurface); michael@0: depthStencilSurface->Release(); michael@0: michael@0: depthSize = depthStencil->getDepthSize(); michael@0: stencilSize = depthStencil->getStencilSize(); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetDepthStencilSurface(NULL); michael@0: } michael@0: michael@0: if (!mDepthStencilInitialized || depthSize != mCurDepthSize) michael@0: { michael@0: mCurDepthSize = depthSize; michael@0: mForceSetRasterState = true; michael@0: } michael@0: michael@0: if (!mDepthStencilInitialized || stencilSize != mCurStencilSize) michael@0: { michael@0: mCurStencilSize = stencilSize; michael@0: mForceSetDepthStencilState = true; michael@0: } michael@0: michael@0: mAppliedDepthbufferSerial = depthbufferSerial; michael@0: mAppliedStencilbufferSerial = stencilbufferSerial; michael@0: mDepthStencilInitialized = true; michael@0: } michael@0: michael@0: if (renderTargetChanged || !mRenderTargetDescInitialized) michael@0: { michael@0: mForceSetScissor = true; michael@0: mForceSetViewport = true; michael@0: michael@0: mRenderTargetDesc.width = renderbufferObject->getWidth(); michael@0: mRenderTargetDesc.height = renderbufferObject->getHeight(); michael@0: mRenderTargetDesc.format = renderbufferObject->getActualFormat(); michael@0: mRenderTargetDescInitialized = true; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances) michael@0: { michael@0: TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; michael@0: GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); michael@0: if (err != GL_NO_ERROR) michael@0: { michael@0: return err; michael@0: } michael@0: michael@0: return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw); michael@0: } michael@0: michael@0: // Applies the indices and element array bindings to the Direct3D 9 device michael@0: GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) michael@0: { michael@0: GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); michael@0: michael@0: if (err == GL_NO_ERROR) michael@0: { michael@0: // Directly binding the storage buffer is not supported for d3d9 michael@0: ASSERT(indexInfo->storage == NULL); michael@0: michael@0: if (indexInfo->serial != mAppliedIBSerial) michael@0: { michael@0: IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer); michael@0: michael@0: mDevice->SetIndices(indexBuffer->getBuffer()); michael@0: mAppliedIBSerial = indexInfo->serial; michael@0: } michael@0: } michael@0: michael@0: return err; michael@0: } michael@0: michael@0: void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances) michael@0: { michael@0: startScene(); michael@0: michael@0: if (mode == GL_LINE_LOOP) michael@0: { michael@0: drawLineLoop(count, GL_NONE, NULL, 0, NULL); michael@0: } michael@0: else if (instances > 0) michael@0: { michael@0: StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count); michael@0: if (countingIB) michael@0: { michael@0: if (mAppliedIBSerial != countingIB->getSerial()) michael@0: { michael@0: IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer()); michael@0: michael@0: mDevice->SetIndices(indexBuffer->getBuffer()); michael@0: mAppliedIBSerial = countingIB->getSerial(); michael@0: } michael@0: michael@0: for (int i = 0; i < mRepeatDraw; i++) michael@0: { michael@0: mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: ERR("Could not create a counting index buffer for glDrawArraysInstanced."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: else // Regular case michael@0: { michael@0: mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount); michael@0: } michael@0: } michael@0: michael@0: void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/) michael@0: { michael@0: startScene(); michael@0: michael@0: if (mode == GL_POINTS) michael@0: { michael@0: drawIndexedPoints(count, type, indices, elementArrayBuffer); michael@0: } michael@0: else if (mode == GL_LINE_LOOP) michael@0: { michael@0: drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); michael@0: } michael@0: else michael@0: { michael@0: for (int i = 0; i < mRepeatDraw; i++) michael@0: { michael@0: GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; michael@0: mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) michael@0: { michael@0: // Get the raw indices for an indexed draw michael@0: if (type != GL_NONE && elementArrayBuffer) michael@0: { michael@0: gl::Buffer *indexBuffer = elementArrayBuffer; michael@0: BufferStorage *storage = indexBuffer->getStorage(); michael@0: intptr_t offset = reinterpret_cast(indices); michael@0: indices = static_cast(storage->getData()) + offset; michael@0: } michael@0: michael@0: unsigned int startIndex = 0; michael@0: michael@0: if (get32BitIndexSupport()) michael@0: { michael@0: if (!mLineLoopIB) michael@0: { michael@0: mLineLoopIB = new StreamingIndexBufferInterface(this); michael@0: if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) michael@0: { michael@0: delete mLineLoopIB; michael@0: mLineLoopIB = NULL; michael@0: michael@0: ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: if (static_cast(count) + 1 > (std::numeric_limits::max() / sizeof(unsigned int))) michael@0: { michael@0: ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: // Checked by Renderer9::applyPrimitiveType michael@0: ASSERT(count >= 0); michael@0: michael@0: const unsigned int spaceNeeded = (static_cast(count) + 1) * sizeof(unsigned int); michael@0: if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) michael@0: { michael@0: ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: void* mappedMemory = NULL; michael@0: unsigned int offset = 0; michael@0: if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset)) michael@0: { michael@0: ERR("Could not map index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: startIndex = static_cast(offset) / 4; michael@0: unsigned int *data = reinterpret_cast(mappedMemory); michael@0: michael@0: switch (type) michael@0: { michael@0: case GL_NONE: // Non-indexed draw michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = i; michael@0: } michael@0: data[count] = 0; michael@0: break; michael@0: case GL_UNSIGNED_BYTE: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: case GL_UNSIGNED_SHORT: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: case GL_UNSIGNED_INT: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: if (!mLineLoopIB->unmapBuffer()) michael@0: { michael@0: ERR("Could not unmap index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (!mLineLoopIB) michael@0: { michael@0: mLineLoopIB = new StreamingIndexBufferInterface(this); michael@0: if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) michael@0: { michael@0: delete mLineLoopIB; michael@0: mLineLoopIB = NULL; michael@0: michael@0: ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: // Checked by Renderer9::applyPrimitiveType michael@0: ASSERT(count >= 0); michael@0: michael@0: if (static_cast(count) + 1 > (std::numeric_limits::max() / sizeof(unsigned short))) michael@0: { michael@0: ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: const unsigned int spaceNeeded = (static_cast(count) + 1) * sizeof(unsigned short); michael@0: if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT)) michael@0: { michael@0: ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: void* mappedMemory = NULL; michael@0: unsigned int offset; michael@0: if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset)) michael@0: { michael@0: ERR("Could not map index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: startIndex = static_cast(offset) / 2; michael@0: unsigned short *data = reinterpret_cast(mappedMemory); michael@0: michael@0: switch (type) michael@0: { michael@0: case GL_NONE: // Non-indexed draw michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = i; michael@0: } michael@0: data[count] = 0; michael@0: break; michael@0: case GL_UNSIGNED_BYTE: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: case GL_UNSIGNED_SHORT: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: case GL_UNSIGNED_INT: michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: data[i] = static_cast(indices)[i]; michael@0: } michael@0: data[count] = static_cast(indices)[0]; michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: if (!mLineLoopIB->unmapBuffer()) michael@0: { michael@0: ERR("Could not unmap index buffer for GL_LINE_LOOP."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: if (mAppliedIBSerial != mLineLoopIB->getSerial()) michael@0: { michael@0: IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer()); michael@0: michael@0: mDevice->SetIndices(indexBuffer->getBuffer()); michael@0: mAppliedIBSerial = mLineLoopIB->getSerial(); michael@0: } michael@0: michael@0: mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count); michael@0: } michael@0: michael@0: template michael@0: static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices) michael@0: { michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: unsigned int indexValue = static_cast(static_cast(indices)[i]); michael@0: device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1); michael@0: } michael@0: } michael@0: michael@0: void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer) michael@0: { michael@0: // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call michael@0: // for each individual point. This call is not expected to happen often. michael@0: michael@0: if (elementArrayBuffer) michael@0: { michael@0: BufferStorage *storage = elementArrayBuffer->getStorage(); michael@0: intptr_t offset = reinterpret_cast(indices); michael@0: indices = static_cast(storage->getData()) + offset; michael@0: } michael@0: michael@0: switch (type) michael@0: { michael@0: case GL_UNSIGNED_BYTE: drawPoints(mDevice, count, indices); break; michael@0: case GL_UNSIGNED_SHORT: drawPoints(mDevice, count, indices); break; michael@0: case GL_UNSIGNED_INT: drawPoints(mDevice, count, indices); break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: michael@0: void Renderer9::applyShaders(gl::ProgramBinary *programBinary) michael@0: { michael@0: unsigned int programBinarySerial = programBinary->getSerial(); michael@0: if (programBinarySerial != mAppliedProgramBinarySerial) michael@0: { michael@0: ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); michael@0: ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); michael@0: michael@0: IDirect3DVertexShader9 *vertexShader = NULL; michael@0: if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader(); michael@0: michael@0: IDirect3DPixelShader9 *pixelShader = NULL; michael@0: if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader(); michael@0: michael@0: mDevice->SetPixelShader(pixelShader); michael@0: mDevice->SetVertexShader(vertexShader); michael@0: programBinary->dirtyAllUniforms(); michael@0: mDxUniformsDirty = true; michael@0: michael@0: mAppliedProgramBinarySerial = programBinarySerial; michael@0: } michael@0: } michael@0: michael@0: void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) michael@0: { michael@0: for (std::vector::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub) michael@0: { michael@0: gl::Uniform *targetUniform = *ub; michael@0: michael@0: if (targetUniform->dirty) michael@0: { michael@0: GLfloat *f = (GLfloat*)targetUniform->data; michael@0: GLint *i = (GLint*)targetUniform->data; michael@0: michael@0: switch (targetUniform->type) michael@0: { michael@0: case GL_SAMPLER_2D: michael@0: case GL_SAMPLER_CUBE: michael@0: break; michael@0: case GL_BOOL: michael@0: case GL_BOOL_VEC2: michael@0: case GL_BOOL_VEC3: michael@0: case GL_BOOL_VEC4: michael@0: applyUniformnbv(targetUniform, i); michael@0: break; michael@0: case GL_FLOAT: michael@0: case GL_FLOAT_VEC2: michael@0: case GL_FLOAT_VEC3: michael@0: case GL_FLOAT_VEC4: michael@0: case GL_FLOAT_MAT2: michael@0: case GL_FLOAT_MAT3: michael@0: case GL_FLOAT_MAT4: michael@0: applyUniformnfv(targetUniform, f); michael@0: break; michael@0: case GL_INT: michael@0: case GL_INT_VEC2: michael@0: case GL_INT_VEC3: michael@0: case GL_INT_VEC4: michael@0: applyUniformniv(targetUniform, i); michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: targetUniform->dirty = false; michael@0: } michael@0: } michael@0: michael@0: // Driver uniforms michael@0: if (mDxUniformsDirty) michael@0: { michael@0: mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4])); michael@0: mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4])); michael@0: mDxUniformsDirty = false; michael@0: } michael@0: } michael@0: michael@0: void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v) michael@0: { michael@0: if (targetUniform->psRegisterIndex >= 0) michael@0: { michael@0: mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount); michael@0: } michael@0: michael@0: if (targetUniform->vsRegisterIndex >= 0) michael@0: { michael@0: mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount); michael@0: } michael@0: } michael@0: michael@0: void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v) michael@0: { michael@0: ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); michael@0: GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; michael@0: michael@0: for (unsigned int i = 0; i < targetUniform->registerCount; i++) michael@0: { michael@0: vector[i][0] = (GLfloat)v[4 * i + 0]; michael@0: vector[i][1] = (GLfloat)v[4 * i + 1]; michael@0: vector[i][2] = (GLfloat)v[4 * i + 2]; michael@0: vector[i][3] = (GLfloat)v[4 * i + 3]; michael@0: } michael@0: michael@0: applyUniformnfv(targetUniform, (GLfloat*)vector); michael@0: } michael@0: michael@0: void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v) michael@0: { michael@0: ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); michael@0: GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; michael@0: michael@0: for (unsigned int i = 0; i < targetUniform->registerCount; i++) michael@0: { michael@0: vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f; michael@0: vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f; michael@0: vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f; michael@0: vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f; michael@0: } michael@0: michael@0: applyUniformnfv(targetUniform, (GLfloat*)vector); michael@0: } michael@0: michael@0: void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) michael@0: { michael@0: D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha), michael@0: gl::unorm<8>(clearParams.colorClearValue.red), michael@0: gl::unorm<8>(clearParams.colorClearValue.green), michael@0: gl::unorm<8>(clearParams.colorClearValue.blue)); michael@0: float depth = gl::clamp01(clearParams.depthClearValue); michael@0: int stencil = clearParams.stencilClearValue & 0x000000FF; michael@0: michael@0: unsigned int stencilUnmasked = 0x0; michael@0: if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil()) michael@0: { michael@0: unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); michael@0: stencilUnmasked = (0x1 << stencilSize) - 1; michael@0: } michael@0: michael@0: bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; michael@0: michael@0: const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && michael@0: (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; michael@0: const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && michael@0: !(clearParams.colorMaskRed && clearParams.colorMaskGreen && michael@0: clearParams.colorMaskBlue && alphaUnmasked); michael@0: michael@0: if (needMaskedColorClear || needMaskedStencilClear) michael@0: { michael@0: // State which is altered in all paths from this point to the clear call is saved. michael@0: // State which is altered in only some paths will be flagged dirty in the case that michael@0: // that path is taken. michael@0: HRESULT hr; michael@0: if (mMaskedClearSavedState == NULL) michael@0: { michael@0: hr = mDevice->BeginStateBlock(); michael@0: ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); michael@0: michael@0: mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); michael@0: mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); michael@0: mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); michael@0: mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); michael@0: mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); michael@0: mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); michael@0: mDevice->SetPixelShader(NULL); michael@0: mDevice->SetVertexShader(NULL); michael@0: mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); michael@0: mDevice->SetStreamSource(0, NULL, 0, 0); michael@0: mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); michael@0: mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); michael@0: mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); michael@0: mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); michael@0: mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); michael@0: mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); michael@0: mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); michael@0: michael@0: for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mDevice->SetStreamSourceFreq(i, 1); michael@0: } michael@0: michael@0: hr = mDevice->EndStateBlock(&mMaskedClearSavedState); michael@0: ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); michael@0: } michael@0: michael@0: ASSERT(mMaskedClearSavedState != NULL); michael@0: michael@0: if (mMaskedClearSavedState != NULL) michael@0: { michael@0: hr = mMaskedClearSavedState->Capture(); michael@0: ASSERT(SUCCEEDED(hr)); michael@0: } michael@0: michael@0: mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); michael@0: mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); michael@0: mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); michael@0: mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); michael@0: michael@0: if (clearParams.mask & GL_COLOR_BUFFER_BIT) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, michael@0: gl_d3d9::ConvertColorMask(clearParams.colorMaskRed, michael@0: clearParams.colorMaskGreen, michael@0: clearParams.colorMaskBlue, michael@0: clearParams.colorMaskAlpha)); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); michael@0: } michael@0: michael@0: if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT)) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); michael@0: mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); michael@0: mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); michael@0: mDevice->SetRenderState(D3DRS_STENCILREF, stencil); michael@0: mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask); michael@0: mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); michael@0: mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); michael@0: mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); michael@0: } michael@0: else michael@0: { michael@0: mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); michael@0: } michael@0: michael@0: mDevice->SetPixelShader(NULL); michael@0: mDevice->SetVertexShader(NULL); michael@0: mDevice->SetFVF(D3DFVF_XYZRHW); michael@0: mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); michael@0: mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); michael@0: mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); michael@0: mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); michael@0: mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); michael@0: mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); michael@0: mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); michael@0: michael@0: for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mDevice->SetStreamSourceFreq(i, 1); michael@0: } michael@0: michael@0: float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges michael@0: quad[0][0] = -0.5f; michael@0: quad[0][1] = mRenderTargetDesc.height - 0.5f; michael@0: quad[0][2] = 0.0f; michael@0: quad[0][3] = 1.0f; michael@0: michael@0: quad[1][0] = mRenderTargetDesc.width - 0.5f; michael@0: quad[1][1] = mRenderTargetDesc.height - 0.5f; michael@0: quad[1][2] = 0.0f; michael@0: quad[1][3] = 1.0f; michael@0: michael@0: quad[2][0] = -0.5f; michael@0: quad[2][1] = -0.5f; michael@0: quad[2][2] = 0.0f; michael@0: quad[2][3] = 1.0f; michael@0: michael@0: quad[3][0] = mRenderTargetDesc.width - 0.5f; michael@0: quad[3][1] = -0.5f; michael@0: quad[3][2] = 0.0f; michael@0: quad[3][3] = 1.0f; michael@0: michael@0: startScene(); michael@0: mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); michael@0: michael@0: if (clearParams.mask & GL_DEPTH_BUFFER_BIT) michael@0: { michael@0: mDevice->SetRenderState(D3DRS_ZENABLE, TRUE); michael@0: mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); michael@0: mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); michael@0: } michael@0: michael@0: if (mMaskedClearSavedState != NULL) michael@0: { michael@0: mMaskedClearSavedState->Apply(); michael@0: } michael@0: } michael@0: else if (clearParams.mask) michael@0: { michael@0: DWORD dxClearFlags = 0; michael@0: if (clearParams.mask & GL_COLOR_BUFFER_BIT) michael@0: { michael@0: dxClearFlags |= D3DCLEAR_TARGET; michael@0: } michael@0: if (clearParams.mask & GL_DEPTH_BUFFER_BIT) michael@0: { michael@0: dxClearFlags |= D3DCLEAR_ZBUFFER; michael@0: } michael@0: if (clearParams.mask & GL_STENCIL_BUFFER_BIT) michael@0: { michael@0: dxClearFlags |= D3DCLEAR_STENCIL; michael@0: } michael@0: michael@0: mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil); michael@0: } michael@0: } michael@0: michael@0: void Renderer9::markAllStateDirty() michael@0: { michael@0: mAppliedRenderTargetSerial = 0; michael@0: mAppliedDepthbufferSerial = 0; michael@0: mAppliedStencilbufferSerial = 0; michael@0: mDepthStencilInitialized = false; michael@0: mRenderTargetDescInitialized = false; michael@0: michael@0: mForceSetDepthStencilState = true; michael@0: mForceSetRasterState = true; michael@0: mForceSetScissor = true; michael@0: mForceSetViewport = true; michael@0: mForceSetBlendState = true; michael@0: michael@0: for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++) michael@0: { michael@0: mForceSetVertexSamplerStates[i] = true; michael@0: mCurVertexTextureSerials[i] = 0; michael@0: } michael@0: for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) michael@0: { michael@0: mForceSetPixelSamplerStates[i] = true; michael@0: mCurPixelTextureSerials[i] = 0; michael@0: } michael@0: michael@0: mAppliedIBSerial = 0; michael@0: mAppliedProgramBinarySerial = 0; michael@0: mDxUniformsDirty = true; michael@0: michael@0: mVertexDeclarationCache.markStateDirty(); michael@0: } michael@0: michael@0: void Renderer9::releaseDeviceResources() michael@0: { michael@0: while (!mEventQueryPool.empty()) michael@0: { michael@0: mEventQueryPool.back()->Release(); michael@0: mEventQueryPool.pop_back(); michael@0: } michael@0: michael@0: if (mMaskedClearSavedState) michael@0: { michael@0: mMaskedClearSavedState->Release(); michael@0: mMaskedClearSavedState = NULL; michael@0: } michael@0: michael@0: mVertexShaderCache.clear(); michael@0: mPixelShaderCache.clear(); michael@0: michael@0: delete mBlit; michael@0: mBlit = NULL; michael@0: michael@0: delete mVertexDataManager; michael@0: mVertexDataManager = NULL; michael@0: michael@0: delete mIndexDataManager; michael@0: mIndexDataManager = NULL; michael@0: michael@0: delete mLineLoopIB; michael@0: mLineLoopIB = NULL; michael@0: michael@0: for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) michael@0: { michael@0: delete mNullColorbufferCache[i].buffer; michael@0: mNullColorbufferCache[i].buffer = NULL; michael@0: } michael@0: michael@0: } michael@0: michael@0: michael@0: void Renderer9::notifyDeviceLost() michael@0: { michael@0: mDeviceLost = true; michael@0: mDisplay->notifyDeviceLost(); michael@0: } michael@0: michael@0: bool Renderer9::isDeviceLost() michael@0: { michael@0: return mDeviceLost; michael@0: } michael@0: michael@0: // set notify to true to broadcast a message to all contexts of the device loss michael@0: bool Renderer9::testDeviceLost(bool notify) michael@0: { michael@0: HRESULT status = S_OK; michael@0: michael@0: if (mDeviceEx) michael@0: { michael@0: status = mDeviceEx->CheckDeviceState(NULL); michael@0: } michael@0: else if (mDevice) michael@0: { michael@0: status = mDevice->TestCooperativeLevel(); michael@0: } michael@0: else michael@0: { michael@0: // No device yet, so no reset required michael@0: } michael@0: michael@0: bool isLost = FAILED(status) || d3d9::isDeviceLostError(status); michael@0: michael@0: if (isLost) michael@0: { michael@0: // ensure we note the device loss -- michael@0: // we'll probably get this done again by notifyDeviceLost michael@0: // but best to remember it! michael@0: // Note that we don't want to clear the device loss status here michael@0: // -- this needs to be done by resetDevice michael@0: mDeviceLost = true; michael@0: if (notify) michael@0: { michael@0: notifyDeviceLost(); michael@0: } michael@0: } michael@0: michael@0: return isLost; michael@0: } michael@0: michael@0: bool Renderer9::testDeviceResettable() michael@0: { michael@0: HRESULT status = D3D_OK; michael@0: michael@0: if (mDeviceEx) michael@0: { michael@0: status = mDeviceEx->CheckDeviceState(NULL); michael@0: } michael@0: else if (mDevice) michael@0: { michael@0: status = mDevice->TestCooperativeLevel(); michael@0: } michael@0: michael@0: // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted michael@0: // DEVICEREMOVED indicates the device has been stopped and must be recreated michael@0: switch (status) michael@0: { michael@0: case D3DERR_DEVICENOTRESET: michael@0: case D3DERR_DEVICEHUNG: michael@0: return true; michael@0: case D3DERR_DEVICELOST: michael@0: return (mDeviceEx != NULL); michael@0: case D3DERR_DEVICEREMOVED: michael@0: UNIMPLEMENTED(); michael@0: return false; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool Renderer9::resetDevice() michael@0: { michael@0: releaseDeviceResources(); michael@0: michael@0: D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); michael@0: michael@0: HRESULT result = D3D_OK; michael@0: bool lost = testDeviceLost(false); michael@0: int attempts = 3; michael@0: michael@0: while (lost && attempts > 0) michael@0: { michael@0: if (mDeviceEx) michael@0: { michael@0: Sleep(500); // Give the graphics driver some CPU time michael@0: result = mDeviceEx->ResetEx(&presentParameters, NULL); michael@0: } michael@0: else michael@0: { michael@0: result = mDevice->TestCooperativeLevel(); michael@0: while (result == D3DERR_DEVICELOST) michael@0: { michael@0: Sleep(100); // Give the graphics driver some CPU time michael@0: result = mDevice->TestCooperativeLevel(); michael@0: } michael@0: michael@0: if (result == D3DERR_DEVICENOTRESET) michael@0: { michael@0: result = mDevice->Reset(&presentParameters); michael@0: } michael@0: } michael@0: michael@0: lost = testDeviceLost(false); michael@0: attempts --; michael@0: } michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Reset/ResetEx failed multiple times: 0x%08X", result); michael@0: return false; michael@0: } michael@0: michael@0: // reset device defaults michael@0: initializeDevice(); michael@0: mDeviceLost = false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: DWORD Renderer9::getAdapterVendor() const michael@0: { michael@0: return mAdapterIdentifier.VendorId; michael@0: } michael@0: michael@0: std::string Renderer9::getRendererDescription() const michael@0: { michael@0: std::ostringstream rendererString; michael@0: michael@0: rendererString << mAdapterIdentifier.Description; michael@0: if (getShareHandleSupport()) michael@0: { michael@0: rendererString << " Direct3D9Ex"; michael@0: } michael@0: else michael@0: { michael@0: rendererString << " Direct3D9"; michael@0: } michael@0: michael@0: rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion); michael@0: rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion); michael@0: michael@0: return rendererString.str(); michael@0: } michael@0: michael@0: GUID Renderer9::getAdapterIdentifier() const michael@0: { michael@0: return mAdapterIdentifier.DeviceIdentifier; michael@0: } michael@0: michael@0: void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) michael@0: { michael@0: for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) michael@0: { michael@0: HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, michael@0: TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); michael@0: michael@0: multiSampleArray[multiSampleIndex] = SUCCEEDED(result); michael@0: } michael@0: } michael@0: michael@0: bool Renderer9::getBGRATextureSupport() const michael@0: { michael@0: // DirectX 9 always supports BGRA michael@0: return true; michael@0: } michael@0: michael@0: bool Renderer9::getDXT1TextureSupport() michael@0: { michael@0: return mDXT1TextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getDXT3TextureSupport() michael@0: { michael@0: return mDXT3TextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getDXT5TextureSupport() michael@0: { michael@0: return mDXT5TextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getDepthTextureSupport() const michael@0: { michael@0: return mDepthTextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) michael@0: { michael@0: *filtering = mFloat32FilterSupport; michael@0: *renderable = mFloat32RenderSupport; michael@0: return mFloat32TextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) michael@0: { michael@0: *filtering = mFloat16FilterSupport; michael@0: *renderable = mFloat16RenderSupport; michael@0: return mFloat16TextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getLuminanceTextureSupport() michael@0: { michael@0: return mLuminanceTextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getLuminanceAlphaTextureSupport() michael@0: { michael@0: return mLuminanceAlphaTextureSupport; michael@0: } michael@0: michael@0: bool Renderer9::getTextureFilterAnisotropySupport() const michael@0: { michael@0: return mSupportsTextureFilterAnisotropy; michael@0: } michael@0: michael@0: float Renderer9::getTextureMaxAnisotropy() const michael@0: { michael@0: if (mSupportsTextureFilterAnisotropy) michael@0: { michael@0: return static_cast(mDeviceCaps.MaxAnisotropy); michael@0: } michael@0: return 1.0f; michael@0: } michael@0: michael@0: bool Renderer9::getEventQuerySupport() michael@0: { michael@0: return mEventQuerySupport; michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxVertexTextureImageUnits() const michael@0: { michael@0: META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); michael@0: return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0; michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxCombinedTextureImageUnits() const michael@0: { michael@0: return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); michael@0: } michael@0: michael@0: unsigned int Renderer9::getReservedVertexUniformVectors() const michael@0: { michael@0: return 2; // dx_ViewAdjust and dx_DepthRange. michael@0: } michael@0: michael@0: unsigned int Renderer9::getReservedFragmentUniformVectors() const michael@0: { michael@0: return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange. michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxVertexUniformVectors() const michael@0: { michael@0: return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors(); michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxFragmentUniformVectors() const michael@0: { michael@0: const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2; michael@0: michael@0: return maxPixelConstantVectors - getReservedFragmentUniformVectors(); michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxVaryingVectors() const michael@0: { michael@0: return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2; michael@0: } michael@0: michael@0: bool Renderer9::getNonPower2TextureSupport() const michael@0: { michael@0: return mSupportsNonPower2Textures; michael@0: } michael@0: michael@0: bool Renderer9::getOcclusionQuerySupport() const michael@0: { michael@0: return mOcclusionQuerySupport; michael@0: } michael@0: michael@0: bool Renderer9::getInstancingSupport() const michael@0: { michael@0: return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); michael@0: } michael@0: michael@0: bool Renderer9::getShareHandleSupport() const michael@0: { michael@0: // PIX doesn't seem to support using share handles, so disable them. michael@0: return (mD3d9Ex != NULL) && !gl::perfActive(); michael@0: } michael@0: michael@0: bool Renderer9::getDerivativeInstructionSupport() const michael@0: { michael@0: return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0; michael@0: } michael@0: michael@0: bool Renderer9::getPostSubBufferSupport() const michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: int Renderer9::getMajorShaderModel() const michael@0: { michael@0: return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion); michael@0: } michael@0: michael@0: float Renderer9::getMaxPointSize() const michael@0: { michael@0: // Point size clamped at 1.0f for SM2 michael@0: return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f; michael@0: } michael@0: michael@0: int Renderer9::getMaxViewportDimension() const michael@0: { michael@0: int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()), michael@0: (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE); michael@0: return maxTextureDimension; michael@0: } michael@0: michael@0: int Renderer9::getMaxTextureWidth() const michael@0: { michael@0: return (int)mDeviceCaps.MaxTextureWidth; michael@0: } michael@0: michael@0: int Renderer9::getMaxTextureHeight() const michael@0: { michael@0: return (int)mDeviceCaps.MaxTextureHeight; michael@0: } michael@0: michael@0: bool Renderer9::get32BitIndexSupport() const michael@0: { michael@0: return mDeviceCaps.MaxVertexIndex >= (1 << 16); michael@0: } michael@0: michael@0: DWORD Renderer9::getCapsDeclTypes() const michael@0: { michael@0: return mDeviceCaps.DeclTypes; michael@0: } michael@0: michael@0: int Renderer9::getMinSwapInterval() const michael@0: { michael@0: return mMinSwapInterval; michael@0: } michael@0: michael@0: int Renderer9::getMaxSwapInterval() const michael@0: { michael@0: return mMaxSwapInterval; michael@0: } michael@0: michael@0: int Renderer9::getMaxSupportedSamples() const michael@0: { michael@0: return mMaxSupportedSamples; michael@0: } michael@0: michael@0: int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const michael@0: { michael@0: if (requested == 0) michael@0: { michael@0: return requested; michael@0: } michael@0: michael@0: std::map::const_iterator itr = mMultiSampleSupport.find(format); michael@0: if (itr == mMultiSampleSupport.end()) michael@0: { michael@0: if (format == D3DFMT_UNKNOWN) michael@0: return 0; michael@0: return -1; michael@0: } michael@0: michael@0: for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) michael@0: { michael@0: if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) michael@0: { michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: unsigned int Renderer9::getMaxRenderTargets() const michael@0: { michael@0: // we do not support MRT in d3d9 michael@0: return 1; michael@0: } michael@0: michael@0: D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) michael@0: { michael@0: switch (internalformat) michael@0: { michael@0: case GL_DEPTH_COMPONENT16: michael@0: case GL_DEPTH_COMPONENT32_OES: michael@0: case GL_DEPTH24_STENCIL8_OES: michael@0: return D3DFMT_INTZ; michael@0: case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: michael@0: case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: michael@0: return D3DFMT_DXT1; michael@0: case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: michael@0: return D3DFMT_DXT3; michael@0: case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: michael@0: return D3DFMT_DXT5; michael@0: case GL_RGBA32F_EXT: michael@0: case GL_RGB32F_EXT: michael@0: case GL_ALPHA32F_EXT: michael@0: case GL_LUMINANCE32F_EXT: michael@0: case GL_LUMINANCE_ALPHA32F_EXT: michael@0: return D3DFMT_A32B32G32R32F; michael@0: case GL_RGBA16F_EXT: michael@0: case GL_RGB16F_EXT: michael@0: case GL_ALPHA16F_EXT: michael@0: case GL_LUMINANCE16F_EXT: michael@0: case GL_LUMINANCE_ALPHA16F_EXT: michael@0: return D3DFMT_A16B16G16R16F; michael@0: case GL_LUMINANCE8_EXT: michael@0: if (getLuminanceTextureSupport()) michael@0: { michael@0: return D3DFMT_L8; michael@0: } michael@0: break; michael@0: case GL_LUMINANCE8_ALPHA8_EXT: michael@0: if (getLuminanceAlphaTextureSupport()) michael@0: { michael@0: return D3DFMT_A8L8; michael@0: } michael@0: break; michael@0: case GL_RGB8_OES: michael@0: case GL_RGB565: michael@0: return D3DFMT_X8R8G8B8; michael@0: } michael@0: michael@0: return D3DFMT_A8R8G8B8; michael@0: } michael@0: michael@0: bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) michael@0: { michael@0: bool result = false; michael@0: michael@0: if (source && dest) michael@0: { michael@0: TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance()); michael@0: TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance()); michael@0: michael@0: int levels = source9->levelCount(); michael@0: for (int i = 0; i < levels; ++i) michael@0: { michael@0: IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false); michael@0: IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false); michael@0: michael@0: result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); michael@0: michael@0: if (srcSurf) srcSurf->Release(); michael@0: if (dstSurf) dstSurf->Release(); michael@0: michael@0: if (!result) michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) michael@0: { michael@0: bool result = false; michael@0: michael@0: if (source && dest) michael@0: { michael@0: TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance()); michael@0: TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance()); michael@0: int levels = source9->levelCount(); michael@0: for (int f = 0; f < 6; f++) michael@0: { michael@0: for (int i = 0; i < levels; i++) michael@0: { michael@0: IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); michael@0: IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); michael@0: michael@0: result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); michael@0: michael@0: if (srcSurf) srcSurf->Release(); michael@0: if (dstSurf) dstSurf->Release(); michael@0: michael@0: if (!result) michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: D3DPOOL Renderer9::getBufferPool(DWORD usage) const michael@0: { michael@0: if (mD3d9Ex != NULL) michael@0: { michael@0: return D3DPOOL_DEFAULT; michael@0: } michael@0: else michael@0: { michael@0: if (!(usage & D3DUSAGE_DYNAMIC)) michael@0: { michael@0: return D3DPOOL_MANAGED; michael@0: } michael@0: } michael@0: michael@0: return D3DPOOL_DEFAULT; michael@0: } michael@0: michael@0: bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, michael@0: GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) michael@0: { michael@0: RECT rect; michael@0: rect.left = sourceRect.x; michael@0: rect.top = sourceRect.y; michael@0: rect.right = sourceRect.x + sourceRect.width; michael@0: rect.bottom = sourceRect.y + sourceRect.height; michael@0: michael@0: return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level); michael@0: } michael@0: michael@0: bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, michael@0: GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) michael@0: { michael@0: RECT rect; michael@0: rect.left = sourceRect.x; michael@0: rect.top = sourceRect.y; michael@0: rect.right = sourceRect.x + sourceRect.width; michael@0: rect.bottom = sourceRect.y + sourceRect.height; michael@0: michael@0: return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level); michael@0: } michael@0: michael@0: bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect, michael@0: bool blitRenderTarget, bool blitDepthStencil) michael@0: { michael@0: endScene(); michael@0: michael@0: if (blitRenderTarget) michael@0: { michael@0: gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0); michael@0: gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0); michael@0: RenderTarget9 *readRenderTarget = NULL; michael@0: RenderTarget9 *drawRenderTarget = NULL; michael@0: IDirect3DSurface9* readSurface = NULL; michael@0: IDirect3DSurface9* drawSurface = NULL; michael@0: michael@0: if (readBuffer) michael@0: { michael@0: readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); michael@0: } michael@0: if (drawBuffer) michael@0: { michael@0: drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); michael@0: } michael@0: michael@0: if (readRenderTarget) michael@0: { michael@0: readSurface = readRenderTarget->getSurface(); michael@0: } michael@0: if (drawRenderTarget) michael@0: { michael@0: drawSurface = drawRenderTarget->getSurface(); michael@0: } michael@0: michael@0: if (!readSurface || !drawSurface) michael@0: { michael@0: ERR("Failed to retrieve the render target."); michael@0: return gl::error(GL_OUT_OF_MEMORY, false); michael@0: } michael@0: michael@0: RECT srcRect; michael@0: srcRect.left = readRect.x; michael@0: srcRect.right = readRect.x + readRect.width; michael@0: srcRect.top = readRect.y; michael@0: srcRect.bottom = readRect.y + readRect.height; michael@0: michael@0: RECT dstRect; michael@0: dstRect.left = drawRect.x; michael@0: dstRect.right = drawRect.x + drawRect.width; michael@0: dstRect.top = drawRect.y; michael@0: dstRect.bottom = drawRect.y + drawRect.height; michael@0: michael@0: HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE); michael@0: michael@0: readSurface->Release(); michael@0: drawSurface->Release(); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: if (blitDepthStencil) michael@0: { michael@0: gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); michael@0: gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); michael@0: RenderTarget9 *readDepthStencil = NULL; michael@0: RenderTarget9 *drawDepthStencil = NULL; michael@0: IDirect3DSurface9* readSurface = NULL; michael@0: IDirect3DSurface9* drawSurface = NULL; michael@0: michael@0: if (readBuffer) michael@0: { michael@0: readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); michael@0: } michael@0: if (drawBuffer) michael@0: { michael@0: drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); michael@0: } michael@0: michael@0: if (readDepthStencil) michael@0: { michael@0: readSurface = readDepthStencil->getSurface(); michael@0: } michael@0: if (drawDepthStencil) michael@0: { michael@0: drawSurface = drawDepthStencil->getSurface(); michael@0: } michael@0: michael@0: if (!readSurface || !drawSurface) michael@0: { michael@0: ERR("Failed to retrieve the render target."); michael@0: return gl::error(GL_OUT_OF_MEMORY, false); michael@0: } michael@0: michael@0: HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); michael@0: michael@0: readSurface->Release(); michael@0: drawSurface->Release(); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, michael@0: GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) michael@0: { michael@0: RenderTarget9 *renderTarget = NULL; michael@0: IDirect3DSurface9 *surface = NULL; michael@0: gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0); michael@0: michael@0: if (colorbuffer) michael@0: { michael@0: renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); michael@0: } michael@0: michael@0: if (renderTarget) michael@0: { michael@0: surface = renderTarget->getSurface(); michael@0: } michael@0: michael@0: if (!surface) michael@0: { michael@0: // context must be lost michael@0: return; michael@0: } michael@0: michael@0: D3DSURFACE_DESC desc; michael@0: surface->GetDesc(&desc); michael@0: michael@0: if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) michael@0: { michael@0: UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: HRESULT result; michael@0: IDirect3DSurface9 *systemSurface = NULL; michael@0: bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && michael@0: x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && michael@0: desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; michael@0: if (directToPixels) michael@0: { michael@0: // Use the pixels ptr as a shared handle to write directly into client's memory michael@0: result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, michael@0: D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); michael@0: if (FAILED(result)) michael@0: { michael@0: // Try again without the shared handle michael@0: directToPixels = false; michael@0: } michael@0: } michael@0: michael@0: if (!directToPixels) michael@0: { michael@0: result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, michael@0: D3DPOOL_SYSTEMMEM, &systemSurface, NULL); michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: result = mDevice->GetRenderTargetData(surface, systemSurface); michael@0: surface->Release(); michael@0: surface = NULL; michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: systemSurface->Release(); michael@0: michael@0: // It turns out that D3D will sometimes produce more error michael@0: // codes than those documented. michael@0: if (d3d9::isDeviceLostError(result)) michael@0: { michael@0: notifyDeviceLost(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: else michael@0: { michael@0: UNREACHABLE(); michael@0: return; michael@0: } michael@0: michael@0: } michael@0: michael@0: if (directToPixels) michael@0: { michael@0: systemSurface->Release(); michael@0: return; michael@0: } michael@0: michael@0: RECT rect; michael@0: rect.left = gl::clamp(x, 0L, static_cast(desc.Width)); michael@0: rect.top = gl::clamp(y, 0L, static_cast(desc.Height)); michael@0: rect.right = gl::clamp(x + width, 0L, static_cast(desc.Width)); michael@0: rect.bottom = gl::clamp(y + height, 0L, static_cast(desc.Height)); michael@0: michael@0: D3DLOCKED_RECT lock; michael@0: result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: UNREACHABLE(); michael@0: systemSurface->Release(); michael@0: michael@0: return; // No sensible error to generate michael@0: } michael@0: michael@0: unsigned char *dest = (unsigned char*)pixels; michael@0: unsigned short *dest16 = (unsigned short*)pixels; michael@0: michael@0: unsigned char *source; michael@0: int inputPitch; michael@0: if (packReverseRowOrder) michael@0: { michael@0: source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); michael@0: inputPitch = -lock.Pitch; michael@0: } michael@0: else michael@0: { michael@0: source = (unsigned char*)lock.pBits; michael@0: inputPitch = lock.Pitch; michael@0: } michael@0: michael@0: unsigned int fastPixelSize = 0; michael@0: michael@0: if (desc.Format == D3DFMT_A8R8G8B8 && michael@0: format == GL_BGRA_EXT && michael@0: type == GL_UNSIGNED_BYTE) michael@0: { michael@0: fastPixelSize = 4; michael@0: } michael@0: else if ((desc.Format == D3DFMT_A4R4G4B4 && michael@0: format == GL_BGRA_EXT && michael@0: type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) || michael@0: (desc.Format == D3DFMT_A1R5G5B5 && michael@0: format == GL_BGRA_EXT && michael@0: type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)) michael@0: { michael@0: fastPixelSize = 2; michael@0: } michael@0: else if (desc.Format == D3DFMT_A16B16G16R16F && michael@0: format == GL_RGBA && michael@0: type == GL_HALF_FLOAT_OES) michael@0: { michael@0: fastPixelSize = 8; michael@0: } michael@0: else if (desc.Format == D3DFMT_A32B32G32R32F && michael@0: format == GL_RGBA && michael@0: type == GL_FLOAT) michael@0: { michael@0: fastPixelSize = 16; michael@0: } michael@0: michael@0: for (int j = 0; j < rect.bottom - rect.top; j++) michael@0: { michael@0: if (fastPixelSize != 0) michael@0: { michael@0: // Fast path for formats which require no translation: michael@0: // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE michael@0: // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT michael@0: // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT michael@0: // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES michael@0: // D3DFMT_A32B32G32R32F to RGBA/FLOAT michael@0: // michael@0: // Note that buffers with no alpha go through the slow path below. michael@0: memcpy(dest + j * outputPitch, michael@0: source + j * inputPitch, michael@0: (rect.right - rect.left) * fastPixelSize); michael@0: continue; michael@0: } michael@0: else if (desc.Format == D3DFMT_A8R8G8B8 && michael@0: format == GL_RGBA && michael@0: type == GL_UNSIGNED_BYTE) michael@0: { michael@0: // Fast path for swapping red with blue michael@0: for (int i = 0; i < rect.right - rect.left; i++) michael@0: { michael@0: unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); michael@0: *(unsigned int*)(dest + 4 * i + j * outputPitch) = michael@0: (argb & 0xFF00FF00) | // Keep alpha and green michael@0: (argb & 0x00FF0000) >> 16 | // Move red to blue michael@0: (argb & 0x000000FF) << 16; // Move blue to red michael@0: } michael@0: continue; michael@0: } michael@0: michael@0: for (int i = 0; i < rect.right - rect.left; i++) michael@0: { michael@0: float r; michael@0: float g; michael@0: float b; michael@0: float a; michael@0: michael@0: switch (desc.Format) michael@0: { michael@0: case D3DFMT_R5G6B5: michael@0: { michael@0: unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); michael@0: michael@0: a = 1.0f; michael@0: b = (rgb & 0x001F) * (1.0f / 0x001F); michael@0: g = (rgb & 0x07E0) * (1.0f / 0x07E0); michael@0: r = (rgb & 0xF800) * (1.0f / 0xF800); michael@0: } michael@0: break; michael@0: case D3DFMT_A1R5G5B5: michael@0: { michael@0: unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); michael@0: michael@0: a = (argb & 0x8000) ? 1.0f : 0.0f; michael@0: b = (argb & 0x001F) * (1.0f / 0x001F); michael@0: g = (argb & 0x03E0) * (1.0f / 0x03E0); michael@0: r = (argb & 0x7C00) * (1.0f / 0x7C00); michael@0: } michael@0: break; michael@0: case D3DFMT_A8R8G8B8: michael@0: { michael@0: unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); michael@0: michael@0: a = (argb & 0xFF000000) * (1.0f / 0xFF000000); michael@0: b = (argb & 0x000000FF) * (1.0f / 0x000000FF); michael@0: g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); michael@0: r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); michael@0: } michael@0: break; michael@0: case D3DFMT_X8R8G8B8: michael@0: { michael@0: unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); michael@0: michael@0: a = 1.0f; michael@0: b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); michael@0: g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); michael@0: r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); michael@0: } michael@0: break; michael@0: case D3DFMT_A2R10G10B10: michael@0: { michael@0: unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); michael@0: michael@0: a = (argb & 0xC0000000) * (1.0f / 0xC0000000); michael@0: b = (argb & 0x000003FF) * (1.0f / 0x000003FF); michael@0: g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); michael@0: r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); michael@0: } michael@0: break; michael@0: case D3DFMT_A32B32G32R32F: michael@0: { michael@0: // float formats in D3D are stored rgba, rather than the other way round michael@0: r = *((float*)(source + 16 * i + j * inputPitch) + 0); michael@0: g = *((float*)(source + 16 * i + j * inputPitch) + 1); michael@0: b = *((float*)(source + 16 * i + j * inputPitch) + 2); michael@0: a = *((float*)(source + 16 * i + j * inputPitch) + 3); michael@0: } michael@0: break; michael@0: case D3DFMT_A16B16G16R16F: michael@0: { michael@0: // float formats in D3D are stored rgba, rather than the other way round michael@0: r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0)); michael@0: g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1)); michael@0: b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2)); michael@0: a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3)); michael@0: } michael@0: break; michael@0: default: michael@0: UNIMPLEMENTED(); // FIXME michael@0: UNREACHABLE(); michael@0: return; michael@0: } michael@0: michael@0: switch (format) michael@0: { michael@0: case GL_RGBA: michael@0: switch (type) michael@0: { michael@0: case GL_UNSIGNED_BYTE: michael@0: dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); michael@0: dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); michael@0: dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); michael@0: dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case GL_BGRA_EXT: michael@0: switch (type) michael@0: { michael@0: case GL_UNSIGNED_BYTE: michael@0: dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); michael@0: dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); michael@0: dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); michael@0: dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); michael@0: break; michael@0: case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: michael@0: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section michael@0: // this type is packed as follows: michael@0: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 michael@0: // -------------------------------------------------------------------------------- michael@0: // | 4th | 3rd | 2nd | 1st component | michael@0: // -------------------------------------------------------------------------------- michael@0: // in the case of BGRA_EXT, B is the first component, G the second, and so forth. michael@0: dest16[i + j * outputPitch / sizeof(unsigned short)] = michael@0: ((unsigned short)(15 * a + 0.5f) << 12)| michael@0: ((unsigned short)(15 * r + 0.5f) << 8) | michael@0: ((unsigned short)(15 * g + 0.5f) << 4) | michael@0: ((unsigned short)(15 * b + 0.5f) << 0); michael@0: break; michael@0: case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: michael@0: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section michael@0: // this type is packed as follows: michael@0: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 michael@0: // -------------------------------------------------------------------------------- michael@0: // | 4th | 3rd | 2nd | 1st component | michael@0: // -------------------------------------------------------------------------------- michael@0: // in the case of BGRA_EXT, B is the first component, G the second, and so forth. michael@0: dest16[i + j * outputPitch / sizeof(unsigned short)] = michael@0: ((unsigned short)( a + 0.5f) << 15) | michael@0: ((unsigned short)(31 * r + 0.5f) << 10) | michael@0: ((unsigned short)(31 * g + 0.5f) << 5) | michael@0: ((unsigned short)(31 * b + 0.5f) << 0); michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case GL_RGB: michael@0: switch (type) michael@0: { michael@0: case GL_UNSIGNED_SHORT_5_6_5: michael@0: dest16[i + j * outputPitch / sizeof(unsigned short)] = michael@0: ((unsigned short)(31 * b + 0.5f) << 0) | michael@0: ((unsigned short)(63 * g + 0.5f) << 5) | michael@0: ((unsigned short)(31 * r + 0.5f) << 11); michael@0: break; michael@0: case GL_UNSIGNED_BYTE: michael@0: dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); michael@0: dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); michael@0: dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: systemSurface->UnlockRect(); michael@0: michael@0: systemSurface->Release(); michael@0: } michael@0: michael@0: RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth) michael@0: { michael@0: SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); michael@0: IDirect3DSurface9 *surface = NULL; michael@0: if (depth) michael@0: { michael@0: surface = swapChain9->getDepthStencil(); michael@0: } michael@0: else michael@0: { michael@0: surface = swapChain9->getRenderTarget(); michael@0: } michael@0: michael@0: RenderTarget9 *renderTarget = new RenderTarget9(this, surface); michael@0: michael@0: return renderTarget; michael@0: } michael@0: michael@0: RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) michael@0: { michael@0: RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples); michael@0: return renderTarget; michael@0: } michael@0: michael@0: ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type) michael@0: { michael@0: ShaderExecutable9 *executable = NULL; michael@0: michael@0: switch (type) michael@0: { michael@0: case rx::SHADER_VERTEX: michael@0: { michael@0: IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length); michael@0: if (vshader) michael@0: { michael@0: executable = new ShaderExecutable9(function, length, vshader); michael@0: } michael@0: } michael@0: break; michael@0: case rx::SHADER_PIXEL: michael@0: { michael@0: IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length); michael@0: if (pshader) michael@0: { michael@0: executable = new ShaderExecutable9(function, length, pshader); michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: break; michael@0: } michael@0: michael@0: return executable; michael@0: } michael@0: michael@0: ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) michael@0: { michael@0: const char *profile = NULL; michael@0: michael@0: switch (type) michael@0: { michael@0: case rx::SHADER_VERTEX: michael@0: profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0"; michael@0: break; michael@0: case rx::SHADER_PIXEL: michael@0: profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0"; michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: return NULL; michael@0: } michael@0: michael@0: ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true); michael@0: if (!binary) michael@0: return NULL; michael@0: michael@0: ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type); michael@0: binary->Release(); michael@0: michael@0: return executable; michael@0: } michael@0: michael@0: bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) michael@0: { michael@0: return mBlit->boxFilter(source, dest); michael@0: } michael@0: michael@0: D3DPOOL Renderer9::getTexturePool(DWORD usage) const michael@0: { michael@0: if (mD3d9Ex != NULL) michael@0: { michael@0: return D3DPOOL_DEFAULT; michael@0: } michael@0: else michael@0: { michael@0: if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) michael@0: { michael@0: return D3DPOOL_DEFAULT; michael@0: } michael@0: } michael@0: michael@0: return D3DPOOL_DEFAULT; michael@0: } michael@0: michael@0: bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) michael@0: { michael@0: if (source && dest) michael@0: { michael@0: HRESULT result = D3DERR_OUTOFVIDEOMEMORY; michael@0: michael@0: if (fromManaged) michael@0: { michael@0: D3DSURFACE_DESC desc; michael@0: source->GetDesc(&desc); michael@0: michael@0: IDirect3DSurface9 *surf = 0; michael@0: result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: Image9::copyLockableSurfaces(surf, source); michael@0: result = mDevice->UpdateSurface(surf, NULL, dest, NULL); michael@0: surf->Release(); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: endScene(); michael@0: result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); michael@0: } michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: Image *Renderer9::createImage() michael@0: { michael@0: return new Image9(); michael@0: } michael@0: michael@0: void Renderer9::generateMipmap(Image *dest, Image *src) michael@0: { michael@0: Image9 *src9 = Image9::makeImage9(src); michael@0: Image9 *dst9 = Image9::makeImage9(dest); michael@0: Image9::generateMipmap(dst9, src9); michael@0: } michael@0: michael@0: TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain) michael@0: { michael@0: SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); michael@0: return new TextureStorage9_2D(this, swapChain9); michael@0: } michael@0: michael@0: TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) michael@0: { michael@0: return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height); michael@0: } michael@0: michael@0: TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) michael@0: { michael@0: return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size); michael@0: } michael@0: michael@0: bool Renderer9::getLUID(LUID *adapterLuid) const michael@0: { michael@0: adapterLuid->HighPart = 0; michael@0: adapterLuid->LowPart = 0; michael@0: michael@0: if (mD3d9Ex) michael@0: { michael@0: mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: }