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