]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.cpp
Cosmetic changes and TODOs
[libs/gl.git] / source / buffer.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_map_buffer_range.h>
4 #include "buffer.h"
5 #include "error.h"
6 #include "mesh.h"
7 #include "misc.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
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 };
16
17 Buffer::Buffer(BufferType t):
18         type(t),
19         usage(STATIC_DRAW),
20         size(0)
21 {
22         require_buffer_type(type);
23
24         if(ARB_direct_state_access)
25                 glCreateBuffers(1, &id);
26         else
27                 glGenBuffers(1, &id);
28 }
29
30 Buffer::~Buffer()
31 {
32         for(unsigned i=0; i<5; ++i)
33                 if(bound[i]==this)
34                         unbind_from(buffer_types[i]);
35         glDeleteBuffers(1, &id);
36 }
37
38 void Buffer::require_buffer_type(BufferType type)
39 {
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);
45 }
46
47 void Buffer::set_usage(BufferUsage u)
48 {
49         // TODO OpenGL ES 2.0 doesn't support read or copy usages
50         usage = u;
51 }
52
53 void Buffer::data(unsigned sz, const void *d)
54 {
55         if(ARB_direct_state_access)
56                 glNamedBufferData(id, sz, d, usage);
57         else
58         {
59                 BindRestore _bind(this, type);
60                 glBufferData(type, sz, d, usage);
61         }
62         size = sz;
63 }
64
65 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
66 {
67         if(ARB_direct_state_access)
68                 glNamedBufferSubData(id, off, sz, d);
69         else
70         {
71                 BindRestore _bind(this, type);
72                 glBufferSubData(type, off, sz, d);
73         }
74 }
75
76 BufferRange *Buffer::create_range(unsigned s, unsigned o)
77 {
78         return new BufferRange(*this, s, o);
79 }
80
81 void *Buffer::map(BufferAccess access)
82 {
83         if(ARB_map_buffer_range)
84         {
85                 GLenum access_bits = 0;
86                 if(access==READ_ONLY)
87                         access_bits = GL_MAP_READ_BIT;
88                 else if(access==WRITE_ONLY)
89                         access_bits = GL_MAP_WRITE_BIT;
90                 else if(access==READ_WRITE)
91                         access_bits = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT;
92                 if(ARB_direct_state_access)
93                         return glMapNamedBufferRange(id, 0, size, access_bits);
94                 else
95                 {
96                         BindRestore _bind(this, type);
97                         return glMapBufferRange(type, 0, size, access_bits);
98                 }
99         }
100         else if(ARB_direct_state_access)
101                 return glMapNamedBuffer(id, access);
102         else if(OES_mapbuffer)
103         {
104                 BindRestore _bind(this, type);
105                 return glMapBuffer(type, access);
106         }
107         else
108                 throw invalid_operation("Buffer::map");
109 }
110
111 bool Buffer::unmap()
112 {
113         if(ARB_direct_state_access)
114                 return glUnmapNamedBuffer(id);
115         else
116         {
117                 BindRestore _bind(this, type);
118                 return glUnmapBuffer(type);
119         }
120 }
121
122 void Buffer::bind_to(BufferType t) const
123 {
124         if(t!=type)
125                 require_buffer_type(t);
126         if(t==ELEMENT_ARRAY_BUFFER)
127                 if(const Mesh *m = Mesh::current())
128                 {
129                         // Don't change the binding in a mesh's vertex array object
130                         if(this==m->get_index_buffer())
131                                 return;
132                         throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
133                 }
134         if(set_current(t, this))
135                 glBindBuffer(t, id);
136 }
137
138 const Buffer *Buffer::current(BufferType t)
139 {
140         if(t==ELEMENT_ARRAY_BUFFER)
141                 if(const Mesh *m = Mesh::current())
142                         return m->get_index_buffer();
143         return binding(t);
144 }
145
146 void Buffer::unbind_from(BufferType type)
147 {
148         if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
149                 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
150         if(set_current(type, 0))
151                 glBindBuffer(type, 0);
152 }
153
154 const Buffer *&Buffer::binding(BufferType type)
155 {
156         switch(type)
157         {
158         case ARRAY_BUFFER:         return bound[0];
159         case ELEMENT_ARRAY_BUFFER: return bound[1];
160         case PIXEL_PACK_BUFFER:    return bound[2];
161         case PIXEL_UNPACK_BUFFER:  return bound[3];
162         case UNIFORM_BUFFER:       return bound[4];
163         default: throw invalid_argument("Buffer::binding");
164         }
165 }
166
167 bool Buffer::set_current(BufferType type, const Buffer *buf)
168 {
169         const Buffer *&ptr = binding(type);
170         if(ptr==buf)
171                 return false;
172
173         ptr = buf;
174         return true;
175 }
176
177
178 vector<const BufferRange *> BufferRange::bound_uniform;
179
180 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
181         buffer(b),
182         offset(o),
183         size(s)
184 {
185         if(o>buffer.get_size() || o+s>buffer.get_size())
186                 throw out_of_range("BufferRange::BufferRange");
187 }
188
189 BufferRange::~BufferRange()
190 {
191         for(unsigned i=0; i<bound_uniform.size(); ++i)
192                 if(bound_uniform[i]==this)
193                         unbind_from(UNIFORM_BUFFER, i);
194 }
195
196 void BufferRange::data(const void *d)
197 {
198         buffer.sub_data(offset, size, d);
199 }
200
201 void BufferRange::bind_to(BufferType t, unsigned i)
202 {
203         if(t!=buffer.type)
204                 Buffer::require_buffer_type(t);
205         if(set_current(t, i, this))
206         {
207                 // The buffer gets bound as a side effect
208                 Buffer::set_current(t, &buffer);
209                 glBindBufferRange(t, i, buffer.id, offset, size);
210         }
211 }
212
213 void BufferRange::unbind_from(BufferType t, unsigned i)
214 {
215         if(set_current(t, i, 0))
216         {
217                 Buffer::set_current(t, 0);
218                 glBindBufferBase(t, i, 0);
219         }
220 }
221
222 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
223 {
224         if(type==UNIFORM_BUFFER)
225         {
226                 if(index>=get_n_uniform_buffer_bindings())
227                         throw out_of_range("BufferRange::binding");
228                 if(bound_uniform.size()<=index)
229                         bound_uniform.resize(index+1);
230                 return bound_uniform[index];
231         }
232         else
233                 throw invalid_argument("BufferRange::binding");
234 }
235
236 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
237 {
238         const BufferRange *&ptr = binding(type, index);
239         if(ptr==buf)
240                 return false;
241
242         ptr = buf;
243         return true;
244 }
245
246 unsigned BufferRange::get_n_uniform_buffer_bindings()
247 {
248         static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
249         return count;
250 }
251
252 unsigned BufferRange::get_uniform_buffer_alignment()
253 {
254         static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
255         return align;
256 }
257
258 } // namespace GL
259 } // namespace Msp