Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
BufferObject.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef BufferObject_INCLUDE_ONCE
33 #define BufferObject_INCLUDE_ONCE
34 
35 #include <vlCore/Vector2.hpp>
36 #include <vlCore/Vector3.hpp>
37 #include <vlCore/Vector4.hpp>
38 #include <vlCore/Buffer.hpp>
39 #include <vlGraphics/OpenGL.hpp>
40 #include <vlCore/vlnamespace.hpp>
41 #include <vlCore/Vector4.hpp>
42 #include <vlCore/Sphere.hpp>
43 #include <vlCore/AABB.hpp>
44 
45 namespace vl
46 {
47 //-----------------------------------------------------------------------------
48 // BufferObject
49 //-----------------------------------------------------------------------------
55  class BufferObject: public Buffer
56  {
58 
59  public:
61  {
62  VL_DEBUG_SET_OBJECT_NAME()
63  mHandle = 0;
66  }
67 
68  BufferObject(const BufferObject& other): Buffer(other)
69  {
70  VL_DEBUG_SET_OBJECT_NAME()
71  mHandle = 0;
74  // copy local data
75  *this = other;
76  }
77 
78  // deletes the BufferObject data and copyes only the local data
80  {
82  super::operator=(other);
83  return *this;
84  }
85 
86  // swaps data with another BufferObject, including BufferObject handle, usage and local data.
87  void swap(BufferObject& other)
88  {
89  // swap local data
90  Buffer::swap(other);
91  // tmp
92  unsigned int tmp_handle = mHandle;
93  EBufferObjectUsage tmp_usage = mUsage;
94  GLsizeiptr tmp_bytes = mByteCountBufferObject;
95  // this <- other
96  mHandle = other.mHandle;
97  mUsage = tmp_usage;
99  // other <- this
100  other.mHandle = tmp_handle;
101  other.mUsage = tmp_usage;
102  other.mByteCountBufferObject = tmp_bytes;
103  }
104 
106  {
108  }
109 
110  void setHandle(unsigned int handle) { mHandle = handle; }
111 
112  unsigned int handle() const { return mHandle; }
113 
114  GLsizeiptr byteCountBufferObject() const { return mByteCountBufferObject; }
115 
117  {
118  VL_CHECK_OGL();
120  if (Has_BufferObject && handle() == 0)
121  {
123  VL_glGenBuffers( 1, &mHandle ); VL_CHECK_OGL();
125  VL_CHECK(handle())
126  }
127  }
128 
130  {
131  // mic fixme: it would be nice to re-enable these
132  // VL_CHECK_OGL();
133  VL_CHECK(Has_BufferObject || handle() == 0)
134  if (Has_BufferObject && handle() != 0)
135  {
136  VL_glDeleteBuffers( 1, &mHandle ); // VL_CHECK_OGL();
137  mHandle = 0;
139  }
140  }
141 
143  {
145  if ( Has_BufferObject && handle() )
146  {
148  void* vbo_ptr = mapBufferObject(BA_READ_ONLY);
149  memcpy( ptr(), vbo_ptr, byteCountBufferObject() );
151  }
152  }
153 
154  // Modifies the BufferObject using data from the local storage.
155  // @note Discarding the local storage might delete data used by other Arrays.
156  void setBufferData( EBufferObjectUsage usage, bool discard_local_storage=false )
157  {
158  setBufferData( (int)bytesUsed(), ptr(), usage );
159  mUsage = usage;
160  if (discard_local_storage)
161  clear();
162  }
163 
164  // Modifies the BufferObject using the supplied data.
165  // @note Use this function to initialize or resize the BufferObject and set it's usage flag.
166  // If data == NULL the buffer will be allocated but no data will be written to the BufferObject.
167  // If data != NULL such data will be copied into the BufferObject.
168  void setBufferData( GLsizeiptr byte_count, const GLvoid* data, EBufferObjectUsage usage )
169  {
170  VL_CHECK_OGL();
172  if ( Has_BufferObject )
173  {
175  // we use the GL_ARRAY_BUFFER slot to send the data for no special reason
176  VL_glBindBuffer( GL_ARRAY_BUFFER, handle() ); VL_CHECK_OGL();
177  VL_glBufferData( GL_ARRAY_BUFFER, byte_count, data, usage ); VL_CHECK_OGL();
178  VL_glBindBuffer( GL_ARRAY_BUFFER, 0 ); VL_CHECK_OGL();
179  mByteCountBufferObject = byte_count;
180  mUsage = usage;
181  }
182  }
183 
184  // Modifies an existing BufferObject using data from the local storage.
185  // @note You can use this function only on already initialized BufferObjects, use setBufferData() to initialize a BufferObject.
186  // @note Discarding the local storage might delete data used by other Arrays.
187  void setBufferSubData( GLintptr offset=0, GLsizeiptr byte_count=-1, bool discard_local_storage=false )
188  {
189  byte_count = byte_count < 0 ? byteCountBufferObject() : byte_count;
190  setBufferSubData( offset, byte_count, ptr() );
191  if (discard_local_storage)
192  clear();
193  }
194 
195  // Modifies an existing BufferObject using the supplied data.
196  // @note You can use this function only on already initialized BufferObjects, use setBufferData() to initialize a BufferObject.
197  // @param[in] data Must be != NULL, pointer to the data being written to the BufferObject.
198  void setBufferSubData( GLintptr offset, GLsizeiptr byte_count, const GLvoid* data )
199  {
200  VL_CHECK_OGL();
202  VL_CHECK(data);
203  VL_CHECK(handle())
204  if (Has_BufferObject && data && handle())
205  {
206  // we use the GL_ARRAY_BUFFER slot to send the data for no special reason
207  VL_glBindBuffer( GL_ARRAY_BUFFER, handle() ); VL_CHECK_OGL();
208  VL_glBufferSubData( GL_ARRAY_BUFFER, offset, byte_count, data ); VL_CHECK_OGL();
209  VL_glBindBuffer( GL_ARRAY_BUFFER, 0 ); VL_CHECK_OGL();
210  }
211  }
212 
213  // Maps a BufferObject so that it can be read or written by the CPU.
214  // @note You can map only one BufferObject at a time and you must unmap it before using the BufferObject again or mapping another one.
216  {
217  VL_CHECK_OGL();
219  if ( Has_BufferObject )
220  {
222  VL_glBindBuffer( GL_ARRAY_BUFFER, handle() ); VL_CHECK_OGL();
223  void* ptr = VL_glMapBuffer( GL_ARRAY_BUFFER, access ); VL_CHECK_OGL();
224  VL_glBindBuffer( GL_ARRAY_BUFFER, 0 ); VL_CHECK_OGL();
225  return ptr;
226  }
227  else
228  return NULL;
229  }
230 
231  // Unmaps a previously mapped BufferObject.
232  // @return Returs true or false based on what's specified in the OpenGL specs:
233  // "UnmapBuffer returns TRUE unless data values in the buffer's data store have
234  // become corrupted during the period that the buffer was mapped. Such corruption
235  // can be the result of a screen resolution change or other window system-dependent
236  // event that causes system heaps such as those for high-performance graphics memory
237  // to be discarded. GL implementations must guarantee that such corruption can
238  // occur only during the periods that a buffer's data store is mapped. If such corruption
239  // has occurred, UnmapBuffer returns FALSE, and the contents of the buffer's
240  // data store become undefined."
242  {
243  VL_CHECK_OGL();
245  if ( Has_BufferObject )
246  {
248  VL_glBindBuffer( GL_ARRAY_BUFFER, handle() ); VL_CHECK_OGL();
249  bool ok = VL_glUnmapBuffer( GL_ARRAY_BUFFER ) == GL_TRUE; VL_CHECK_OGL();
250  VL_glBindBuffer( GL_ARRAY_BUFFER, 0 ); VL_CHECK_OGL();
251  VL_CHECK_OGL();
252  return ok;
253  }
254  else
255  return false;
256  }
257 
259  EBufferObjectUsage usage() const { return mUsage; }
260 
261  protected:
262  unsigned int mHandle;
265  };
266 }
267 
268 #endif
void * mapBufferObject(EBufferObjectAccess access)
EBufferObjectUsage mUsage
BufferObject & operator=(const BufferObject &other)
void swap(Buffer &other)
Definition: Buffer.hpp:98
Implements a buffer whose storage is in local memory.
Definition: Buffer.hpp:46
void clear()
Definition: Buffer.hpp:119
void setBufferData(GLsizeiptr byte_count, const GLvoid *data, EBufferObjectUsage usage)
EBufferObjectUsage
void setBufferSubData(GLintptr offset=0, GLsizeiptr byte_count=-1, bool discard_local_storage=false)
void setBufferSubData(GLintptr offset, GLsizeiptr byte_count, const GLvoid *data)
size_t bytesUsed() const
Definition: Buffer.hpp:197
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
EBufferObjectAccess
GLsizeiptr mByteCountBufferObject
Visualization Library main namespace.
void downloadBufferObject()
bool Has_BufferObject
Definition: OpenGL.cpp:82
void swap(BufferObject &other)
EBufferObjectUsage usage() const
BufferObject usage flag as specified by setBufferData().
#define NULL
Definition: OpenGLDefs.hpp:81
The BufferObject class is a Buffer that can upload its data on the GPU memory.
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
unsigned int handle() const
unsigned char * ptr()
Definition: Buffer.hpp:201
GLsizeiptr byteCountBufferObject() const
void setBufferData(EBufferObjectUsage usage, bool discard_local_storage=false)
unsigned int mHandle
BufferObject(const BufferObject &other)
void resize(size_t byte_count, size_t alignment=0)
Definition: Buffer.hpp:130
Data is specified once and used many times as the source of drawing or image specification commands...
void setHandle(unsigned int handle)
#define VL_CHECK(expr)
Definition: checks.hpp:73
Visualization Library&#39;s enums in the &#39;vl&#39; namespace.