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