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>
8 #include "deviceinfo.h"
11 #include "vertexsetup.h"
18 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
19 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
21 Buffer::Buffer(BufferType t):
26 require_buffer_type(type);
28 if(ARB_direct_state_access)
29 glCreateBuffers(1, &id);
36 for(unsigned i=0; i<5; ++i)
38 unbind_from(buffer_types[i]);
39 glDeleteBuffers(1, &id);
42 void Buffer::require_buffer_type(BufferType type)
44 static Require _req_vbo(ARB_vertex_buffer_object);
45 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
46 static Require _req_pbo(ARB_pixel_buffer_object);
47 else if(type==UNIFORM_BUFFER)
48 static Require _req_ubo(ARB_uniform_buffer_object);
51 void Buffer::storage(unsigned sz)
56 throw incompatible_data("Buffer::storage");
60 throw invalid_argument("Buffer::storage");
65 void Buffer::allocate()
68 throw invalid_operation("Buffer::allocate");
72 if(ARB_buffer_storage)
74 static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
75 if(ARB_direct_state_access)
76 glNamedBufferStorage(id, size, 0, flags);
79 BindRestore _bind(this, type);
80 glBufferStorage(type, size, 0, flags);
89 void Buffer::set_usage(BufferUsage)
93 void Buffer::data(const void *d)
96 throw invalid_operation("Buffer::data");
98 if(ARB_buffer_storage)
99 return sub_data(0, size, d);
101 if(ARB_direct_state_access)
102 glNamedBufferData(id, size, d, STATIC_DRAW);
105 BindRestore _bind(this, type);
106 glBufferData(type, size, d, STATIC_DRAW);
112 void Buffer::data(unsigned sz, const void *d)
117 throw incompatible_data("Buffer::data");
122 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
125 throw invalid_operation("Buffer::sub_data");
129 if(ARB_direct_state_access)
130 glNamedBufferSubData(id, off, sz, d);
133 BindRestore _bind(this, type);
134 glBufferSubData(type, off, sz, d);
138 void Buffer::require_size(unsigned req_sz) const
141 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
144 BufferRange *Buffer::create_range(unsigned s, unsigned o)
146 return new BufferRange(*this, s, o);
152 if(ARB_map_buffer_range)
154 if(ARB_direct_state_access)
155 return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
158 BindRestore _bind(this, type);
159 return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
162 else if(ARB_direct_state_access)
163 return glMapNamedBuffer(id, GL_READ_WRITE);
164 else if(OES_mapbuffer)
166 BindRestore _bind(this, type);
167 return glMapBuffer(type, GL_READ_WRITE);
170 throw invalid_operation("Buffer::map");
175 // TODO check if it's mapped
176 if(ARB_direct_state_access)
177 return glUnmapNamedBuffer(id);
178 else if(OES_mapbuffer)
180 BindRestore _bind(this, type);
181 return glUnmapBuffer(type);
187 void Buffer::bind_to(BufferType t) const
190 require_buffer_type(t);
191 if(t==ELEMENT_ARRAY_BUFFER)
192 if(const VertexSetup *vs = VertexSetup::current())
194 // Don't change the binding in a vertex array object
195 if(this==vs->get_index_buffer())
197 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
199 if(set_current(t, this))
203 const Buffer *Buffer::current(BufferType t)
205 if(t==ELEMENT_ARRAY_BUFFER)
206 if(const VertexSetup *vs = VertexSetup::current())
207 return vs->get_index_buffer();
211 void Buffer::unbind_from(BufferType type)
213 if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
214 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
215 if(set_current(type, 0))
216 glBindBuffer(type, 0);
219 const Buffer *&Buffer::binding(BufferType type)
223 case ARRAY_BUFFER: return bound[0];
224 case ELEMENT_ARRAY_BUFFER: return bound[1];
225 case PIXEL_PACK_BUFFER: return bound[2];
226 case PIXEL_UNPACK_BUFFER: return bound[3];
227 case UNIFORM_BUFFER: return bound[4];
228 default: throw invalid_argument("Buffer::binding");
232 bool Buffer::set_current(BufferType type, const Buffer *buf)
234 const Buffer *&ptr = binding(type);
242 void Buffer::set_debug_name(const string &name)
246 glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
253 vector<const BufferRange *> BufferRange::bound_uniform;
255 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
260 if(o>buffer.get_size() || o+s>buffer.get_size())
261 throw out_of_range("BufferRange::BufferRange");
264 BufferRange::~BufferRange()
266 for(unsigned i=0; i<bound_uniform.size(); ++i)
267 if(bound_uniform[i]==this)
268 unbind_from(UNIFORM_BUFFER, i);
271 void BufferRange::data(const void *d)
273 buffer.sub_data(offset, size, d);
276 void BufferRange::bind_to(BufferType t, unsigned i)
279 Buffer::require_buffer_type(t);
280 if(set_current(t, i, this))
282 // The buffer gets bound as a side effect
283 Buffer::set_current(t, &buffer);
284 glBindBufferRange(t, i, buffer.id, offset, size);
288 void BufferRange::unbind_from(BufferType t, unsigned i)
290 if(set_current(t, i, 0))
292 Buffer::set_current(t, 0);
293 glBindBufferBase(t, i, 0);
297 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
299 if(type==UNIFORM_BUFFER)
301 if(index>=Limits::get_global().max_uniform_bindings)
302 throw out_of_range("BufferRange::binding");
303 if(bound_uniform.size()<=index)
304 bound_uniform.resize(index+1);
305 return bound_uniform[index];
308 throw invalid_argument("BufferRange::binding");
311 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
313 const BufferRange *&ptr = binding(type, index);
321 unsigned BufferRange::get_n_uniform_buffer_bindings()
323 return Limits::get_global().max_uniform_bindings;
326 unsigned BufferRange::get_uniform_buffer_alignment()
328 return Limits::get_global().uniform_buffer_alignment;