]> git.tdb.fi Git - libs/gl.git/blob - source/bufferable.h
Further refactor Bufferable's API for derived classes
[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 public:
18         class AsyncUpdater
19         {
20         private:
21                 const Bufferable &bufferable;
22                 char *mapped_address;
23                 bool buffer_resized;
24
25         public:
26                 AsyncUpdater(const Bufferable &);
27                 ~AsyncUpdater();
28
29                 void upload_data();
30         };
31
32 private:
33         Buffer *buffer;
34         unsigned offset;
35         Bufferable *next_in_buffer;
36         Bufferable *prev_in_buffer;
37         mutable bool location_dirty;
38 protected:
39         mutable bool dirty;
40
41         Bufferable();
42 public:
43         virtual ~Bufferable();
44
45         /** Sets the buffer to use.  If prev is not null, it must use the same
46         buffer, and this object is inserted after it. */
47         void use_buffer(Buffer *buf, Bufferable *prev = 0);
48
49         /** Uploads new data into the buffer if necessary. */
50         void refresh() const { if(buffer && dirty) upload_data(0); }
51
52         AsyncUpdater *refresh_async() const;
53
54 private:
55         void unlink_from_buffer();
56
57 public:
58         /** Returns the buffer in which the data is stored. */
59         const Buffer *get_buffer() const { return buffer; }
60
61 protected:
62         /** Returns the amount of data to be stored in the buffer, in bytes. */
63         virtual unsigned get_data_size() const = 0;
64
65         /** Returns a pointer to the start of data in client memory. */
66         virtual const void *get_data_pointer() const = 0;
67
68         /** Returns the alignment required for the data, in bytes.  The offset is
69         guaranteed to be a multiple of this. */
70         virtual unsigned get_alignment() const { return 1; }
71
72         /** Updates the offsets for the chain so that data from different objects
73         does not overlap.  Should be called if either data size or alignment
74         changes. */
75         void update_offset();
76
77         /** Returns the offset where the data should be uploaded. */
78         unsigned get_offset() const { return offset; }
79
80         /** Called when the target buffer or offset within it has changed. */
81         virtual void location_changed(Buffer *, unsigned, unsigned) const { }
82
83 private:
84         bool resize_buffer() const;
85
86         void update_buffer_size() const;
87
88         /** Uploads data to the buffer.  Receives pointer to mapped buffer memory as
89         parameter.  If null, buffer interface should be used instead. */
90         void upload_data(char *) const;
91 };
92
93 } // namespace GL
94 } // namespace Msp
95
96 #endif