]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture.cpp
Move texture sampler state to a separate object
[libs/gl.git] / source / texture.cpp
index d738bb36e9a63c2df69ef97a205e292b59aa5b49..7983cfb50e5c4272f76c2ceb9e1c3a5628639635 100644 (file)
@@ -1,7 +1,10 @@
-#include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
-#include <msp/gl/extensions/sgis_generate_mipmap.h>
-#include <msp/strings/format.h>
+#include <msp/gl/extensions/arb_direct_state_access.h>
+#include <msp/gl/extensions/arb_texture_swizzle.h>
+#include <msp/gl/extensions/ext_framebuffer_object.h>
+#include <msp/io/memory.h>
+#include "bindable.h"
 #include "error.h"
+#include "resourcemanager.h"
 #include "resources.h"
 #include "texture.h"
 #include "texunit.h"
@@ -11,61 +14,87 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-void operator>>(const LexicalConverter &c, TextureFilter &tf)
-{
-       if(c.get()=="NEAREST")
-               tf = NEAREST;
-       else if(c.get()=="LINEAR")
-               tf = LINEAR;
-       else if(c.get()=="NEAREST_MIPMAP_NEAREST")
-               tf = NEAREST_MIPMAP_NEAREST;
-       else if(c.get()=="NEAREST_MIPMAP_LINEAR")
-               tf = NEAREST_MIPMAP_LINEAR;
-       else if(c.get()=="LINEAR_MIPMAP_NEAREST")
-               tf = LINEAR_MIPMAP_NEAREST;
-       else if(c.get()=="LINEAR_MIPMAP_LINEAR")
-               tf = LINEAR_MIPMAP_LINEAR;
+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
+};
+
+Texture::Texture(GLenum t, ResourceManager *m):
+       id(0),
+       target(t),
+       ifmt(RGB),
+       auto_gen_mipmap(false),
+       dirty_params(0),
+       default_sampler(*this)
+{
+       if(m)
+               set_manager(m);
+       else if(ARB_direct_state_access)
+               glCreateTextures(target, 1, &id);
        else
-               throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
+               glGenTextures(1, &id);
 }
 
-
-void operator>>(const LexicalConverter &c, TextureWrap &tw)
+Texture::~Texture()
 {
-       if(c.get()=="REPEAT")
-               tw = REPEAT;
-       else if(c.get()=="CLAMP_TO_EDGE")
-               tw = CLAMP_TO_EDGE;
-       else if(c.get()=="MIRRORED_REPEAT")
-               tw = MIRRORED_REPEAT;
-       else
-               throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
+       while(TexUnit *unit = TexUnit::find_unit(this))
+               unbind_from(unit->get_index());
+
+       if(id)
+               glDeleteTextures(1, &id);
 }
 
+DataType Texture::get_alloc_type(PixelFormat fmt)
+{
+       return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
+}
 
