2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_map_buffer_range.h>
14 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
15 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
17 Buffer::Buffer(BufferType t):
22 require_buffer_type(type);
24 if(ARB_direct_state_access)
25 glCreateBuffers(1, &id);
32 for(unsigned i=0; i<5; ++i)
34 unbind_from(buffer_types[i]);
35 glDeleteBuffers(1, &id);
38 void Buffer::require_buffer_type(BufferType type)
40 static Require _req_vbo(ARB_vertex_buffer_object);
41 if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
42 static Require _req_pbo(ARB_pixel_buffer_object);
43 else if(type==UNIFORM_BUFFER)
44 static Require _req_ubo(ARB_uniform_buffer_object);
47 void Buffer::set_usage(BufferUsage u)
52 void Buffer::data(unsigned sz, const void *d)
54 if(ARB_direct_state_access)
55 glNamedBufferData(id, sz, d, usage);
58 BindRestore _bind(this, type);
59 glBufferData(type, sz, d, usage);
64 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
66 if(ARB_direct_state_access)
67 glNamedBufferSubData(id, off, sz, d);
70 BindRestore _bind(this, type);
71 glBufferSubData(type, off, sz, d);
75 BufferRange *Buffer::create_range(unsigned s, unsigned o)
77 return new BufferRange(*this, s, o);
80 void *Buffer::map(BufferAccess access)
82 if(ARB_map_buffer_range)
84 GLenum access_bits = 0;
86 access_bits = GL_MAP_READ_BIT;
87 else if(access==WRITE_ONLY)
88 access_bits = GL_MAP_WRITE_BIT;
89 else if(access==READ_WRITE)
90 access_bits = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT;
91 if(ARB_direct_state_access)
92 return glMapNamedBufferRange(id, 0, size, access_bits);
95 BindRestore _bind(this, type);
96 return glMapBufferRange(type, 0, size, access_bits);
99 else if(ARB_direct_state_access)
100 return glMapNamedBuffer(id, access);
101 else if(OES_mapbuffer)
103 BindRestore _bind(this, type);
104 return glMapBuffer(type, access);
107 throw invalid_operation("Buffer::map");
112 if(ARB_direct_state_access)
113 return glUnmapNamedBuffer(id);
116 BindRestore _bind(this, type);
117 return glUnmapBuffer(type);
121 void Buffer::bind_to(BufferType t) const
124 require_buffer_type(t);
125 if(t==ELEMENT_ARRAY_BUFFER)
126 if(const Mesh *m = Mesh::current())
128 // Don't change the binding in a mesh's vertex array object
129 if(this==m->get_index_buffer())
131 throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
133 if(set_current(t, this))
137 const Buffer *Buffer::current(BufferType t)
139 if(t==ELEMENT_ARRAY_BUFFER)
140 if(const Mesh *m = Mesh::current())
141 return m->get_index_buffer();
145 void Buffer::unbind_from(BufferType type)
147 if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
148 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
149 if(set_current(type, 0))
150 glBindBuffer(type, 0);
153 const Buffer *&Buffer::binding(BufferType type)
157 case ARRAY_BUFFER: return bound[0];
158 case ELEMENT_ARRAY_BUFFER: return bound[1];
159 case PIXEL_PACK_BUFFER: return bound[2];
160 case PIXEL_UNPACK_BUFFER: return bound[3];
161 case UNIFORM_BUFFER: return bound[4];
162 default: throw invalid_argument("Buffer::binding");
166 bool Buffer::set_current(BufferType type, const Buffer *buf)
168 const Buffer *&ptr = binding(type);
177 vector<const BufferRange *> BufferRange::bound_uniform;
179 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
184 if(o>buffer.get_size() || o+s>buffer.get_size())
185 throw out_of_range("BufferRange::BufferRange");
188 BufferRange::~BufferRange()
190 for(unsigned i=0; i<bound_uniform.size(); ++i)
191 if(bound_uniform[i]==this)
192 unbind_from(UNIFORM_BUFFER, i);
195 void BufferRange::data(const void *d)
197 buffer.sub_data(offset, size, d);
200 void BufferRange::bind_to(BufferType t, unsigned i)
203 Buffer::require_buffer_type(t);
204 if(set_current(t, i, this))
206 // The buffer gets bound as a side effect
207 Buffer::set_current(t, &buffer);
208 glBindBufferRange(t, i, buffer.id, offset, size);
212 void BufferRange::unbind_from(BufferType t, unsigned i)
214 if(set_current(t, i, 0))
216 Buffer::set_current(t, 0);
217 glBindBufferBase(t, i, 0);
221 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
223 if(type==UNIFORM_BUFFER)
225 if(index>=get_n_uniform_buffer_bindings())
226 throw out_of_range("BufferRange::binding");
227 if(bound_uniform.size()<=index)
228 bound_uniform.resize(index+1);
229 return bound_uniform[index];
232 throw invalid_argument("BufferRange::binding");
235 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
237 const BufferRange *&ptr = binding(type, index);
245 unsigned BufferRange::get_n_uniform_buffer_bindings()
247 static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
251 unsigned BufferRange::get_uniform_buffer_alignment()
253 static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);