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