source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreHardwareBufferManager.h @ 1812

Revision 1812, 19.1 KB checked in by gumbau, 18 years ago (diff)
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#ifndef __HardwareBufferManager__
26#define __HardwareBufferManager__
27
28// Precompiler options
29#include "OgrePrerequisites.h"
30
31#include "OgreSingleton.h"
32#include "OgreHardwareVertexBuffer.h"
33#include "OgreHardwareIndexBuffer.h"
34
35
36namespace Ogre {
37
38    /** Abtract interface representing a 'licensee' of a hardware buffer copy.
39    remarks
40        Often it's useful to have temporary buffers which are used for working
41        but are not necessarily needed permanently. However, creating and
42        destroying buffers is expensive, so we need a way to share these
43        working areas, especially those based on existing fixed buffers.
44        This class represents a licensee of one of those temporary buffers,
45        and must be implemented by any user of a temporary buffer if they
46        wish to be notified when the license is expired.
47    */
48    class _OgreExport HardwareBufferLicensee
49    {
50    public:
51        virtual ~HardwareBufferLicensee() { }
52        /** This method is called when the buffer license is expired and is about
53        to be returned to the shared pool. */
54        virtual void licenseExpired(HardwareBuffer* buffer) = 0;
55    };
56
57    /** Structure for recording the use of temporary blend buffers */
58    class _OgreExport TempBlendedBufferInfo : public HardwareBufferLicensee
59    {
60    private:
61        // Pre-blended
62        HardwareVertexBufferSharedPtr srcPositionBuffer;
63        HardwareVertexBufferSharedPtr srcNormalBuffer;
64        // Post-blended
65        HardwareVertexBufferSharedPtr destPositionBuffer;
66        HardwareVertexBufferSharedPtr destNormalBuffer;
67        /// Both positions and normals are contained in the same buffer
68        bool posNormalShareBuffer;
69        unsigned short posBindIndex;
70        unsigned short normBindIndex;
71        bool bindPositions;
72        bool bindNormals;
73
74    public:
75        ~TempBlendedBufferInfo(void);
76        /// Utility method, extract info from the given VertexData
77        void extractFrom(const VertexData* sourceData);
78        /// Utility method, checks out temporary copies of src into dest
79        void checkoutTempCopies(bool positions = true, bool normals = true);
80        /// Utility method, binds dest copies into a given VertexData struct
81        void bindTempCopies(VertexData* targetData, bool suppressHardwareUpload);
82        /** Overridden member from HardwareBufferLicensee. */
83        void licenseExpired(HardwareBuffer* buffer);
84                /** Detect currently have buffer copies checked out and touch it */
85                bool buffersCheckedOut(bool positions = true, bool normals = true) const;
86    };
87
88
89    /** Abstract singleton class for managing hardware buffers, a concrete instance
90    of this will be created by the RenderSystem. */
91    class _OgreExport HardwareBufferManager : public Singleton<HardwareBufferManager>
92    {
93        friend class HardwareVertexBufferSharedPtr;
94        friend class HardwareIndexBufferSharedPtr;
95    protected:
96        /** WARNING: The following two members should place before all other members.
97            Members destruct order is very important here, because destructing other
98            members will cause notify back to this class, and then will access to this
99            two members.
100        */
101        typedef std::set<HardwareVertexBuffer*> VertexBufferList;
102        typedef std::set<HardwareIndexBuffer*> IndexBufferList;
103        VertexBufferList mVertexBuffers;
104        IndexBufferList mIndexBuffers;
105
106
107        typedef std::set<VertexDeclaration*> VertexDeclarationList;
108                typedef std::set<VertexBufferBinding*> VertexBufferBindingList;
109        VertexDeclarationList mVertexDeclarations;
110                VertexBufferBindingList mVertexBufferBindings;
111
112
113        /// Internal method for destroys all vertex declarations
114        virtual void destroyAllDeclarations(void);
115        /// Internal method for destroys all vertex buffer bindings
116        virtual void destroyAllBindings(void);
117
118        /// Internal method for creates a new vertex declaration, may be overridden by certain rendering APIs
119        virtual VertexDeclaration* createVertexDeclarationImpl(void);
120        /// Internal method for destroys a vertex declaration, may be overridden by certain rendering APIs
121        virtual void destroyVertexDeclarationImpl(VertexDeclaration* decl);
122
123                /// Internal method for creates a new VertexBufferBinding, may be overridden by certain rendering APIs
124                virtual VertexBufferBinding* createVertexBufferBindingImpl(void);
125                /// Internal method for destroys a VertexBufferBinding, may be overridden by certain rendering APIs
126                virtual void destroyVertexBufferBindingImpl(VertexBufferBinding* binding);
127
128    public:
129
130        enum BufferLicenseType
131        {
132            /// Licensee will only release buffer when it says so
133            BLT_MANUAL_RELEASE,
134            /// Licensee can have license revoked
135            BLT_AUTOMATIC_RELEASE
136        };
137
138    protected:
139        /** Struct holding details of a license to use a temporary shared buffer. */
140        class _OgrePrivate VertexBufferLicense
141        {
142        public:
143            HardwareVertexBuffer* originalBufferPtr;
144            BufferLicenseType licenseType;
145            size_t expiredDelay;
146            HardwareVertexBufferSharedPtr buffer;
147            HardwareBufferLicensee* licensee;
148            VertexBufferLicense(
149                HardwareVertexBuffer* orig,
150                BufferLicenseType ltype,
151                size_t delay,
152                HardwareVertexBufferSharedPtr buf,
153                HardwareBufferLicensee* lic)
154                : originalBufferPtr(orig)
155                , licenseType(ltype)
156                , expiredDelay(delay)
157                , buffer(buf)
158                , licensee(lic)
159            {}
160
161        };
162
163        /// Map from original buffer to temporary buffers
164        typedef std::multimap<HardwareVertexBuffer*, HardwareVertexBufferSharedPtr> FreeTemporaryVertexBufferMap;
165        /// Map of current available temp buffers
166        FreeTemporaryVertexBufferMap mFreeTempVertexBufferMap;
167        /// Map from temporary buffer to details of a license
168        typedef std::map<HardwareVertexBuffer*, VertexBufferLicense> TemporaryVertexBufferLicenseMap;
169        /// Map of currently licensed temporary buffers
170        TemporaryVertexBufferLicenseMap mTempVertexBufferLicenses;
171        /// Number of frames elapsed since temporary buffers utilization was above half the available
172        size_t mUnderUsedFrameCount;
173        /// Number of frames to wait before free unused temporary buffers
174        static const size_t UNDER_USED_FRAME_THRESHOLD;
175        /// Frame delay for BLT_AUTOMATIC_RELEASE temporary buffers
176        static const size_t EXPIRED_DELAY_FRAME_THRESHOLD;
177
178
179        /// Creates  a new buffer as a copy of the source, does not copy data
180        HardwareVertexBufferSharedPtr makeBufferCopy(
181            const HardwareVertexBufferSharedPtr& source,
182            HardwareBuffer::Usage usage, bool useShadowBuffer);
183
184    public:
185        HardwareBufferManager();
186        virtual ~HardwareBufferManager();
187                /** Create a hardware vertex buffer.
188        @remarks
189            This method creates a new vertex buffer; this will act as a source of geometry
190            data for rendering objects. Note that because the meaning of the contents of
191            the vertex buffer depends on the usage, this method does not specify a
192            vertex format; the user of this buffer can actually insert whatever data
193            they wish, in any format. However, in order to use this with a RenderOperation,
194            the data in this vertex buffer will have to be associated with a semantic element
195            of the rendering pipeline, e.g. a position, or texture coordinates. This is done
196            using the VertexDeclaration class, which itself contains VertexElement structures
197            referring to the source data.
198        @remarks Note that because vertex buffers can be shared, they are reference
199            counted so you do not need to worry about destroying themm this will be done
200            automatically.
201        @param vertexSize The size in bytes of each vertex in this buffer; you must calculate
202            this based on the kind of data you expect to populate this buffer with.
203        @param numVerts The number of vertices in this buffer.
204        @param usage One or more members of the HardwareBuffer::Usage enumeration; you are
205            strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
206            update regularly, consider HBU_DYNAMIC_WRITE_ONLY and useShadowBuffer=true.
207                @param useShadowBuffer If set to true, this buffer will be 'shadowed' by one stored in
208            system memory rather than GPU or AGP memory. You should set this flag if you intend
209            to read data back from the vertex buffer, because reading data from a buffer
210                        in the GPU or AGP memory is very expensive, and is in fact impossible if you
211            specify HBU_WRITE_ONLY for the main buffer. If you use this option, all
212            reads and writes will be done to the shadow buffer, and the shadow buffer will
213            be synchronised with the real buffer at an appropriate time.
214        */
215                virtual HardwareVertexBufferSharedPtr
216            createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
217                        bool useShadowBuffer = false) = 0;
218                /** Create a hardware index buffer.
219        @remarks Note that because buffers can be shared, they are reference
220            counted so you do not need to worry about destroying themm this will be done
221            automatically.
222                @param itype The type in index, either 16- or 32-bit, depending on how many vertices
223                        you need to be able to address
224                @param numIndexes The number of indexes in the buffer
225        @param usage One or more members of the HardwareBuffer::Usage enumeration.
226                @param useShadowBuffer If set to true, this buffer will be 'shadowed' by one stored in
227            system memory rather than GPU or AGP memory. You should set this flag if you intend
228            to read data back from the index buffer, because reading data from a buffer
229                        in the GPU or AGP memory is very expensive, and is in fact impossible if you
230            specify HBU_WRITE_ONLY for the main buffer. If you use this option, all
231            reads and writes will be done to the shadow buffer, and the shadow buffer will
232            be synchronised with the real buffer at an appropriate time.
233        */
234                virtual HardwareIndexBufferSharedPtr
235            createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
236                        HardwareBuffer::Usage usage, bool useShadowBuffer = false) = 0;
237
238        /** Creates a new vertex declaration. */
239        virtual VertexDeclaration* createVertexDeclaration(void);
240        /** Destroys a vertex declaration. */
241        virtual void destroyVertexDeclaration(VertexDeclaration* decl);
242
243                /** Creates a new VertexBufferBinding. */
244                virtual VertexBufferBinding* createVertexBufferBinding(void);
245                /** Destroys a VertexBufferBinding. */
246                virtual void destroyVertexBufferBinding(VertexBufferBinding* binding);
247
248                /** Registers a vertex buffer as a copy of another.
249                @remarks
250                        This is useful for registering an existing buffer as a temporary buffer
251                        which can be allocated just like a copy.
252                */
253                virtual void registerVertexBufferSourceAndCopy(
254                        const HardwareVertexBufferSharedPtr& sourceBuffer,
255                        const HardwareVertexBufferSharedPtr& copy);
256
257        /** Allocates a copy of a given vertex buffer.
258        @remarks
259            This method allocates a temporary copy of an existing vertex buffer.
260            This buffer is subsequently stored and can be made available for
261            other purposes later without incurring the cost of construction /
262            destruction.
263        @param sourceBuffer The source buffer to use as a copy
264        @param licenseType The type of license required on this buffer - automatic
265            release causes this class to release licenses every frame so that
266            they can be reallocated anew.
267        @param licensee Pointer back to the class requesting the copy, which must
268            implement HardwareBufferLicense in order to be notified when the license
269            expires.
270        @param copyData If true, the current data is copied as well as the
271            structure of the buffer
272        */
273        virtual HardwareVertexBufferSharedPtr allocateVertexBufferCopy(
274            const HardwareVertexBufferSharedPtr& sourceBuffer,
275            BufferLicenseType licenseType,
276            HardwareBufferLicensee* licensee,
277            bool copyData = false);
278
279        /** Manually release a vertex buffer copy for others to subsequently use.
280        @remarks
281            Only required if the original call to allocateVertexBufferCopy
282            included a licenseType of BLT_MANUAL_RELEASE.
283        @param bufferCopy The buffer copy. The caller is expected to delete
284            or at least no longer use this reference, since another user may
285            well begin to modify the contents of the buffer.
286        */
287        virtual void releaseVertexBufferCopy(
288            const HardwareVertexBufferSharedPtr& bufferCopy);
289
290        /** Tell engine that the vertex buffer copy intent to reuse.
291        @remarks
292            Ogre internal keep an expired delay counter of BLT_AUTOMATIC_RELEASE
293            buffers, when the counter count down to zero, it'll release for other
294            purposes later. But you can use this function to reset the counter to
295            the internal configured value, keep the buffer not get released for
296            some frames.
297        @param bufferCopy The buffer copy. The caller is expected to keep this
298            buffer copy for use.
299        */
300        virtual void touchVertexBufferCopy(
301            const HardwareVertexBufferSharedPtr& bufferCopy);
302
303        /** Free all unused vertex buffer copies.
304        @remarks
305            This method free all temporary vertex buffers that not in used.
306            In normally, temporary vertex buffers are subsequently stored and can
307            be made available for other purposes later without incurring the cost
308            of construction / destruction. But in some cases you want to free them
309            to save hardware memory (e.g. application was runs in a long time, you
310            might free temporary buffers periodically to avoid memory overload).
311        */
312        virtual void _freeUnusedBufferCopies(void);
313
314        /** Internal method for releasing all temporary buffers which have been
315           allocated using BLT_AUTOMATIC_RELEASE; is called by OGRE.
316        @param forceFreeUnused If true, free all unused temporary buffers.
317            If false, auto detect and free all unused temporary buffers based on
318            temporary buffers utilization.
319        */
320        virtual void _releaseBufferCopies(bool forceFreeUnused = false);
321
322        /** Internal method that forces the release of copies of a given buffer.
323        @remarks
324            This usually means that the buffer which the copies are based on has
325            been changed in some fundamental way, and the owner of the original
326            wishes to make that known so that new copies will reflect the
327            changes.
328        @param sourceBuffer the source buffer as a shared pointer.  Any buffer copies created from the source buffer
329            are deleted.
330        */
331        virtual void _forceReleaseBufferCopies(
332            const HardwareVertexBufferSharedPtr& sourceBuffer);
333
334        /** Internal method that forces the release of copies of a given buffer.
335        @remarks
336            This usually means that the buffer which the copies are based on has
337            been changed in some fundamental way, and the owner of the original
338            wishes to make that known so that new copies will reflect the
339            changes.
340        @param sourceBuffer the source buffer as a pointer.  Any buffer copies created from the source buffer
341            are deleted.
342        */
343        virtual void _forceReleaseBufferCopies(HardwareVertexBuffer* sourceBuffer);
344
345                /// Notification that a hardware vertex buffer has been destroyed
346                void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf);
347                /// Notification that a hardware index buffer has been destroyed
348                void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf);
349
350        /** Override standard Singleton retrieval.
351        @remarks
352        Why do we do this? Well, it's because the Singleton
353        implementation is in a .h file, which means it gets compiled
354        into anybody who includes it. This is needed for the
355        Singleton template to work, but we actually only want it
356        compiled into the implementation of the class based on the
357        Singleton, not all of them. If we don't change this, we get
358        link errors when trying to use the Singleton-based class from
359        an outside dll.
360        @par
361        This method just delegates to the template version anyway,
362        but the implementation stays in this single compilation unit,
363        preventing link errors.
364        */
365        static HardwareBufferManager& getSingleton(void);
366        /** Override standard Singleton retrieval.
367        @remarks
368        Why do we do this? Well, it's because the Singleton
369        implementation is in a .h file, which means it gets compiled
370        into anybody who includes it. This is needed for the
371        Singleton template to work, but we actually only want it
372        compiled into the implementation of the class based on the
373        Singleton, not all of them. If we don't change this, we get
374        link errors when trying to use the Singleton-based class from
375        an outside dll.
376        @par
377        This method just delegates to the template version anyway,
378        but the implementation stays in this single compilation unit,
379        preventing link errors.
380        */
381        static HardwareBufferManager* getSingletonPtr(void);
382           
383    };
384
385}
386
387#endif
388
Note: See TracBrowser for help on using the repository browser.