2 #include <msp/gl/extensions/arb_pixel_buffer_object.h>
3 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
4 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
15 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
17 Buffer::Buffer(BufferType t):
22 require_buffer_type(type);
29 glDeleteBuffers(1, &id);
32 void Buffer::require_buffer_type(BufferType type)
34 static Require _req_vbo(ARB_vertex_buffer_object);
35 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
36 static Require _req_pbo(ARB_pixel_buffer_object);
37 else if(type==UNIFORM_BUFFER)
38 static Require _req_ubo(ARB_uniform_buffer_object);
41 void Buffer::set_usage(BufferUsage u)
46 void Buffer::data(unsigned sz, const void *d)
48 BindRestore _bind(this, type);
49 glBufferData(type, sz, d, usage);
53 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
55 BindRestore _bind(this, type);
56 glBufferSubData(type, off, sz, d);
59 BufferRange *Buffer::create_range(unsigned s, unsigned o)
61 return new BufferRange(*this, s, o);
64 void *Buffer::map(BufferAccess access)
66 BindRestore _bind(this, type);
67 return glMapBuffer(type, access);
72 BindRestore _bind(this, type);
73 return glUnmapBuffer(type);
76 void Buffer::bind_to(BufferType t) const
79 require_buffer_type(t);
80 if(t==ELEMENT_ARRAY_BUFFER)
81 if(const Mesh *m = Mesh::current())
83 // Don't change the binding in a mesh's vertex array object
84 if(this==m->get_index_buffer())
86 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
88 if(set_current(t, this))
92 const Buffer *Buffer::current(BufferType t)
94 if(t==ELEMENT_ARRAY_BUFFER)
95 if(const Mesh *m = Mesh::current())
96 return m->get_index_buffer();
100 void Buffer::unbind_from(BufferType type)
102 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
103 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
104 if(set_current(type, 0))
105 glBindBuffer(type, 0);
108 const Buffer *&Buffer::binding(BufferType type)
112 case ARRAY_BUFFER: return bound[0];
113 case ELEMENT_ARRAY_BUFFER: return bound[1];
114 case PIXEL_PACK_BUFFER: return bound[2];
115 case PIXEL_UNPACK_BUFFER: return bound[3];
116 case UNIFORM_BUFFER: return bound[4];
117 default: throw invalid_argument("Buffer::binding");
121 bool Buffer::set_current(BufferType type, const Buffer *buf)
123 const Buffer *&ptr = binding(type);
132 vector<const BufferRange *> BufferRange::bound_uniform;
134 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
139 if(o>buffer.get_size() || o+s>buffer.get_size())
140 throw out_of_range("BufferRange::BufferRange");
143 void BufferRange::data(const void *d)
145 buffer.sub_data(offset, size, d);
148 void BufferRange::bind_to(BufferType t, unsigned i)
151 Buffer::require_buffer_type(t);
152 // Intentionally using bitwise | to avoid short-circuiting
153 if(Buffer::set_current(t, &buffer) | set_current(t, i, this))
154 glBindBufferRange(t, i, buffer.id, offset, size);
157 void BufferRange::unbind_from(BufferType t, unsigned i)
159 if(set_current(t, i, 0))
161 Buffer::set_current(t, 0);
162 glBindBufferBase(t, i, 0);
166 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
168 if(type==UNIFORM_BUFFER)
170 if(index>=get_n_uniform_buffer_bindings())
171 throw out_of_range("BufferRange::binding");
172 if(bound_uniform.size()<=index)
173 bound_uniform.resize(index+1);
174 return bound_uniform[index];
177 throw invalid_argument("BufferRange::binding");
180 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
182 const BufferRange *&ptr = binding(type, index);
190 unsigned BufferRange::get_n_uniform_buffer_bindings()
192 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
196 unsigned BufferRange::get_uniform_buffer_alignment()
198 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);