]> git.tdb.fi Git - libs/gl.git/blob - source/bufferable.h
Improvements to Bufferable
[libs/gl.git] / source / bufferable.h
1 #ifndef MSP_GL_BUFFERABLE_H_
2 #define MSP_GL_BUFFERABLE_H_
3
4 namespace Msp {
5 namespace GL {
6
7 class Buffer;
8
9 /**
10 Base class for things that can store data in buffers.  Supports buffer sharing.
11 A dirty flag is provided for derived classes.  It should be set when the data
12 in the buffer is considered out of date, and is cleared by Bufferable after
13 uploading fresh data to the buffer.
14 */
15 class Bufferable
16 {
17 private:
18         Buffer *buffer;
19         unsigned offset;
20         Bufferable *next_in_buffer;
21         Bufferable *prev_in_buffer;
22 protected:
23         mutable bool dirty;
24
25 protected:
26         Bufferable();
27 public:
28         virtual ~Bufferable();
29
30         /** Sets the buffer to use.  If prev is not null, it must use the same
31         buffer, and this object is inserted after it. */
32         void use_buffer(Buffer *buf, Bufferable *prev = 0);
33
34 private:
35         void unlink_from_buffer();
36
37 protected:
38         /** Returns the buffer in which the data is stored. */
39         Buffer *get_buffer() const { return buffer; }
40
41         /** Returns the amount of data to be stored in the buffer, in bytes. */
42         virtual unsigned get_data_size() const = 0;
43
44         /** Returns the alignment required for the data, in bytes.  The offset is
45         guaranteed to be a multiple of this. */
46         virtual unsigned get_alignment() const { return 1; }
47
48         /** Updates the offsets for the chain so that data from different objects
49         does not overlap.  Should be called if either data size or alignment
50         changes. */
51         void update_offset();
52
53         /** Returns the offset where the data should be uploaded. */
54         unsigned get_offset() const { return offset; }
55
56         /** Called when the offset for the data has changed. */
57         virtual void offset_changed() { }
58
59         /** Resizes the buffer if necessary and calls upload_data(). */
60         void update_buffer() const;
61
62         /** Uploads data to the buffer. */
63         virtual void upload_data() const = 0;
64 };
65
66 } // namespace GL
67 } // namespace Msp
68
69 #endif