]> git.tdb.fi Git - libs/gl.git/blobdiff - source/sampler.cpp
Move texture sampler state to a separate object
[libs/gl.git] / source / sampler.cpp
diff --git a/source/sampler.cpp b/source/sampler.cpp
new file mode 100644 (file)
index 0000000..9476ffe
--- /dev/null
@@ -0,0 +1,273 @@
+#include <msp/gl/extensions/arb_direct_state_access.h>
+#include <msp/gl/extensions/arb_sampler_objects.h>
+#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/strings/format.h>
+#include "error.h"
+#include "sampler.h"
+#include "texture.h"
+#include "texunit.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+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;
+       compare = false;
+       cmp_func = LEQUAL;
+       dirty_params = 0;
+}
+
+void Sampler::update_parameter(int mask) const
+{
+       if(!owner->get_id() || (!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&COMPARE)
+       {
+               set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
+               if(compare)
+                       set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
+       }
+}
+
+void Sampler::set_parameter_i(unsigned param, int value) const
+{
+       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(ARB_direct_state_access)
+               glTextureParameterf(owner->get_id(), param, value);
+       else
+               glTexParameterf(owner->get_target(), param, value);
+}
+
+void Sampler::set_min_filter(TextureFilter f)
+{
+       min_filter = f;
+       update_parameter(MIN_FILTER);
+}
+
+void Sampler::set_mag_filter(TextureFilter f)
+{
+       mag_filter = f;
+       update_parameter(MAG_FILTER);
+}
+
+void Sampler::set_filter(TextureFilter f)
+{
+       set_min_filter(f);
+       set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
+}
+
+void Sampler::set_max_anisotropy(float a)
+{
+       if(a<1.0f)
+               throw invalid_argument("Sampler::set_max_anisotropy");
+       else if(a>1.0f)
+               static Require _req(EXT_texture_filter_anisotropic);
+       max_anisotropy = a;
+       if(EXT_texture_filter_anisotropic)
+               update_parameter(MAX_ANISOTROPY);
+}
+
+void Sampler::set_wrap(TextureWrap w)
+{
+       set_wrap_s(w);
+       set_wrap_t(w);
+       if(EXT_texture3D)
+               set_wrap_r(w);
+}
+
+void Sampler::set_wrap_s(TextureWrap w)
+{
+       wrap_s = w;
+       update_parameter(WRAP_S);
+}
+
+void Sampler::set_wrap_t(TextureWrap w)
+{
+       wrap_t = w;
+       update_parameter(WRAP_T);
+}
+
+void Sampler::set_wrap_r(TextureWrap w)
+{
+       static Require _req(EXT_texture3D);
+       wrap_r = w;
+       update_parameter(WRAP_R);
+}
+
+void Sampler::disable_compare()
+{
+       compare = false;
+       update_parameter(COMPARE);
+}
+
+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!=unit.get_texture())
+               throw invalid_operation("Sampler::bind_to");
+
+       if(dirty_params)
+       {
+               update_parameter(dirty_params);
+               dirty_params = 0;
+       }
+}
+
+void Sampler::unload()
+{
+       // TODO check which params actually need refreshing
+       if(!owner->get_id())
+               dirty_params = -1;
+}
+
+
+Sampler::Loader::Loader(Sampler &s):
+       DataFile::ObjectLoader<Sampler>(s)
+{
+       add("filter", &Loader::filter);
+       add("mag_filter", &Loader::mag_filter);
+       add("max_anisotropy", &Loader::max_anisotropy);
+       add("min_filter", &Loader::min_filter);
+       add("wrap", &Loader::wrap);
+       add("wrap_r", &Loader::wrap_r);
+       add("wrap_s", &Loader::wrap_s);
+       add("wrap_t", &Loader::wrap_t);
+}
+
+void Sampler::Loader::filter(TextureFilter f)
+{
+       obj.set_filter(f);
+}
+
+void Sampler::Loader::mag_filter(TextureFilter f)
+{
+       obj.set_mag_filter(f);
+}
+
+void Sampler::Loader::max_anisotropy(float a)
+{
+       obj.set_max_anisotropy(a);
+}
+
+void Sampler::Loader::min_filter(TextureFilter f)
+{
+       obj.set_min_filter(f);
+}
+
+void Sampler::Loader::wrap(TextureWrap w)
+{
+       obj.set_wrap(w);
+}
+
+void Sampler::Loader::wrap_r(TextureWrap w)
+{
+       obj.set_wrap_r(w);
+}
+
+void Sampler::Loader::wrap_s(TextureWrap w)
+{
+       obj.set_wrap_s(w);
+}
+
+void Sampler::Loader::wrap_t(TextureWrap w)
+{
+       obj.set_wrap_t(w);
+}
+
+
+bool is_mipmapped(TextureFilter filter)
+{
+       return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
+               filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
+}
+
+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;
+       else
+               throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
+}
+
+void operator>>(const LexicalConverter &c, TextureWrap &tw)
+{
+       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()));
+}
+
+
+} // namespace GL
+} // namespace Msp