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