]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.h
83e75927ae31c3a44cd81420cea8f626a39c605e
[libs/gl.git] / source / core / 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 <msp/core/attributes.h>
8 #include "gl.h"
9 #include <msp/gl/extensions/arb_pixel_buffer_object.h>
10 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
11 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
12 #include <msp/gl/extensions/oes_mapbuffer.h>
13
14 namespace Msp {
15 namespace GL {
16
17 class buffer_too_small: public std::logic_error
18 {
19 public:
20         buffer_too_small(const std::string &w): std::logic_error(w) { }
21         virtual ~buffer_too_small() throw() { }
22 };
23
24 enum BufferType
25 {
26         ARRAY_BUFFER         = GL_ARRAY_BUFFER,
27         ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER,
28         PIXEL_PACK_BUFFER    = GL_PIXEL_PACK_BUFFER,
29         PIXEL_UNPACK_BUFFER  = GL_PIXEL_UNPACK_BUFFER,
30         UNIFORM_BUFFER       = GL_UNIFORM_BUFFER
31 };
32
33 enum BufferUsage
34 {
35         STREAM_DRAW  = GL_STREAM_DRAW,
36         STREAM_READ  = GL_STREAM_READ,
37         STREAM_COPY  = GL_STREAM_COPY,
38         STATIC_DRAW  = GL_STATIC_DRAW,
39         STATIC_READ  = GL_STATIC_READ,
40         STATIC_COPY  = GL_STATIC_COPY,
41         DYNAMIC_DRAW = GL_DYNAMIC_DRAW,
42         DYNAMIC_READ = GL_DYNAMIC_READ,
43         DYNAMIC_COPY = GL_DYNAMIC_COPY
44 };
45
46 enum BufferAccess
47 {
48         READ_ONLY = GL_READ_ONLY,
49         WRITE_ONLY = GL_WRITE_ONLY,
50         READ_WRITE = GL_READ_WRITE
51 };
52
53 class BufferRange;
54
55 /**
56 A buffer for storing data in GL memory.  Putting vertex and index data in
57 buffers can improve rendering performance.  The VertexArray, Mesh and
58 UniformBlock classes contain built-in support for buffers.
59 */
60 class Buffer
61 {
62         friend class BufferRange;
63
64 private:
65         unsigned id;
66         unsigned size;
67         bool allocated;
68
69         static const Buffer *bound[5];
70
71 public:
72         Buffer();
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         /** Defines the storage size of the buffer.  Must be called before data can
83         be uploaded.  Storage cannot be changed once set. */
84         void storage(unsigned);
85
86         /** Allocates storage for the buffer.  The contents are initially undefined.
87         If storage has already been allocated, does nothing. */
88         void allocate();
89
90         /** Sets the usage hint of the buffer.  It will take effect the next time
91         the buffer's contents are defined. */
92         DEPRECATED void set_usage(BufferUsage);
93
94         /** Uploads data into the buffer, completely replacing any previous
95         contents.  Storage must be defined beforehand.  The data must have size
96         matching the defined storage. */
97         void data(const void *);
98
99         DEPRECATED void data(unsigned, const void *);
100
101         /** Overwrites part of the buffer data with new data.  Storage must be
102         defined beforehand. */
103         void sub_data(unsigned, unsigned, const void *);
104
105         unsigned get_size() const { return size; }
106
107         void require_size(unsigned) const;
108
109         BufferRange *create_range(unsigned, unsigned);
110
111         void *map();
112         DEPRECATED void *map(BufferAccess) { return map(); }
113         bool unmap();
114
115         /** Binds the buffer. */
116         void bind_to(BufferType) const;
117
118         static const Buffer *current(BufferType);
119         static void unbind_from(BufferType);
120 private:
121         static const Buffer *&binding(BufferType);
122         static bool set_current(BufferType, const Buffer *);
123
124 public:
125         void set_debug_name(const std::string &);
126 };
127
128
129 /**
130 A proxy for a subset of a buffer.  Can be bound for use with uniform blocks.
131 */
132 class BufferRange
133 {
134 private:
135         Buffer &buffer;
136         unsigned offset;
137         unsigned size;
138
139         static std::vector<const BufferRange *> bound_uniform;
140
141 public:
142         BufferRange(Buffer &, unsigned, unsigned);
143         ~BufferRange();
144
145         void data(const void *);
146
147         void bind_to(BufferType, unsigned);
148
149         static const BufferRange *current(BufferType t, unsigned i) { return binding(t, i); }
150         static void unbind_from(BufferType, unsigned);
151 private:
152         static const BufferRange *&binding(BufferType, unsigned);
153         static bool set_current(BufferType, unsigned, const BufferRange *);
154
155 public:
156         DEPRECATED static unsigned get_n_uniform_buffer_bindings();
157         DEPRECATED static unsigned get_uniform_buffer_alignment();
158 };
159
160 } // namespace GL
161 } // namespace Msp
162
163 #endif