-Texture::Texture(GLenum t):
-       target(t),
-       min_filter(NEAREST_MIPMAP_LINEAR),
-       mag_filter(LINEAR),
-       wrap_s(REPEAT),
-       wrap_t(REPEAT),
-       wrap_r(REPEAT),
-       gen_mipmap(false),
-       compare(false),
-       cmp_func(LEQUAL),
-       dirty_params(0)
+void Texture::set_internal_format(PixelFormat fmt)
 {
-       glGenTextures(1, &id);
+       FormatSwizzle swiz = NO_SWIZZLE;
+       if(ARB_texture_rg && ARB_texture_swizzle)
+       {
+               if(fmt==LUMINANCE)
+               {
+                       fmt = RED;
+                       swiz = R_TO_LUMINANCE;
+               }
+               else if(fmt==LUMINANCE_ALPHA)
+               {
+                       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;
+       if(swizzle)
+               update_parameter(FORMAT_SWIZZLE);
 }
 
-Texture::~Texture()
+PixelFormat Texture::get_upload_format(PixelFormat fmt) const
 {
-       glDeleteTextures(1, &id);
+       if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
+               return get_base_pixelformat(ifmt);
+       else
+               return fmt;
 }
 
 void Texture::update_parameter(int mask) const
 {
-       if(TexUnit::current().get_texture()!=this)
+       if(!id)
+       {
+               dirty_params |= mask;
+               return;
+       }
+
+       if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
        {
                TexUnit *unit = TexUnit::find_unit(this);
                if(!unit)
@@ -77,117 +106,173 @@ void Texture::update_parameter(int mask) const
                unit->bind();
        }
 
-       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);
+       if(mask&FORMAT_SWIZZLE)
+       {
+               if(get_gl_api()==OPENGL_ES2)
+               {
+                       set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
+                       set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
+                       set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
+                       set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
+               }
+               else
+               {
+                       if(ARB_direct_state_access)
+                               glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
+                       else
+                               glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
+               }
+       }
+}
+
+void Texture::set_parameter_i(GLenum param, int value) const
+{
+       if(ARB_direct_state_access)
+               glTextureParameteri(id, param, value);
+       else
+               glTexParameteri(target, param, value);
+}
+
+void Texture::set_parameter_f(GLenum param, float value) const
+{
+       if(ARB_direct_state_access)
+               glTextureParameterf(id, param, value);
+       else
+               glTexParameterf(target, param, value);
 }
 
 void Texture::set_min_filter(TextureFilter f)
 {
-       min_filter = f;
-       update_parameter(MIN_FILTER);
+       default_sampler.set_min_filter(f);
 }
 
 void Texture::set_mag_filter(TextureFilter f)
 {
-       mag_filter = f;
-       update_parameter(MAG_FILTER);
+       default_sampler.set_mag_filter(f);
 }
 
 void Texture::set_filter(TextureFilter f)
 {
-       set_min_filter(f);
-       set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
+       default_sampler.set_filter(f);
 }
 
 void Texture::set_max_anisotropy(float a)
 {
-       if(a<1.0f)
-               throw invalid_argument("Texture::set_max_anisotropy");
-       else if(a>1.0f)
-               static Require _req(EXT_texture_filter_anisotropic);
-       max_anisotropy = a;
-       update_parameter(MAX_ANISOTROPY);
+       default_sampler.set_max_anisotropy(a);
 }
 
 void Texture::set_wrap(TextureWrap w)
 {
-       set_wrap_s(w);
-       set_wrap_t(w);
-       set_wrap_r(w);
+       default_sampler.set_wrap(w);
 }
 
 void Texture::set_wrap_s(TextureWrap w)
 {
-       wrap_s = w;
-       update_parameter(WRAP_S);
+       default_sampler.set_wrap_s(w);
 }
 
 void Texture::set_wrap_t(TextureWrap w)
 {
-       wrap_t = w;
-       update_parameter(WRAP_T);
+       default_sampler.set_wrap_t(w);
 }
 
 void Texture::set_wrap_r(TextureWrap w)
 {
-       wrap_r = w;
-       update_parameter(WRAP_R);
+       default_sampler.set_wrap_r(w);
+}
+
+bool Texture::can_generate_mipmap()
+{
+       return EXT_framebuffer_object;
+}
+
+void Texture::generate_mipmap()
+{
+       // glGenerateMipmap is defined here
+       static Require _req(EXT_framebuffer_object);
+
+       if(ARB_direct_state_access)
+               glGenerateTextureMipmap(id);
+       else
+       {
+               BindRestore _bind(this);
+               glGenerateMipmap(target);
+       }
 }
 
-void Texture::set_generate_mipmap(bool gm)
+void Texture::set_auto_generate_mipmap(bool gm)
 {
        if(gm)
-               static Require _req(SGIS_generate_mipmap);
-       gen_mipmap = gm;
-       update_parameter(GENERATE_MIPMAP);
+               static Require _req(EXT_framebuffer_object);
+
+       auto_gen_mipmap = gm;
 }
 
 void Texture::set_compare_enabled(bool c)
 {
-       compare = c;
-       update_parameter(COMPARE);
+       if(c)
+               default_sampler.set_compare(default_sampler.get_compare_function());
+       else
+               default_sampler.disable_compare();
 }
 
 void Texture::set_compare_func(Predicate f)
 {
-       cmp_func = f;
-       update_parameter(COMPARE_FUNC);
+       default_sampler.set_compare(f);
+}
+
+void Texture::load_image(const string &fn, bool srgb)
+{
+       load_image(fn, 0, srgb);
+}
+
+void Texture::load_image(const string &fn, unsigned lv, bool srgb)
+{
+       Graphics::Image img;
+       img.load_file(fn);
+
+       image(img, lv, srgb);
+}
+
+void Texture::image(const Graphics::Image &img, bool srgb)
+{
+       image(img, 0, srgb);
 }
 
 void Texture::bind_to(unsigned i) const
 {
+       if(!id)
+       {
+               if(manager)
+                       manager->resource_used(*this);
+               if(!id)
+               {
+                       unbind_from(i);
+                       return;
+               }
+       }
+
        TexUnit &unit = TexUnit::get_unit(i);
-       const Texture *cur = unit.get_texture();
        if(unit.set_texture(this))
        {
-               unit.bind();
-               if(cur && cur->target!=target)
-                       glDisable(cur->target);
-               if(!cur || cur->target!=target)
-                       glEnable(target);
-               glBindTexture(target, id);
+               if(manager)
+                       manager->resource_used(*this);
+
+               if(ARB_direct_state_access)
+                       glBindTextureUnit(i, id);
+               else
+               {
+                       unit.bind();
+                       glBindTexture(target, id);
+               }
 
                if(dirty_params)
                {
                        update_parameter(dirty_params);
                        dirty_params = 0;
                }
+
+               default_sampler.bind_to(i);
        }
 }
 
@@ -202,9 +287,13 @@ void Texture::unbind_from(unsigned i)
        const Texture *cur = unit.get_texture();
        if(unit.set_texture(0))
        {
-               unit.bind();
-               glBindTexture(cur->target, 0);
-               glDisable(cur->target);
+               if(ARB_direct_state_access)
+                       glBindTextureUnit(i, 0);
+               else
+               {
+                       unit.bind();
+                       glBindTexture(cur->target, 0);
+               }
        }
 }
 
