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