]> git.tdb.fi Git - libs/gl.git/commitdiff
Bind textures in the modern way when shaders are used
authorMikko Rasa <tdb@tdb.fi>
Sun, 1 Jul 2018 09:52:07 +0000 (12:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 1 Jul 2018 10:04:27 +0000 (13:04 +0300)
source/renderer.cpp
source/texture.cpp
source/texture.h
source/texturing.cpp
source/texturing.h
source/texunit.cpp
source/texunit.h

index 76ac5846f2aad1e9417780e39d87f3c816a81088..50acd8e728c60e9531fd7b23dccc8a28cfb4a7c1 100644 (file)
@@ -265,14 +265,15 @@ void Renderer::apply_state()
        changed */
 
        bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
+       bool legacy_textures = !state->shprog;
 
        if(state->texturing)
-               state->texturing->bind();
+               state->texturing->bind(legacy_textures);
        else
        {
                Texturing::unbind();
                if(state->texture)
-                       state->texture->bind_to(0);
+                       state->texture->bind_to(0, legacy_textures);
                else
                        Texture::unbind_from(0);
        }
index f095fe98aadcb99500ecad562275027453d14dcf..1b7bdc2297ec3a1be84d95fdcd091b55b59bae20 100644 (file)
@@ -301,7 +301,7 @@ void Texture::load_image(const string &fn, bool srgb)
        image(img, srgb);
 }
 
-void Texture::bind_to(unsigned i) const
+void Texture::bind_to(unsigned i, bool legacy) const
 {
        if(!id)
        {
@@ -316,21 +316,22 @@ void Texture::bind_to(unsigned i) const
 
        TexUnit &unit = TexUnit::get_unit(i);
        const Texture *old = unit.get_texture();
-       if(unit.set_texture(this))
+       bool old_legacy = unit.get_texture_legacy();
+       if(unit.set_texture(this, legacy))
        {
                if(manager)
                        manager->resource_used(*this);
 
-               if(ARB_direct_state_access && !unit.supports_legacy())
+               if(ARB_direct_state_access && !old_legacy && (!unit.supports_legacy() || !legacy))
                        glBindTextureUnit(i, id);
                else
                {
                        unit.bind();
                        if(unit.supports_legacy())
                        {
-                               if(old && old->target!=target)
+                               if(old && old->target!=target && old_legacy)
                                        glDisable(old->target);
-                               if((!old || old->target!=target))
+                               if((!old || old->target!=target) && legacy)
                                        glEnable(target);
                        }
                        glBindTexture(target, id);
@@ -353,15 +354,16 @@ 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 && !unit.supports_legacy())
+               if(ARB_direct_state_access && !legacy)
                        glBindTextureUnit(i, 0);
                else
                {
                        unit.bind();
                        glBindTexture(cur->target, 0);
-                       if(unit.supports_legacy())
+                       if(unit.supports_legacy() && legacy)
                                glDisable(cur->target);
                }
        }
index 9c43b76911ecb5d00fcf517847f6a04001ed6241..f491297d692816de8d2503ba8e6071f77a852ca6 100644 (file)
@@ -196,8 +196,8 @@ public:
        GLenum get_target() const { return target; }
        unsigned get_id() const { return id; }
 
-       void bind() const { bind_to(0); }
-       void bind_to(unsigned) const;
+       void bind(bool legacy = true) const { bind_to(0, legacy); }
+       void bind_to(unsigned, bool = true) const;
 
        static const Texture *current(unsigned = 0);
        static void unbind() { unbind_from(0); }
index dff3757fdb6d9b8985234e87241153d94b41a80b..77bb15435c7391a62a0298ea653f6699417eb55f 100644 (file)
@@ -7,6 +7,8 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+bool Texturing::legacy_used = true;
+
 Texturing::~Texturing()
 {
        if(current()==this)
@@ -34,7 +36,7 @@ void Texturing::set_attachment(unsigned attch, const Texture *tex)
        attachments[attch] = tex;
 
        if(current()==this)
-               bind_attachment(attch);
+               bind_attachment(attch, legacy_used);
 }
 
 const Texture *Texturing::get_attached_texture(unsigned i) const
@@ -42,10 +44,10 @@ const Texture *Texturing::get_attached_texture(unsigned i) const
        return i<attachments.size() ? attachments[i] : 0;
 }
 
-void Texturing::bind_attachment(unsigned i) const
+void Texturing::bind_attachment(unsigned i, bool legacy) const
 {
        if(const Texture *tex = attachments[i])
-               tex->bind_to(i);
+               tex->bind_to(i, legacy);
        else
                Texture::unbind_from(i);
 }
@@ -55,13 +57,14 @@ void Texturing::unbind_attachment(unsigned i)
        Texture::unbind_from(i);
 }
 
-void Texturing::bind() const
+void Texturing::bind(bool legacy) const
 {
        const Texturing *old = current();
-       if(set_current(this))
+       if(set_current(this) || legacy!=legacy_used)
        {
+               legacy_used = legacy;
                for(unsigned i=0; i<attachments.size(); ++i)
-                       bind_attachment(i);
+                       bind_attachment(i, legacy);
                if(old)
                {
                        for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
index 0b3a9751b5f98ebe17f6e55a48fe3d4a998b92c1..2b409d64fb713ee221a4842110832258bd92f711 100644 (file)
@@ -14,6 +14,8 @@ class Texturing: public Bindable<Texturing>
 private:
        std::vector<const Texture *> attachments;
 
+       static bool legacy_used;
+
 public:
        ~Texturing();
 
@@ -25,12 +27,12 @@ public:
        const Texture *get_attached_texture(unsigned) const;
 
 private:
-       void bind_attachment(unsigned) const;
+       void bind_attachment(unsigned, bool) const;
 
        static void unbind_attachment(unsigned);
 
 public:
-       void bind() const;
+       void bind(bool = true) const;
 
        static void unbind();
 };
index 31c90777aad10eccd5a4e9d110de2a0846809f26..9277c27704ebd8347c3c6a782fc1b6ff05fc8658 100644 (file)
@@ -16,13 +16,16 @@ TexUnit *TexUnit::cur_unit = 0;
 
 TexUnit::TexUnit():
        legacy(false),
-       texture(0)
+       texture(0),
+       tex_legacy(false)
 { }
 
-bool TexUnit::set_texture(const Texture *tex)
+bool TexUnit::set_texture(const Texture *tex, bool lgc)
 {
-       bool result = (tex!=texture);
+       lgc = (lgc && legacy && tex);
+       bool result = (tex!=texture || lgc!=tex_legacy);
        texture = tex;
+       tex_legacy = lgc;
        return result;
 }
 
index 0bba15d6a48fc86ccfe945351b04190f95fef73f..7cf43a7832b4dc6bb3949685f4f90f09a80c1bbb 100644 (file)
@@ -19,6 +19,7 @@ private:
        unsigned index;
        bool legacy;
        const Texture *texture;
+       bool tex_legacy;
 
        static std::vector<TexUnit> units;
        static TexUnit *cur_unit;
@@ -28,8 +29,9 @@ private:
 public:
        unsigned get_index() const { return index; }
        bool supports_legacy() const { return legacy; }
-       bool set_texture(const Texture *);
+       bool set_texture(const Texture *, bool = true);
        const Texture *get_texture() const { return texture; }
+       bool get_texture_legacy() const { return tex_legacy; }
        void bind();
 
        static unsigned get_n_units();