]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / buffer.cpp
1 #include "arb_vertex_buffer_object.h"
2 #include "extension.h"
3 #include "buffer.h"
4
5 namespace Msp {
6 namespace GL {
7
8 Buffer::Buffer(BufferType t):
9         type(t),
10         usage(STATIC_DRAW)
11 {
12         static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
13         if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
14                 static RequireExtension _req_pbo("GL_ARB_pixel_buffer_object");
15
16         glGenBuffersARB(1, &id);
17 }
18
19 Buffer::~Buffer()
20 {
21         glDeleteBuffersARB(1, &id);
22 }
23
24 void Buffer::set_usage(BufferUsage u)
25 {
26         usage = u;
27 }
28
29 void Buffer::data(unsigned size, const void *d)
30 {
31         const Buffer *old = current(type);
32         bind();
33         glBufferDataARB(type, size, d, usage);
34         restore(old, type);
35 }
36
37 void Buffer::sub_data(unsigned offset, unsigned size, const void *d)
38 {
39         const Buffer *old = current(type);
40         bind();
41         glBufferSubDataARB(type, offset, size, d);
42         restore(old, type);
43 }
44
45 void Buffer::bind_to(BufferType t) const
46 {
47         const Buffer *&ptr = binding(t);
48         if(ptr!=this)
49         {
50                 glBindBufferARB(t, id);
51                 ptr = this;
52         }
53 }
54
55 void Buffer::unbind_from(BufferType type)
56 {
57         const Buffer *&ptr = binding(type);
58         if(ptr)
59         {
60                 glBindBufferARB(type, 0);
61                 ptr = 0;
62         }
63 }
64
65 const Buffer *&Buffer::binding(BufferType type)
66 {
67         switch(type)
68         {
69         case ARRAY_BUFFER:         return bound[0];
70         case ELEMENT_ARRAY_BUFFER: return bound[1];
71         case PIXEL_PACK_BUFFER:    return bound[2];
72         case PIXEL_UNPACK_BUFFER:  return bound[3];
73         default: throw InvalidParameterValue("Invalid buffer type");
74         }
75 }
76
77 void Buffer::restore(const Buffer *buf, BufferType type)
78 {
79         if(buf!=current(type))
80         {
81                 if(buf)
82                         buf->bind_to(type);
83                 else
84                         unbind_from(type);
85         }
86 }
87
88 const Buffer *Buffer::bound[4] = { 0, 0, 0, 0 };
89
90 } // namespace GL
91 } // namespace Msp