]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.cpp
Present Mesh's index buffer as current while the Mesh is bound
[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         BindRestore _bind(this, type);
49         glBufferData(type, sz, d, usage);
50         size = sz;
51 }
52
53 void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
54 {
55         BindRestore _bind(this, type);
56         glBufferSubData(type, off, sz, d);
57 }
58
59 BufferRange *Buffer::create_range(unsigned s, unsigned o)
60 {
61         return new BufferRange(*this, s, o);
62 }
63
64 void Buffer::bind_to(BufferType t) const
65 {
66         if(t!=type)
67                 require_buffer_type(t);
68         if(t==ELEMENT_ARRAY_BUFFER)
69                 if(const Mesh *m = Mesh::current())
70                 {
71                         // Don't change the binding in a mesh's vertex array object
72                         if(this==m->get_index_buffer())
73                                 return;
74                         throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
75                 }
76         if(set_current(t, this))
77                 glBindBuffer(t, id);
78 }
79
80 const Buffer *Buffer::current(BufferType t)
81 {
82         if(t==ELEMENT_ARRAY_BUFFER)
83                 if(const Mesh *m = Mesh::current())
84                         return m->get_index_buffer();
85         return binding(t);
86 }
87
88 void Buffer::unbind_from(BufferType type)
89 {
90         if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
91                 throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
92         if(set_current(type, 0))
93                 glBindBuffer(type, 0);
94 }
95
96 const Buffer *&Buffer::binding(BufferType type)
97 {
98         switch(type)
99         {
100         case ARRAY_BUFFER:         return bound[0];
101         case ELEMENT_ARRAY_BUFFER: return bound[1];
102         case PIXEL_PACK_BUFFER:    return bound[2];
103         case PIXEL_UNPACK_BUFFER:  return bound[3];
104         case UNIFORM_BUFFER:       return bound[4];
105         default: throw invalid_argument("Buffer::binding");
106         }
107 }
108
109 bool Buffer::set_current(BufferType type, const Buffer *buf)
110 {
111         const Buffer *&ptr = binding(type);
112         if(ptr==buf)
113                 return false;
114
115         ptr = buf;
116         return true;
117 }
118
119
120 vector<const BufferRange *> BufferRange::bound_uniform;
121
122 BufferRange::BufferRange(Buffer &b, unsigned o, unsigned s):
123         buffer(b),
124         offset(o),
125         size(s)
126 {
127         if(o>buffer.get_size() || o+s>buffer.get_size())
128                 throw out_of_range("BufferRange::BufferRange");
129 }
130
131 void BufferRange::data(const void *d)
132 {
133         buffer.sub_data(offset, size, d);
134 }
135
136 void BufferRange::bind_to(BufferType t, unsigned i)
137 {
138         if(t!=buffer.type)
139                 Buffer::require_buffer_type(t);
140         // Intentionally using bitwise | to avoid short-circuiting
141         if(Buffer::set_current(t, &buffer) | set_current(t, i, this))
142                 glBindBufferRange(t, i, buffer.id, offset, size);
143 }
144
145 void BufferRange::unbind_from(BufferType t, unsigned i)
146 {
147         if(set_current(t, i, 0))
148         {
149                 Buffer::set_current(t, 0);
150                 glBindBufferBase(t, i, 0);
151         }
152 }
153
154 const BufferRange *&BufferRange::binding(BufferType type, unsigned index)
155 {
156         if(type==UNIFORM_BUFFER)
157         {
158                 if(index>=get_n_uniform_buffer_bindings())
159                         throw out_of_range("BufferRange::binding");
160                 if(bound_uniform.size()<=index)
161                         bound_uniform.resize(index+1);
162                 return bound_uniform[index];
163         }
164         else
165                 throw invalid_argument("BufferRange::binding");
166 }
167
168 bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange *buf)
169 {
170         const BufferRange *&ptr = binding(type, index);
171         if(ptr==buf)
172                 return false;
173
174         ptr = buf;
175         return true;
176 }
177
178 unsigned BufferRange::get_n_uniform_buffer_bindings()
179 {
180         static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS);
181         return count;
182 }
183
184 unsigned BufferRange::get_uniform_buffer_alignment()
185 {
186         static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
187         return align;
188 }
189
190 } // namespace GL
191 } // namespace Msp