]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/texture.cpp
Use a scratch binding to modify textures and buffers
[libs/gl.git] / source / core / texture.cpp
index 5197b3da4bd7461564305e8d9f7ffa7e8312a794..b40046705b32bdfbecd459b56ce5e9b7c81ae4d9 100644 (file)
@@ -3,7 +3,6 @@
 #include <msp/gl/extensions/ext_framebuffer_object.h>
 #include <msp/gl/extensions/khr_debug.h>
 #include <msp/io/memory.h>
-#include "bindable.h"
 #include "error.h"
 #include "resourcemanager.h"
 #include "resources.h"
@@ -22,6 +21,8 @@ int Texture::swizzle_orders[] =
        GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
 };
 
+Texture *Texture::scratch_binding = 0;
+
 Texture::Texture(GLenum t, ResourceManager *m):
        id(0),
        target(t),
@@ -35,10 +36,19 @@ Texture::Texture(GLenum t, ResourceManager *m):
                set_manager(m);
        else
                generate_id();
+
+       static bool alignment_init = false;
+       if(!alignment_init)
+       {
+               glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+               alignment_init = true;
+       }
 }
 
 Texture::~Texture()
 {
+       if(this==scratch_binding)
+               unbind_scratch();
        if(id)
                glDeleteTextures(1, &id);
 }
@@ -133,22 +143,13 @@ void Texture::generate_mipmap()
        // glGenerateMipmap is defined here
        static Require _req(EXT_framebuffer_object);
 
-       if(!ARB_direct_state_access)
-       {
-               glActiveTexture(GL_TEXTURE0);
-               glBindTexture(target, id);
-       }
-       generate_mipmap_();
-       if(!ARB_direct_state_access)
-               glBindTexture(target, 0);
-}
-
-void Texture::generate_mipmap_()
-{
        if(ARB_direct_state_access)
                glGenerateTextureMipmap(id);
        else
+       {
+               bind_scratch();
                glGenerateMipmap(target);
+       }
 }
 
 void Texture::set_auto_generate_mipmap(bool gm)
@@ -188,6 +189,28 @@ void Texture::set_debug_name(const string &name)
 #endif
 }
 
+void Texture::bind_scratch()
+{
+       if(!scratch_binding)
+               glActiveTexture(GL_TEXTURE0);
+       if(scratch_binding!=this)
+       {
+               if(scratch_binding && scratch_binding->target!=target)
+                       glBindTexture(scratch_binding->target, 0);
+               glBindTexture(target, id);
+               scratch_binding = this;
+       }
+}
+
+void Texture::unbind_scratch()
+{
+       if(scratch_binding)
+       {
+               glBindTexture(scratch_binding->target, 0);
+               scratch_binding = 0;
+       }
+}
+
 
 Texture::Loader::Loader(Texture &t):
        DataFile::CollectionObjectLoader<Texture>(t, 0)