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

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