1 #ifndef MSP_GL_BUFFER_H_
2 #define MSP_GL_BUFFER_H_
7 #include <msp/core/attributes.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>
17 class buffer_too_small: public std::logic_error
20 buffer_too_small(const std::string &w): std::logic_error(w) { }
21 virtual ~buffer_too_small() throw() { }
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
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
48 READ_ONLY = GL_READ_ONLY,
49 WRITE_ONLY = GL_WRITE_ONLY,
50 READ_WRITE = GL_READ_WRITE
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.
62 friend class BufferRange;
70 static const Buffer *bound[5];
77 static void require_buffer_type(BufferType);
80 /** Returns the OpenGL ID of the buffer. For internal use only. */
81 unsigned get_id() const { return id; }
83 /** Returns the default binding type for the buffer. */
84 BufferType get_type() const { return type; }
86 /** Sets the usage hint of the buffer. It will take effect the next time
87 the buffer's contents are defined. */
88 DEPRECATED void set_usage(BufferUsage);
90 /** Uploads data into the buffer, completely replacing any previous
92 void data(unsigned, const void *);
94 /** Overwrites part of the buffer data with new data. The buffer size can
95 not be changed with this call. */
96 void sub_data(unsigned, unsigned, const void *);
98 unsigned get_size() const { return size; }
100 void require_size(unsigned) const;
102 BufferRange *create_range(unsigned, unsigned);
105 DEPRECATED void *map(BufferAccess) { return map(); }
108 /** Binds the buffer in its default slot. */
109 void bind() const { bind_to(type); }
111 /** Binds the buffer in an alternate slot. */
112 void bind_to(BufferType) const;
114 /** Unbinds the buffer from its default slot. */
115 void unbind() const { unbind_from(type); }
117 static const Buffer *current(BufferType);
118 static void unbind_from(BufferType);
120 static const Buffer *&binding(BufferType);
121 static bool set_current(BufferType, const Buffer *);
126 A proxy for a subset of a buffer. Can be bound for use with uniform blocks.
135 static std::vector<const BufferRange *> bound_uniform;
138 BufferRange(Buffer &, unsigned, unsigned);
141 void data(const void *);
143 void bind_to(BufferType, unsigned);
145 static const BufferRange *current(BufferType t, unsigned i) { return binding(t, i); }
146 static void unbind_from(BufferType, unsigned);
148 static const BufferRange *&binding(BufferType, unsigned);
149 static bool set_current(BufferType, unsigned, const BufferRange *);
152 static unsigned get_n_uniform_buffer_bindings();
153 static unsigned get_uniform_buffer_alignment();