Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre/OgreMain/include/OgreHardwareBufferManager.h @ 29

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

=hoffentlich gehts jetzt

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