]> git.tdb.fi Git - libs/gl.git/commitdiff
Improve binding of buffers
authorMikko Rasa <tdb@tdb.fi>
Fri, 24 Aug 2012 07:25:52 +0000 (10:25 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 24 Aug 2012 07:25:52 +0000 (10:25 +0300)
Add a set_current function, as seen in some other classes.

Buffer type support is now also checked in bind_to.

source/buffer.cpp
source/buffer.h

index c19f3727f8f5c685a2ab5abc761a0293c3aa7c85..1762b20482eaa45944d94529b7c044aa25c898b5 100644 (file)
@@ -15,9 +15,7 @@ Buffer::Buffer(BufferType t):
        usage(STATIC_DRAW),
        size(0)
 {
-       static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
-       if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
-               static RequireExtension _req_pbo("GL_ARB_pixel_buffer_object");
+       require_buffer_type(type);
 
        glGenBuffersARB(1, &id);
 }
@@ -27,6 +25,13 @@ Buffer::~Buffer()
        glDeleteBuffersARB(1, &id);
 }
 
+void Buffer::require_buffer_type(BufferType type)
+{
+       static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
+       if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
+               static RequireExtension _req_pbo("GL_ARB_pixel_buffer_object");
+}
+
 void Buffer::set_usage(BufferUsage u)
 {
        usage = u;
@@ -51,22 +56,16 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
 
 void Buffer::bind_to(BufferType t) const
 {
-       const Buffer *&ptr = binding(t);
-       if(ptr!=this)
-       {
+       if(t!=type)
+               require_buffer_type(t);
+       if(set_current(t, this))
                glBindBufferARB(t, id);
-               ptr = this;
-       }
 }
 
 void Buffer::unbind_from(BufferType type)
 {
-       const Buffer *&ptr = binding(type);
-       if(ptr)
-       {
+       if(set_current(type, 0))
                glBindBufferARB(type, 0);
-               ptr = 0;
-       }
 }
 
 const Buffer *&Buffer::binding(BufferType type)
@@ -81,6 +80,16 @@ const Buffer *&Buffer::binding(BufferType type)
        }
 }
 
+bool Buffer::set_current(BufferType type, const Buffer *buf)
+{
+       const Buffer *&ptr = binding(type);
+       if(ptr==buf)
+               return false;
+
+       ptr = buf;
+       return true;
+}
+
 void Buffer::restore(const Buffer *buf, BufferType type)
 {
        if(buf!=current(type))
index d8a8419c0211fbc9b70429a95bcb7cc7c5065145..117f4ea6a1f5d0a699192ebdaffae808e400c68e 100644 (file)
@@ -47,7 +47,7 @@ public:
        ~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
@@ -77,6 +77,7 @@ 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);
 };