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

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

adding ogre 1.2 and dependencies

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        HardwareIndexBufferSharedPtr
78    D3D9HardwareBufferManager::
79    createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
80        HardwareBuffer::Usage usage, bool useShadowBuffer)
81    {
82                assert (numIndexes > 0);
83#if OGRE_D3D_MANAGE_BUFFERS
84        // Override shadow buffer setting; managed buffers are automatically
85        // backed by system memory
86        if (useShadowBuffer)
87        {
88            useShadowBuffer = false;
89            // Also drop any WRITE_ONLY so we can read direct
90            if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
91            {
92                usage = HardwareBuffer::HBU_DYNAMIC;
93            }
94            else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
95            {
96                usage = HardwareBuffer::HBU_STATIC;
97            }
98        }
99#endif
100                D3D9HardwareIndexBuffer* idx = new D3D9HardwareIndexBuffer(
101                        itype, numIndexes, usage, mlpD3DDevice, false, useShadowBuffer);
102                mIndexBuffers.insert(idx);
103                return HardwareIndexBufferSharedPtr(idx);
104           
105    }
106    //-----------------------------------------------------------------------
107    VertexDeclaration* D3D9HardwareBufferManager::createVertexDeclarationImpl(void)
108    {
109        return new D3D9VertexDeclaration(mlpD3DDevice);
110    }
111    //-----------------------------------------------------------------------
112    void D3D9HardwareBufferManager::destroyVertexDeclarationImpl(VertexDeclaration* decl)
113    {
114        delete decl;
115    }
116        //-----------------------------------------------------------------------
117        void D3D9HardwareBufferManager::releaseDefaultPoolResources(void)
118        {
119                size_t iCount = 0;
120                size_t vCount = 0;
121
122                VertexBufferList::iterator v, vend;
123                vend = mVertexBuffers.end();
124                for (v = mVertexBuffers.begin(); v != vend; ++v)
125                {
126                        D3D9HardwareVertexBuffer* vbuf =
127                                static_cast<D3D9HardwareVertexBuffer*>(*v);
128                        if (vbuf->releaseIfDefaultPool())
129                                vCount++;
130                }
131
132
133                IndexBufferList::iterator i, iend;
134                iend = mIndexBuffers.end();
135                for (i = mIndexBuffers.begin(); i != iend; ++i)
136                {
137                        D3D9HardwareIndexBuffer* ibuf =
138                                static_cast<D3D9HardwareIndexBuffer*>(*i);
139                        if (ibuf->releaseIfDefaultPool())
140                                iCount++;
141
142                }
143
144                LogManager::getSingleton().logMessage("D3D9HardwareBufferManager released:");
145                LogManager::getSingleton().logMessage(
146                        StringConverter::toString(vCount) + " unmanaged vertex buffers");
147                LogManager::getSingleton().logMessage(
148                        StringConverter::toString(iCount) + " unmanaged index buffers");
149        }
150        //-----------------------------------------------------------------------
151        void D3D9HardwareBufferManager::recreateDefaultPoolResources(void)
152        {
153                size_t iCount = 0;
154                size_t vCount = 0;
155
156                VertexBufferList::iterator v, vend;
157                vend = mVertexBuffers.end();
158                for (v = mVertexBuffers.begin(); v != vend; ++v)
159                {
160                        D3D9HardwareVertexBuffer* vbuf =
161                                static_cast<D3D9HardwareVertexBuffer*>(*v);
162                        if (vbuf->recreateIfDefaultPool(mlpD3DDevice))
163                                vCount++;
164                }
165
166
167                IndexBufferList::iterator i, iend;
168                iend = mIndexBuffers.end();
169                for (i = mIndexBuffers.begin(); i != iend; ++i)
170                {
171                        D3D9HardwareIndexBuffer* ibuf =
172                                static_cast<D3D9HardwareIndexBuffer*>(*i);
173                        if (ibuf->recreateIfDefaultPool(mlpD3DDevice))
174                                iCount++;
175
176                }
177
178                LogManager::getSingleton().logMessage("D3D9HardwareBufferManager recreated:");
179                LogManager::getSingleton().logMessage(
180                        StringConverter::toString(vCount) + " unmanaged vertex buffers");
181                LogManager::getSingleton().logMessage(
182                        StringConverter::toString(iCount) + " unmanaged index buffers");
183        }
184        //-----------------------------------------------------------------------
185
186}
Note: See TracBrowser for help on using the repository browser.