2 #include <msp/gl/extensions/arb_map_buffer_range.h>
13 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
14 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
16 Buffer::Buffer(BufferType t):
21 require_buffer_type(type);
28 for(unsigned i=0; i<5; ++i)
30 unbind_from(buffer_types[i]);
31 glDeleteBuffers(1, &id);
34 void Buffer::require_buffer_type(BufferType type)
36 static Require _req_vbo(ARB_vertex_buffer_object);
37 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
38 static Require _req_pbo(ARB_pixel_buffer_object);
39 else if(type==UNIFORM_BUFFER)
40 static Require _req_ubo(ARB_uniform_buffer_object);
43 void Buffer::set_usage(BufferUsage u)
48 void Buffer::data(unsigned sz, const void *d)
50 BindRestore _bind(this, type);
51 glBufferData(type, sz, d, usage);
55 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
57 BindRestore _bind(this, type);
58 glBufferSubData(type, off, sz, d);
61 BufferRange *Buffer::create_range(unsigned s, unsigned o)
63 return new BufferRange(*this, s, o);
66 void *Buffer::map(BufferAccess access)
68 if(ARB_map_buffer_range)
70 BindRestore _bind(this, type);
71 GLenum access_bits = 0;
73 access_bits = GL_MAP_READ_BIT;
74 else if(access==WRITE_ONLY)
75 access_bits = GL_MAP_WRITE_BIT;
76 else if(access==READ_WRITE)
77 access_bits = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT;
78 return glMapBufferRange(type, 0, size, access_bits);
82 BindRestore _bind(this, type);
83 return glMapBuffer(type, access);
89 BindRestore _bind(this, type);
90 return glUnmapBuffer(type);
93 void Buffer::bind_to(BufferType t) const
96 require_buffer_type(t);
97 if(t==ELEMENT_ARRAY_BUFFER)
98 if(const Mesh *m = Mesh::current())
100 // Don't change the binding in a mesh's vertex array object
101 if(this==m->get_index_buffer())
103 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
105 if(set_current(t, this))
109 const Buffer *Buffer::current(BufferType t)
111 if(t==ELEMENT_ARRAY_BUFFER)
112 if(const Mesh *m = Mesh::current())
113 return m->get_index_buffer();
117 void Buffer::unbind_from(BufferType type)
119 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
120 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
121 if(set_current(type, 0))
122 glBindBuffer(type, 0);
125 const Buffer *&Buffer::binding(BufferType type)
129 case ARRAY_BUFFER: return bound[0];
130 case ELEMENT_ARRAY_BUFFER: return bound[1];
131 case PIXEL_PACK_BUFFER: return bound[2];
132 case PIXEL_UNPACK_BUFFER: return bound[3];
133 case UNIFORM_BUFFER: return bound[4];
134 default: throw invalid_argument("Buffer::binding");
138 bool Buffer::set_current(BufferType type, const Buffer *buf)
140 const Buffer *&ptr = binding(type);
149 vector<const BufferRange *> BufferRange::bound_uniform;
151 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
156 if(o>buffer.get_size() || o+s>buffer.get_size())
157 throw out_of_range("BufferRange::BufferRange");
160 BufferRange::~BufferRange()
162 for(unsigned i=0; i<bound_uniform.size(); ++i)
163 if(bound_uniform[i]==this)
164 unbind_from(UNIFORM_BUFFER, i);
167 void BufferRange::data(const void *d)
169 buffer.sub_data(offset, size, d);
172 void BufferRange::bind_to(BufferType t, unsigned i)
175 Buffer::require_buffer_type(t);
176 if(set_current(t, i, this))
178 // The buffer gets bound as a side effect
179 Buffer::set_current(t, &buffer);
180 glBindBufferRange(t, i, buffer.id, offset, size);
184 void BufferRange::unbind_from(BufferType t, unsigned i)
186 if(set_current(t, i, 0))
188 Buffer::set_current(t, 0);
189 glBindBufferBase(t, i, 0);
193 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
195 if(type==UNIFORM_BUFFER)
197 if(index>=get_n_uniform_buffer_bindings())
198 throw out_of_range("BufferRange::binding");
199 if(bound_uniform.size()<=index)
200 bound_uniform.resize(index+1);
201 return bound_uniform[index];
204 throw invalid_argument("BufferRange::binding");
207 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
209 const BufferRange *&ptr = binding(type, index);
217 unsigned BufferRange::get_n_uniform_buffer_bindings()
219 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
223 unsigned BufferRange::get_uniform_buffer_alignment()
225 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);