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_DSA_BUFFER_MAP_1309301821_HPP
14 #define OGLPLUS_DSA_BUFFER_MAP_1309301821_HPP
15 
16 #include <oglplus/buffer.hpp>
17 
18 namespace oglplus {
19 
20 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_5 || GL_ARB_direct_state_access
21 
22 class DSABufferRawMap
23 {
24 private:
25  const GLintptr _offset;
26  GLsizeiptr _size;
27  GLvoid* _ptr;
28  const GLuint _name;
29 
30  static GLsizeiptr _get_size(GLuint name)
31  {
32  GLint value = 0;
33  OGLPLUS_GLFUNC(GetNamedBufferParameteriv)(
34  name,
35  GL_BUFFER_SIZE,
36  &value
37  );
38  OGLPLUS_CHECK(
39  GetNamedBufferParameteriv,
40  ObjectError,
41  Object(BufferName(name))
42  );
43  return GLsizeiptr(value);
44  }
45 
46  static GLenum _translate(GLbitfield access)
47  {
48  switch(access)
49  {
50  case GL_MAP_READ_BIT:
51  return GL_READ_ONLY;
52  case GL_MAP_WRITE_BIT:
53  return GL_WRITE_ONLY;
54  case GL_MAP_READ_BIT|GL_MAP_WRITE_BIT:
55  return GL_READ_WRITE;
56  }
57  return GL_READ_ONLY;
58  }
59 public:
61 
69  DSABufferRawMap(
70  BufferName buffer,
71  BufferSize offset,
72  BufferSize size,
73  Bitfield<BufferMapAccess> access
74  ): _offset(GLintptr(offset.Get()))
75  , _size(GLsizeiptr(size.Get()))
76  , _ptr(
77  OGLPLUS_GLFUNC(MapNamedBufferRange)(
78  GetGLName(buffer),
79  _offset,
80  _size,
81  GLbitfield(access)
82  )
83  ), _name(GetGLName(buffer))
84  {
85  OGLPLUS_CHECK(
86  MapNamedBufferRange,
87  ObjectError,
88  Object(buffer)
89  );
90  }
91 
93 
101  DSABufferRawMap(
102  BufferName buffer,
103  Bitfield<BufferMapAccess> access
104  ): _offset(0)
105  , _size(_get_size(GetGLName(buffer)))
106  , _ptr(
107  OGLPLUS_GLFUNC(MapNamedBuffer)(
108  GetGLName(buffer),
109  _translate(GLbitfield(access))
110  )
111  ), _name(GetGLName(buffer))
112  {
113  OGLPLUS_CHECK(
114  MapNamedBuffer,
115  ObjectError,
116  Object(buffer)
117  );
118  }
119 
120 #if !OGLPLUS_NO_DELETED_FUNCTIONS
121  DSABufferRawMap(const DSABufferRawMap&) = delete;
122 #else
123 private:
124  DSABufferRawMap(const DSABufferRawMap&);
125 public:
126 #endif
127 
129  DSABufferRawMap(DSABufferRawMap&& temp)
130  : _offset(temp._offset)
131  , _size(temp._size)
132  , _ptr(temp._ptr)
133  , _name(temp._name)
134  {
135  temp._ptr = nullptr;
136  }
137 
138  ~DSABufferRawMap(void)
139  {
140  try { Unmap(); }
141  catch(...) { }
142  }
143 
145 
151  void Unmap(void)
152  {
153  if(_ptr != nullptr)
154  {
155  OGLPLUS_GLFUNC(UnmapNamedBuffer)(_name);
156  OGLPLUS_IGNORE(UnmapNamedBuffer);
157  _ptr = nullptr;
158  }
159  }
160 
162  bool Mapped(void) const
163  {
164  return _ptr != nullptr;
165  }
166 
168  GLsizeiptr Size(void) const
169  {
170  return _size;
171  }
172 
174 
177  const GLvoid* RawData(void) const
178  {
179  assert(Mapped());
180  return _ptr;
181  }
182 
184 
187  GLvoid* RawData(void)
188  {
189  assert(Mapped());
190  return _ptr;
191  }
192 
194 
202  void FlushRange(BufferSize offset, BufferSize length)
203  {
204  OGLPLUS_GLFUNC(FlushMappedNamedBufferRange)(
205  _name,
206  GLintptr(offset.Get()),
207  GLsizeiptr(length.Get())
208  );
209  OGLPLUS_CHECK(
210  FlushMappedNamedBufferRange,
211  ObjectError,
212  Object(BufferName(_name))
213  );
214  }
215 };
216 
218 template <typename Type>
220  : public DSABufferRawMap
221 {
222 public:
224 
233  BufferName buffer,
234  BufferTypedSize<Type> offset,
235  BufferTypedSize<Type> size,
237  ): DSABufferRawMap(buffer, offset, size, access)
238  { }
239 
241 
250  BufferName buffer,
252  ): DSABufferRawMap(buffer, access)
253  { }
254 
257  : DSABufferRawMap(static_cast<DSABufferRawMap&&>(temp))
258  { }
259 
261  GLsizeiptr Count(void) const
262  {
263  assert(this->Size() % sizeof(Type) == 0);
264  return this->Size() / sizeof(Type);
265  }
266 
268  const Type* Data(void) const
269  {
270  return static_cast<const Type*>(this->RawData());
271  }
272 
274  Type* Data(void)
275  {
276  return static_cast<Type*>(this->RawData());
277  }
278 
280  const Type& At(GLuint index) const
281  {
282  assert(Data() != nullptr);
283  assert(((index+1)*sizeof(Type)) <= std::size_t(this->Size()));
284  return Data()[index];
285  }
286 
288  Type& At(GLuint index)
289  {
290  assert(Data() != nullptr);
291  assert(((index+1)*sizeof(Type)) <= std::size_t(this->Size()));
292  return Data()[index];
293  }
294 };
295 
296 #endif // GL_ARB_direct_state_access
297 
298 } // namespace oglplus
299 
300 #endif // include guard
DSABufferTypedMap(BufferName buffer, BufferTypedSize< Type > offset, BufferTypedSize< Type > size, Bitfield< BufferMapAccess > access)
Maps a range of the buffer.
Definition: buffer_map.hpp:232
DSABufferTypedMap(BufferName buffer, Bitfield< BufferMapAccess > access)
Maps the whole buffer.
Definition: buffer_map.hpp:249
Untyped mapping of the buffer to the client address space.
Definition: buffer_map.hpp:219
GLsizeiptr Count(void) const
Returns the count of elements of Type in the mapped buffer.
Definition: buffer_map.hpp:261
This template serves as a wrapper for OpenGL bitfields.
Definition: bitfield.hpp:56
const Type & At(GLuint index) const
Returns a const reference to the element at the specified index.
Definition: buffer_map.hpp:280
GLuint GetGLName(ObjectName< ObjTag > named)
Returns the GLuint OpenGL name assigned to named object.
Definition: name.hpp:38
DSABufferTypedMap(DSABufferTypedMap &&temp)
Move construction is enabled.
Definition: buffer_map.hpp:256
const Type * Data(void) const
Returns a const pointer to the mapped data.
Definition: buffer_map.hpp:268
Type * Data(void)
Returns a pointer to the mapped data.
Definition: buffer_map.hpp:274
Type & At(GLuint index)
Returns a reference to the element at the specified index.
Definition: buffer_map.hpp:288
Buffer wrappers.
A common template for "named" objects like textures, buffers, etc.
Definition: fwd.hpp:136

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).