]> git.tdb.fi Git - libs/gl.git/blobdiff - source/buffer.cpp
Keep track of buffer size
[libs/gl.git] / source / buffer.cpp
index b4b71c3a0130efadcaea894f0fd921b9e833629b..c19f3727f8f5c685a2ab5abc761a0293c3aa7c85 100644 (file)
@@ -1,20 +1,19 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <stdexcept>
 #include "arb_vertex_buffer_object.h"
 #include "extension.h"
 #include "buffer.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
+const Buffer *Buffer::bound[4] = { 0, 0, 0, 0 };
+
 Buffer::Buffer(BufferType t):
        type(t),
-       usage(STATIC_DRAW)
+       usage(STATIC_DRAW),
+       size(0)
 {
        static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
        if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
@@ -28,42 +27,45 @@ Buffer::~Buffer()
        glDeleteBuffersARB(1, &id);
 }
 
-void Buffer::bind(BufferType t) const
+void Buffer::set_usage(BufferUsage u)
 {
-       glBindBufferARB(t, id);
-       binding(t)=this;
+       usage = u;
 }
 
-void Buffer::maybe_bind() const
+void Buffer::data(unsigned sz, const void *d)
 {
-       if(binding(type)!=this)
-               bind();
+       const Buffer *old = current(type);
+       bind();
+       glBufferDataARB(type, sz, d, usage);
+       size = sz;
+       restore(old, type);
 }
 
-void Buffer::set_usage(BufferUsage u)
+void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
 {
-       usage=u;
+       const Buffer *old = current(type);
+       bind();
+       glBufferSubDataARB(type, off, sz, d);
+       restore(old, type);
 }
 
-void Buffer::data(unsigned size, const void *d)
+void Buffer::bind_to(BufferType t) const
 {
-       maybe_bind();
-       glBufferDataARB(type, size, d, usage);
-}
-
-void Buffer::sub_data(unsigned offset, unsigned size, const void *d)
-{
-       maybe_bind();
-       glBufferSubDataARB(type, offset, size, d);
+       const Buffer *&ptr = binding(t);
+       if(ptr!=this)
+       {
+               glBindBufferARB(t, id);
+               ptr = this;
+       }
 }
 
-void Buffer::unbind(BufferType type)
+void Buffer::unbind_from(BufferType type)
 {
-       const Buffer *&ptr=binding(type);
+       const Buffer *&ptr = binding(type);
        if(ptr)
        {
                glBindBufferARB(type, 0);
-               ptr=0;
+               ptr = 0;
        }
 }
 
@@ -75,11 +77,20 @@ const Buffer *&Buffer::binding(BufferType type)
        case ELEMENT_ARRAY_BUFFER: return bound[1];
        case PIXEL_PACK_BUFFER:    return bound[2];
        case PIXEL_UNPACK_BUFFER:  return bound[3];
-       default: throw InvalidParameterValue("Invalid buffer type");
+       default: throw invalid_argument("Buffer::binding");
        }
 }
 
-const Buffer *Buffer::bound[4]={ 0, 0, 0, 0 };
+void Buffer::restore(const Buffer *buf, BufferType type)
+{
+       if(buf!=current(type))
+       {
+               if(buf)
+                       buf->bind_to(type);
+               else
+                       unbind_from(type);
+       }
+}
 
 } // namespace GL
 } // namespace Msp