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