]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/texture.cpp
Mark constant data as const
[libs/gl.git] / source / core / texture.cpp
index ea503ed1f9d4414f28f200c89be286fdc49f6190..e607792c9dc6f50103c2bd30d195343d107976b3 100644 (file)
@@ -3,19 +3,17 @@
 #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"
 #include "texture.h"
-#include "texunit.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-int Texture::swizzle_orders[] =
+const int Texture::swizzle_orders[] =
 {
        GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
        GL_RED, GL_RED, GL_RED, GL_ONE,
@@ -23,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),
@@ -36,13 +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()
 {
-       while(TexUnit *unit = TexUnit::find_unit(this))
-               unbind_from(unit->get_index());
-
+       if(this==scratch_binding)
+               unbind_scratch();
        if(id)
                glDeleteTextures(1, &id);
 }
@@ -127,11 +133,6 @@ void Texture::set_parameter_i(GLenum param, int value) const
                glTexParameteri(target, param, value);
 }
 
-bool Texture::can_generate_mipmap()
-{
-       return EXT_framebuffer_object;
-}
-
 void Texture::generate_mipmap()
 {
        // glGenerateMipmap is defined here
@@ -141,24 +142,11 @@ void Texture::generate_mipmap()
                glGenerateTextureMipmap(id);
        else
        {
-               BindRestore _bind(this);
+               bind_scratch();
                glGenerateMipmap(target);
        }
 }
 
-void Texture::set_auto_generate_mipmap(bool gm)
-{
-       if(gm)
-               static Require _req(EXT_framebuffer_object);
-
-       auto_gen_mipmap = gm;
-}
-
-void Texture::load_image(const string &fn, bool)
-{
-       load_image(fn, 0U);
-}
-
 void Texture::load_image(const string &fn, unsigned lv)
 {
        Graphics::Image img;
@@ -167,72 +155,39 @@ void Texture::load_image(const string &fn, unsigned lv)
        image(img, lv);
 }
 
-void Texture::image(const Graphics::Image &img, bool)
+void Texture::set_debug_name(const string &name)
 {
-       image(img, 0U);
+#ifdef DEBUG
+       debug_name = name;
+       if(id && KHR_debug)
+               glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
 }
 
-void Texture::bind_to(unsigned i) const
+void Texture::bind_scratch()
 {
-       if(!id)
-       {
-               if(manager)
-                       manager->resource_used(*this);
-               if(!id)
-               {
-                       unbind_from(i);
-                       return;
-               }
-       }
-
-       TexUnit &unit = TexUnit::get_unit(i);
-       if(unit.set_texture(this))
+       if(!scratch_binding)
+               glActiveTexture(GL_TEXTURE0);
+       if(scratch_binding!=this)
        {
-               if(manager)
-                       manager->resource_used(*this);
-
-               if(ARB_direct_state_access)
-                       glBindTextureUnit(i, id);
-               else
-               {
-                       unit.bind();
-                       glBindTexture(target, id);
-               }
+               if(scratch_binding && scratch_binding->target!=target)
+                       glBindTexture(scratch_binding->target, 0);
+               glBindTexture(target, id);
+               scratch_binding = this;
        }
 }
 
-const Texture *Texture::current(unsigned i)
-{
-       return TexUnit::get_unit(i).get_texture();
-}
-
-void Texture::unbind_from(unsigned i)
+void Texture::unbind_scratch()
 {
-       TexUnit &unit = TexUnit::get_unit(i);
-       const Texture *cur = unit.get_texture();
-       if(unit.set_texture(0))
+       if(scratch_binding)
        {
-               if(ARB_direct_state_access)
-                       glBindTextureUnit(i, 0);
-               else
-               {
-                       unit.bind();
-                       glBindTexture(cur->target, 0);
-               }
+               glBindTexture(scratch_binding->target, 0);
+               scratch_binding = 0;
        }
 }
 
-void Texture::set_debug_name(const string &name)
-{
-#ifdef DEBUG
-       debug_name = name;
-       if(id && KHR_debug)
-               glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
-#else
-       (void)name;
-#endif
-}
-
 
 Texture::Loader::Loader(Texture &t):
        DataFile::CollectionObjectLoader<Texture>(t, 0)
@@ -257,6 +212,12 @@ void Texture::Loader::init()
        add("mipmap_levels", &Loader::mipmap_levels);
 }
 
+void Texture::Loader::finish()
+{
+       if(obj.auto_gen_mipmap)
+               obj.generate_mipmap();
+}
+
 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
 {
        RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
@@ -291,7 +252,7 @@ void Texture::Loader::external_image_common(const string &fn)
 
 void Texture::Loader::generate_mipmap(bool gm)
 {
-       obj.set_auto_generate_mipmap(gm);
+       obj.auto_gen_mipmap = gm;
 }
 
 void Texture::Loader::image_data(const string &data)