1 #ifndef MSP_GL_BUFFER_H_
2 #define MSP_GL_BUFFER_H_
6 #include "buffer_backend.h"
11 class buffer_too_small: public std::logic_error
14 buffer_too_small(const std::string &w): std::logic_error(w) { }
15 virtual ~buffer_too_small() throw() { }
25 Stores data in GPU memory.
27 Memory must be allocated for the buffer by calling storage(). Contents can
28 then be modified either synchronously with the data() and sub_data() functions,
29 or asynchronously by memory-mapping the buffer.
31 Buffers can have a static or streaming usage pattern. Streaming buffers can be
32 memory mapped for less update overhead, but memory space is more limited. If
33 the buffer is updated only rarely, static is recommended.
35 Applications normally don't need to deal with Buffers directly. They're
36 managed by other classes such as Mesh and ProgramData.
38 class Buffer: public BufferBackend
44 An RAII handle for asynchronously writing data into a buffer.
46 class AsyncTransfer: public NonCopyable
53 std::size_t offset = 0;
57 AsyncTransfer(Buffer &, std::size_t, std::size_t);
59 AsyncTransfer(AsyncTransfer &&);
67 /** Returns an address for writing the data. It should not be used
68 beyond the lifetime of the object. */
69 void *get_address() { return dest_addr; }
74 BufferUsage usage = STATIC;
78 /** Sets storage size and usage pattern and allocates memory for the buffer.
79 Size and usage cannot be changed once set. */
80 void storage(std::size_t, BufferUsage);
82 /** Replaces contents of the entire buffer. Allocated storage must exist.
83 The data must have size matching the defined storage. */
84 void data(const void *);
86 /** Replaces a range of bytes in the buffer. Allocated storage must exist.
87 The range must be fully inside the buffer. */
88 void sub_data(std::size_t, std::size_t, const void *);
90 /** Creates an asynchronous transfer for writing data to a range of bytes in
91 the buffer. While the transfer is pending, the state of the buffer region
93 AsyncTransfer sub_data_async(std::size_t, std::size_t);
96 void check_sub_data(std::size_t, std::size_t, const char *);
99 std::size_t get_size() const { return size; }
100 using BufferBackend::get_multiplicity;
101 std::size_t get_total_size() const { return size*get_multiplicity(); }
102 BufferUsage get_usage() const { return usage; }
104 void require_size(std::size_t) const;
109 using BufferBackend::set_debug_name;