Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/RenderSystems/Direct3D9/src/OgreD3D9HardwareBufferManager.cpp @ 5

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

=hoffentlich gehts jetzt

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