X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fbuffer.h;h=9b1293615832a615318cf0212400c08b3af10961;hp=9144afaed3d1682f28efd8076988c8e815a977db;hb=HEAD;hpb=ada4b7614137221b64a00f31fde1498064e9fb19 diff --git a/source/core/buffer.h b/source/core/buffer.h index 9144afae..9b129361 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -3,8 +3,7 @@ #include #include -#include -#include "gl.h" +#include "buffer_backend.h" namespace Msp { namespace GL { @@ -16,54 +15,100 @@ public: virtual ~buffer_too_small() throw() { } }; -class BufferRange; +enum BufferUsage +{ + STATIC, + STREAMING +}; /** -A buffer for storing data in GL memory. Putting vertex and index data in -buffers can improve rendering performance. The VertexArray, Mesh and -UniformBlock classes contain built-in support for buffers. +Stores data in GPU memory. + +Memory must be allocated for the buffer by calling storage(). Contents can +then be modified either synchronously with the data() and sub_data() functions, +or asynchronously by memory-mapping the buffer. + +Buffers can have a static or streaming usage pattern. Streaming buffers can be +memory mapped for less update overhead, but memory space is more limited. If +the buffer is updated only rarely, static is recommended. + +Applications normally don't need to deal with Buffers directly. They're +managed by other classes such as Mesh and ProgramData. */ -class Buffer +class Buffer: public BufferBackend { -private: - unsigned id; - unsigned size; - - static Buffer *scratch_binding; + friend BufferBackend; public: - Buffer(); - ~Buffer(); + /** + An RAII handle for asynchronously writing data into a buffer. + */ + class AsyncTransfer: public NonCopyable + { + friend BufferBackend; + friend class Buffer; + + private: + Buffer *buffer = 0; + std::size_t offset = 0; + std::size_t size = 0; + void *dest_addr = 0; + + AsyncTransfer(Buffer &, std::size_t, std::size_t); + public: + AsyncTransfer() = default; + AsyncTransfer(AsyncTransfer &&); + AsyncTransfer &operator=(AsyncTransfer &&); + ~AsyncTransfer(); + + private: + void allocate(); + void finalize(); + + public: + /** Returns an address for writing the data. It should not be used + beyond the lifetime of the object. */ + void *get_address() { return dest_addr; } + }; - /** Returns the OpenGL ID of the buffer. For internal use only. */ - unsigned get_id() const { return id; } +private: + std::size_t size = 0; + BufferUsage usage = STATIC; + bool mapped = false; - /** Defines the storage size of the buffer. Must be called before data can - be uploaded. Storage cannot be changed once set. */ - void storage(unsigned); +public: + /** Sets storage size and usage pattern and allocates memory for the buffer. + Size and usage cannot be changed once set. */ + void storage(std::size_t, BufferUsage); - /** Uploads data into the buffer, completely replacing any previous - contents. Storage must be defined beforehand. The data must have size - matching the defined storage. */ + /** Replaces contents of the entire buffer. Allocated storage must exist. + The data must have size matching the defined storage. */ void data(const void *); - /** Overwrites part of the buffer data with new data. Storage must be - defined beforehand. */ - void sub_data(unsigned, unsigned, const void *); + /** Replaces a range of bytes in the buffer. Allocated storage must exist. + The range must be fully inside the buffer. */ + void sub_data(std::size_t, std::size_t, const void *); + + /** Creates an asynchronous transfer for writing data to a range of bytes in + the buffer. While the transfer is pending, the state of the buffer region + is indeterminate. */ + AsyncTransfer sub_data_async(std::size_t, std::size_t); + +private: + void check_sub_data(std::size_t, std::size_t, const char *); - unsigned get_size() const { return size; } +public: + std::size_t get_size() const { return size; } + using BufferBackend::get_multiplicity; + std::size_t get_total_size() const { return size*get_multiplicity(); } + BufferUsage get_usage() const { return usage; } - void require_size(unsigned) const; + void require_size(std::size_t) const; void *map(); bool unmap(); - void set_debug_name(const std::string &); - -private: - void bind_scratch(); -public: - static void unbind_scratch(); + using BufferBackend::set_debug_name; }; } // namespace GL