X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbuffer.h;h=6f0a9708e18069485460587d3295b63aaf6055b1;hb=ab83db6f1e31d44ced585119a57fd10896e469cb;hp=5e3cd29a4162c124a3b259f419cd9a558760d3db;hpb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;p=libs%2Fgl.git diff --git a/source/buffer.h b/source/buffer.h index 5e3cd29a..6f0a9708 100644 --- a/source/buffer.h +++ b/source/buffer.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_BUFFER_H_ #define MSP_GL_BUFFER_H_ +#include #include "gl.h" namespace Msp { @@ -11,7 +12,8 @@ enum BufferType ARRAY_BUFFER = GL_ARRAY_BUFFER_ARB, ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER_ARB, PIXEL_PACK_BUFFER = GL_PIXEL_PACK_BUFFER_ARB, - PIXEL_UNPACK_BUFFER = GL_PIXEL_UNPACK_BUFFER_ARB + PIXEL_UNPACK_BUFFER = GL_PIXEL_UNPACK_BUFFER_ARB, + UNIFORM_BUFFER = GL_UNIFORM_BUFFER }; enum BufferUsage @@ -27,26 +29,31 @@ enum BufferUsage DYNAMIC_COPY = GL_DYNAMIC_COPY_ARB }; +class BufferRange; + /** A buffer for storing data in GL memory. Putting vertex and index data in -buffers can improve rendering performance. The VertexArray and Mesh classes -contain built-in support for buffers. +buffers can improve rendering performance. The VertexArray, Mesh and +UniformBlock classes contain built-in support for buffers. */ class Buffer { + friend class BufferRange; + private: BufferType type; BufferUsage usage; unsigned id; + unsigned size; - static const Buffer *bound[4]; + static const Buffer *bound[5]; public: Buffer(BufferType); ~Buffer(); private: - const Buffer *maybe_bind() const; + static void require_buffer_type(BufferType); public: /** Sets the usage hint of the buffer. It will take effect the next time @@ -61,6 +68,10 @@ public: not be changed with this call. */ void sub_data(unsigned, unsigned, const void *); + unsigned get_size() const { return size; } + + BufferRange *create_range(unsigned, unsigned); + /** Binds the buffer in its default slot. */ void bind() const { bind_to(type); } @@ -74,9 +85,11 @@ public: static void unbind_from(BufferType); private: static const Buffer *&binding(BufferType); + static bool set_current(BufferType, const Buffer *); static void restore(const Buffer *, BufferType); }; + /** An adaptor for Buffer to make it compatible with Bind. */ @@ -94,6 +107,36 @@ public: static void unbind() { Buffer::unbind_from(T); } }; + +/** +A proxy for a subset of a buffer. Can be bound for use with uniform blocks. +*/ +class BufferRange +{ +private: + Buffer &buffer; + unsigned offset; + unsigned size; + + static std::vector bound_uniform; + +public: + BufferRange(Buffer &, unsigned, unsigned); + + void data(const void *); + + void bind_to(BufferType, unsigned); + + static const BufferRange *current(BufferType t, unsigned i) { return binding(t, i); } + static void unbind_from(BufferType, unsigned); +private: + static const BufferRange *&binding(BufferType, unsigned); + static bool set_current(BufferType, unsigned, const BufferRange *); + +public: + static unsigned get_n_uniform_buffer_bindings(); +}; + } // namespace GL } // namespace Msp