OGLplus (0.52.0) a C++ wrapper for OpenGL

buffer_map.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_BUFFER_MAP_1107121519_HPP
14 #define OGLPLUS_BUFFER_MAP_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
17 #include <oglplus/error/object.hpp>
20 #include <oglplus/buffer_size.hpp>
21 
22 namespace oglplus {
23 
24 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
25 class BufferRawMap
27 {
28 private:
29  const GLintptr _offset;
30  GLsizeiptr _size;
31  GLvoid* _ptr;
32  const BufferTarget _target;
33 
34  static GLsizeiptr _get_size(BufferTarget target)
35  {
36  GLint value = 0;
37  OGLPLUS_GLFUNC(GetBufferParameteriv)(
38  GLenum(target),
39  GL_BUFFER_SIZE,
40  &value
41  );
42  OGLPLUS_CHECK(
43  GetBufferParameteriv,
44  ObjectError,
45  ObjectBinding(target)
46  );
47  return GLsizeiptr(value);
48  }
49 
50  static GLenum _translate(GLbitfield access)
51  {
52  switch(access)
53  {
54  case GL_MAP_READ_BIT:
55  return GL_READ_ONLY;
56  case GL_MAP_WRITE_BIT:
57  return GL_WRITE_ONLY;
58  case GL_MAP_READ_BIT|GL_MAP_WRITE_BIT:
59  return GL_READ_WRITE;
60  }
61  return GL_READ_ONLY;
62  }
63 public:
65 
77  BufferTarget target,
78  BufferSize byte_offset,
79  BufferSize size_bytes,
81  ): _offset(GLintptr(byte_offset.Get()))
82  , _size(GLsizeiptr(size_bytes.Get()))
83  , _ptr(
84  OGLPLUS_GLFUNC(MapBufferRange)(
85  GLenum(target),
86  _offset,
87  _size,
88  GLbitfield(access)
89  )
90  ), _target(target)
91  {
92  OGLPLUS_CHECK(
93  MapBufferRange,
94  Error,
95  EnumParam(target)
96  );
97  }
98 
100 
112  : _offset(0)
113  , _size(_get_size(target))
114  , _ptr(
115  OGLPLUS_GLFUNC(MapBuffer)(
116  GLenum(target),
117  _translate(GLbitfield(access))
118  )
119  ), _target(target)
120  {
121  OGLPLUS_CHECK(
122  MapBuffer,
123  ObjectError,
124  ObjectBinding(_target)
125  );
126  }
127 
128 #if !OGLPLUS_NO_DELETED_FUNCTIONS
129  BufferRawMap(const BufferRawMap&) = delete;
130 #else
131 private:
132  BufferRawMap(const BufferRawMap&);
133 public:
134 #endif
135  BufferRawMap(BufferRawMap&& temp)
137  : _offset(temp._offset)
138  , _size(temp._size)
139  , _ptr(temp._ptr)
140  , _target(temp._target)
141  {
142  temp._ptr = nullptr;
143  }
144 
147  {
148  try { Unmap(); }
149  catch(...){ }
150  }
151 
153 
159  void Unmap(void)
160  {
161  if(_ptr != nullptr)
162  {
163  OGLPLUS_GLFUNC(UnmapBuffer)(GLenum(_target));
164  OGLPLUS_VERIFY(
165  UnmapBuffer,
166  ObjectError,
167  ObjectBinding(_target)
168  );
169  _ptr = nullptr;
170  }
171  }
172 
174  bool Mapped(void) const
175  {
176  return _ptr != nullptr;
177  }
178 
180  GLsizeiptr Size(void) const
181  {
182  return _size;
183  }
184 
186 
189  const GLvoid* RawData(void) const
190  {
191  assert(Mapped());
192  return _ptr;
193  }
194 
196 
199  GLvoid* RawData(void)
200  {
201  assert(Mapped());
202  return _ptr;
203  }
204 
206 
214  void FlushRange(BufferSize offset, BufferSize length)
215  {
216  OGLPLUS_GLFUNC(FlushMappedBufferRange)(
217  GLenum(_target),
218  GLintptr(offset.Get()),
219  GLsizeiptr(length.Get())
220  );
221  OGLPLUS_CHECK(
222  FlushMappedBufferRange,
223  ObjectError,
224  ObjectBinding(_target)
225  );
226  }
227 };
228 
230 template <typename Type>
232  : public BufferRawMap
233 {
234 public:
236 
245  BufferTarget target,
246  BufferTypedSize<Type> offset,
247  BufferTypedSize<Type> size,
249  ): BufferRawMap(target, offset, size, access)
250  { }
251 
253 
262  : BufferRawMap(target, access)
263  { }
264 
267  : BufferRawMap(static_cast<BufferRawMap&&>(temp))
268  { }
269 
271  GLsizeiptr Count(void) const
272  {
273  assert(this->Size() % sizeof(Type) == 0);
274  return this->Size() / sizeof(Type);
275  }
276 
278  const Type* Data(void) const
279  {
280  return static_cast<const Type*>(this->RawData());
281  }
282 
284  Type* Data(void)
285  {
286  return static_cast<Type*>(this->RawData());
287  }
288 
290  const Type& At(GLuint index) const
291  {
292  assert(Data() != nullptr);
293  assert(((index+1)*sizeof(Type)) <= std::size_t(this->Size()));
294  return Data()[index];
295  }
296 
298  Type& At(GLuint index)
299  {
300  assert(Data() != nullptr);
301  assert(((index+1)*sizeof(Type)) <= std::size_t(this->Size()));
302  return Data()[index];
303  }
304 
306 
318  BufferTypedSize<Type> start,
319  BufferTypedSize<Type> count
320  )
321  {
322  this->FlushRange(start, count);
323  }
324 };
325 #endif // GL_VERSION_3_0
326 
327 } // namespace oglplus
328 
329 #endif // include guard
BufferTypedMap(BufferTarget target, BufferTypedSize< Type > offset, BufferTypedSize< Type > size, Bitfield< BufferMapAccess > access)
Maps a range of the buffer.
Definition: buffer_map.hpp:244
BufferTypedMap(BufferTarget target, Bitfield< BufferMapAccess > access)
Maps the whole buffer.
Definition: buffer_map.hpp:261
Type * Data(void)
Returns a pointer to the mapped data.
Definition: buffer_map.hpp:284
bool Mapped(void) const
Returns true if the buffer is mapped.
Definition: buffer_map.hpp:174
GLsizeiptr Count(void) const
Returns the count of elements of Type in the mapped buffer.
Definition: buffer_map.hpp:271
Buffer bind target enumerations.
This class represents the size of a GPU buffer in bytes.
Definition: buffer_size.hpp:22
Typed mapping of the buffer to the client address space.
Definition: buffer_map.hpp:231
Declaration of OGLplus object-related error.
const Type & At(GLuint index) const
Returns a const reference to the element at the specified index.
Definition: buffer_map.hpp:290
Buffer map access enumeration/bitfield.
GLsizeiptr Size(void) const
Returns the size (in bytes) of the mapped buffer.
Definition: buffer_map.hpp:180
This template serves as a wrapper for OpenGL bitfields.
Definition: bitfield.hpp:56
BufferTypedMap(BufferTypedMap &&temp)
Move construction is enabled.
Definition: buffer_map.hpp:266
Object representing Buffer's storage size in bytes.
BufferRawMap(BufferTarget target, Bitfield< BufferMapAccess > access)
Maps the whole buffer.
Definition: buffer_map.hpp:111
BufferRawMap(BufferTarget target, BufferSize byte_offset, BufferSize size_bytes, Bitfield< BufferMapAccess > access)
Maps a range of the buffer.
Definition: buffer_map.hpp:76
const Type * Data(void) const
Returns a const pointer to the mapped data.
Definition: buffer_map.hpp:278
GLvoid * RawData(void)
Returns a pointer to the mapped data.
Definition: buffer_map.hpp:199
void Unmap(void)
Unmaps the buffer from client address space.
Definition: buffer_map.hpp:159
Helper macro for optional checking of availability of GL function.
~BufferRawMap(void)
Unmaps the buffer from client address space (if mapped)
Definition: buffer_map.hpp:146
Type & At(GLuint index)
Returns a reference to the element at the specified index.
Definition: buffer_map.hpp:298
GLsizeiptr Get(void) const
Gets the size in bytes.
Definition: buffer_size.hpp:58
Exception class for general OpenGL errors.
Definition: basic.hpp:43
Exception class for GL object-related errors.
Definition: object.hpp:24
void FlushElements(BufferTypedSize< Type > start, BufferTypedSize< Type > count)
Indicate modifications to a mapped range of elements of Type.
Definition: buffer_map.hpp:317
void FlushRange(BufferSize offset, BufferSize length)
Indicate modifications to a mapped range.
Definition: buffer_map.hpp:214
Untyped mapping of the buffer to the client address space.
Definition: buffer_map.hpp:26
const GLvoid * RawData(void) const
Returns a const pointer to the mapped data.
Definition: buffer_map.hpp:189
BufferTarget
Buffer bind target.
Definition: buffer_target.hpp:24

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).