]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.h
Keep track of buffer size
[libs/gl.git] / source / buffer.h
1 #ifndef MSP_GL_BUFFER_H_
2 #define MSP_GL_BUFFER_H_
3
4 #include "gl.h"
5
6 namespace Msp {
7 namespace GL {
8
9 enum BufferType
10 {
11         ARRAY_BUFFER         = GL_ARRAY_BUFFER_ARB,
12         ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER_ARB,
13         PIXEL_PACK_BUFFER    = GL_PIXEL_PACK_BUFFER_ARB,
14         PIXEL_UNPACK_BUFFER  = GL_PIXEL_UNPACK_BUFFER_ARB
15 };
16
17 enum BufferUsage
18 {
19         STREAM_DRAW  = GL_STREAM_DRAW_ARB,
20         STREAM_READ  = GL_STREAM_READ_ARB,
21         STREAM_COPY  = GL_STREAM_COPY_ARB,
22         STATIC_DRAW  = GL_STATIC_DRAW_ARB,
23         STATIC_READ  = GL_STATIC_READ_ARB,
24         STATIC_COPY  = GL_STATIC_COPY_ARB,
25         DYNAMIC_DRAW = GL_DYNAMIC_DRAW_ARB,
26         DYNAMIC_READ = GL_DYNAMIC_READ_ARB,
27         DYNAMIC_COPY = GL_DYNAMIC_COPY_ARB
28 };
29
30 /**
31 A buffer for storing data in GL memory.  Putting vertex and index data in
32 buffers can improve rendering performance.  The VertexArray and Mesh classes
33 contain built-in support for buffers.
34 */
35 class Buffer
36 {
37 private:
38         BufferType type;
39         BufferUsage usage;
40         unsigned id;
41         unsigned size;
42
43         static const Buffer *bound[4];
44
45 public:
46         Buffer(BufferType);
47         ~Buffer();
48
49 private:
50         const Buffer *maybe_bind() const;
51
52 public:
53         /** Sets the usage hint of the buffer.  It will take effect the next time
54         the buffer's contents are defined. */
55         void set_usage(BufferUsage);
56
57         /** Uploads data into the buffer, completely replacing any previous
58         contents. */
59         void data(unsigned, const void *);
60
61         /** Overwrites part of the buffer data with new data.  The buffer size can
62         not be changed with this call. */
63         void sub_data(unsigned, unsigned, const void *);
64
65         unsigned get_size() const { return size; }
66
67         /** Binds the buffer in its default slot. */
68         void bind() const { bind_to(type); }
69
70         /** Binds the buffer in an alternate slot. */
71         void bind_to(BufferType) const;
72
73         /** Unbinds the buffer from its default slot. */
74         void unbind() const { unbind_from(type); }
75
76         static const Buffer *current(BufferType t) { return binding(t); }
77         static void unbind_from(BufferType);
78 private:
79         static const Buffer *&binding(BufferType);
80         static void restore(const Buffer *, BufferType);
81 };
82
83 /**
84 An adaptor for Buffer to make it compatible with Bind.
85 */
86 template<BufferType T>
87 class BufferAlias
88 {
89 private:
90         const Buffer &buffer;
91
92 public:
93         BufferAlias(const Buffer &b): buffer(b) { }
94
95         void bind() const { buffer.bind_to(T); }
96         static const Buffer *current() { return Buffer::current(T); }
97         static void unbind() { Buffer::unbind_from(T); }
98 };
99
100 } // namespace GL
101 } // namespace Msp
102
103 #endif