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