]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.h
Use friend declarations to access OpenGL IDs of objects
[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         friend class PipelineState;
29         friend class VertexSetup;
30
31 private:
32         unsigned id;
33         unsigned size;
34
35         static Buffer *scratch_binding;
36
37 public:
38         Buffer();
39         ~Buffer();
40
41         /** Returns the OpenGL ID of the buffer.  For internal use only. */
42         unsigned get_id() const { return id; }
43
44         /** Defines the storage size of the buffer.  Must be called before data can
45         be uploaded.  Storage cannot be changed once set. */
46         void storage(unsigned);
47
48         /** Uploads data into the buffer, completely replacing any previous
49         contents.  Storage must be defined beforehand.  The data must have size
50         matching the defined storage. */
51         void data(const void *);
52
53         /** Overwrites part of the buffer data with new data.  Storage must be
54         defined beforehand. */
55         void sub_data(unsigned, unsigned, const void *);
56
57         unsigned get_size() const { return size; }
58
59         void require_size(unsigned) const;
60
61         void *map();
62         bool unmap();
63
64         void set_debug_name(const std::string &);
65
66 private:
67         void bind_scratch();
68 public:
69         static void unbind_scratch();
70 };
71
72 } // namespace GL
73 } // namespace Msp
74
75 #endif