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