X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbuffer.h;h=bd3a999eeab0d19da7aea2f94b90abae390f4a74;hb=97cd6e0695f69f3d404e22a6c831aee22d3f0987;hp=7aea9665d8dd9a2ddaf77695891aa7c840a0810c;hpb=85c82f93f4874bcf0b04332c9abb62cd2a5b181b;p=libs%2Fgl.git diff --git a/source/buffer.h b/source/buffer.h index 7aea9665..bd3a999e 100644 --- a/source/buffer.h +++ b/source/buffer.h @@ -1,13 +1,7 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_GL_BUFFER_H_ #define MSP_GL_BUFFER_H_ +#include #include "gl.h" namespace Msp { @@ -18,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 @@ -34,6 +29,8 @@ 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 @@ -41,19 +38,22 @@ 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 @@ -68,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); } @@ -81,9 +85,58 @@ 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. +*/ +template +class BufferAlias +{ +private: + const Buffer &buffer; + +public: + BufferAlias(const Buffer &b): buffer(b) { } + + void bind() const { buffer.bind_to(T); } + static const Buffer *current() { return Buffer::current(T); } + 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