]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/sampler.cpp
Decouple the Predicate enum from OpenGL constants
[libs/gl.git] / source / core / sampler.cpp
index 273f2bb712cf96a1acb0ee93f7afb6e0343f86c5..c810789b605130626c1e84d4f27ecd9f7ce6a81e 100644 (file)
@@ -3,11 +3,11 @@
 #include <msp/gl/extensions/arb_shadow.h>
 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
 #include <msp/gl/extensions/ext_texture3d.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include <msp/strings/format.h>
 #include "error.h"
 #include "sampler.h"
 #include "texture.h"
-#include "texunit.h"
 
 using namespace std;
 
@@ -15,10 +15,17 @@ namespace Msp {
 namespace GL {
 
 Sampler::Sampler():
-       owner(0)
+       min_filter(NEAREST_MIPMAP_LINEAR),
+       mag_filter(LINEAR),
+       max_anisotropy(1.0f),
+       wrap_s(REPEAT),
+       wrap_t(REPEAT),
+       wrap_r(REPEAT),
+       border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
+       compare(false),
+       cmp_func(LEQUAL),
+       dirty_params(0)
 {
-       init();
-
        Require _req(ARB_sampler_objects);
        if(ARB_direct_state_access)
                glCreateSamplers(1, &id);
@@ -26,115 +33,42 @@ Sampler::Sampler():
                glGenSamplers(1, &id);
 }
 
-Sampler::Sampler(const Texture &tex):
-       id(0),
-       owner(&tex)
-{
-       if(this!=&tex.get_default_sampler())
-               throw invalid_argument("Sampler::Sampler");
-
-       init();
-}
-
-void Sampler::init()
-{
-       min_filter = NEAREST_MIPMAP_LINEAR;
-       mag_filter = LINEAR;
-       max_anisotropy = 1.0f;
-       wrap_s = REPEAT;
-       wrap_t = REPEAT;
-       wrap_r = REPEAT;
-       border_color = Color(0.0f, 0.0f, 0.0f, 0.0f);
-       compare = false;
-       cmp_func = LEQUAL;
-       dirty_params = 0;
-}
-
-void Sampler::update_parameter(int mask) const
-{
-       if(owner)
+void Sampler::update() const
+{
+       if(dirty_params&MIN_FILTER)
+               glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, min_filter);
+       if(dirty_params&MAG_FILTER)
+               glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, mag_filter);
+       if(dirty_params&MAX_ANISOTROPY)
+               glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
+       if(dirty_params&WRAP_S)
+               glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap_s);
+       if(dirty_params&WRAP_T)
+               glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t);
+       if(dirty_params&WRAP_R)
+               glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap_r);
+       if(dirty_params&BORDER_COLOR)
+               glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
+       if(dirty_params&COMPARE)
        {
-               if(!owner->get_id())
-               {
-                       dirty_params |= mask;
-                       return;
-               }
-
-               if(!ARB_direct_state_access && TexUnit::current().get_texture()!=owner)
-               {
-                       TexUnit *unit = TexUnit::find_unit(owner);
-                       if(!unit)
-                       {
-                               dirty_params |= mask;
-                               return;
-                       }
-
-                       unit->bind();
-               }
-       }
-
-       if(mask&MIN_FILTER)
-               set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
-       if(mask&MAG_FILTER)
-               set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
-       if(mask&MAX_ANISOTROPY)
-               set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
-       if(mask&WRAP_S)
-               set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
-       if(mask&WRAP_T)
-               set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
-       if(mask&WRAP_R)
-               set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
-       if(mask&BORDER_COLOR)
-               set_parameter_fv(GL_TEXTURE_BORDER_COLOR, &border_color.r);
-       if(mask&COMPARE)
-       {
-               set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
+               glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
                if(compare)
-                       set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
+                       glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(cmp_func));
        }
-}
-
-void Sampler::set_parameter_i(unsigned param, int value) const
-{
-       if(id)
-               glSamplerParameteri(id, param, value);
-       else if(ARB_direct_state_access)
-               glTextureParameteri(owner->get_id(), param, value);
-       else
-               glTexParameteri(owner->get_target(), param, value);
-}
-
-void Sampler::set_parameter_f(unsigned param, float value) const
-{
-       if(id)
-               glSamplerParameterf(id, param, value);
-       else if(ARB_direct_state_access)
-               glTextureParameterf(owner->get_id(), param, value);
-       else
-               glTexParameterf(owner->get_target(), param, value);
-}
 
