]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.h
Use a scratch binding to modify 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 <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         static Buffer *scratch_binding;
61
62 public:
63         Buffer();
64         ~Buffer();
65
66         /** Returns the OpenGL ID of the buffer.  For internal use only. */
67         unsigned get_id() const { return id; }
68
69         /** Defines the storage size of the buffer.  Must be called before data can
70         be uploaded.  Storage cannot be changed once set. */
71         void storage(unsigned);
72
73         /** Allocates storage for the buffer.  The contents are initially undefined.
74         If storage has already been allocated, does nothing. */
75         void allocate();
76
77         /** Sets the usage hint of the buffer.  It will take effect the next time
78         the buffer's contents are defined. */
79         DEPRECATED void set_usage(BufferUsage);
80
81         /** Uploads data into the buffer, completely replacing any previous
82         contents.  Storage must be defined beforehand.  The data must have size
83         matching the defined storage. */
84         void data(const void *);
85
86         DEPRECATED void data(unsigned, const void *);
87
88         /** Overwrites part of the buffer data with new data.  Storage must be
89         defined beforehand. */
90         void sub_data(unsigned, unsigned, const void *);
91
92         unsigned get_size() const { return size; }
93
94         void require_size(unsigned) const;
95
96         void *map();
97         DEPRECATED void *map(BufferAccess) { return map(); }
98         bool unmap();
99
100         void set_debug_name(const std::string &);
101
102 private:
103         void bind_scratch();
104 public:
105         static void unbind_scratch();
106 };
107
108 } // namespace GL
109 } // namespace Msp
110
111 #endif