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 };
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);
30 for(unsigned i=0; i<5; ++i)
32 unbind_from(buffer_types[i]);
33 glDeleteBuffers(1, &id);
36 void Buffer::require_buffer_type(BufferType type)
38 static Require _req_vbo(ARB_vertex_buffer_object);
39 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
40 static Require _req_pbo(ARB_pixel_buffer_object);
41 else if(type==UNIFORM_BUFFER)
42 static Require _req_ubo(ARB_uniform_buffer_object);
45 void Buffer::set_usage(BufferUsage u)
50 void Buffer::data(unsigned sz, const void *d)
52 BindRestore _bind(this, type);
53 glBufferData(type, sz, d, usage);
57 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
59 BindRestore _bind(this, type);
60 glBufferSubData(type, off, sz, d);
63 BufferRange *Buffer::create_range(unsigned s, unsigned o)
65 return new BufferRange(*this, s, o);
68 void *Buffer::map(BufferAccess access)
70 BindRestore _bind(this, type);
71 return glMapBuffer(type, access);
76 BindRestore _bind(this, type);
77 return glUnmapBuffer(type);
80 void Buffer::bind_to(BufferType t) const
83 require_buffer_type(t);
84 if(t==ELEMENT_ARRAY_BUFFER)
85 if(const Mesh *m = Mesh::current())
87 // Don't change the binding in a mesh's vertex array object
88 if(this==m->get_index_buffer())
90 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
92 if(set_current(t, this))
96 const Buffer *Buffer::current(BufferType t)
98 if(t==ELEMENT_ARRAY_BUFFER)
99 if(const Mesh *m = Mesh::current())
100 return m->get_index_buffer();
104 void Buffer::unbind_from(BufferType type)
106 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
107 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
108 if(set_current(type, 0))
109 glBindBuffer(type, 0);
112 const Buffer *&Buffer::binding(BufferType type)
116 case ARRAY_BUFFER: return bound[0];
117 case ELEMENT_ARRAY_BUFFER: return bound[1];
118 case PIXEL_PACK_BUFFER: return bound[2];
119 case PIXEL_UNPACK_BUFFER: return bound[3];
120 case UNIFORM_BUFFER: return bound[4];
121 default: throw invalid_argument("Buffer::binding");
125 bool Buffer::set_current(BufferType type, const Buffer *buf)
127 const Buffer *&ptr = binding(type);
136 vector<const BufferRange *> BufferRange::bound_uniform;
138 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
143 if(o>buffer.get_size() || o+s>buffer.get_size())
144 throw out_of_range("BufferRange::BufferRange");
147 BufferRange::~BufferRange()
149 for(unsigned i=0; i<bound_uniform.size(); ++i)
150 if(bound_uniform[i]==this)
151 unbind_from(UNIFORM_BUFFER, i);
154 void BufferRange::data(const void *d)
156 buffer.sub_data(offset, size, d);
159 void BufferRange::bind_to(BufferType t, unsigned i)
162 Buffer::require_buffer_type(t);
163 if(set_current(t, i, this))
165 // The buffer gets bound as a side effect
166 Buffer::set_current(t, &buffer);
167 glBindBufferRange(t, i, buffer.id, offset, size);
171 void BufferRange::unbind_from(BufferType t, unsigned i)
173 if(set_current(t, i, 0))
175 Buffer::set_current(t, 0);
176 glBindBufferBase(t, i, 0);
180 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
182 if(type==UNIFORM_BUFFER)
184 if(index>=get_n_uniform_buffer_bindings())
185 throw out_of_range("BufferRange::binding");
186 if(bound_uniform.size()<=index)
187 bound_uniform.resize(index+1);
188 return bound_uniform[index];
191 throw invalid_argument("BufferRange::binding");
194 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
196 const BufferRange *&ptr = binding(type, index);
204 unsigned BufferRange::get_n_uniform_buffer_bindings()
206 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
210 unsigned BufferRange::get_uniform_buffer_alignment()
212 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);