]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.h
Remove the separate allocation step from textures and buffers
[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 "gl.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class buffer_too_small: public std::logic_error
13 {
14 public:
15         buffer_too_small(const std::string &w): std::logic_error(w) { }
16         virtual ~buffer_too_small() throw() { }
17 };
18
19 class BufferRange;
20
21 /**
22 A buffer for storing data in GL memory.  Putting vertex and index data in
23 buffers can improve rendering performance.  The VertexArray, Mesh and
24 UniformBlock classes contain built-in support for buffers.
25 */
26 class Buffer
27 {
28 private:
29         unsigned id;
30         unsigned size;
31
32         static Buffer *scratch_binding;
33
34 public:
35         Buffer();
36         ~Buffer();
37
38         /** Returns the OpenGL ID of the buffer.  For internal use only. */
39         unsigned get_id() const { return id; }
40
41         /** Defines the storage size of the buffer.  Must be called before data can
42         be uploaded.  Storage cannot be changed once set. */
43         void storage(unsigned);
44
45         /** Uploads data into the buffer, completely replacing any previous
46         contents.  Storage must be defined beforehand.  The data must have size
47         matching the defined storage. */
48         void data(const void *);
49
50         /** Overwrites part of the buffer data with new data.  Storage must be
51         defined beforehand. */
52         void sub_data(unsigned, unsigned, const void *);
53
54         unsigned get_size() const { return size; }
55
56         void require_size(unsigned) const;
57
58         void *map();
59         bool unmap();
60
61         void set_debug_name(const std::string &);
62
63 private:
64         void bind_scratch();
65 public:
66         static void unbind_scratch();
67 };
68
69 } // namespace GL
70 } // namespace Msp
71
72 #endif