]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.h
Unbind things if they are deleted while current
[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 enum BufferAccess
35 {
36         READ_ONLY = GL_READ_ONLY,
37         WRITE_ONLY = GL_WRITE_ONLY,
38         READ_WRITE = GL_READ_WRITE
39 };
40
41 class BufferRange;
42
43 /**
44 A buffer for storing data in GL memory.  Putting vertex and index data in
45 buffers can improve rendering performance.  The VertexArray, Mesh and
46 UniformBlock classes contain built-in support for buffers.
47 */
48 class Buffer
49 {
50         friend class BufferRange;
51
52 private:
53         BufferType type;
54         BufferUsage usage;
55         unsigned id;
56         unsigned size;
57
58         static const Buffer *bound[5];
59
60 public:
61         Buffer(BufferType);
62         ~Buffer();
63
64 private:
65         static void require_buffer_type(BufferType);
66
67 public:
68         /** Returns the OpenGL ID of the buffer.  For internal use only. */
69         unsigned get_id() const { return id; }
70
71         /** Returns the default binding type for the buffer. */
72         BufferType get_type() const { return type; }
73
74         /** Sets the usage hint of the buffer.  It will take effect the next time
75         the buffer's contents are defined. */
76         void set_usage(BufferUsage);
77
78         /** Uploads data into the buffer, completely replacing any previous
79         contents. */
80         void data(unsigned, const void *);
81
82         /** Overwrites part of the buffer data with new data.  The buffer size can
83         not be changed with this call. */
84         void sub_data(unsigned, unsigned, const void *);
85
86         unsigned get_size() const { return size; }
87
88         BufferRange *create_range(unsigned, unsigned);
89
90         void *map(BufferAccess);
91         bool unmap();
92
93         /** Binds the buffer in its default slot. */
94         void bind() const { bind_to(type); }
95
96         /** Binds the buffer in an alternate slot. */
97         void bind_to(BufferType) const;
98
99         /** Unbinds the buffer from its default slot. */
100         void unbind() const { unbind_from(type); }
101
102         static const Buffer *current(BufferType);
103         static void unbind_from(BufferType);
104 private:
105         static const Buffer *&binding(BufferType);
106         static bool set_current(BufferType, const Buffer *);
107 };
108
109
110 /**
111 A proxy for a subset of a buffer.  Can be bound for use with uniform blocks.
112 */
113 class BufferRange
114 {
115 private:
116         Buffer &buffer;
117         unsigned offset;
118         unsigned size;
119
120         static std::vector<const BufferRange *> bound_uniform;
121
122 public:
123         BufferRange(Buffer &, unsigned, unsigned);
124         ~BufferRange();
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         static unsigned get_uniform_buffer_alignment();
139 };
140
141 } // namespace GL
142 } // namespace Msp
143
144 #endif