]> git.tdb.fi Git - libs/gl.git/commitdiff
Add CLAMP_TO_BORDER and border color to Sampler
authorMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 11:26:52 +0000 (14:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 11:26:52 +0000 (14:26 +0300)
source/core/sampler.cpp
source/core/sampler.h

index 3f853ea04126f1ac95ce2c164cc1ee1ff5229a2e..273f2bb712cf96a1acb0ee93f7afb6e0343f86c5 100644 (file)
@@ -44,6 +44,7 @@ void Sampler::init()
        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;
@@ -84,6 +85,8 @@ void Sampler::update_parameter(int mask) const
                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));
@@ -112,6 +115,16 @@ void Sampler::set_parameter_f(unsigned param, float value) const
                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);
+}
+
 void Sampler::set_min_filter(TextureFilter f)
 {
        min_filter = f;
@@ -168,6 +181,12 @@ void Sampler::set_wrap_r(TextureWrap w)
        update_parameter(WRAP_R);
 }
 
+void Sampler::set_border_color(const Color &c)
+{
+       border_color = c;
+       update_parameter(BORDER_COLOR);
+}
+
 void Sampler::disable_compare()
 {
        compare = false;
@@ -240,6 +259,7 @@ void Sampler::unload()
 Sampler::Loader::Loader(Sampler &s):
        DataFile::ObjectLoader<Sampler>(s)
 {
+       add("border_color", &Loader::border_color);
        add("compare", &Loader::compare);
        add("filter", &Loader::filter);
        add("mag_filter", &Loader::mag_filter);
@@ -251,6 +271,11 @@ Sampler::Loader::Loader(Sampler &s):
        add("wrap_t", &Loader::wrap_t);
 }
 
+void Sampler::Loader::border_color(float r, float g, float b, float a)
+{
+       obj.set_border_color(Color(r, g, b, a));
+}
+
 void Sampler::Loader::compare(Predicate c)
 {
        obj.set_compare(c);
@@ -327,6 +352,8 @@ void operator>>(const LexicalConverter &c, TextureWrap &tw)
                tw = REPEAT;
        else if(c.get()=="CLAMP_TO_EDGE")
                tw = CLAMP_TO_EDGE;
+       else if(c.get()=="CLAMP_TO_BORDER")
+               tw = CLAMP_TO_BORDER;
        else if(c.get()=="MIRRORED_REPEAT")
                tw = MIRRORED_REPEAT;
        else
index 4208f069da4f4c6566c2fca362949ef20702d51b..79aea8f8b76621f6ed9f5e5f44d0df08bf84aa0d 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_SAMPLER_H_
 
 #include <msp/datafile/objectloader.h>
+#include "color.h"
 #include "gl.h"
 #include "predicate.h"
 
@@ -38,6 +39,9 @@ enum TextureWrap
        /// Extend the texels at the edge of the texture to infinity
        CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
 
+       /// Sampling outside the texture will return border color
+       CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER,
+
        /// Tile the texture, with every other repetition mirrored
        MIRRORED_REPEAT = GL_MIRRORED_REPEAT
 };
@@ -69,6 +73,7 @@ public:
        private:
                void init();
 
+               void border_color(float, float, float, float);
                void compare(Predicate);
                void filter(TextureFilter);
                void mag_filter(TextureFilter);
@@ -89,7 +94,8 @@ private:
                WRAP_S = 8,
                WRAP_T = 16,
                WRAP_R = 32,
-               COMPARE = 64
+               BORDER_COLOR = 64,
+               COMPARE = 128
        };
 
        unsigned id;
@@ -100,6 +106,7 @@ private:
        TextureWrap wrap_s;
        TextureWrap wrap_t;
        TextureWrap wrap_r;
+       Color border_color;
        bool compare;
        Predicate cmp_func;
        mutable int dirty_params;
@@ -113,6 +120,7 @@ private:
        void update_parameter(int) const;
        void set_parameter_i(unsigned, int) const;
        void set_parameter_f(unsigned, float) const;
+       void set_parameter_fv(unsigned, const float *) const;
 
 public:
        void set_min_filter(TextureFilter);
@@ -135,6 +143,9 @@ public:
        void set_wrap_t(TextureWrap);
        void set_wrap_r(TextureWrap);
 
+       void set_border_color(const Color &);
+       const Color &get_border_color() const { return border_color; }
+
        /** Disables depth comparison. */
        void disable_compare();