source: OGRE/trunk/ogrenew/RenderSystems/Direct3D9/src/OgreD3D9HardwareVertexBuffer.cpp @ 692

Revision 692, 5.4 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

RevLine 
[692]1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreD3D9HardwareVertexBuffer.h"
26#include "OgreD3D9Mappings.h"
27#include "OgreException.h"
28#include "OgreD3D9HardwareBufferManager.h"
29
30namespace Ogre {
31
32        //---------------------------------------------------------------------
33    D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer(size_t vertexSize,
34        size_t numVertices, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev,
35        bool useSystemMemory, bool useShadowBuffer)
36        : HardwareVertexBuffer(vertexSize, numVertices, usage, useSystemMemory, useShadowBuffer)
37    {
38        // Create the vertex buffer
39#if OGRE_D3D_MANAGE_BUFFERS
40                mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM :
41                        // If not system mem, use managed pool UNLESS buffer is discardable
42                        // if discardable, keeping the software backing is expensive
43                        (usage & HardwareBuffer::HBU_DISCARDABLE)? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
44#else
45                mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT;
46#endif
47        HRESULT hr = pDev->CreateVertexBuffer(
48            static_cast<UINT>(mSizeInBytes),
49            D3D9Mappings::get(usage),
50            0, // No FVF here, thankyou
51                        mD3DPool,
52            &mlpD3DBuffer,
53            NULL);
54        if (FAILED(hr))
55        {
56                        String msg = DXGetErrorDescription9(hr);
57            OGRE_EXCEPT(hr, "Cannot create D3D9 vertex buffer: " + msg,
58                "D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer");
59        }
60
61    }
62        //---------------------------------------------------------------------
63    D3D9HardwareVertexBuffer::~D3D9HardwareVertexBuffer()
64    {
65        SAFE_RELEASE(mlpD3DBuffer);
66    }
67        //---------------------------------------------------------------------
68    void* D3D9HardwareVertexBuffer::lockImpl(size_t offset,
69        size_t length, LockOptions options)
70    {
71        void* pBuf;
72        HRESULT hr = mlpD3DBuffer->Lock(
73            static_cast<UINT>(offset),
74            static_cast<UINT>(length),
75            &pBuf,
76            D3D9Mappings::get(options, mUsage));
77
78        if (FAILED(hr))
79        {
80            OGRE_EXCEPT(hr, "Cannot lock D3D9 vertex buffer",
81                "D3D9HardwareVertexBuffer::lock");
82        }
83
84        return pBuf;
85    }
86        //---------------------------------------------------------------------
87        void D3D9HardwareVertexBuffer::unlockImpl(void)
88    {
89        HRESULT hr = mlpD3DBuffer->Unlock();
90    }
91        //---------------------------------------------------------------------
92    void D3D9HardwareVertexBuffer::readData(size_t offset, size_t length,
93        void* pDest)
94    {
95        // There is no functional interface in D3D, just do via manual
96        // lock, copy & unlock
97        void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
98        memcpy(pDest, pSrc, length);
99        this->unlock();
100
101    }
102        //---------------------------------------------------------------------
103    void D3D9HardwareVertexBuffer::writeData(size_t offset, size_t length,
104            const void* pSource,
105                        bool discardWholeBuffer)
106    {
107        // There is no functional interface in D3D, just do via manual
108        // lock, copy & unlock
109        void* pDst = this->lock(offset, length,
110            discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
111        memcpy(pDst, pSource, length);
112        this->unlock();
113    }
114        //---------------------------------------------------------------------
115        bool D3D9HardwareVertexBuffer::releaseIfDefaultPool(void)
116        {
117                if (mD3DPool == D3DPOOL_DEFAULT)
118                {
119                        SAFE_RELEASE(mlpD3DBuffer);
120                        return true;
121                }
122                return false;
123        }
124        //---------------------------------------------------------------------
125        bool D3D9HardwareVertexBuffer::recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev)
126        {
127                if (mD3DPool == D3DPOOL_DEFAULT)
128                {
129                        HRESULT hr = pDev->CreateVertexBuffer(
130                                static_cast<UINT>(mSizeInBytes),
131                                D3D9Mappings::get(mUsage),
132                                0, // No FVF here, thankyou
133                                mD3DPool,
134                                &mlpD3DBuffer,
135                                NULL);
136                        if (FAILED(hr))
137                        {
138                                String msg = DXGetErrorDescription9(hr);
139                                OGRE_EXCEPT(hr, "Cannot restore D3D9 vertex buffer: " + msg,
140                                        "D3D9HardwareVertexBuffer::recreateIfDefaultPool");
141                        }
142                        return true;
143                }
144                return false;
145        }
146        //---------------------------------------------------------------------
147}
Note: See TracBrowser for help on using the repository browser.