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

OgreD3D9HardwareVertexBuffer.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 "OgreD3D9HardwareVertexBuffer.h"
00026 #include "OgreD3D9Mappings.h"
00027 #include "OgreException.h"
00028 
00029 namespace Ogre {
00030 
00031     //---------------------------------------------------------------------
00032     D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer(size_t vertexSize, 
00033         size_t numVertices, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev, 
00034         bool useSystemMemory, bool useShadowBuffer)
00035         : HardwareVertexBuffer(vertexSize, numVertices, usage, useSystemMemory, useShadowBuffer)
00036     {
00037         // Create the vertex buffer
00038         HRESULT hr = pDev->CreateVertexBuffer(
00039             static_cast<UINT>(mSizeInBytes), 
00040             D3D9Mappings::get(usage), 
00041             0, // No FVF here, thankyou
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         if (FAILED(hr))
00053         {
00054             String msg = DXGetErrorDescription9(hr);
00055             Except(hr, "Cannot create D3D9 vertex buffer: " + msg, 
00056                 "D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer");
00057         }
00058 
00059     }
00060     //---------------------------------------------------------------------
00061     D3D9HardwareVertexBuffer::~D3D9HardwareVertexBuffer()
00062     {
00063         SAFE_RELEASE(mlpD3DBuffer);
00064     }
00065     //---------------------------------------------------------------------
00066     void* D3D9HardwareVertexBuffer::lockImpl(size_t offset, 
00067         size_t length, LockOptions options)
00068     {
00069         void* pBuf;
00070         DWORD lockOpts;
00071         if (!(mUsage & HBU_DYNAMIC) && 
00072                 (options == HBL_DISCARD || options == HBL_NO_OVERWRITE))
00073         {
00074             // D3D doesn't like discard or no_overwrite on non-dynamic buffers
00075             lockOpts = 0;
00076         }
00077         else
00078         {
00079             lockOpts= D3D9Mappings::get(options);
00080         } 
00081         HRESULT hr = mlpD3DBuffer->Lock(
00082             static_cast<UINT>(offset), 
00083             static_cast<UINT>(length), 
00084             &pBuf,
00085             lockOpts);
00086 
00087         if (FAILED(hr))
00088         {
00089             Except(hr, "Cannot lock D3D9 vertex buffer", 
00090                 "D3D9HardwareVertexBuffer::lock");
00091         }
00092 
00093         return pBuf;
00094     }
00095     //---------------------------------------------------------------------
00096     void D3D9HardwareVertexBuffer::unlockImpl(void)
00097     {
00098         HRESULT hr = mlpD3DBuffer->Unlock();
00099     }
00100     //---------------------------------------------------------------------
00101     void D3D9HardwareVertexBuffer::readData(size_t offset, size_t length, 
00102         void* pDest)
00103     {
00104         // There is no functional interface in D3D, just do via manual 
00105         // lock, copy & unlock
00106         void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
00107         memcpy(pDest, pSrc, length);
00108         this->unlock();
00109 
00110     }
00111     //---------------------------------------------------------------------
00112     void D3D9HardwareVertexBuffer::writeData(size_t offset, size_t length, 
00113             const void* pSource,
00114             bool discardWholeBuffer)
00115     {
00116         // There is no functional interface in D3D, just do via manual 
00117         // lock, copy & unlock
00118         void* pDst = this->lock(offset, length, 
00119             discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
00120         memcpy(pDst, pSource, length);
00121         this->unlock();
00122     }
00123     //---------------------------------------------------------------------
00124 
00125 }

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