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/strings/format.h>
9 #include "vertexsetup.h"
16 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
17 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
19 Buffer::Buffer(BufferType t):
24 require_buffer_type(type);
26 if(ARB_direct_state_access)
27 glCreateBuffers(1, &id);
34 for(unsigned i=0; i<5; ++i)
36 unbind_from(buffer_types[i]);
37 glDeleteBuffers(1, &id);
40 void Buffer::require_buffer_type(BufferType type)
42 static Require _req_vbo(ARB_vertex_buffer_object);
43 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
44 static Require _req_pbo(ARB_pixel_buffer_object);
45 else if(type==UNIFORM_BUFFER)
46 static Require _req_ubo(ARB_uniform_buffer_object);
49 void Buffer::storage(unsigned sz)
52 throw invalid_operation("Buffer::storage");
54 throw invalid_argument("Buffer::storage");
59 void Buffer::allocate()
62 throw invalid_operation("Buffer::allocate");
66 if(ARB_buffer_storage)
68 static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
69 if(ARB_direct_state_access)
70 glNamedBufferStorage(id, size, 0, flags);
73 BindRestore _bind(this, type);
74 glBufferStorage(type, size, 0, flags);
83 void Buffer::set_usage(BufferUsage)
87 void Buffer::data(const void *d)
90 throw invalid_operation("Buffer::data");
92 if(ARB_buffer_storage)
93 return sub_data(0, size, d);
95 if(ARB_direct_state_access)
96 glNamedBufferData(id, size, d, STATIC_DRAW);
99 BindRestore _bind(this, type);
100 glBufferData(type, size, d, STATIC_DRAW);
106 void Buffer::data(unsigned sz, const void *d)
111 throw incompatible_data("Buffer::data");
116 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
119 throw invalid_operation("Buffer::sub_data");
123 if(ARB_direct_state_access)
124 glNamedBufferSubData(id, off, sz, d);
127 BindRestore _bind(this, type);
128 glBufferSubData(type, off, sz, d);
132 void Buffer::require_size(unsigned req_sz) const
135 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
138 BufferRange *Buffer::create_range(unsigned s, unsigned o)
140 return new BufferRange(*this, s, o);
145 if(ARB_map_buffer_range)
147 if(ARB_direct_state_access)
148 return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
151 BindRestore _bind(this, type);
152 return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
155 else if(ARB_direct_state_access)
156 return glMapNamedBuffer(id, GL_READ_WRITE);
157 else if(OES_mapbuffer)
159 BindRestore _bind(this, type);
160 return glMapBuffer(type, GL_READ_WRITE);
163 throw invalid_operation("Buffer::map");
168 // TODO check if it's mapped
169 if(ARB_direct_state_access)
170 return glUnmapNamedBuffer(id);
171 else if(OES_mapbuffer)
173 BindRestore _bind(this, type);
174 return glUnmapBuffer(type);
180 void Buffer::bind_to(BufferType t) const
183 require_buffer_type(t);
184 if(t==ELEMENT_ARRAY_BUFFER)
185 if(const VertexSetup *vs = VertexSetup::current())
187 // Don't change the binding in a vertex array object
188 if(this==vs->get_index_buffer())
190 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
192 if(set_current(t, this))
196 const Buffer *Buffer::current(BufferType t)
198 if(t==ELEMENT_ARRAY_BUFFER)
199 if(const VertexSetup *vs = VertexSetup::current())
200 return vs->get_index_buffer();
204 void Buffer::unbind_from(BufferType type)
206 if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
207 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
208 if(set_current(type, 0))
209 glBindBuffer(type, 0);
212 const Buffer *&Buffer::binding(BufferType type)
216 case ARRAY_BUFFER: return bound[0];
217 case ELEMENT_ARRAY_BUFFER: return bound[1];
218 case PIXEL_PACK_BUFFER: return bound[2];
219 case PIXEL_UNPACK_BUFFER: return bound[3];
220 case UNIFORM_BUFFER: return bound[4];
221 default: throw invalid_argument("Buffer::binding");
225 bool Buffer::set_current(BufferType type, const Buffer *buf)
227 const Buffer *&ptr = binding(type);
236 vector<const BufferRange *> BufferRange::bound_uniform;
238 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
243 if(o>buffer.get_size() || o+s>buffer.get_size())
244 throw out_of_range("BufferRange::BufferRange");
247 BufferRange::~BufferRange()
249 for(unsigned i=0; i<bound_uniform.size(); ++i)
250 if(bound_uniform[i]==this)
251 unbind_from(UNIFORM_BUFFER, i);
254 void BufferRange::data(const void *d)
256 buffer.sub_data(offset, size, d);
259 void BufferRange::bind_to(BufferType t, unsigned i)
262 Buffer::require_buffer_type(t);
263 if(set_current(t, i, this))
265 // The buffer gets bound as a side effect
266 Buffer::set_current(t, &buffer);
267 glBindBufferRange(t, i, buffer.id, offset, size);
271 void BufferRange::unbind_from(BufferType t, unsigned i)
273 if(set_current(t, i, 0))
275 Buffer::set_current(t, 0);
276 glBindBufferBase(t, i, 0);
280 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
282 if(type==UNIFORM_BUFFER)
284 if(index>=get_n_uniform_buffer_bindings())
285 throw out_of_range("BufferRange::binding");
286 if(bound_uniform.size()<=index)
287 bound_uniform.resize(index+1);
288 return bound_uniform[index];
291 throw invalid_argument("BufferRange::binding");
294 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
296 const BufferRange *&ptr = binding(type, index);
304 unsigned BufferRange::get_n_uniform_buffer_bindings()
306 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
310 unsigned BufferRange::get_uniform_buffer_alignment()
312 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);