]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.cpp
Store implementation limits in a central struct
[libs/gl.git] / source / core / buffer.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_buffer_storage.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_map_buffer_range.h>
5 #include <msp/gl/extensions/khr_debug.h>
6 #include <msp/strings/format.h>
7 #include "buffer.h"
8 #include "deviceinfo.h"
9 #include "error.h"
10 #include "misc.h"
11 #include "vertexsetup.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 const Buffer *Buffer::bound[5] = { 0, 0, 0, 0, 0 };
19 BufferType buffer_types[] = { ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER, PIXEL_PACK_BUFFER, PIXEL_UNPACK_BUFFER, UNIFORM_BUFFER };
20
21 Buffer::Buffer(BufferType t):
22         type(t),
23         size(0),
24         allocated(false)
25 {
26         require_buffer_type(type);
27
28         if(ARB_direct_state_access)
29                 glCreateBuffers(1, &id);
30         else
31                 glGenBuffers(1, &id);
32 }
33
34 Buffer::~Buffer()
35 {
36         for(unsigned i=0; i<5; ++i)
37                 if(bound[i]==this)
38                         unbind_from(buffer_types[i]);
39         glDeleteBuffers(1, &id);
40 }
41
42 void Buffer::require_buffer_type(BufferType type)
43 {
44         static Require _req_vbo(ARB_vertex_buffer_object);
45         if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
46                 static Require _req_pbo(ARB_pixel_buffer_object);
47         else if(type==UNIFORM_BUFFER)
48                 static Require _req_ubo(ARB_uniform_buffer_object);
49 }
50
51 void Buffer::storage(unsigned sz)
52 {
53         if(size>0)
54         {
55                 if(sz!=size)
56                         throw incompatible_data("Buffer::storage");
57                 return;
58         }
59         if(sz==0)
60                 throw invalid_argument("Buffer::storage");
61
62         size = sz;
63 }
64
65 void Buffer::allocate()
66 {
67         if(size==0)
68                 throw invalid_operation("Buffer::allocate");
69         if(allocated)
70                 return;
71
72         if(ARB_buffer_storage)
73         {
74                 static const int flags = GL_MAP_READ_BIT|GL_MAP_WRITE_BIT|GL_DYNAMIC_STORAGE_BIT;
75                 if(ARB_direct_state_access)
76                         glNamedBufferStorage(id, size, 0, flags);
77                 else
78                 {
79                         BindRestore _bind(this, type);
80                         glBufferStorage(type, size, 0, flags);
81                 }
82
83                 allocated = true;
84         }
85         else
86                 data(0);
87 }
88
89 void Buffer::set_usage(BufferUsage)
90 {
91 }
92
93 void Buffer::data(const void *d)
94 {
95         if(size==0)
96                 throw invalid_operation("Buffer::data");
97
98         if(ARB_buffer_storage)
99                 return sub_data(0, size, d);
100
101         if(ARB_direct_state_access)
102                 glNamedBufferData(id, size, d, STATIC_DRAW);
103         else
104         {
105                 BindRestore _bind(this, type);
106                 glBufferData(type, size, d, STATIC_DRAW);
107         }
108
109         allocated = true;
110 }
111
112 void Buffer::data(unsigned sz, const void *d)
113 {
114         if(size==0)
115                 storage(sz);
116         else if(sz!=size)
117                 throw incompatible_data("Buffer::data");
118
119         data(d);
120 }
121
122 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
123 {
124         if(size==0)
125                 throw invalid_operation("Buffer::sub_data");
126
127         allocate();
128
129         if(ARB_direct_state_access)
130                 glNamedBufferSubData(id, off, sz, d);
131         else
132         {
133                 BindRestore _bind(this, type);
134                 glBufferSubData(type, off, sz, d);
135         }
136 }
137
138 void Buffer::require_size(unsigned req_sz) const
139 {
140         if(size<req_sz)
141                 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
142 }
143
144 BufferRange *Buffer::create_range(unsigned s, unsigned o)
145 {
146         return new BufferRange(*this, s, o);
147 }
148
149 void *Buffer::map()
150 {
151         allocate();
152         if(ARB_map_buffer_range)
153         {
154                 if(ARB_direct_state_access)
155                         return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
156                 else
157                 {
158                         BindRestore _bind(this, type);
159                         return glMapBufferRange(type, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
160                 }
161         }
162         else if(ARB_direct_state_access)
163                 return glMapNamedBuffer(id, GL_READ_WRITE);
164         else if(OES_mapbuffer)
165         {
166                 BindRestore _bind(this, type);
167                 return glMapBuffer(type, GL_READ_WRITE);
168         }
169         else
170                 throw invalid_operation("Buffer::map");
171 }
172
173 bool Buffer::unmap()
174 {
175         // TODO check if it's mapped
176         if(ARB_direct_state_access)
177                 return glUnmapNamedBuffer(id);
178         else if(OES_mapbuffer)
179         {
180                 BindRestore _bind(this, type);
181                 return glUnmapBuffer(type);
182         }
183         else
184                 return true;
185 }
186
187 void Buffer::bind_to(BufferType t) const
188 {
189         if(t!=type)
190                 require_buffer_type(t);
191         if(t==ELEMENT_ARRAY_BUFFER)
192                 if(const VertexSetup *vs = VertexSetup::current())
193                 {
194                         // Don't change the binding in a vertex array object
195                         if(this==vs->get_index_buffer())
196                                 return;
197                         throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
198                 }
199         if(set_current(t, this))
200                 glBindBuffer(t, id);
201 }
202
203 const Buffer *Buffer::current(BufferType t)
204 {
205         if(t==ELEMENT_ARRAY_BUFFER)
206                 if(const VertexSetup *vs = VertexSetup::current())
207                         return vs->get_index_buffer();
208         return binding(t);
209 }
210
211 void Buffer::unbind_from(BufferType type)
212 {
213         if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
214                 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
215         if(set_current(type, 0))
216                 glBindBuffer(type, 0);
217 }
218
219 const Buffer *&Buffer::binding(BufferType type)
220 {
221         switch(type)
222         {
223         case ARRAY_BUFFER:         return bound[0];
224         case ELEMENT_ARRAY_BUFFER: return bound[1];
225         case PIXEL_PACK_BUFFER:    return bound[2];
226         case PIXEL_UNPACK_BUFFER:  return bound[3];
227         case UNIFORM_BUFFER:       return bound[4];
228         default: throw invalid_argument("Buffer::binding");
229         }
230 }
231
232 bool Buffer::set_current(BufferType type, const Buffer *buf)
233 {
234         const Buffer *&ptr = binding(type);
235         if(ptr==buf)
236                 return false;
237
238         ptr = buf;
239         return true;
240 }
241
242 void Buffer::set_debug_name(const string &name)
243 {
244 #ifdef DEBUG
245         if(KHR_debug)
246                 glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
247 #else
248         (void)name;
249 #endif
250 }
251
252
253 vector<const BufferRange *> BufferRange::bound_uniform;
254
255 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
256         buffer(b),
257         offset(o),
258         size(s)
259 {
260         if(o>buffer.get_size() || o+s>buffer.get_size())
261                 throw out_of_range("BufferRange::BufferRange");
262 }
263
264 BufferRange::~BufferRange()
265 {
266         for(unsigned i=0; i<bound_uniform.size(); ++i)
267                 if(bound_uniform[i]==this)
268                         unbind_from(UNIFORM_BUFFER, i);
269 }
270
271 void BufferRange::data(const void *d)
272 {
273         buffer.sub_data(offset, size, d);
274 }
275
276 void BufferRange::bind_to(BufferType t, unsigned i)
277 {
278         if(t!=buffer.type)
279                 Buffer::require_buffer_type(t);
280         if(set_current(t, i, this))
281         {
282                 // The buffer gets bound as a side effect
283                 Buffer::set_current(t, &buffer);
284                 glBindBufferRange(t, i, buffer.id, offset, size);
285         }
286 }
287
288 void BufferRange::unbind_from(BufferType t, unsigned i)
289 {
290         if(set_current(t, i, 0))
291         {
292                 Buffer::set_current(t, 0);
293                 glBindBufferBase(t, i, 0);
294         }
295 }
296
297 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
298 {
299         if(type==UNIFORM_BUFFER)
300         {
301                 if(index>=Limits::get_global().max_uniform_bindings)
302                         throw out_of_range("BufferRange::binding");
303                 if(bound_uniform.size()<=index)
304                         bound_uniform.resize(index+1);
305                 return bound_uniform[index];
306         }
307         else
308                 throw invalid_argument("BufferRange::binding");
309 }
310
311 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
312 {
313         const BufferRange *&ptr = binding(type, index);
314         if(ptr==buf)
315                 return false;
316
317         ptr = buf;
318         return true;
319 }
320
321 unsigned BufferRange::get_n_uniform_buffer_bindings()
322 {
323         return Limits::get_global().max_uniform_bindings;
324 }
325
326 unsigned BufferRange::get_uniform_buffer_alignment()
327 {
328         return Limits::get_global().uniform_buffer_alignment;
329 }
330
331 } // namespace GL
332 } // namespace Msp