-void Sampler::set_parameter_fv(unsigned param, const float *value) const
-{
-       if(id)
-               glSamplerParameterfv(id, param, value);
-       else if(ARB_direct_state_access)
-               glTextureParameterfv(owner->get_id(), param, value);
-       else
-               glTexParameterfv(owner->get_target(), param, value);
+       dirty_params = 0;
 }
 
 void Sampler::set_min_filter(TextureFilter f)
 {
        min_filter = f;
-       update_parameter(MIN_FILTER);
+       dirty_params |= MIN_FILTER;
 }
 
 void Sampler::set_mag_filter(TextureFilter f)
 {
        mag_filter = f;
-       update_parameter(MAG_FILTER);
+       dirty_params |= MAG_FILTER;
 }
 
 void Sampler::set_filter(TextureFilter f)
@@ -151,7 +85,7 @@ void Sampler::set_max_anisotropy(float a)
                static Require _req(EXT_texture_filter_anisotropic);
        max_anisotropy = a;
        if(EXT_texture_filter_anisotropic)
-               update_parameter(MAX_ANISOTROPY);
+               dirty_params |= MAX_ANISOTROPY;
 }
 
 void Sampler::set_wrap(TextureWrap w)
@@ -165,32 +99,32 @@ void Sampler::set_wrap(TextureWrap w)
 void Sampler::set_wrap_s(TextureWrap w)
 {
        wrap_s = w;
-       update_parameter(WRAP_S);
+       dirty_params |= WRAP_S;
 }
 
 void Sampler::set_wrap_t(TextureWrap w)
 {
        wrap_t = w;
-       update_parameter(WRAP_T);
+       dirty_params |= WRAP_T;
 }
 
 void Sampler::set_wrap_r(TextureWrap w)
 {
        static Require _req(EXT_texture3D);
        wrap_r = w;
-       update_parameter(WRAP_R);
+       dirty_params |= WRAP_R;
 }
 
 void Sampler::set_border_color(const Color &c)
 {
        border_color = c;
-       update_parameter(BORDER_COLOR);
+       dirty_params |= BORDER_COLOR;
 }
 
 void Sampler::disable_compare()
 {
        compare = false;
-       update_parameter(COMPARE);
+       dirty_params |= COMPARE;
 }
 
 void Sampler::set_compare(Predicate f)
@@ -198,61 +132,17 @@ void Sampler::set_compare(Predicate f)
        static Require _req(ARB_shadow);
        compare = true;
        cmp_func = f;
-       update_parameter(COMPARE);
-}
-
-void Sampler::bind_to(unsigned i) const
-{
-       TexUnit &unit = TexUnit::get_unit(i);
-       if(owner && owner!=unit.get_texture())
-               throw invalid_operation("Sampler::bind_to");
-
-       const Sampler *cur = unit.get_sampler();
-       if(unit.set_sampler(this))
-       {
-               if(!owner || (cur && cur->id))
-                       glBindSampler(i, id);
-
-               if(dirty_params)
-               {
-                       update_parameter(dirty_params);
-                       dirty_params = 0;
-               }
-       }
-}
-
-const Sampler *Sampler::current(unsigned i)
-{
-       return TexUnit::get_unit(i).get_sampler();
+       dirty_params |= COMPARE;
 }
 
-void Sampler::unbind_from(unsigned i)
+void Sampler::set_debug_name(const string &name)
 {
-       TexUnit &unit = TexUnit::get_unit(i);
-       const Sampler *cur = unit.get_sampler();
-       if(unit.set_sampler(0) && cur->id)
-               glBindSampler(i, 0);
-}
-
-void Sampler::unload()
-{
-       if(owner && !owner->get_id())
-       {
-               if(min_filter!=NEAREST_MIPMAP_LINEAR)
-                       dirty_params |= MIN_FILTER;
-               if(mag_filter!=LINEAR)
-                       dirty_params |= MAG_FILTER;
-               if(max_anisotropy!=1.0f)
-                       dirty_params |= MAX_ANISOTROPY;
-               if(wrap_s!=REPEAT)
-                       dirty_params |= WRAP_S;
-               if(wrap_t!=REPEAT)
-                       dirty_params |= WRAP_T;
-               if(wrap_r!=REPEAT)
-                       dirty_params |= WRAP_R;
-               if(compare || cmp_func!=LEQUAL)
-                       dirty_params |= COMPARE;
-       }
+#ifdef DEBUG
+       if(id && KHR_debug)
+               glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
 }