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::bind_to(BufferType t) const
67 require_buffer_type(t);
68 // Don't change the binding in a mesh's vertex array object
69 if(t==ELEMENT_ARRAY_BUFFER && Mesh::current())
70 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
71 if(set_current(t, this))
75 void Buffer::unbind_from(BufferType type)
77 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
78 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
79 if(set_current(type, 0))
80 glBindBuffer(type, 0);
83 const Buffer *&Buffer::binding(BufferType type)
87 case ARRAY_BUFFER: return bound[0];
88 case ELEMENT_ARRAY_BUFFER: return bound[1];
89 case PIXEL_PACK_BUFFER: return bound[2];
90 case PIXEL_UNPACK_BUFFER: return bound[3];
91 case UNIFORM_BUFFER: return bound[4];
92 default: throw invalid_argument("Buffer::binding");
96 bool Buffer::set_current(BufferType type, const Buffer *buf)
98 const Buffer *&ptr = binding(type);
107 vector<const BufferRange *> BufferRange::bound_uniform;
109 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
114 if(o>buffer.get_size() || o+s>buffer.get_size())
115 throw out_of_range("BufferRange::BufferRange");
118 void BufferRange::data(const void *d)
120 buffer.sub_data(offset, size, d);
123 void BufferRange::bind_to(BufferType t, unsigned i)
126 Buffer::require_buffer_type(t);
127 // Intentionally using bitwise | to avoid short-circuiting
128 if(Buffer::set_current(t, &buffer) | set_current(t, i, this))
129 glBindBufferRange(t, i, buffer.id, offset, size);
132 void BufferRange::unbind_from(BufferType t, unsigned i)
134 if(set_current(t, i, 0))
136 Buffer::set_current(t, 0);
137 glBindBufferBase(t, i, 0);
141 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
143 if(type==UNIFORM_BUFFER)
145 if(index>=get_n_uniform_buffer_bindings())
146 throw out_of_range("BufferRange::binding");
147 if(bound_uniform.size()<=index)
148 bound_uniform.resize(index+1);
149 return bound_uniform[index];
152 throw invalid_argument("BufferRange::binding");
155 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
157 const BufferRange *&ptr = binding(type, index);
165 unsigned BufferRange::get_n_uniform_buffer_bindings()
167 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
171 unsigned BufferRange::get_uniform_buffer_alignment()
173 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);