From: Mikko Rasa Date: Sun, 1 Jul 2018 09:52:07 +0000 (+0300) Subject: Bind textures in the modern way when shaders are used X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=0f890ce60a560ba2ccc0719229be304bb597d919 Bind textures in the modern way when shaders are used --- diff --git a/source/renderer.cpp b/source/renderer.cpp index 76ac5846..50acd8e7 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -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); } diff --git a/source/texture.cpp b/source/texture.cpp index f095fe98..1b7bdc22 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -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); } } diff --git a/source/texture.h b/source/texture.h index 9c43b769..f491297d 100644 --- a/source/texture.h +++ b/source/texture.h @@ -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); } diff --git a/source/texturing.cpp b/source/texturing.cpp index dff3757f..77bb1543 100644 --- a/source/texturing.cpp +++ b/source/texturing.cpp @@ -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 ibind_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; iattachments.size(); ++i) diff --git a/source/texturing.h b/source/texturing.h index 0b3a9751..2b409d64 100644 --- a/source/texturing.h +++ b/source/texturing.h @@ -14,6 +14,8 @@ class Texturing: public Bindable private: std::vector 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(); }; diff --git a/source/texunit.cpp b/source/texunit.cpp index 31c90777..9277c277 100644 --- a/source/texunit.cpp +++ b/source/texunit.cpp @@ -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; } diff --git a/source/texunit.h b/source/texunit.h index 0bba15d6..7cf43a78 100644 --- a/source/texunit.h +++ b/source/texunit.h @@ -19,6 +19,7 @@ private: unsigned index; bool legacy; const Texture *texture; + bool tex_legacy; static std::vector 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();