2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_map_buffer_range.h>
4 #include <msp/strings/format.h>
8 #include "vertexsetup.h"
15 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
16 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
18 Buffer::Buffer(BufferType t):
23 require_buffer_type(type);
25 if(ARB_direct_state_access)
26 glCreateBuffers(1, &id);
33 for(unsigned i=0; i<5; ++i)
35 unbind_from(buffer_types[i]);
36 glDeleteBuffers(1, &id);
39 void Buffer::require_buffer_type(BufferType type)
41 static Require _req_vbo(ARB_vertex_buffer_object);
42 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
43 static Require _req_pbo(ARB_pixel_buffer_object);
44 else if(type==UNIFORM_BUFFER)
45 static Require _req_ubo(ARB_uniform_buffer_object);
48 void Buffer::set_usage(BufferUsage u)
50 // TODO OpenGL ES 2.0 doesn't support read or copy usages
54 void Buffer::data(unsigned sz, const void *d)
56 if(ARB_direct_state_access)
57 glNamedBufferData(id, sz, d, usage);
60 BindRestore _bind(this, type);
61 glBufferData(type, sz, d, usage);
66 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
68 if(ARB_direct_state_access)
69 glNamedBufferSubData(id, off, sz, d);
72 BindRestore _bind(this, type);
73 glBufferSubData(type, off, sz, d);
77 void Buffer::require_size(unsigned req_sz) const
80 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
83 BufferRange *Buffer::create_range(unsigned s, unsigned o)
85 return new BufferRange(*this, s, o);
90 if(ARB_map_buffer_range)
92 if(ARB_direct_state_access)
93 return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
96 BindRestore _bind(this, type);
97 return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
100 else if(ARB_direct_state_access)
101 return glMapNamedBuffer(id, GL_READ_WRITE);
102 else if(OES_mapbuffer)
104 BindRestore _bind(this, type);
105 return glMapBuffer(type, GL_READ_WRITE);
108 throw invalid_operation("Buffer::map");
113 // TODO check if it's mapped
114 if(ARB_direct_state_access)
115 return glUnmapNamedBuffer(id);
116 else if(OES_mapbuffer)
118 BindRestore _bind(this, type);
119 return glUnmapBuffer(type);
125 void Buffer::bind_to(BufferType t) const
128 require_buffer_type(t);
129 if(t==ELEMENT_ARRAY_BUFFER)
130 if(const VertexSetup *vs = VertexSetup::current())
132 // Don't change the binding in a vertex array object
133 if(this==vs->get_index_buffer())
135 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
137 if(set_current(t, this))
141 const Buffer *Buffer::current(BufferType t)
143 if(t==ELEMENT_ARRAY_BUFFER)
144 if(const VertexSetup *vs = VertexSetup::current())
145 return vs->get_index_buffer();
149 void Buffer::unbind_from(BufferType type)
151 if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
152 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
153 if(set_current(type, 0))
154 glBindBuffer(type, 0);
157 const Buffer *&Buffer::binding(BufferType type)
161 case ARRAY_BUFFER: return bound[0];
162 case ELEMENT_ARRAY_BUFFER: return bound[1];
163 case PIXEL_PACK_BUFFER: return bound[2];
164 case PIXEL_UNPACK_BUFFER: return bound[3];
165 case UNIFORM_BUFFER: return bound[4];
166 default: throw invalid_argument("Buffer::binding");
170 bool Buffer::set_current(BufferType type, const Buffer *buf)
172 const Buffer *&ptr = binding(type);
181 vector<const BufferRange *> BufferRange::bound_uniform;
183 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
188 if(o>buffer.get_size() || o+s>buffer.get_size())
189 throw out_of_range("BufferRange::BufferRange");
192 BufferRange::~BufferRange()
194 for(unsigned i=0; i<bound_uniform.size(); ++i)
195 if(bound_uniform[i]==this)
196 unbind_from(UNIFORM_BUFFER, i);
199 void BufferRange::data(const void *d)
201 buffer.sub_data(offset, size, d);
204 void BufferRange::bind_to(BufferType t, unsigned i)
207 Buffer::require_buffer_type(t);
208 if(set_current(t, i, this))
210 // The buffer gets bound as a side effect
211 Buffer::set_current(t, &buffer);
212 glBindBufferRange(t, i, buffer.id, offset, size);
216 void BufferRange::unbind_from(BufferType t, unsigned i)
218 if(set_current(t, i, 0))
220 Buffer::set_current(t, 0);
221 glBindBufferBase(t, i, 0);
225 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
227 if(type==UNIFORM_BUFFER)
229 if(index>=get_n_uniform_buffer_bindings())
230 throw out_of_range("BufferRange::binding");
231 if(bound_uniform.size()<=index)
232 bound_uniform.resize(index+1);
233 return bound_uniform[index];
236 throw invalid_argument("BufferRange::binding");
239 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
241 const BufferRange *&ptr = binding(type, index);
249 unsigned BufferRange::get_n_uniform_buffer_bindings()
251 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
255 unsigned BufferRange::get_uniform_buffer_alignment()
257 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);