source: OGRE/trunk/ogrenew/RenderSystems/Direct3D9/src/OgreD3D9HardwareBufferManager.cpp @ 657

Revision 657, 7.3 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

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-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 "OgreD3D9HardwareBufferManager.h"
26#include "OgreD3D9HardwareVertexBuffer.h"
27#include "OgreD3D9HardwareIndexBuffer.h"
28#include "OgreD3D9VertexDeclaration.h"
29#include "OgreLogManager.h"
30#include "OgreStringConverter.h"
31
32
33namespace Ogre {
34    //-----------------------------------------------------------------------
35    D3D9HardwareBufferManager::D3D9HardwareBufferManager(LPDIRECT3DDEVICE9 device)
36        : mlpD3DDevice(device)
37    {
38    }
39    //-----------------------------------------------------------------------
40    D3D9HardwareBufferManager::~D3D9HardwareBufferManager()
41    {
42        destroyAllDeclarations();
43        destroyAllBindings();
44    }
45    //-----------------------------------------------------------------------
46    HardwareVertexBufferSharedPtr
47    D3D9HardwareBufferManager::
48    createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
49                bool useShadowBuffer)
50    {
51                assert (numVerts > 0);
52#if OGRE_D3D_MANAGE_BUFFERS
53        // Override shadow buffer setting; managed buffers are automatically
54        // backed by system memory
55        // Don't override shadow buffer if discardable, since then we use
56        // unmanaged buffers for speed (avoids write-through overhead)
57        if (useShadowBuffer && !(usage & HardwareBuffer::HBU_DISCARDABLE))
58        {
59            useShadowBuffer = false;
60            // Also drop any WRITE_ONLY so we can read direct
61            if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
62            {
63                usage = HardwareBuffer::HBU_DYNAMIC;
64            }
65            else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
66            {
67                usage = HardwareBuffer::HBU_STATIC;
68            }
69        }
70#endif
71                D3D9HardwareVertexBuffer* vbuf = new D3D9HardwareVertexBuffer(
72                        vertexSize, numVerts, usage, mlpD3DDevice, false, useShadowBuffer);
73                mVertexBuffers.insert(vbuf);
74        return HardwareVertexBufferSharedPtr(vbuf);
75    }
76    //-----------------------------------------------------------------------
77    void D3D9HardwareBufferManager::destroyAllDeclarations(void)
78    {
79        VertexDeclarationList::iterator decl;
80        for (decl = mVertexDeclarations.begin(); decl != mVertexDeclarations.end(); ++decl)
81        {
82            delete *decl;
83        }
84        mVertexDeclarations.clear();
85    }
86    //-----------------------------------------------------------------------
87        HardwareIndexBufferSharedPtr
88    D3D9HardwareBufferManager::
89    createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
90        HardwareBuffer::Usage usage, bool useShadowBuffer)
91    {
92                assert (numIndexes > 0);
93#if OGRE_D3D_MANAGE_BUFFERS
94        // Override shadow buffer setting; managed buffers are automatically
95        // backed by system memory
96        if (useShadowBuffer)
97        {
98            useShadowBuffer = false;
99            // Also drop any WRITE_ONLY so we can read direct
100            if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
101            {
102                usage = HardwareBuffer::HBU_DYNAMIC;
103            }
104            else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
105            {
106                usage = HardwareBuffer::HBU_STATIC;
107            }
108        }
109#endif
110                D3D9HardwareIndexBuffer* idx = new D3D9HardwareIndexBuffer(
111                        itype, numIndexes, usage, mlpD3DDevice, false, useShadowBuffer);
112                mIndexBuffers.insert(idx);
113                return HardwareIndexBufferSharedPtr(idx);
114           
115    }
116    //-----------------------------------------------------------------------
117    VertexDeclaration* D3D9HardwareBufferManager::createVertexDeclaration(void)
118    {
119        VertexDeclaration* decl = new D3D9VertexDeclaration(mlpD3DDevice);
120        mVertexDeclarations.push_back(decl);
121        return decl;
122       
123    }
124    //-----------------------------------------------------------------------
125    void D3D9HardwareBufferManager::destroyVertexDeclaration(VertexDeclaration* decl)
126    {
127        mVertexDeclarations.remove(decl);
128        delete decl;
129    }
130        //-----------------------------------------------------------------------
131        void D3D9HardwareBufferManager::releaseDefaultPoolResources(void)
132        {
133                size_t iCount = 0;
134                size_t vCount = 0;
135
136                VertexBufferList::iterator v, vend;
137                vend = mVertexBuffers.end();
138                for (v = mVertexBuffers.begin(); v != vend; ++v)
139                {
140                        D3D9HardwareVertexBuffer* vbuf =
141                                static_cast<D3D9HardwareVertexBuffer*>(*v);
142                        if (vbuf->releaseIfDefaultPool())
143                                vCount++;
144                }
145
146
147                IndexBufferList::iterator i, iend;
148                iend = mIndexBuffers.end();
149                for (i = mIndexBuffers.begin(); i != iend; ++i)
150                {
151                        D3D9HardwareIndexBuffer* ibuf =
152                                static_cast<D3D9HardwareIndexBuffer*>(*i);
153                        if (ibuf->releaseIfDefaultPool())
154                                iCount++;
155
156                }
157
158                LogManager::getSingleton().logMessage("D3D9HardwareBufferManager released:");
159                LogManager::getSingleton().logMessage(
160                        StringConverter::toString(vCount) + " unmanaged vertex buffers");
161                LogManager::getSingleton().logMessage(
162                        StringConverter::toString(iCount) + " unmanaged index buffers");
163        }
164        //-----------------------------------------------------------------------
165        void D3D9HardwareBufferManager::recreateDefaultPoolResources(void)
166        {
167                size_t iCount = 0;
168                size_t vCount = 0;
169
170                VertexBufferList::iterator v, vend;
171                vend = mVertexBuffers.end();
172                for (v = mVertexBuffers.begin(); v != vend; ++v)
173                {
174                        D3D9HardwareVertexBuffer* vbuf =
175                                static_cast<D3D9HardwareVertexBuffer*>(*v);
176                        if (vbuf->recreateIfDefaultPool(mlpD3DDevice))
177                                vCount++;
178                }
179
180
181                IndexBufferList::iterator i, iend;
182                iend = mIndexBuffers.end();
183                for (i = mIndexBuffers.begin(); i != iend; ++i)
184                {
185                        D3D9HardwareIndexBuffer* ibuf =
186                                static_cast<D3D9HardwareIndexBuffer*>(*i);
187                        if (ibuf->recreateIfDefaultPool(mlpD3DDevice))
188                                iCount++;
189
190                }
191
192                LogManager::getSingleton().logMessage("D3D9HardwareBufferManager recreated:");
193                LogManager::getSingleton().logMessage(
194                        StringConverter::toString(vCount) + " unmanaged vertex buffers");
195                LogManager::getSingleton().logMessage(
196                        StringConverter::toString(iCount) + " unmanaged index buffers");
197        }
198        //-----------------------------------------------------------------------
199
200}
Note: See TracBrowser for help on using the repository browser.