]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture.cpp
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / texture.cpp
index f0c55999250263f641e546341e22b7545177748b..f6594ab14c7e5143434ac0823c862427c70f2c9d 100644 (file)
@@ -18,14 +18,17 @@ int Texture::swizzle_orders[] =
 {
        GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
        GL_RED, GL_RED, GL_RED, GL_ONE,
-       GL_RED, GL_RED, GL_RED, GL_GREEN
+       GL_RED, GL_RED, GL_RED, GL_GREEN,
+       GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
 };
 
 Texture::Texture(GLenum t, ResourceManager *m):
        id(0),
        target(t),
-       ifmt(RGB8),
+       format(RGB8),
+       storage_fmt(RGB8),
        swizzle(NO_SWIZZLE),
+       use_srgb_format(false),
        auto_gen_mipmap(false),
        default_sampler(*this)
 {
@@ -46,37 +49,40 @@ Texture::~Texture()
                glDeleteTextures(1, &id);
 }
 
-void Texture::set_internal_format(PixelFormat fmt)
+void Texture::set_format(PixelFormat fmt)
 {
        PixelComponents comp = get_components(fmt);
+       PixelComponents st_comp = comp;
        FormatSwizzle swiz = NO_SWIZZLE;
        switch(comp)
        {
        case LUMINANCE:
-               comp = RED;
+               st_comp = RED;
                swiz = R_TO_LUMINANCE;
                break;
        case LUMINANCE_ALPHA:
-               comp = RG;
+               st_comp = RG;
                swiz = RG_TO_LUMINANCE_ALPHA;
                break;
+       case BGR:
+               st_comp = RGB;
+               swiz = RGB_TO_BGR;
+               break;
+       case BGRA:
+               st_comp = RGBA;
+               swiz = RGB_TO_BGR;
+               break;
        default:;
        }
 
-       fmt = make_pixelformat(comp, get_component_type(fmt));
-       require_pixelformat(fmt);
+       PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt));
+       require_pixelformat(st_fmt);
        if(swiz!=NO_SWIZZLE)
                static Require _req(ARB_texture_swizzle);
-       ifmt = fmt;
-       swizzle = swiz;
-}
 
-PixelComponents Texture::get_upload_components(PixelComponents comp) const
-{
-       if(comp==LUMINANCE || comp==LUMINANCE_ALPHA)
-               return get_components(ifmt);
-       else
-               return comp;
+       format = fmt;
+       storage_fmt = st_fmt;
+       swizzle = swiz;
 }
 
 void Texture::apply_swizzle()
@@ -188,22 +194,22 @@ void Texture::set_compare_func(Predicate f)
        default_sampler.set_compare(f);
 }
 
-void Texture::load_image(const string &fn, bool srgb)
+void Texture::load_image(const string &fn, bool)
 {
-       load_image(fn, 0, srgb);
+       load_image(fn, 0U);
 }
 
-void Texture::load_image(const string &fn, unsigned lv, bool srgb)
+void Texture::load_image(const string &fn, unsigned lv)
 {
        Graphics::Image img;
        img.load_file(fn);
 
-       image(img, lv, srgb);
+       image(img, lv);
 }
 
-void Texture::image(const Graphics::Image &img, bool srgb)
+void Texture::image(const Graphics::Image &img, bool)
 {
-       image(img, 0, srgb);
+       image(img, 0U);
 }
 
 void Texture::bind_to(unsigned i) const
@@ -220,6 +226,7 @@ void Texture::bind_to(unsigned i) const
        }
 
        TexUnit &unit = TexUnit::get_unit(i);
+       const Texture *cur = unit.get_texture();
        if(unit.set_texture(this))
        {
                if(manager)
@@ -233,7 +240,8 @@ void Texture::bind_to(unsigned i) const
                        glBindTexture(target, id);
                }
 
-               default_sampler.bind_to(i);
+               if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler)
+                       default_sampler.bind_to(i);
        }
 }
 
@@ -255,6 +263,9 @@ void Texture::unbind_from(unsigned i)
                        unit.bind();
                        glBindTexture(cur->target, 0);
                }
+
+               if(unit.get_sampler()==&cur->default_sampler)
+                       Sampler::unbind_from(i);
        }
 }
 
@@ -274,12 +285,9 @@ Texture::Loader::Loader(Texture &t, Collection &c):
 void Texture::Loader::init()
 {
        levels = 0;
-       if(Resources *res = dynamic_cast<Resources *>(coll))
-               srgb = res->get_srgb_conversion();
-       else
-               srgb = false;
 
        add("external_image", &Loader::external_image);
+       add("external_image_srgb", &Loader::external_image);
        add("filter", &Loader::filter);
        add("generate_mipmap", &Loader::generate_mipmap);
        add("image_data", &Loader::image_data);
@@ -301,15 +309,36 @@ unsigned Texture::Loader::get_levels() const
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-void Texture::Loader::external_image(const string &fn)
+void Texture::Loader::load_external_image(Graphics::Image &img, const std::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);
+}
+
+void Texture::Loader::external_image(const string &fn)
+{
+       obj.use_srgb_format = false;
+       external_image_common(fn);
+}
+
+void Texture::Loader::external_image_srgb(const string &fn)
+{
+       obj.use_srgb_format = true;
+       external_image_common(fn);
+}
 
-       obj.image(img, get_levels(), srgb);
+void Texture::Loader::external_image_common(const string &fn)
+{
+       if(obj.manager)
+               obj.manager->set_resource_location(obj, get_collection(), fn);
+       else
+       {
+               Graphics::Image img;
+               load_external_image(img, fn);
+               obj.image(img, get_levels());
+       }
 }
 
 void Texture::Loader::filter(TextureFilter f)
@@ -328,7 +357,7 @@ void Texture::Loader::image_data(const string &data)
        IO::Memory mem(data.data(), data.size());
        img.load_io(mem);
 
-       obj.image(img, get_levels(), srgb);
+       obj.image(img, get_levels());
 }
 
 void Texture::Loader::mag_filter(TextureFilter f)