Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreD3D9HardwareIndexBuffer.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreD3D9HardwareIndexBuffer.h"
00026 #include "OgreD3D9Mappings.h"
00027 #include "OgreException.h"
00028 
00029 namespace Ogre {
00030 
00031     //---------------------------------------------------------------------
00032     D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer(HardwareIndexBuffer::IndexType idxType, 
00033         size_t numIndexes, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev, 
00034         bool useSystemMemory, bool useShadowBuffer)
00035         : HardwareIndexBuffer(idxType, numIndexes, usage, useSystemMemory, useShadowBuffer)
00036     {
00037         // Create the Index buffer
00038         HRESULT hr = pDev->CreateIndexBuffer(
00039             static_cast<UINT>(mSizeInBytes),
00040             D3D9Mappings::get(usage),
00041             D3D9Mappings::get(idxType),
00042 #if OGRE_D3D_MANAGE_BUFFERS
00043             useSystemMemory? D3DPOOL_SYSTEMMEM : 
00044             // If not system mem, use managed pool UNLESS buffer is discardable
00045             // if discardable, keeping the software backing is expensive
00046                 (usage & HardwareBuffer::HBU_DISCARDABLE)? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, 
00047 #else
00048             useSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT, 
00049 #endif
00050             &mlpD3DBuffer,
00051             NULL
00052             );
00053             
00054         if (FAILED(hr))
00055         {
00056             String msg = DXGetErrorDescription9(hr);
00057             Except(hr, "Cannot create D3D9 Index buffer: " + msg, 
00058                 "D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer");
00059         }
00060 
00061     }
00062     //---------------------------------------------------------------------
00063     D3D9HardwareIndexBuffer::~D3D9HardwareIndexBuffer()
00064     {
00065         SAFE_RELEASE(mlpD3DBuffer);
00066     }
00067     //---------------------------------------------------------------------
00068     void* D3D9HardwareIndexBuffer::lockImpl(size_t offset, 
00069         size_t length, LockOptions options)
00070     {
00071         void* pBuf;
00072         DWORD lockOpts;
00073         if (!(mUsage & HBU_DYNAMIC) && options == HBL_DISCARD)
00074         {
00075             // D3D doesn't like discard on non-dynamic buffers
00076             lockOpts = 0;
00077         }
00078         else
00079         {
00080             lockOpts= D3D9Mappings::get(options);
00081         } 
00082         HRESULT hr = mlpD3DBuffer->Lock(
00083             static_cast<UINT>(offset), 
00084             static_cast<UINT>(length), 
00085             &pBuf,
00086             lockOpts);
00087 
00088         if (FAILED(hr))
00089         {
00090             Except(hr, "Cannot lock D3D9 Index buffer", 
00091                 "D3D9HardwareIndexBuffer::lock");
00092         }
00093 
00094 
00095         return pBuf;
00096 
00097 
00098     }
00099     //---------------------------------------------------------------------
00100     void D3D9HardwareIndexBuffer::unlockImpl(void)
00101     {
00102         HRESULT hr = mlpD3DBuffer->Unlock();
00103     }
00104     //---------------------------------------------------------------------
00105     void D3D9HardwareIndexBuffer::readData(size_t offset, size_t length, 
00106         void* pDest)
00107     {
00108        // There is no functional interface in D3D, just do via manual 
00109         // lock, copy & unlock
00110         void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
00111         memcpy(pDest, pSrc, length);
00112         this->unlock();
00113 
00114     }
00115     //---------------------------------------------------------------------
00116     void D3D9HardwareIndexBuffer::writeData(size_t offset, size_t length, 
00117             const void* pSource,
00118             bool discardWholeBuffer)
00119     {
00120        // There is no functional interface in D3D, just do via manual 
00121         // lock, copy & unlock
00122         void* pDst = this->lock(offset, length, 
00123             discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
00124         memcpy(pDst, pSource, length);
00125         this->unlock();    }
00126     //---------------------------------------------------------------------
00127 
00128 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:20 2004