2 #include "arb_pixel_buffer_object.h"
3 #include "arb_uniform_buffer_object.h"
4 #include "arb_vertex_buffer_object.h"
13 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
15 Buffer::Buffer(BufferType t):
20 require_buffer_type(type);
27 glDeleteBuffers(1, &id);
30 void Buffer::require_buffer_type(BufferType type)
32 static Require _req_vbo(ARB_vertex_buffer_object);
33 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
34 static Require _req_pbo(ARB_pixel_buffer_object);
35 else if(type==UNIFORM_BUFFER)
36 static Require _req_ubo(ARB_uniform_buffer_object);
39 void Buffer::set_usage(BufferUsage u)
44 void Buffer::data(unsigned sz, const void *d)
46 const Buffer *old = current(type);
48 glBufferData(type, sz, d, usage);
53 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
55 const Buffer *old = current(type);
57 glBufferSubData(type, off, sz, d);
61 BufferRange *Buffer::create_range(unsigned s, unsigned o)
63 return new BufferRange(*this, s, o);
66 void Buffer::bind_to(BufferType t) const
69 require_buffer_type(t);
70 if(set_current(t, this))
74 void Buffer::unbind_from(BufferType type)
76 if(set_current(type, 0))
77 glBindBuffer(type, 0);
80 const Buffer *&Buffer::binding(BufferType type)
84 case ARRAY_BUFFER: return bound[0];
85 case ELEMENT_ARRAY_BUFFER: return bound[1];
86 case PIXEL_PACK_BUFFER: return bound[2];
87 case PIXEL_UNPACK_BUFFER: return bound[3];
88 case UNIFORM_BUFFER: return bound[4];
89 default: throw invalid_argument("Buffer::binding");
93 bool Buffer::set_current(BufferType type, const Buffer *buf)
95 const Buffer *&ptr = binding(type);
103 void Buffer::restore(const Buffer *buf, BufferType type)
105 if(buf!=current(type))
115 vector<const BufferRange *> BufferRange::bound_uniform;
117 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
122 if(o>buffer.get_size() || o+s>buffer.get_size())
123 throw out_of_range("BufferRange::BufferRange");
126 void BufferRange::data(const void *d)
128 buffer.sub_data(offset, size, d);
131 void BufferRange::bind_to(BufferType t, unsigned i)
134 Buffer::require_buffer_type(t);
135 // Intentionally using bitwise | to avoid short-circuiting
136 if(Buffer::set_current(t, &buffer) | set_current(t, i, this))
137 glBindBufferRange(t, i, buffer.id, offset, size);
140 void BufferRange::unbind_from(BufferType t, unsigned i)
142 if(set_current(t, i, 0))
144 Buffer::set_current(t, 0);
145 glBindBufferBase(t, i, 0);
149 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
151 if(type==UNIFORM_BUFFER)
153 if(index>=get_n_uniform_buffer_bindings())
154 throw out_of_range("BufferRange::binding");
155 if(bound_uniform.size()<=index)
156 bound_uniform.resize(index+1);
157 return bound_uniform[index];
160 throw invalid_argument("BufferRange::binding");
163 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
165 const BufferRange *&ptr = binding(type, index);
173 unsigned BufferRange::get_n_uniform_buffer_bindings()
175 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
179 unsigned BufferRange::get_uniform_buffer_alignment()
181 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);