]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.cpp
Always use ARB_map_buffer_range for mapping buffers
[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         static Require _req(ARB_map_buffer_range);
152
153         allocate();
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
163 bool Buffer::unmap()
164 {
165         // TODO check if it's mapped
166         if(ARB_direct_state_access)
167                 return glUnmapNamedBuffer(id);
168         else if(OES_mapbuffer)
169         {
170                 BindRestore _bind(this, type);
171                 return glUnmapBuffer(type);
172         }
173         else
174                 return true;
175 }
176
177 void Buffer::bind_to(BufferType t) const
178 {
179         if(t!=type)
180                 require_buffer_type(t);
181         if(t==ELEMENT_ARRAY_BUFFER)
182                 if(const VertexSetup *vs = VertexSetup::current())
183                 {
184                         // Don't change the binding in a vertex array object
185                         if(this==vs->get_index_buffer())
186                                 return;
187                         throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
188                 }
189         if(set_current(t, this))
190                 glBindBuffer(t, id);
191 }
192
193 const Buffer *Buffer::current(BufferType t)
194 {
195         if(t==ELEMENT_ARRAY_BUFFER)
196                 if(const VertexSetup *vs = VertexSetup::current())
197                         return vs->get_index_buffer();
198         return binding(t);
199 }
200
201 void Buffer::unbind_from(BufferType type)
202 {
203         if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
204                 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
205         if(set_current(type, 0))
206                 glBindBuffer(type, 0);
207 }
208
209 const Buffer *&Buffer::binding(BufferType type)
210 {
211         switch(type)
212         {
213         case ARRAY_BUFFER:         return bound[0];
214         case ELEMENT_ARRAY_BUFFER: return bound[1];
215         case PIXEL_PACK_BUFFER:    return bound[2];
216         case PIXEL_UNPACK_BUFFER:  return bound[3];
217         case UNIFORM_BUFFER:       return bound[4];
218         default: throw invalid_argument("Buffer::binding");
219         }
220 }
221
222 bool Buffer::set_current(BufferType type, const Buffer *buf)
223 {
224         const Buffer *&ptr = binding(type);
225         if(ptr==buf)
226                 return false;
227
228         ptr = buf;
229         return true;
230 }
231
232 void Buffer::set_debug_name(const string &name)
233 {
234 #ifdef DEBUG
235         if(KHR_debug)
236                 glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
237 #else
238         (void)name;
239 #endif
240 }
241
242
243 vector<const BufferRange *> BufferRange::bound_uniform;
244
245 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
246         buffer(b),
247         offset(o),
248         size(s)
249 {
250         if(o>buffer.get_size() || o+s>buffer.get_size())
251                 throw out_of_range("BufferRange::BufferRange");
252 }
253
254 BufferRange::~BufferRange()
255 {
256         for(unsigned i=0; i<bound_uniform.size(); ++i)
257                 if(bound_uniform[i]==this)
258                         unbind_from(UNIFORM_BUFFER, i);
259 }
260
261 void BufferRange::data(const void *d)
262 {
263         buffer.sub_data(offset, size, d);
264 }
265
266 void BufferRange::bind_to(BufferType t, unsigned i)
267 {
268         if(t!=buffer.type)
269                 Buffer::require_buffer_type(t);
270         if(set_current(t, i, this))
271         {
272                 // The buffer gets bound as a side effect
273                 Buffer::set_current(t, &buffer);
274                 glBindBufferRange(t, i, buffer.id, offset, size);
275         }
276 }
277
278 void BufferRange::unbind_from(BufferType t, unsigned i)
279 {
280         if(set_current(t, i, 0))
281         {
282                 Buffer::set_current(t, 0);
283                 glBindBufferBase(t, i, 0);
284         }
285 }
286
287 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
288 {
289         if(type==UNIFORM_BUFFER)
290         {
291                 if(index>=Limits::get_global().max_uniform_bindings)
292                         throw out_of_range("BufferRange::binding");
293                 if(bound_uniform.size()<=index)
294                         bound_uniform.resize(index+1);
295                 return bound_uniform[index];
296         }
297         else
298                 throw invalid_argument("BufferRange::binding");
299 }
300
301 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
302 {
303         const BufferRange *&ptr = binding(type, index);
304         if(ptr==buf)
305                 return false;
306
307         ptr = buf;
308         return true;
309 }
310
311 unsigned BufferRange::get_n_uniform_buffer_bindings()
312 {
313         return Limits::get_global().max_uniform_bindings;
314 }
315
316 unsigned BufferRange::get_uniform_buffer_alignment()
317 {
318         return Limits::get_global().uniform_buffer_alignment;
319 }
320
321 } // namespace GL
322 } // namespace Msp