]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture.cpp
Introduce a typedef for uniform layout hashes in Program
[libs/gl.git] / source / texture.cpp
index f6b5f8e4be06f6e0706de648d9a8f30da78bff56..0fa65eadaed0f7b31508c6e9e4fa55a2d6ba4cfe 100644 (file)
@@ -2,6 +2,8 @@
 #include <msp/gl/extensions/sgis_generate_mipmap.h>
 #include <msp/strings/format.h>
 #include "error.h"
+#include "resourcemanager.h"
+#include "resources.h"
 #include "texture.h"
 #include "texunit.h"
 
@@ -42,7 +44,8 @@ void operator>>(const LexicalConverter &c, TextureWrap &tw)
 }
 
 
-Texture::Texture(GLenum t):
+Texture::Texture(GLenum t, ResourceManager *m):
+       id(0),
        target(t),
        min_filter(NEAREST_MIPMAP_LINEAR),
        mag_filter(LINEAR),
@@ -54,39 +57,53 @@ Texture::Texture(GLenum t):
        cmp_func(LEQUAL),
        dirty_params(0)
 {
-       glGenTextures(1, &id);
+       if(m)
+               set_manager(m);
+       else
+               glGenTextures(1, &id);
 }
 
 Texture::~Texture()
 {
-       glDeleteTextures(1, &id);
+       while(TexUnit *unit = TexUnit::find_unit(this))
+               unbind_from(unit->get_index());
+
+       if(id)
+               glDeleteTextures(1, &id);
 }
 
 void Texture::update_parameter(int mask) const
 {
-       if(TexUnit::current().get_texture()==this)
+       if(TexUnit::current().get_texture()!=this)
        {
-               if(mask&MIN_FILTER)
-                       glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
-               if(mask&MAG_FILTER)
-                       glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
-               if(mask&MAX_ANISOTROPY)
-                       glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
-               if(mask&WRAP_S)
-                       glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
-               if(mask&WRAP_T)
-                       glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
-               if(mask&WRAP_R)
-                       glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
-               if(mask&GENERATE_MIPMAP)
-                       glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
-               if(mask&COMPARE)
-                       glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
-               if(mask&COMPARE_FUNC)
-                       glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
+               TexUnit *unit = TexUnit::find_unit(this);
+               if(!unit)
+               {
+                       dirty_params |= mask;
+                       return;
+               }
+
+               unit->bind();
        }
-       else
-               dirty_params |= mask;
+
+       if(mask&MIN_FILTER)
+               glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
+       if(mask&MAG_FILTER)
+               glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
+       if(mask&MAX_ANISOTROPY)
+               glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
+       if(mask&WRAP_S)
+               glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
+       if(mask&WRAP_T)
+               glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
+       if(mask&WRAP_R)
+               glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
+       if(mask&GENERATE_MIPMAP)
+               glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
+       if(mask&COMPARE)
+               glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
+       if(mask&COMPARE_FUNC)
+               glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
 }
 
 void Texture::set_min_filter(TextureFilter f)
@@ -168,6 +185,9 @@ void Texture::bind_to(unsigned i) const
        const Texture *cur = unit.get_texture();
        if(unit.set_texture(this))
        {
+               if(manager)
+                       manager->resource_used(*this);
+
                unit.bind();
                if(cur && cur->target!=target)
                        glDisable(cur->target);
@@ -202,8 +222,24 @@ void Texture::unbind_from(unsigned i)
 
 
 Texture::Loader::Loader(Texture &t):
-       DataFile::ObjectLoader<Texture>(t)
+       DataFile::CollectionObjectLoader<Texture>(t, 0)
 {
+       init();
+}
+
+Texture::Loader::Loader(Texture &t, Collection &c):
+       DataFile::CollectionObjectLoader<Texture>(t, &c)
+{
+       init();
+}
+
+void Texture::Loader::init()
+{
+       if(Resources *res = dynamic_cast<Resources *>(coll))
+               srgb = res->get_srgb_conversion();
+       else
+               srgb = false;
+
        add("filter", &Loader::filter);
        add("max_anisotropy", &Loader::max_anisotropy);
        add("generate_mipmap", &Loader::generate_mipmap);