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