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);
151 static Require _req(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);
165 // TODO check if it's mapped
166 if(ARB_direct_state_access)
167 return glUnmapNamedBuffer(id);
168 else if(OES_mapbuffer)
170 BindRestore _bind(this, type);
171 return glUnmapBuffer(type);
177 void Buffer::bind_to(BufferType t) const
180 require_buffer_type(t);
181 if(t==ELEMENT_ARRAY_BUFFER)
182 if(const VertexSetup *vs = VertexSetup::current())
184 // Don't change the binding in a vertex array object
185 if(this==vs->get_index_buffer())
187 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
189 if(set_current(t, this))
193 const Buffer *Buffer::current(BufferType t)
195 if(t==ELEMENT_ARRAY_BUFFER)
196 if(const VertexSetup *vs = VertexSetup::current())
197 return vs->get_index_buffer();
201 void Buffer::unbind_from(BufferType type)
203 if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
204 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
205 if(set_current(type, 0))
206 glBindBuffer(type, 0);
209 const Buffer *&Buffer::binding(BufferType type)
213 case ARRAY_BUFFER: return bound[0];
214 case ELEMENT_ARRAY_BUFFER: return bound[1];
215 case PIXEL_PACK_BUFFER: return bound[2];
216 case PIXEL_UNPACK_BUFFER: return bound[3];
217 case UNIFORM_BUFFER: return bound[4];
218 default: throw invalid_argument("Buffer::binding");
222 bool Buffer::set_current(BufferType type, const Buffer *buf)
224 const Buffer *&ptr = binding(type);
232 void Buffer::set_debug_name(const string &name)
236 glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
243 vector<const BufferRange *> BufferRange::bound_uniform;
245 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
250 if(o>buffer.get_size() || o+s>buffer.get_size())
251 throw out_of_range("BufferRange::BufferRange");
254 BufferRange::~BufferRange()
256 for(unsigned i=0; i<bound_uniform.size(); ++i)
257 if(bound_uniform[i]==this)
258 unbind_from(UNIFORM_BUFFER, i);
261 void BufferRange::data(const void *d)
263 buffer.sub_data(offset, size, d);
266 void BufferRange::bind_to(BufferType t, unsigned i)
269 Buffer::require_buffer_type(t);
270 if(set_current(t, i, this))
272 // The buffer gets bound as a side effect
273 Buffer::set_current(t, &buffer);
274 glBindBufferRange(t, i, buffer.id, offset, size);
278 void BufferRange::unbind_from(BufferType t, unsigned i)
280 if(set_current(t, i, 0))
282 Buffer::set_current(t, 0);
283 glBindBufferBase(t, i, 0);
287 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
289 if(type==UNIFORM_BUFFER)
291 if(index>=Limits::get_global().max_uniform_bindings)
292 throw out_of_range("BufferRange::binding");
293 if(bound_uniform.size()<=index)
294 bound_uniform.resize(index+1);
295 return bound_uniform[index];
298 throw invalid_argument("BufferRange::binding");
301 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
303 const BufferRange *&ptr = binding(type, index);
311 unsigned BufferRange::get_n_uniform_buffer_bindings()
313 return Limits::get_global().max_uniform_bindings;
316 unsigned BufferRange::get_uniform_buffer_alignment()
318 return Limits::get_global().uniform_buffer_alignment;