15 #ifndef OSG_BUFFEROBJECT
16 #define OSG_BUFFEROBJECT 1
19 #include <osg/GLExtensions>
21 #include <osg/buffered_value>
22 #include <osg/FrameStamp>
28 #ifndef GL_ARB_vertex_buffer_object
29 #define GL_ARRAY_BUFFER_ARB 0x8892
30 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
31 #define GL_ARRAY_BUFFER_BINDING_ARB 0x8894
32 #define GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB 0x8895
33 #define GL_VERTEX_ARRAY_BUFFER_BINDING_ARB 0x8896
34 #define GL_NORMAL_ARRAY_BUFFER_BINDING_ARB 0x8897
35 #define GL_COLOR_ARRAY_BUFFER_BINDING_ARB 0x8898
36 #define GL_INDEX_ARRAY_BUFFER_BINDING_ARB 0x8899
37 #define GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING_ARB 0x889A
38 #define GL_EDGE_FLAG_ARRAY_BUFFER_BINDING_ARB 0x889B
39 #define GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING_ARB 0x889C
40 #define GL_FOG_COORDINATE_ARRAY_BUFFER_BINDING_ARB 0x889D
41 #define GL_WEIGHT_ARRAY_BUFFER_BINDING_ARB 0x889E
42 #define GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB 0x889F
43 #define GL_STREAM_DRAW_ARB 0x88E0
44 #define GL_STREAM_READ_ARB 0x88E1
45 #define GL_STREAM_COPY_ARB 0x88E2
46 #define GL_STATIC_DRAW_ARB 0x88E4
47 #define GL_STATIC_READ_ARB 0x88E5
48 #define GL_STATIC_COPY_ARB 0x88E6
49 #define GL_DYNAMIC_DRAW_ARB 0x88E8
50 #define GL_DYNAMIC_READ_ARB 0x88E9
51 #define GL_DYNAMIC_COPY_ARB 0x88EA
52 #define GL_READ_ONLY_ARB 0x88B8
53 #define GL_WRITE_ONLY_ARB 0x88B9
54 #define GL_READ_WRITE_ARB 0x88BA
55 #define GL_BUFFER_SIZE_ARB 0x8764
56 #define GL_BUFFER_USAGE_ARB 0x8765
57 #define GL_BUFFER_ACCESS_ARB 0x88BB
58 #define GL_BUFFER_MAPPED_ARB 0x88BC
59 #define GL_BUFFER_MAP_POINTER_ARB 0x88BD
62 #ifndef GL_VERSION_1_5
63 #define GL_STREAM_DRAW 0x88E0
64 #define GL_STREAM_READ 0x88E1
65 #define GL_STREAM_COPY 0x88E2
66 #define GL_STATIC_DRAW 0x88E4
67 #define GL_STATIC_READ 0x88E5
68 #define GL_STATIC_COPY 0x88E6
69 #define GL_DYNAMIC_DRAW 0x88E8
70 #define GL_DYNAMIC_READ 0x88E9
71 #define GL_DYNAMIC_COPY 0x88EA
74 #ifndef GL_VERSION_2_1
75 #define GL_PIXEL_PACK_BUFFER 0x88EB
76 #define GL_PIXEL_UNPACK_BUFFER 0x88EC
77 #define GL_PIXEL_PACK_BUFFER_BINDING 0x88ED
78 #define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF
82 #ifndef GL_ARB_pixel_buffer_object
83 #define GL_PIXEL_PACK_BUFFER_ARB 0x88EB
84 #define GL_PIXEL_UNPACK_BUFFER_ARB 0x88EC
85 #define GL_PIXEL_PACK_BUFFER_BINDING_ARB 0x88ED
86 #define GL_PIXEL_UNPACK_BUFFER_BINDING_ARB 0x88EF
130 void setProfile(GLenum target, GLenum usage,
unsigned int size)
168 BufferEntry(): numRead(0), modifiedCount(0),dataSize(0),offset(0),dataSource(0) {}
171 numRead(rhs.numRead),
172 modifiedCount(rhs.modifiedCount),
173 dataSize(rhs.dataSize),
175 dataSource(rhs.dataSource) {}
179 if (&rhs==
this)
return *
this;
188 unsigned int getNumClients()
const;
203 inline void bindBuffer();
207 _extensions->glBindBuffer(_profile._target,0);
210 inline bool isDirty()
const {
return _dirty; }
216 void compileBuffer();
218 void deleteGLObject();
226 static void deleteAllBufferObjects(
unsigned int contextID);
227 static void discardAllBufferObjects(
unsigned int contextID);
228 static void flushAllDeletedBufferObjects(
unsigned int contextID);
229 static void discardAllDeletedBufferObjects(
unsigned int contextID);
230 static void flushDeletedBufferObjects(
unsigned int contextID,
double currentTime,
double& availbleTime);
231 static void releaseGLBufferObject(
unsigned int contextID,
GLBufferObject* to);
233 bool hasAllBufferDataBeenRead()
const;
243 if (bufferAlignment<2)
return pos;
244 if ((pos%bufferAlignment)==0)
return pos;
245 return ((pos/bufferAlignment)+1)*bufferAlignment;
282 void handlePendingOrphandedGLBufferObjects();
284 void deleteAllGLBufferObjects();
285 void discardAllGLBufferObjects();
286 void flushAllDeletedGLBufferObjects();
287 void discardAllDeletedGLBufferObjects();
288 void flushDeletedGLBufferObjects(
double currentTime,
double& availableTime);
299 unsigned int size()
const {
return _profile._size * _numOfGLBufferObjects; }
301 bool makeSpace(
unsigned int& size);
303 bool checkConsistency()
const;
307 unsigned int computeNumGLBufferObjectsInList()
const;
309 unsigned int getNumOrphans()
const {
return static_cast<unsigned int>(_orphanedGLBufferObjects.size()); }
310 unsigned int getNumPendingOrphans()
const {
return static_cast<unsigned int>(_pendingOrphanedGLBufferObjects.size()); }
350 void setMaxGLBufferObjectPoolSize(
unsigned int size);
353 bool hasSpace(
unsigned int size)
const {
return (_currGLBufferObjectPoolSize+size)<=_maxGLBufferObjectPoolSize; }
354 bool makeSpace(
unsigned int size);
358 void handlePendingOrphandedGLBufferObjects();
360 void deleteAllGLBufferObjects();
361 void discardAllGLBufferObjects();
362 void flushAllDeletedGLBufferObjects();
363 void discardAllDeletedGLBufferObjects();
364 void flushDeletedGLBufferObjects(
double currentTime,
double& availableTime);
371 void reportStats(std::ostream& out);
372 void recomputeStats(std::ostream& out);
424 virtual const char*
className()
const {
return "BufferObject"; }
426 void setTarget(GLenum target) { _profile._target = target; }
434 void setUsage(GLenum usage) { _profile._usage = usage; }
437 GLenum
getUsage()
const {
return _profile._usage; }
453 virtual void resizeGLObjectBuffers(
unsigned int maxSize);
458 void releaseGLObjects(
State* state=0)
const;
461 void removeBufferData(
unsigned int index);
464 void setBufferData(
unsigned int index,
BufferData* bd);
468 unsigned int getNumBufferData()
const {
return static_cast<unsigned int>(_bufferDataList.size()); }
477 return _glBufferObjects[contextID].get();
480 unsigned int computeRequiredBufferSize()
const;
483 static void deleteBufferObject(
unsigned int contextID,GLuint globj);
516 _modifiedCallback(bd._modifiedCallback),
521 virtual const char*
className()
const {
return "BufferData"; }
523 virtual const GLvoid* getDataPointer()
const = 0;
524 virtual unsigned int getTotalDataSize()
const = 0;
565 if (_modifiedCallback.valid()) _modifiedCallback->modified(
this);
566 if (_bufferObject.valid()) _bufferObject->dirty();
576 virtual void resizeGLObjectBuffers(
unsigned int maxSize);
581 void releaseGLObjects(
State* state=0)
const;
618 void setArray(
unsigned int i,
Array* array);
619 Array* getArray(
unsigned int i);
620 const Array* getArray(
unsigned int i)
const;
641 void setDrawElements(
unsigned int i,
DrawElements* PrimitiveSet);
643 const DrawElements* getDrawElements(
unsigned int i)
const;
665 const Image* getImage()
const;
667 bool isPBOSupported(
unsigned int contextID)
const {
return _glBufferObjects[contextID]->isPBOSupported(); }
689 inline void setDataSize(
unsigned int size) { _profile._size = size; dirty(); }
692 inline unsigned int getDataSize()
const {
return _profile._size; }
695 virtual void compileBuffer(
State& state)
const;
698 virtual void bindBufferInReadMode(
State& state);
701 virtual void bindBufferInWriteMode(
State& state);
704 virtual void unbindBuffer(
unsigned int contextID)
const;
707 virtual void resizeGLObjectBuffers(
unsigned int maxSize);
void setCurrGLBufferObjectPoolSize(unsigned int size)
unsigned int _allocatedSize
unsigned int _frameNumber
virtual const osg::Array * asArray() const
void setDataSize(unsigned int size)
Set new size of the buffer object. This will reallocate the memory on the next compile.
GLBufferObject * getGLBufferObject(unsigned int contextID) const
void setProfile(const BufferObjectProfile &profile)
BufferEntry(const BufferEntry &rhs)
unsigned int & getNumberFrames()
virtual const char * className() const
This class provides an object-oriented thread mutex interface.
BufferDataList _bufferDataList
unsigned int & getNumberDeleted()
virtual void modified(BufferData *) const
unsigned int getNumOrphans() const
std::vector< BufferData * > BufferDataList
unsigned int _numActiveGLBufferObjects
Mode getMode(unsigned int contextID) const
unsigned int _numGenerated
BufferData * getBufferData(unsigned int index)
void setNumberOrphanedGLBufferObjects(unsigned int size)
unsigned int _currGLBufferObjectPoolSize
unsigned int & getCurrGLBufferObjectPoolSize()
unsigned int getNumClients() const
BufferObjectProfile _profile
const BufferData * getBufferData(unsigned int index) const
unsigned int getNumberOrphanedGLBufferObjects() const
unsigned int modifiedCount
void setGLBufferObject(unsigned int contextID, GLBufferObject *glbo)
unsigned int getNumBufferData() const
unsigned int getNumOfGLBufferObjects() const
void setProfile(GLenum target, GLenum usage, unsigned int size)
bool isPBOSupported(unsigned int contextID) const
bool operator<(const BufferObjectProfile &rhs) const
GLBufferObject * getOrCreateGLBufferObject(unsigned int contextID) const
unsigned int getCurrGLBufferObjectPoolSize() const
void setCopyDataAndReleaseGLBufferObject(bool copyAndRelease)
unsigned int & getFrameNumber()
bool _copyDataAndReleaseGLBufferObject
bool getCopyDataAndReleaseGLBufferObject() const
virtual const osg::Image * asImage() const
virtual osg::Array * asArray()
unsigned int _modifiedCount
unsigned int getNumberActiveGLBufferObjects() const
unsigned int _frameLastUsed
const BufferObjectProfile & getProfile() const
unsigned int _numOrphanedGLBufferObjects
std::vector< BufferEntry > BufferEntries
BufferObject * _bufferObject
OpenThreads::Mutex _mutex
unsigned int computeBufferAlignment(unsigned int pos, unsigned int bufferAlignment) const
GLExtensions * _extensions
osg::buffered_object< osg::ref_ptr< GLBufferObject > > GLBufferObjects
GLBufferObjectList _pendingOrphanedGLBufferObjects
bool hasSpace(unsigned int size) const
osg::ref_ptr< ModifiedCallback > _modifiedCallback
#define META_Object(library, name)
BufferObject * getBufferObject()
unsigned int & getNumberApplied()
void moveToBack(GLBufferObject *to)
unsigned int getMaxGLBufferObjectPoolSize() const
unsigned int getContextID() const
unsigned int _bufferIndex
double & getGenerateTime()
GLuint getGLObjectID() const
GLsizeiptr getOffset(unsigned int i) const
virtual const char * libraryName() const
GLBufferObject * getGLBufferObject(unsigned int contextID) const
unsigned int getBufferIndex() const
GLBufferObjectList _orphanedGLBufferObjects
osg::buffered_value< unsigned int > ModeList
GLBufferObjects _glBufferObjects
BufferObjectProfile _profile
GLBufferObject * _previous
BufferData(const BufferData &bd, const CopyOp ©op=CopyOp::SHALLOW_COPY)
void setNumberActiveGLBufferObjects(unsigned int size)
unsigned int size() const
unsigned int getDataSize() const
Get data size of the used buffer.
void setBufferIndex(unsigned int index)
BufferObjectProfile(const BufferObjectProfile &bpo)
void setModifiedCallback(ModifiedCallback *md)
bool isPBOSupported() const
unsigned int getNumPendingOrphans() const
virtual const char * libraryName() const
GLBufferObject * getOrCreateGLBufferObject(unsigned int contextID) const
void setUsage(GLenum usage)
void addClient(osg::Object *)
GLBufferObjectSetMap _glBufferObjectSetMap
unsigned int _maxGLBufferObjectPoolSize
ModifiedCallback(const ModifiedCallback &, const CopyOp &)
BufferObjectProfile & getProfile()
unsigned int & getNumberActiveGLBufferObjects()
unsigned int getContextID() const
unsigned int getModifiedCount() const
const BufferObject * getBufferObject() const
virtual const osg::PrimitiveSet * asPrimitiveSet() const
static osg::ref_ptr< GLBufferObject > createGLBufferObject(unsigned int contextID, const BufferObject *bufferObject)
BufferObjectProfile & operator=(const BufferObjectProfile &rhs)
BufferObjectProfile(GLenum target, GLenum usage, unsigned int size)
void setTarget(GLenum target)
GLBufferObjectManager * _parent
BufferEntries _bufferEntries
const ModifiedCallback * getModifiedCallback() const
virtual bool isSameKindAs(const Object *obj) const
const BufferObjectProfile & getProfile() const
ModifiedCallback * getModifiedCallback()
bool operator==(const BufferObjectProfile &rhs) const
virtual const char * className() const
osg::ref_ptr< BufferObject > _bufferObject
void setModifiedCount(unsigned int value)
void removeClient(osg::Object *)
GLBufferObjectManager * getParent()
std::list< ref_ptr< GLBufferObject > > GLBufferObjectList
virtual bool isSameKindAs(const Object *obj) const
virtual osg::PrimitiveSet * asPrimitiveSet()
virtual osg::Image * asImage()
std::map< BufferObjectProfile, osg::ref_ptr< GLBufferObjectSet > > GLBufferObjectSetMap
unsigned int _numOfGLBufferObjects
unsigned int & getNumberGenerated()
const BufferObjectProfile & getProfile() const
BufferObjectProfile _profile
unsigned int & getNumberOrphanedGLBufferObjects()
BufferObject * getBufferObject()