]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.h
Add support for integer vertex attributes
[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 <msp/core/attributes.h>
8 #include "gl.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>
13
14 namespace Msp {
15 namespace GL {
16
17 class buffer_too_small: public std::logic_error
18 {
19 public:
20         buffer_too_small(const std::string &w): std::logic_error(w) { }
21         virtual ~buffer_too_small() throw() { }
22 };
23
24 enum BufferUsage
25 {
26         STREAM_DRAW  = GL_STREAM_DRAW,
27         STREAM_READ  = GL_STREAM_READ,
28         STREAM_COPY  = GL_STREAM_COPY,
29         STATIC_DRAW  = GL_STATIC_DRAW,
30         STATIC_READ  = GL_STATIC_READ,
31         STATIC_COPY  = GL_STATIC_COPY,
32         DYNAMIC_DRAW = GL_DYNAMIC_DRAW,
33         DYNAMIC_READ = GL_DYNAMIC_READ,
34         DYNAMIC_COPY = GL_DYNAMIC_COPY
35 };
36
37 enum BufferAccess
38 {
39         READ_ONLY = GL_READ_ONLY,
40         WRITE_ONLY = GL_WRITE_ONLY,
41         READ_WRITE = GL_READ_WRITE
42 };
43
44 class BufferRange;
45
46 /**
47 A buffer for storing data in GL memory.  Putting vertex and index data in
48 buffers can improve rendering performance.  The VertexArray, Mesh and
49 UniformBlock classes contain built-in support for buffers.
50 */
51 class Buffer
52 {
53         friend class BufferRange;
54
55 private:
56         unsigned id;
57         unsigned size;
58         bool allocated;
59
60 public:
61         Buffer();
62         ~Buffer();
63
64         /** Returns the OpenGL ID of the buffer.  For internal use only. */
65         unsigned get_id() const { return id; }
66
67         /** Defines the storage size of the buffer.  Must be called before data can
68         be uploaded.  Storage cannot be changed once set. */
69         void storage(unsigned);
70
71         /** Allocates storage for the buffer.  The contents are initially undefined.
72         If storage has already been allocated, does nothing. */
73         void allocate();
74
75         /** Sets the usage hint of the buffer.  It will take effect the next time
76         the buffer's contents are defined. */
77         DEPRECATED void set_usage(BufferUsage);
78
79         /** Uploads data into the buffer, completely replacing any previous
80         contents.  Storage must be defined beforehand.  The data must have size
81         matching the defined storage. */
82         void data(const void *);
83
84         DEPRECATED void data(unsigned, const void *);
85
86         /** Overwrites part of the buffer data with new data.  Storage must be
87         defined beforehand. */
88         void sub_data(unsigned, unsigned, const void *);
89
90         unsigned get_size() const { return size; }
91
92         void require_size(unsigned) const;
93
94         void *map();
95         DEPRECATED void *map(BufferAccess) { return map(); }
96         bool unmap();
97
98         void set_debug_name(const std::string &);
99 };
100
101 } // namespace GL
102 } // namespace Msp
103
104 #endif