]> git.tdb.fi Git - libs/gl.git/blob - source/core/bufferable.h
Make various enums use uint8_t as their underlying type
[libs/gl.git] / source / core / bufferable.h
1 #ifndef MSP_GL_BUFFERABLE_H_
2 #define MSP_GL_BUFFERABLE_H_
3
4 #include <msp/core/noncopyable.h>
5 #include "buffer.h"
6
7 namespace Msp {
8 namespace GL {
9
10 /**
11 Base class for things that can store data in buffers.  Multiple Bufferables
12 may be put in the same buffer.
13
14 Derived classes should call mark_dirty() when the stored data has changed.
15 */
16 class Bufferable: public NonCopyable
17 {
18 public:
19         /**
20         Uploads data to the buffer asynchronously, through a memory mapping.  API
21         calls are done in the constructor and desctructor, so upload_data may be
22         called from a different thread.
23         */
24         class AsyncUpdater
25         {
26         private:
27                 const Bufferable &bufferable;
28                 Buffer::AsyncTransfer transfer;
29
30         public:
31                 AsyncUpdater(const Bufferable &);
32
33                 void upload_data();
34         };
35
36 private:
37         Buffer *buffer = 0;
38         std::size_t offset = 0;
39         Bufferable *next_in_buffer = 0;
40         Bufferable *prev_in_buffer = 0;
41         mutable bool location_dirty = false;
42         mutable uint8_t dirty = 0;
43
44 protected:
45         Bufferable() = default;
46         Bufferable(Bufferable &&);
47 public:
48         virtual ~Bufferable();
49
50         /** Sets the buffer to use.  If prev is not null, it must use the same
51         buffer, and this object is inserted after it.
52
53         Data is not uploaded immediately, but only when refresh() is called. */
54         void use_buffer(Buffer *, Bufferable *prev = 0);
55
56         /** Sets the buffer for the entire chain of objects. */
57         void change_buffer(Buffer *);
58
59         /** Returns the total amount of storage required by this object and others
60         in the same chain, including any padding required by object alignment. */
61         std::size_t get_required_buffer_size(bool = false) const;
62
63         /** Uploads new data into the buffer if necessary. */
64         void refresh(unsigned f) const { if(dirty) upload_data(f, 0); }
65
66         /** Returns an object which can be used to upload data to the buffer using
67         mapped memory.  If data is not dirty, returns null. */
68         AsyncUpdater *refresh_async() const { return dirty ? create_async_updater() : 0; }
69
70 private:
71         void unlink_from_buffer();
72
73 public:
74         /** Returns the buffer in which the data is stored. */
75         const Buffer *get_buffer() const { return buffer; }
76
77         /** Returns the size of the data, in bytes. */
78         virtual std::size_t get_data_size() const = 0;
79
80 protected:
81         /** Returns a pointer to the start of the data in client memory. */
82         virtual const void *get_data_pointer() const = 0;
83
84         /** Returns the alignment required for the data, in bytes.  The offset
85         within the buffer is guaranteed to be a multiple of the alignment. */
86         virtual std::size_t get_alignment() const { return 1; }
87
88         /** Updates the offsets for the chain so that data from different objects
89         does not overlap.  Should be called if either data size or alignment
90         changes. */
91         void update_offset();
92
93         /* Indicates that the data of the bufferable has changed and should be
94         uploaded to the buffer again. */
95         void mark_dirty();
96
97 public:
98         /** Returns the offset of the data from the beginning of the buffer. */
99         std::size_t get_offset() const { return offset; }
100
101 private:
102         /** Uploads data to the buffer.  Receives pointer to mapped buffer memory as
103         parameter, or null to use the buffer upload interface. */
104         void upload_data(unsigned, char *) const;
105
106         AsyncUpdater *create_async_updater() const;
107 };
108
109 } // namespace GL
110 } // namespace Msp
111
112 #endif