2 #include <msp/gl/extensions/arb_buffer_storage.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_map_buffer_range.h>
5 #include <msp/gl/extensions/khr_debug.h>
6 #include <msp/strings/format.h>
10 #include "vertexsetup.h"
17 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
18 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
20 Buffer::Buffer(BufferType t):
25 require_buffer_type(type);
27 if(ARB_direct_state_access)
28 glCreateBuffers(1, &id);
35 for(unsigned i=0; i<5; ++i)
37 unbind_from(buffer_types[i]);
38 glDeleteBuffers(1, &id);
41 void Buffer::require_buffer_type(BufferType type)
43 static Require _req_vbo(ARB_vertex_buffer_object);
44 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
45 static Require _req_pbo(ARB_pixel_buffer_object);
46 else if(type==UNIFORM_BUFFER)
47 static Require _req_ubo(ARB_uniform_buffer_object);
50 void Buffer::storage(unsigned sz)
55 throw incompatible_data("Buffer::storage");
59 throw invalid_argument("Buffer::storage");
64 void Buffer::allocate()
67 throw invalid_operation("Buffer::allocate");
71 if(ARB_buffer_storage)
73 static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
74 if(ARB_direct_state_access)
75 glNamedBufferStorage(id, size, 0, flags);
78 BindRestore _bind(this, type);
79 glBufferStorage(type, size, 0, flags);
88 void Buffer::set_usage(BufferUsage)
92 void Buffer::data(const void *d)
95 throw invalid_operation("Buffer::data");
97 if(ARB_buffer_storage)
98 return sub_data(0, size, d);
100 if(ARB_direct_state_access)
101 glNamedBufferData(id, size, d, STATIC_DRAW);
104 BindRestore _bind(this, type);
105 glBufferData(type, size, d, STATIC_DRAW);
111 void Buffer::data(unsigned sz, const void *d)
116 throw incompatible_data("Buffer::data");
121 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
124 throw invalid_operation("Buffer::sub_data");
128 if(ARB_direct_state_access)
129 glNamedBufferSubData(id, off, sz, d);
132 BindRestore _bind(this, type);
133 glBufferSubData(type, off, sz, d);
137 void Buffer::require_size(unsigned req_sz) const
140 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
143 BufferRange *Buffer::create_range(unsigned s, unsigned o)
145 return new BufferRange(*this, s, o);
151 if(ARB_map_buffer_range)
153 if(ARB_direct_state_access)
154 return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
157 BindRestore _bind(this, type);
158 return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
161 else if(ARB_direct_state_access)
162 return glMapNamedBuffer(id, GL_READ_WRITE);
163 else if(OES_mapbuffer)
165 BindRestore _bind(this, type);
166 return glMapBuffer(type, GL_READ_WRITE);
169 throw invalid_operation("Buffer::map");
174 // TODO check if it's mapped
175 if(ARB_direct_state_access)
176 return glUnmapNamedBuffer(id);
177 else if(OES_mapbuffer)
179 BindRestore _bind(this, type);
180 return glUnmapBuffer(type);
186 void Buffer::bind_to(BufferType t) const
189 require_buffer_type(t);
190 if(t==ELEMENT_ARRAY_BUFFER)
191 if(const VertexSetup *vs = VertexSetup::current())
193 // Don't change the binding in a vertex array object
194 if(this==vs->get_index_buffer())
196 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
198 if(set_current(t, this))
202 const Buffer *Buffer::current(BufferType t)
204 if(t==ELEMENT_ARRAY_BUFFER)
205 if(const VertexSetup *vs = VertexSetup::current())
206 return vs->get_index_buffer();
210 void Buffer::unbind_from(BufferType type)
212 if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
213 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
214 if(set_current(type, 0))
215 glBindBuffer(type, 0);
218 const Buffer *&Buffer::binding(BufferType type)
222 case ARRAY_BUFFER: return bound[0];
223 case ELEMENT_ARRAY_BUFFER: return bound[1];
224 case PIXEL_PACK_BUFFER: return bound[2];
225 case PIXEL_UNPACK_BUFFER: return bound[3];
226 case UNIFORM_BUFFER: return bound[4];
227 default: throw invalid_argument("Buffer::binding");
231 bool Buffer::set_current(BufferType type, const Buffer *buf)
233 const Buffer *&ptr = binding(type);
241 void Buffer::set_debug_name(const string &name)
245 glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
252 vector<const BufferRange *> BufferRange::bound_uniform;
254 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
259 if(o>buffer.get_size() || o+s>buffer.get_size())
260 throw out_of_range("BufferRange::BufferRange");
263 BufferRange::~BufferRange()
265 for(unsigned i=0; i<bound_uniform.size(); ++i)
266 if(bound_uniform[i]==this)
267 unbind_from(UNIFORM_BUFFER, i);
270 void BufferRange::data(const void *d)
272 buffer.sub_data(offset, size, d);
275 void BufferRange::bind_to(BufferType t, unsigned i)
278 Buffer::require_buffer_type(t);
279 if(set_current(t, i, this))
281 // The buffer gets bound as a side effect
282 Buffer::set_current(t, &buffer);
283 glBindBufferRange(t, i, buffer.id, offset, size);
287 void BufferRange::unbind_from(BufferType t, unsigned i)
289 if(set_current(t, i, 0))
291 Buffer::set_current(t, 0);
292 glBindBufferBase(t, i, 0);
296 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
298 if(type==UNIFORM_BUFFER)
300 if(index>=get_n_uniform_buffer_bindings())
301 throw out_of_range("BufferRange::binding");
302 if(bound_uniform.size()<=index)
303 bound_uniform.resize(index+1);
304 return bound_uniform[index];
307 throw invalid_argument("BufferRange::binding");
310 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
312 const BufferRange *&ptr = binding(type, index);
320 unsigned BufferRange::get_n_uniform_buffer_bindings()
322 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
326 unsigned BufferRange::get_uniform_buffer_alignment()
328 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);