]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture.cpp
Don't try to set parameters for textures which aren't loaded yet
[libs/gl.git] / source / texture.cpp
index 719d4a56ac029218cbd176ac076644865fc20c72..68e4486120cb9cecdc919142727eb5f881a8d8a6 100644 (file)
@@ -4,9 +4,9 @@
 #include <msp/gl/extensions/ext_framebuffer_object.h>
 #include <msp/gl/extensions/ext_texture3d.h>
 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
-#include <msp/gl/extensions/sgis_generate_mipmap.h>
 #include <msp/io/memory.h>
 #include <msp/strings/format.h>
+#include "bindable.h"
 #include "error.h"
 #include "resourcemanager.h"
 #include "resources.h"
@@ -68,7 +68,7 @@ Texture::Texture(GLenum t, ResourceManager *m):
        wrap_s(REPEAT),
        wrap_t(REPEAT),
        wrap_r(REPEAT),
-       gen_mipmap(false),
+       auto_gen_mipmap(false),
        compare(false),
        cmp_func(LEQUAL),
        dirty_params(0)
@@ -97,24 +97,24 @@ DataType Texture::get_alloc_type(PixelFormat fmt)
 
 void Texture::set_internal_format(PixelFormat fmt)
 {
-       if(!get_component_size(fmt) && OES_required_internalformat)
-               fmt = get_default_sized_pixelformat(fmt);
-
        FormatSwizzle swiz = NO_SWIZZLE;
        if(ARB_texture_rg && ARB_texture_swizzle)
        {
-               if(fmt==LUMINANCE8 || fmt==SLUMINANCE8)
+               if(fmt==LUMINANCE)
                {
-                       fmt = R8;
+                       fmt = RED;
                        swiz = R_TO_LUMINANCE;
                }
-               else if(fmt==LUMINANCE8_ALPHA8 || fmt==SLUMINANCE8_ALPHA8)
+               else if(fmt==LUMINANCE_ALPHA)
                {
-                       fmt = RG8;
+                       fmt = RG;
                        swiz = RG_TO_LUMINANCE_ALPHA;
                }
        }
 
+       if(!get_component_size(fmt) && OES_required_internalformat)
+               fmt = get_default_sized_pixelformat(fmt);
+
        require_pixelformat(fmt);
        ifmt = fmt;
        swizzle = swiz;
@@ -132,6 +132,12 @@ PixelFormat Texture::get_upload_format(PixelFormat fmt) const
 
 void Texture::update_parameter(int mask) const
 {
+       if(!id)
+       {
+               dirty_params |= mask;
+               return;
+       }
+
        if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
        {
                TexUnit *unit = TexUnit::find_unit(this);
@@ -156,8 +162,6 @@ void Texture::update_parameter(int mask) const
                set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
        if(mask&WRAP_R)
                set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
-       if(mask&GENERATE_MIPMAP)
-               set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
        if(mask&COMPARE)
                set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
        if(mask&COMPARE_FUNC)
@@ -263,30 +267,31 @@ void Texture::set_wrap_r(TextureWrap w)
 
 bool Texture::can_generate_mipmap()
 {
-       return (EXT_framebuffer_object || SGIS_generate_mipmap);
+       return EXT_framebuffer_object;
 }
 
-void Texture::set_generate_mipmap(bool gm)
-{
-       if(gm && !EXT_framebuffer_object)
-               static Require _req(SGIS_generate_mipmap);
-       gen_mipmap = gm;
-       if(!EXT_framebuffer_object)
-               update_parameter(GENERATE_MIPMAP);
-}
-
-void Texture::auto_generate_mipmap()
+void Texture::generate_mipmap()
 {
        // glGenerateMipmap is defined here
-       if(EXT_framebuffer_object)
+       static Require _req(EXT_framebuffer_object);
+
+       if(ARB_direct_state_access)
+               glGenerateTextureMipmap(id);
+       else
        {
-               if(ARB_direct_state_access)
-                       glGenerateTextureMipmap(id);
-               else
-                       glGenerateMipmap(target);
+               BindRestore _bind(this);
+               glGenerateMipmap(target);
        }
 }
 
+void Texture::set_auto_generate_mipmap(bool gm)
+{
+       if(gm)
+               static Require _req(EXT_framebuffer_object);
+
+       auto_gen_mipmap = gm;
+}
+
 void Texture::set_compare_enabled(bool c)
 {
        if(c)
@@ -310,7 +315,7 @@ void Texture::load_image(const string &fn, bool srgb)
        image(img, srgb);
 }
 
-void Texture::bind_to(unsigned i, bool legacy) const
+void Texture::bind_to(unsigned i) const
 {
        if(!id)
        {
@@ -323,28 +328,17 @@ void Texture::bind_to(unsigned i, bool legacy) const
                }
        }
 
-       legacy = (legacy && is_legacy_target(target));
-
        TexUnit &unit = TexUnit::get_unit(i);
-       const Texture *old = unit.get_texture();
-       bool old_legacy = unit.get_texture_legacy();
-       if(unit.set_texture(this, legacy))
+       if(unit.set_texture(this))
        {
                if(manager)
                        manager->resource_used(*this);
 
-               if(ARB_direct_state_access && !old_legacy && (!unit.supports_legacy() || !legacy))
+               if(ARB_direct_state_access)
                        glBindTextureUnit(i, id);
                else
                {
                        unit.bind();
-                       if(unit.supports_legacy())
-                       {
-                               if(old && old->target!=target && old_legacy)
-                                       glDisable(old->target);
-                               if((!old || old->target!=target) && legacy)
-                                       glEnable(target);
-                       }
                        glBindTexture(target, id);
                }
 
@@ -365,26 +359,18 @@ void Texture::unbind_from(unsigned i)
 {
        TexUnit &unit = TexUnit::get_unit(i);
        const Texture *cur = unit.get_texture();
-       bool legacy = unit.get_texture_legacy();
        if(unit.set_texture(0))
        {
-               if(ARB_direct_state_access && !legacy)
+               if(ARB_direct_state_access)
                        glBindTextureUnit(i, 0);
                else
                {
                        unit.bind();
                        glBindTexture(cur->target, 0);
-                       if(unit.supports_legacy() && legacy)
-                               glDisable(cur->target);
                }
        }
 }
 
-bool Texture::is_legacy_target(GLenum target)
-{
-       return target<GL_TEXTURE_1D_ARRAY;
-}
-
 
 Texture::Loader::Loader(Texture &t):
        DataFile::CollectionObjectLoader<Texture>(t, 0)
@@ -423,6 +409,8 @@ void Texture::Loader::external_image(const string &fn)
 {
        Graphics::Image img;
        RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
+       if(!io)
+               throw IO::file_not_found(fn);
        img.load_io(*io);
 
        obj.image(img, srgb);
@@ -435,7 +423,7 @@ void Texture::Loader::filter(TextureFilter f)
 
 void Texture::Loader::generate_mipmap(bool gm)
 {
-       obj.set_generate_mipmap(gm);
+       obj.set_auto_generate_mipmap(gm);
 }
 
 void Texture::Loader::image_data(const string &data)