1 #ifndef MSP_GL_BUFFERABLE_H_
2 #define MSP_GL_BUFFERABLE_H_
4 #include <msp/core/noncopyable.h>
11 Base class for things that can store data in buffers. Multiple Bufferables
12 may be put in the same buffer.
14 Derived classes should call mark_dirty() when the stored data has changed.
16 class Bufferable: public NonCopyable
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.
27 const Bufferable &bufferable;
28 Buffer::AsyncTransfer transfer;
31 AsyncUpdater(const Bufferable &);
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;
45 Bufferable() = default;
46 Bufferable(Bufferable &&);
48 virtual ~Bufferable();
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.
53 Data is not uploaded immediately, but only when refresh() is called. */
54 void use_buffer(Buffer *, Bufferable *prev = 0);
56 /** Sets the buffer for the entire chain of objects. */
57 void change_buffer(Buffer *);
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;
63 /** Uploads new data into the buffer if necessary. */
64 void refresh(unsigned f) const { if(dirty) upload_data(f, 0); }
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; }
71 void unlink_from_buffer();
74 /** Returns the buffer in which the data is stored. */
75 const Buffer *get_buffer() const { return buffer; }
77 /** Returns the size of the data, in bytes. */
78 virtual std::size_t get_data_size() const = 0;
81 /** Returns a pointer to the start of the data in client memory. */
82 virtual const void *get_data_pointer() const = 0;
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; }
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
93 /* Indicates that the data of the bufferable has changed and should be
94 uploaded to the buffer again. */
98 /** Returns the offset of the data from the beginning of the buffer. */
99 std::size_t get_offset() const { return offset; }
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;
106 AsyncUpdater *create_async_updater() const;