]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/buffer.cpp
Use a scratch binding to modify textures and buffers
[libs/gl.git] / source / core / buffer.cpp
index ecf7b49a15e35d29493c83f1a6ea2f8013065483..78e85af0456b721a27316d9622849f00a2b37c3e 100644 (file)
@@ -15,6 +15,8 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+Buffer *Buffer::scratch_binding = 0;
+
 Buffer::Buffer():
        size(0),
        allocated(false)
@@ -29,6 +31,8 @@ Buffer::Buffer():
 
 Buffer::~Buffer()
 {
+       if(this==scratch_binding)
+               unbind_scratch();
        glDeleteBuffers(1, &id);
 }
 
@@ -60,9 +64,8 @@ void Buffer::allocate()
                        glNamedBufferStorage(id, size, 0, flags);
                else
                {
-                       glBindBuffer(GL_ARRAY_BUFFER, id);
+                       bind_scratch();
                        glBufferStorage(GL_ARRAY_BUFFER, size, 0, flags);
-                       glBindBuffer(GL_ARRAY_BUFFER, 0);
                }
 
                allocated = true;
@@ -87,9 +90,8 @@ void Buffer::data(const void *d)
                glNamedBufferData(id, size, d, STATIC_DRAW);
        else
        {
-               glBindBuffer(GL_ARRAY_BUFFER, id);
+               bind_scratch();
                glBufferData(GL_ARRAY_BUFFER, size, d, STATIC_DRAW);
-               glBindBuffer(GL_ARRAY_BUFFER, 0);
        }
 
        allocated = true;
@@ -116,9 +118,8 @@ void Buffer::sub_data(unsigned off, unsigned sz, const void *d)
                glNamedBufferSubData(id, off, sz, d);
        else
        {
-               glBindBuffer(GL_ARRAY_BUFFER, id);
+               bind_scratch();
                glBufferSubData(GL_ARRAY_BUFFER, off, sz, d);
-               glBindBuffer(GL_ARRAY_BUFFER, 0);
        }
 }
 
@@ -137,9 +138,8 @@ void *Buffer::map()
                return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
        else
        {
-               glBindBuffer(GL_ARRAY_BUFFER, id);
+               bind_scratch();
                void *result = glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
-               glBindBuffer(GL_ARRAY_BUFFER, 0);
                return result;
        }
 }
@@ -151,9 +151,8 @@ bool Buffer::unmap()
                return glUnmapNamedBuffer(id);
        else if(OES_mapbuffer)
        {
-               glBindBuffer(GL_ARRAY_BUFFER, id);
+               bind_scratch();
                bool result = glUnmapBuffer(GL_ARRAY_BUFFER);
-               glBindBuffer(GL_ARRAY_BUFFER, 0);
                return result;
        }
        else
@@ -170,5 +169,23 @@ void Buffer::set_debug_name(const string &name)
 #endif
 }
 
+void Buffer::bind_scratch()
+{
+       if(scratch_binding!=this)
+       {
+               glBindBuffer(GL_ARRAY_BUFFER, id);
+               scratch_binding = this;
+       }
+}
+
+void Buffer::unbind_scratch()
+{
+       if(scratch_binding)
+       {
+               glBindBuffer(GL_ARRAY_BUFFER, 0);
+               scratch_binding = 0;
+       }
+}
+
 } // namespace GL
 } // namespace Msp