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