root/OpenSceneGraph/trunk/include/osg/BufferObject @ 10601

Revision 10601, 27.2 kB (checked in by robert, 5 years ago)

Introduced new GLBufferObject pool for managing the memory footprint taken up by VertexBufferObejct?, ElementBufferObject? and PixelBufferObject?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[5328]1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
[3819]2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_BUFFEROBJECT
15#define OSG_BUFFEROBJECT 1
16
[4018]17#include <osg/GL>
18#include <osg/Object>
[3819]19#include <osg/buffered_value>
[10601]20#include <osg/FrameStamp>
[3819]21
[10601]22#include <list>
23#include <map>
24
[3819]25#ifndef GL_ARB_vertex_buffer_object
26
27    #define GL_ARB_vertex_buffer_object
28
29    // for compatibility with gl.h headers that don't support VBO,
30    #if defined(_WIN64)
31        typedef __int64 GLintptrARB;
32        typedef __int64 GLsizeiptrARB;
33    #elif defined(__ia64__) || defined(__x86_64__)
34        typedef long int GLintptrARB;
35        typedef long int GLsizeiptrARB;
36    #else
37        typedef int GLintptrARB;
38        typedef int GLsizeiptrARB;
39    #endif
40
41    #define GL_ARRAY_BUFFER_ARB               0x8892
42    #define GL_ELEMENT_ARRAY_BUFFER_ARB       0x8893
43    #define GL_ARRAY_BUFFER_BINDING_ARB       0x8894
44    #define GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB 0x8895
45    #define GL_VERTEX_ARRAY_BUFFER_BINDING_ARB 0x8896
46    #define GL_NORMAL_ARRAY_BUFFER_BINDING_ARB 0x8897
47    #define GL_COLOR_ARRAY_BUFFER_BINDING_ARB 0x8898
48    #define GL_INDEX_ARRAY_BUFFER_BINDING_ARB 0x8899
49    #define GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING_ARB 0x889A
50    #define GL_EDGE_FLAG_ARRAY_BUFFER_BINDING_ARB 0x889B
51    #define GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING_ARB 0x889C
52    #define GL_FOG_COORDINATE_ARRAY_BUFFER_BINDING_ARB 0x889D
53    #define GL_WEIGHT_ARRAY_BUFFER_BINDING_ARB 0x889E
54    #define GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB 0x889F
55    #define GL_STREAM_DRAW_ARB                0x88E0
56    #define GL_STREAM_READ_ARB                0x88E1
57    #define GL_STREAM_COPY_ARB                0x88E2
58    #define GL_STATIC_DRAW_ARB                0x88E4
59    #define GL_STATIC_READ_ARB                0x88E5
60    #define GL_STATIC_COPY_ARB                0x88E6
61    #define GL_DYNAMIC_DRAW_ARB               0x88E8
62    #define GL_DYNAMIC_READ_ARB               0x88E9
63    #define GL_DYNAMIC_COPY_ARB               0x88EA
64    #define GL_READ_ONLY_ARB                  0x88B8
65    #define GL_WRITE_ONLY_ARB                 0x88B9
66    #define GL_READ_WRITE_ARB                 0x88BA
67    #define GL_BUFFER_SIZE_ARB                0x8764
68    #define GL_BUFFER_USAGE_ARB               0x8765
69    #define GL_BUFFER_ACCESS_ARB              0x88BB
70    #define GL_BUFFER_MAPPED_ARB              0x88BC
71    #define GL_BUFFER_MAP_POINTER_ARB         0x88BD
72
73#endif
74
[8336]75#ifndef GL_VERSION_1_5
76    #define GL_STREAM_DRAW                    0x88E0
77    #define GL_STREAM_READ                    0x88E1
78    #define GL_STREAM_COPY                    0x88E2
79    #define GL_STATIC_DRAW                    0x88E4
80    #define GL_STATIC_READ                    0x88E5
81    #define GL_STATIC_COPY                    0x88E6
82    #define GL_DYNAMIC_DRAW                   0x88E8
83    #define GL_DYNAMIC_READ                   0x88E9
84    #define GL_DYNAMIC_COPY                   0x88EA
85#endif
86
87#ifndef GL_VERSION_2_1
88    #define GL_PIXEL_PACK_BUFFER              0x88EB
89    #define GL_PIXEL_UNPACK_BUFFER            0x88EC
90    #define GL_PIXEL_PACK_BUFFER_BINDING      0x88ED
91    #define GL_PIXEL_UNPACK_BUFFER_BINDING    0x88EF
92#endif
93
94
[3828]95#ifndef GL_ARB_pixel_buffer_object
[8336]96    #define GL_PIXEL_PACK_BUFFER_ARB            0x88EB
97    #define GL_PIXEL_UNPACK_BUFFER_ARB          0x88EC
[3828]98    #define GL_PIXEL_PACK_BUFFER_BINDING_ARB    0x88ED
99    #define GL_PIXEL_UNPACK_BUFFER_BINDING_ARB  0x88EF
100#endif
101
[3819]102namespace osg
103{
104
[4018]105class State;
[10600]106class BufferData;
107class BufferObject;
[4018]108
[10601]109class BufferObjectProfile
110{
111    public:
112        BufferObjectProfile():
113            _target(0),
114            _usage(0),
115            _size(0) {}
116
117        BufferObjectProfile(GLenum target, GLenum usage, unsigned int size):
118            _target(target),
119            _usage(usage),
120            _size(size) {}
121
122        BufferObjectProfile(const BufferObjectProfile& bpo):
123            _target(bpo._target),
124            _usage(bpo._usage),
125            _size(bpo._size) {}
126
127        bool operator < (const BufferObjectProfile& rhs) const
128        {
129            if (_target < rhs._target) return true;
130            else if (_target > rhs._target) return false;
131            if (_usage < rhs._usage) return true;
132            else if (_usage > rhs._usage) return false;
133            return _size < rhs._size;
134        }
135
136        bool operator == (const BufferObjectProfile& rhs) const
137        {
138            return (_target == rhs._target) &&
139                   (_usage == rhs._usage) &&
140                   (_size == rhs._size);
141        }
142
143        void setProfile(GLenum target, GLenum usage, unsigned int size)
144        {
145            _target = target;
146            _usage = usage;
147            _size = size;
148        }
149
150        BufferObjectProfile& operator = (const BufferObjectProfile& rhs)
151        {
152            _target = rhs._target;
153            _usage = rhs._usage;
154            _size = rhs._size;
155            return *this;
156        }
157
158        GLenum _target;
159        GLenum _usage;
160        GLenum _size;
161};
162
163// forward declare
164class GLBufferObjectSet;
165class GLBufferObjectManager;
166
[10600]167class OSG_EXPORT GLBufferObject : public Referenced
[3819]168{
169    public:
170
[10600]171        GLBufferObject(unsigned int contextID, BufferObject* bufferObject=0);
[3819]172
[10601]173        void setProfile(const BufferObjectProfile& profile) { _profile = profile; }
174        const BufferObjectProfile& getProfile() const { return _profile; }
175
[10600]176        void setBufferObject(BufferObject* bufferObject);
177        BufferObject* getBufferObject() { return _bufferObject; }
[3819]178
179        struct BufferEntry
180        {
[10600]181            BufferEntry(): modifiedCount(0),dataSize(0),offset(0),dataSource(0) {}
[3819]182
[10600]183            BufferEntry(const BufferEntry& rhs):
184                modifiedCount(rhs.modifiedCount),
185                dataSize(rhs.dataSize),
186                offset(rhs.offset),
187                dataSource(rhs.dataSource) {}
188
189            BufferEntry& operator = (const BufferEntry& rhs)
190            {
191                if (&rhs==this) return *this;
192                modifiedCount = rhs.modifiedCount;
193                dataSize = rhs.dataSize;
194                offset = rhs.offset;
195                dataSource = rhs.dataSource;
196                return *this;
197            }
198
199            unsigned int        modifiedCount;
200            GLsizeiptrARB       dataSize;
201            GLsizeiptrARB       offset;
202            BufferData*         dataSource;
[3819]203        };
204
[10600]205        inline unsigned int getContextID() const { return _contextID; }
[3819]206
[10600]207        inline GLuint& getGLObjectID() { return _glObjectID; }
208        inline GLuint getGLObjectID() const { return _glObjectID; }
209        inline GLsizeiptrARB getOffset(unsigned int i) const { return _bufferEntries[i].offset; }
210
[10601]211        void bindBuffer();
[3819]212
[10601]213        inline void unbindBuffer()
[3819]214        {
[10601]215            _extensions->glBindBuffer(_profile._target,0);
[3819]216        }
217
[10600]218        inline bool isDirty() const { return _dirty; }
[6574]219
[10600]220        void dirty() { _dirty = true; }
[6574]221
[10600]222        void clear();
[3819]223
[10600]224        void compileBuffer();
[6574]225
[10600]226        void deleteGLObject();
[3819]227
[10600]228        void assign(BufferObject* bufferObject);
229
230        bool isPBOSupported() const { return _extensions->isPBOSupported(); }
231
232        static GLBufferObject* createGLBufferObject(unsigned int contextID, const BufferObject* bufferObject);
233
234
[3819]235        /** Use deleteVertexBufferObject instead of glDeleteBuffers to allow
236          * OpenGL buffer objects to be cached until they can be deleted
237          * by the OpenGL context in which they were created, specified
238          * by contextID.*/
239        static void deleteBufferObject(unsigned int contextID,GLuint globj);
240
[10601]241        static void flushAllDeletedBufferObjects(unsigned int contextID);
242        static void discardAllDeletedBufferObjects(unsigned int contextID);
243        static void flushDeletedBufferObjects(unsigned int contextID,double currentTime, double& availbleTime);
244        static void releaseGLBufferObject(unsigned int contextID, GLBufferObject* to);
[3819]245
246        /** Extensions class which encapsulates the querying of extensions and
247        * associated function pointers, and provide convenience wrappers to
248        * check for the extensions or use the associated functions.*/
[4021]249        class OSG_EXPORT Extensions : public osg::Referenced
[3819]250        {
251        public:
[4102]252            Extensions(unsigned int contextID);
[3819]253
254            Extensions(const Extensions& rhs);
255
256            void lowestCommonDenominator(const Extensions& rhs);
257
[7038]258            void setupGLExtensions(unsigned int contextID);
[3819]259
260            bool isBufferObjectSupported() const { return _glGenBuffers!=0; }
[5474]261            bool isPBOSupported() const { return _isPBOSupported; }
[3819]262
263            void glGenBuffers (GLsizei n, GLuint *buffers) const;
264            void glBindBuffer (GLenum target, GLuint buffer) const;
265            void glBufferData (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage) const;
266            void glBufferSubData (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid *data) const;
267            void glDeleteBuffers (GLsizei n, const GLuint *buffers) const;
268            GLboolean glIsBuffer (GLuint buffer) const;
269            void glGetBufferSubData (GLenum target, GLintptrARB offset, GLsizeiptrARB size, GLvoid *data) const;
270            GLvoid* glMapBuffer (GLenum target, GLenum access) const;
271            GLboolean glUnmapBuffer (GLenum target) const;
272            void glGetBufferParameteriv (GLenum target, GLenum pname, GLint *params) const;
273            void glGetBufferPointerv (GLenum target, GLenum pname, GLvoid* *params) const;
274
275        protected:
276
277            typedef void (APIENTRY * GenBuffersProc) (GLsizei n, GLuint *buffers);
278            typedef void (APIENTRY * BindBufferProc) (GLenum target, GLuint buffer);
279            typedef void (APIENTRY * BufferDataProc) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
280            typedef void (APIENTRY * BufferSubDataProc) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid *data);
281            typedef void (APIENTRY * DeleteBuffersProc) (GLsizei n, const GLuint *buffers);
282            typedef GLboolean (APIENTRY * IsBufferProc) (GLuint buffer);
283            typedef void (APIENTRY * GetBufferSubDataProc) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, GLvoid *data);
284            typedef GLvoid* (APIENTRY * MapBufferProc) (GLenum target, GLenum access);
285            typedef GLboolean (APIENTRY * UnmapBufferProc) (GLenum target);
286            typedef void (APIENTRY * GetBufferParameterivProc) (GLenum target, GLenum pname, GLint *params);
287            typedef void (APIENTRY * GetBufferPointervProc) (GLenum target, GLenum pname, GLvoid* *params);
288
289            GenBuffersProc          _glGenBuffers;
290            BindBufferProc          _glBindBuffer;
291            BufferDataProc          _glBufferData;
292            BufferSubDataProc       _glBufferSubData;
293            DeleteBuffersProc       _glDeleteBuffers;
294            IsBufferProc            _glIsBuffer;
295            GetBufferSubDataProc    _glGetBufferSubData;
296            MapBufferProc           _glMapBuffer;
297            UnmapBufferProc         _glUnmapBuffer;
298            GetBufferParameterivProc _glGetBufferParameteriv;
299            GetBufferPointervProc   _glGetBufferPointerv;
300
[5474]301            bool _isPBOSupported;
[3819]302        };
303
304        /** Function to call to get the extension of a specified context.
[7648]305        * If the Extension object for that context has not yet been created 
[3819]306        * and the 'createIfNotInitalized' flag been set to false then returns NULL.
307        * If 'createIfNotInitalized' is true then the Extensions object is
308        * automatically created.  However, in this case the extension object is
309        * only created with the graphics context associated with ContextID..*/
310        static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized);
311
312        /** setExtensions allows users to override the extensions across graphics contexts.
313        * typically used when you have different extensions supported across graphics pipes
314        * but need to ensure that they all use the same low common denominator extensions.*/
315        static void setExtensions(unsigned int contextID,Extensions* extensions);
316
317    protected:
318   
[10600]319        virtual ~GLBufferObject();
[3819]320
[10600]321        unsigned int            _contextID;
322        GLuint                  _glObjectID;
[3819]323
[10601]324        BufferObjectProfile     _profile;
325        unsigned int            _allocatedSize;
[10600]326
327        bool                    _dirty;
328
329        typedef std::vector<BufferEntry> BufferEntries;
330        BufferEntries           _bufferEntries;
331
332        BufferObject*           _bufferObject;
333
334    public:
[10601]335
336        GLBufferObjectSet*      _set;
337        GLBufferObject*         _previous;
338        GLBufferObject*         _next;
339        unsigned int            _frameLastUsed;
340
341    public:
[10600]342        Extensions*             _extensions;
343
[3819]344};
345
[10601]346typedef std::list< ref_ptr<GLBufferObject> > GLBufferObjectList;
347
348class OSG_EXPORT GLBufferObjectSet : public Referenced
349{
350    public:
351        GLBufferObjectSet(GLBufferObjectManager* parent, const BufferObjectProfile& profile);
352
353        void handlePendingOrphandedGLBufferObjects();
354        void flushAllDeletedGLBufferObjects();
355        void discardAllDeletedGLBufferObjects();
356        void flushDeletedGLBufferObjects(double currentTime, double& availableTime);
357
358        GLBufferObject* takeFromOrphans(BufferObject* bufferObject);
359        GLBufferObject* takeOrGenerate(BufferObject* bufferObject);
360
361        void moveToBack(GLBufferObject* to);
362        void addToBack(GLBufferObject* to);
363        void orphan(GLBufferObject* to);
364        void remove(GLBufferObject* to);
365
366        unsigned int size() const { return _profile._size * _numOfGLBufferObjects; }
367
368        bool makeSpace(unsigned int& size);
369
370        bool checkConsistency() const;
371
372        GLBufferObjectManager* getParent() { return _parent; }
373
374
375    protected:
376
377        virtual ~GLBufferObjectSet();
378
379        OpenThreads::Mutex  _mutex;
380
381        GLBufferObjectManager*  _parent;
382        unsigned int            _contextID;
383        BufferObjectProfile     _profile;
384        unsigned int            _numOfGLBufferObjects;
385        GLBufferObjectList      _orphanedGLBufferObjects;
386        GLBufferObjectList      _pendingOrphanedGLBufferObjects;
387
388        GLBufferObject*         _head;
389        GLBufferObject*         _tail;
390};
391
392class OSG_EXPORT GLBufferObjectManager : public osg::Referenced
393{
394    public:
395        GLBufferObjectManager(unsigned int contextID);
396
397        unsigned int getContextID() const { return _contextID; }
398
399
400        void setNumberActiveGLBufferObjects(unsigned int size) { _numActiveGLBufferObjects = size; }
401        unsigned int& getNumberActiveGLBufferObjects() { return _numActiveGLBufferObjects; }
402        unsigned int getNumberActiveGLBufferObjects() const { return _numActiveGLBufferObjects; }
403
404        void setNumberOrphanedGLBufferObjects(unsigned int size) { _numOrphanedGLBufferObjects = size; }
405        unsigned int& getNumberOrphanedGLBufferObjects() { return _numOrphanedGLBufferObjects; }
406        unsigned int getNumberOrphanedGLBufferObjects() const { return _numOrphanedGLBufferObjects; }
407
408        void setCurrGLBufferObjectPoolSize(unsigned int size) { _currGLBufferObjectPoolSize = size; }
409        unsigned int& getCurrGLBufferObjectPoolSize() { return _currGLBufferObjectPoolSize; }
410        unsigned int getCurrGLBufferObjectPoolSize() const { return _currGLBufferObjectPoolSize; }
411
412        void setMaxGLBufferObjectPoolSize(unsigned int size);
413        unsigned int getMaxGLBufferObjectPoolSize() const { return _maxGLBufferObjectPoolSize; }
414
415        bool hasSpace(unsigned int size) const { return (_currGLBufferObjectPoolSize+size)<=_maxGLBufferObjectPoolSize; }
416        bool makeSpace(unsigned int size);
417
418        GLBufferObject* generateGLBufferObject(const osg::BufferObject* bufferObject);
419
420        void handlePendingOrphandedGLBufferObjects();
421        void flushAllDeletedGLBufferObjects();
422        void discardAllDeletedGLBufferObjects();
423        void flushDeletedGLBufferObjects(double currentTime, double& availableTime);
424        void releaseGLBufferObject(GLBufferObject* to);
425
426        GLBufferObjectSet* getGLBufferObjectSet(const BufferObjectProfile& profile);
427
428        void newFrame(osg::FrameStamp* fs);
429        void resetStats();
430        void reportStats();
431
432        unsigned int& getFrameNumber() { return _frameNumber; }
433        unsigned int& getNumberFrames() { return _numFrames; }
434
435        unsigned int& getNumberDeleted() { return _numDeleted; }
436        double& getDeleteTime() { return _deleteTime; }
437
438        unsigned int& getNumberGenerated() { return _numGenerated; }
439        double& getGenerateTime() { return _generateTime; }
440
441        unsigned int& getNumberApplied() { return _numApplied; }
442        double& getApplyTime() { return _applyTime; }
443
444        static osg::ref_ptr<GLBufferObjectManager>& getGLBufferObjectManager(unsigned int contextID);
445
446    protected:
447
448        typedef std::map< BufferObjectProfile, osg::ref_ptr<GLBufferObjectSet> > GLBufferObjectSetMap;
449        unsigned int            _contextID;
450        unsigned int            _numActiveGLBufferObjects;
451        unsigned int            _numOrphanedGLBufferObjects;
452        unsigned int            _currGLBufferObjectPoolSize;
453        unsigned int            _maxGLBufferObjectPoolSize;
454        GLBufferObjectSetMap    _glBufferObjectSetMap;
455
456        unsigned int            _frameNumber;
457
458        unsigned int            _numFrames;
459        unsigned int            _numDeleted;
460        double                  _deleteTime;
461
462        unsigned int            _numGenerated;
463        double                  _generateTime;
464
465        unsigned int            _numApplied;
466        double                  _applyTime;
467
468};
469
470
[10600]471class OSG_EXPORT BufferObject : public Object
472{
473    public:
474
475        BufferObject();
476
477        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
478        BufferObject(const BufferObject& bo,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
479
480        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferObject*>(obj)!=NULL; }
481        virtual const char* libraryName() const { return "osg"; }
482        virtual const char* className() const { return "BufferObject"; }
483
[10601]484        void setTarget(GLenum target) { _profile._target = target; }
485        GLenum getTarget() const { return _profile._target; }
[10600]486
487        /** Set what type of usage the buffer object will have. Options are:
488          *          GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY,
489          *          GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY,
490          *          GL_DYNAMIC_DRAW, GL_DYNAMIC_READ, or GL_DYNAMIC_COPY.
491          */
[10601]492        void setUsage(GLenum usage) { _profile._usage = usage; }
[10600]493
494        /** Get the type of usage the buffer object has been set up for.*/
[10601]495        GLenum getUsage() const { return _profile._usage; }
[10600]496
[10601]497        BufferObjectProfile& getProfile() { return _profile; }
498        const BufferObjectProfile& getProfile() const { return _profile; }
499
[10600]500        void dirty();
501
502        /** Resize any per context GLObject buffers to specified size. */
503        virtual void resizeGLObjectBuffers(unsigned int maxSize);
504
505        /** If State is non-zero, this function releases OpenGL objects for
506          * the specified graphics context. Otherwise, releases OpenGL objects
507          * for all graphics contexts. */
508        void releaseGLObjects(State* state=0) const;
509
510        unsigned int addBufferData(BufferData* bd);
511        void removeBufferData(unsigned int index);
512        void removeBufferData(BufferData* bd);
513
514        void setBufferData(unsigned int index, BufferData* bd);
515        BufferData* getBufferData(unsigned int index) { return _bufferDataList[index]; }
516        const BufferData* getBufferData(unsigned int index) const { return _bufferDataList[index]; }
517
518        unsigned int getNumBufferData() const { return _bufferDataList.size(); }
519
[10601]520        void setGLBufferObject(unsigned int contextID, GLBufferObject* glbo) { _glBufferObjects[contextID] = glbo; }
521
[10600]522        GLBufferObject* getGLBufferObject(unsigned int contextID) const { return _glBufferObjects[contextID].get(); }
523
524        GLBufferObject* getOrCreateGLBufferObject(unsigned int contextID) const
525        {
526            if (!_glBufferObjects[contextID]) _glBufferObjects[contextID] = GLBufferObject::createGLBufferObject(contextID, this);
527            return _glBufferObjects[contextID].get();
528        }
529
[10601]530        unsigned int computeRequiredBufferSize() const;
531
[10600]532    protected:
533
534        ~BufferObject();
535
536        typedef std::vector< BufferData* > BufferDataList;
537        typedef osg::buffered_object< osg::ref_ptr<GLBufferObject> > GLBufferObjects;
538
[10601]539        BufferObjectProfile     _profile;
540
[10600]541        BufferDataList          _bufferDataList;
542
543        mutable GLBufferObjects _glBufferObjects;
544};
545
546class BufferData : public Object
547{
548    public:
549
550        BufferData():
551            Object(true),
552            _modifiedCount(0),
553            _bufferIndex(0) {}
554
555        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
556        BufferData(const BufferData& bd,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
557            osg::Object(bd,copyop),
558            _modifiedCount(0),
559            _bufferIndex(0) {}
560
561        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferData*>(obj)!=NULL; }
562        virtual const char* libraryName() const { return "osg"; }
563        virtual const char* className() const { return "BufferData"; }
564
565        virtual const GLvoid*   getDataPointer() const = 0;
566        virtual unsigned int    getTotalDataSize() const = 0;
567
568        void setBufferObject(BufferObject* bufferObject);
569        BufferObject* getBufferObject() { return _bufferObject.get(); }
570        const BufferObject* getBufferObject() const { return _bufferObject.get(); }
571
572        void setBufferIndex(unsigned int index) { _bufferIndex = index; }
573        unsigned int getBufferIndex() const { return _bufferIndex; }
574
575        GLBufferObject* getGLBufferObject(unsigned int contextID) const { return _bufferObject.valid() ? _bufferObject->getGLBufferObject(contextID) : 0; }
576        GLBufferObject* getOrCreateGLBufferObject(unsigned int contextID) const { return _bufferObject.valid() ? _bufferObject->getOrCreateGLBufferObject(contextID) : 0; }
577
578        /** Dirty the primitive, which increments the modified count, to force buffer objects to update. */
579        inline void dirty() { ++_modifiedCount; if (_bufferObject.valid()) _bufferObject->dirty(); }
580
581        /** Set the modified count value.*/
582        inline void setModifiedCount(unsigned int value) { _modifiedCount=value; }
583
584        /** Get modified count value.*/
585        inline unsigned int getModifiedCount() const { return _modifiedCount; }
586
587    protected:
588
589        virtual ~BufferData();
590
591        unsigned int                    _modifiedCount;
592
593        unsigned int                    _bufferIndex;
594        osg::ref_ptr<BufferObject>      _bufferObject;
595};
596
597
[6555]598class Array;
599class OSG_EXPORT VertexBufferObject : public BufferObject
600{
601    public:
602
603        VertexBufferObject();
604
605        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
606        VertexBufferObject(const VertexBufferObject& vbo,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
607
608        META_Object(osg,VertexBufferObject);
609
610        unsigned int addArray(osg::Array* array);
[6582]611        void removeArray(osg::Array* array);
[6555]612
613        void setArray(unsigned int i, Array* array);
[10600]614        Array* getArray(unsigned int i);
615        const Array* getArray(unsigned int i) const;
[6565]616
[6555]617    protected:
618        virtual ~VertexBufferObject();
619};
620
621class DrawElements;
[6578]622class OSG_EXPORT ElementBufferObject : public BufferObject
[6555]623{
624    public:
625
[6578]626        ElementBufferObject();
[6555]627
628        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
[6578]629        ElementBufferObject(const ElementBufferObject& pbo,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
[6555]630
[6578]631        META_Object(osg,ElementBufferObject);
[6555]632       
633        unsigned int addDrawElements(osg::DrawElements* PrimitiveSet);
[6582]634        void removeDrawElements(osg::DrawElements* PrimitiveSet);
[6555]635
636        void setDrawElements(unsigned int i, DrawElements* PrimitiveSet);
[10600]637        DrawElements* getDrawElements(unsigned int i);
638        const DrawElements* getDrawElements(unsigned int i) const;
[6565]639
[6555]640    protected:
641   
[6578]642        virtual ~ElementBufferObject();
[6555]643};
644
[3819]645class Image;
[4021]646class OSG_EXPORT PixelBufferObject : public BufferObject
[3819]647{
648    public:
649
650        PixelBufferObject(osg::Image* image=0);
651
652        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
653        PixelBufferObject(const PixelBufferObject& pbo,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
654
655        META_Object(osg,PixelBufferObject);
656
657        void setImage(osg::Image* image);
658
[10600]659        Image* getImage();
660        const Image* getImage() const;
[3819]661
[10600]662        bool isPBOSupported(unsigned int contextID) const { return _glBufferObjects[contextID]->isPBOSupported(); }
[3819]663
664    protected:
665   
666        virtual ~PixelBufferObject();
667};
668
[9302]669/**
670 * This object represent a general class of pixel buffer objects,
671 * which are capable of allocating buffer object (memory)
672 * on the GPU. The memory can then be used either for CPU-GPU
673 * pixel transfer or directly for GPU-GPU transfer, without CPU intervention.
674 **/
675class OSG_EXPORT PixelDataBufferObject : public BufferObject
676{
677    public:
678        PixelDataBufferObject();
679        PixelDataBufferObject(const PixelDataBufferObject& pbo, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
[3819]680
[9302]681        META_Object(osg, PixelDataBufferObject);
682
683        //! Set new size of the buffer object. This will reallocate the memory on the next compile
[10601]684        inline void setDataSize(unsigned int size) { _profile._size = size; dirty(); }
[9302]685
686        //! Get data size of the used buffer
[10601]687        inline unsigned int getDataSize() const { return _profile._size; }
[9302]688
689        //! Compile the buffer (reallocate the memory if buffer is dirty)
690        virtual void compileBuffer(State& state) const;
691
692        //! Bind the buffer in read mode, which means that data can be downloaded from the buffer (note: GL_PIXEL_UNPACK_BUFFER_ARB)
693        virtual void bindBufferInReadMode(State& state);
694
695        //! Bind the buffer in write mode, which means following OpenGL instructions will write data into the buffer (note: GL_PIXEL_PACK_BUFFER_ARB)
696        virtual void bindBufferInWriteMode(State& state);
697
698        //! Unbind the buffer
699        virtual void unbindBuffer(unsigned int contextID) const;
700
701        /** Resize any per context GLObject buffers to specified size. */
702        virtual void resizeGLObjectBuffers(unsigned int maxSize);
703
704        enum Mode
705        {
706            //! A normal mode of this data buffer
707            NONE = 0,
708
709            //! Buffer is in read mode (@see bindBufferInReadMode)
710            READ = 1,
711
712            //! Buffer is in write mode (@see bindBufferInWriteMode)
713            WRITE = 2
714        };
715
716        Mode getMode(unsigned int contextID) const { return (Mode)_mode[contextID]; }
717
718    protected:
719
720        virtual ~PixelDataBufferObject();
721
722        typedef osg::buffered_value<unsigned int> ModeList;
723       
724        mutable ModeList _mode;
725       
726};
727
728
[3819]729}
730
731#endif
Note: See TracBrowser for help on using the browser.