@@ -223,22 +312,45 @@ 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("filter", &Loader::filter);
-       add("max_anisotropy", &Loader::max_anisotropy);
        add("generate_mipmap", &Loader::generate_mipmap);
+       add("image_data", &Loader::image_data);
        add("mag_filter", &Loader::mag_filter);
+       add("max_anisotropy", &Loader::max_anisotropy);
        add("min_filter", &Loader::min_filter);
+       add("mipmap_levels", &Loader::mipmap_levels);
+       add("sampler",    &Loader::sampler);
        add("wrap",       &Loader::wrap);
        add("wrap_r",     &Loader::wrap_r);
        add("wrap_s",     &Loader::wrap_s);
        add("wrap_t",     &Loader::wrap_t);
 }
 
+unsigned Texture::Loader::get_levels() const
+{
+       return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
+}
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+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, get_levels(), srgb);
+}
+
 void Texture::Loader::filter(TextureFilter f)
 {
        obj.set_filter(f);
@@ -246,7 +358,16 @@ 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)
+{
+       Graphics::Image img;
+       IO::Memory mem(data.data(), data.size());
+       img.load_io(mem);
+
+       obj.image(img, get_levels(), srgb);
 }
 
 void Texture::Loader::mag_filter(TextureFilter f)
@@ -264,6 +385,16 @@ void Texture::Loader::min_filter(TextureFilter f)
        obj.set_min_filter(f);
 }
 
+void Texture::Loader::mipmap_levels(unsigned l)
+{
+       levels = l;
+}
+
+void Texture::Loader::sampler()
+{
+       load_sub(obj.default_sampler);
+}
+
 void Texture::Loader::wrap(TextureWrap w)
 {
        obj.set_wrap(w);
@@ -283,6 +414,7 @@ void Texture::Loader::wrap_t(TextureWrap w)
 {
        obj.set_wrap_t(w);
 }
+#pragma GCC diagnostic pop
 
 } // namespace GL
 } // namespace Msp