Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/RenderSystems/Direct3D9/src/OgreD3D9HardwareIndexBuffer.cpp @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 5.7 KB
Line 
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-2006 Torus Knot Software Ltd
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
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreD3D9HardwareIndexBuffer.h"
30#include "OgreD3D9Mappings.h"
31#include "OgreException.h"
32#include "OgreD3D9HardwareBufferManager.h"
33
34namespace Ogre {
35
36        //---------------------------------------------------------------------
37    D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer(HardwareIndexBuffer::IndexType idxType, 
38        size_t numIndexes, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev, 
39        bool useSystemMemory, bool useShadowBuffer)
40        : HardwareIndexBuffer(idxType, numIndexes, usage, useSystemMemory, useShadowBuffer)
41    {
42#if OGRE_D3D_MANAGE_BUFFERS
43                mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : 
44                        // If not system mem, use managed pool UNLESS buffer is discardable
45                        // if discardable, keeping the software backing is expensive
46                        (usage & HardwareBuffer::HBU_DISCARDABLE)? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
47#else
48                mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT;
49#endif
50        // Create the Index buffer
51        HRESULT hr = pDev->CreateIndexBuffer(
52            static_cast<UINT>(mSizeInBytes),
53            D3D9Mappings::get(mUsage),
54            D3D9Mappings::get(mIndexType),
55                        mD3DPool,
56            &mlpD3DBuffer,
57            NULL
58            );
59           
60        if (FAILED(hr))
61        {
62                        String msg = DXGetErrorDescription9(hr);
63            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, 
64                                "Cannot create D3D9 Index buffer: " + msg, 
65                "D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer");
66        }
67
68    }
69        //---------------------------------------------------------------------
70    D3D9HardwareIndexBuffer::~D3D9HardwareIndexBuffer()
71    {
72        SAFE_RELEASE(mlpD3DBuffer);
73    }
74        //---------------------------------------------------------------------
75    void* D3D9HardwareIndexBuffer::lockImpl(size_t offset, 
76        size_t length, LockOptions options)
77    {
78        void* pBuf;
79        HRESULT hr = mlpD3DBuffer->Lock(
80            static_cast<UINT>(offset), 
81            static_cast<UINT>(length), 
82            &pBuf,
83            D3D9Mappings::get(options, mUsage));
84
85        if (FAILED(hr))
86        {
87                        String msg = DXGetErrorDescription9(hr);
88            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, 
89                                "Cannot lock D3D9 Index buffer: " + msg, 
90                "D3D9HardwareIndexBuffer::lock");
91        }
92
93
94        return pBuf;
95
96
97    }
98        //---------------------------------------------------------------------
99        void D3D9HardwareIndexBuffer::unlockImpl(void)
100    {
101        HRESULT hr = mlpD3DBuffer->Unlock();
102    }
103        //---------------------------------------------------------------------
104    void D3D9HardwareIndexBuffer::readData(size_t offset, size_t length, 
105        void* pDest)
106    {
107       // There is no functional interface in D3D, just do via manual
108        // lock, copy & unlock
109        void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
110        memcpy(pDest, pSrc, length);
111        this->unlock();
112
113    }
114        //---------------------------------------------------------------------
115    void D3D9HardwareIndexBuffer::writeData(size_t offset, size_t length, 
116            const void* pSource,
117                        bool discardWholeBuffer)
118    {
119       // There is no functional interface in D3D, just do via manual
120        // lock, copy & unlock
121        void* pDst = this->lock(offset, length, 
122            discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
123        memcpy(pDst, pSource, length);
124        this->unlock();    }
125        //---------------------------------------------------------------------
126        bool D3D9HardwareIndexBuffer::releaseIfDefaultPool(void)
127        {
128                if (mD3DPool == D3DPOOL_DEFAULT)
129                {
130                        SAFE_RELEASE(mlpD3DBuffer);
131                        return true;
132                }
133                return false;
134
135        }
136        //---------------------------------------------------------------------
137        bool D3D9HardwareIndexBuffer::recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev)
138        {
139                if (mD3DPool == D3DPOOL_DEFAULT)
140                {
141                        // Create the Index buffer
142                        HRESULT hr = pDev->CreateIndexBuffer(
143                                static_cast<UINT>(mSizeInBytes),
144                                D3D9Mappings::get(mUsage),
145                                D3D9Mappings::get(mIndexType),
146                                mD3DPool,
147                                &mlpD3DBuffer,
148                                NULL
149                                );
150
151                        if (FAILED(hr))
152                        {
153                                String msg = DXGetErrorDescription9(hr);
154                                OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, 
155                                        "Cannot create D3D9 Index buffer: " + msg, 
156                                        "D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer");
157                        }
158
159                        return true;
160                }
161                return false;
162        }
163        //---------------------------------------------------------------------
164
165}
Note: See TracBrowser for help on using the repository browser.