]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.h
Add some accessors to Buffer
[libs/gl.git] / source / buffer.h
1 #ifndef MSP_GL_BUFFER_H_
2 #define MSP_GL_BUFFER_H_
3
4 #include <vector>
5 #include "gl.h"
6 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
7 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
8
9 namespace Msp {
10 namespace GL {
11
12 enum BufferType
13 {
14         ARRAY_BUFFER         = GL_ARRAY_BUFFER,
15         ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER,
16         PIXEL_PACK_BUFFER    = GL_PIXEL_PACK_BUFFER,
17         PIXEL_UNPACK_BUFFER  = GL_PIXEL_UNPACK_BUFFER,
18         UNIFORM_BUFFER       = GL_UNIFORM_BUFFER
19 };
20
21 enum BufferUsage
22 {
23         STREAM_DRAW  = GL_STREAM_DRAW,
24         STREAM_READ  = GL_STREAM_READ,
25         STREAM_COPY  = GL_STREAM_COPY,
26         STATIC_DRAW  = GL_STATIC_DRAW,
27         STATIC_READ  = GL_STATIC_READ,
28         STATIC_COPY  = GL_STATIC_COPY,
29         DYNAMIC_DRAW = GL_DYNAMIC_DRAW,
30         DYNAMIC_READ = GL_DYNAMIC_READ,
31         DYNAMIC_COPY = GL_DYNAMIC_COPY
32 };
33
34 class BufferRange;
35
36 /**
37 A buffer for storing data in GL memory.  Putting vertex and index data in
38 buffers can improve rendering performance.  The VertexArray, Mesh and
39 UniformBlock classes contain built-in support for buffers.
40 */
41 class Buffer
42 {
43         friend class BufferRange;
44
45 private:
46         BufferType type;
47         BufferUsage usage;
48         unsigned id;
49         unsigned size;
50
51         static const Buffer *bound[5];
52
53 public:
54         Buffer(BufferType);
55         ~Buffer();
56
57 private:
58         static void require_buffer_type(BufferType);
59
60 public:
61         /** Returns the OpenGL ID of the buffer.  For internal use only. */
62         unsigned get_id() const { return id; }
63
64         /** Returns the default binding type for the buffer. */
65         BufferType get_type() const { return type; }
66
67         /** Sets the usage hint of the buffer.  It will take effect the next time
68         the buffer's contents are defined. */
69         void set_usage(BufferUsage);
70
71         /** Uploads data into the buffer, completely replacing any previous
72         contents. */
73         void data(unsigned, const void *);
74
75         /** Overwrites part of the buffer data with new data.  The buffer size can
76         not be changed with this call. */
77         void sub_data(unsigned, unsigned, const void *);
78
79         unsigned get_size() const { return size; }
80
81         BufferRange *create_range(unsigned, unsigned);
82
83         /** Binds the buffer in its default slot. */
84         void bind() const { bind_to(type); }
85
86         /** Binds the buffer in an alternate slot. */
87         void bind_to(BufferType) const;
88
89         /** Unbinds the buffer from its default slot. */
90         void unbind() const { unbind_from(type); }
91
92         static const Buffer *current(BufferType t) { return binding(t); }
93         static void unbind_from(BufferType);
94 private:
95         static const Buffer *&binding(BufferType);
96         static bool set_current(BufferType, const Buffer *);
97         static void restore(const Buffer *, BufferType);
98 };
99
100
101 /**
102 An adaptor for Buffer to make it compatible with Bind.
103 */
104 template<BufferType T>
105 class BufferAlias
106 {
107 private:
108         const Buffer &buffer;
109
110 public:
111         BufferAlias(const Buffer &b): buffer(b) { }
112
113         void bind() const { buffer.bind_to(T); }
114         static const Buffer *current() { return Buffer::current(T); }
115         static void unbind() { Buffer::unbind_from(T); }
116 };
117
118
119 /**
120 A proxy for a subset of a buffer.  Can be bound for use with uniform blocks.
121 */
122 class BufferRange
123 {
124 private:
125         Buffer &buffer;
126         unsigned offset;
127         unsigned size;
128
129         static std::vector<const BufferRange *> bound_uniform;
130
131 public:
132         BufferRange(Buffer &, unsigned, unsigned);
133
134         void data(const void *);
135
136         void bind_to(BufferType, unsigned);
137
138         static const BufferRange *current(BufferType t, unsigned i) { return binding(t, i); }
139         static void unbind_from(BufferType, unsigned);
140 private:
141         static const BufferRange *&binding(BufferType, unsigned);
142         static bool set_current(BufferType, unsigned, const BufferRange *);
143
144 public:
145         static unsigned get_n_uniform_buffer_bindings();
146         static unsigned get_uniform_buffer_alignment();
147 };
148
149 } // namespace GL
150 } // namespace Msp
151
152 #endif