12 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
13 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
15 Buffer::Buffer(BufferType t):
20 require_buffer_type(type);
27 for(unsigned i=0; i<5; ++i)
29 unbind_from(buffer_types[i]);
30 glDeleteBuffers(1, &id);
33 void Buffer::require_buffer_type(BufferType type)
35 static Require _req_vbo(ARB_vertex_buffer_object);
36 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
37 static Require _req_pbo(ARB_pixel_buffer_object);
38 else if(type==UNIFORM_BUFFER)
39 static Require _req_ubo(ARB_uniform_buffer_object);
42 void Buffer::set_usage(BufferUsage u)
47 void Buffer::data(unsigned sz, const void *d)
49 BindRestore _bind(this, type);
50 glBufferData(type, sz, d, usage);
54 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
56 BindRestore _bind(this, type);
57 glBufferSubData(type, off, sz, d);
60 BufferRange *Buffer::create_range(unsigned s, unsigned o)
62 return new BufferRange(*this, s, o);
65 void *Buffer::map(BufferAccess access)
67 BindRestore _bind(this, type);
68 return glMapBuffer(type, access);
73 BindRestore _bind(this, type);
74 return glUnmapBuffer(type);
77 void Buffer::bind_to(BufferType t) const
80 require_buffer_type(t);
81 if(t==ELEMENT_ARRAY_BUFFER)
82 if(const Mesh *m = Mesh::current())
84 // Don't change the binding in a mesh's vertex array object
85 if(this==m->get_index_buffer())
87 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
89 if(set_current(t, this))
93 const Buffer *Buffer::current(BufferType t)
95 if(t==ELEMENT_ARRAY_BUFFER)
96 if(const Mesh *m = Mesh::current())
97 return m->get_index_buffer();
101 void Buffer::unbind_from(BufferType type)
103 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
104 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
105 if(set_current(type, 0))
106 glBindBuffer(type, 0);
109 const Buffer *&Buffer::binding(BufferType type)
113 case ARRAY_BUFFER: return bound[0];
114 case ELEMENT_ARRAY_BUFFER: return bound[1];
115 case PIXEL_PACK_BUFFER: return bound[2];
116 case PIXEL_UNPACK_BUFFER: return bound[3];
117 case UNIFORM_BUFFER: return bound[4];
118 default: throw invalid_argument("Buffer::binding");
122 bool Buffer::set_current(BufferType type, const Buffer *buf)
124 const Buffer *&ptr = binding(type);
133 vector<const BufferRange *> BufferRange::bound_uniform;
135 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
140 if(o>buffer.get_size() || o+s>buffer.get_size())
141 throw out_of_range("BufferRange::BufferRange");
144 BufferRange::~BufferRange()
146 for(unsigned i=0; i<bound_uniform.size(); ++i)
147 if(bound_uniform[i]==this)
148 unbind_from(UNIFORM_BUFFER, i);
151 void BufferRange::data(const void *d)
153 buffer.sub_data(offset, size, d);
156 void BufferRange::bind_to(BufferType t, unsigned i)
159 Buffer::require_buffer_type(t);
160 if(set_current(t, i, this))
162 // The buffer gets bound as a side effect
163 Buffer::set_current(t, &buffer);
164 glBindBufferRange(t, i, buffer.id, offset, size);
168 void BufferRange::unbind_from(BufferType t, unsigned i)
170 if(set_current(t, i, 0))
172 Buffer::set_current(t, 0);
173 glBindBufferBase(t, i, 0);
177 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
179 if(type==UNIFORM_BUFFER)
181 if(index>=get_n_uniform_buffer_bindings())
182 throw out_of_range("BufferRange::binding");
183 if(bound_uniform.size()<=index)
184 bound_uniform.resize(index+1);
185 return bound_uniform[index];
188 throw invalid_argument("BufferRange::binding");
191 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
193 const BufferRange *&ptr = binding(type, index);
201 unsigned BufferRange::get_n_uniform_buffer_bindings()
203 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
207 unsigned BufferRange::get_uniform_buffer_alignment()
209 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);