]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/sampler.h
Rearrange soucre files into subdirectories
[libs/gl.git] / source / core / sampler.h
diff --git a/source/core/sampler.h b/source/core/sampler.h
new file mode 100644 (file)
index 0000000..e1e9bec
--- /dev/null
@@ -0,0 +1,168 @@
+#ifndef MSP_GL_SAMPLER_H_
+#define MSP_GL_SAMPLER_H_
+
+#include <msp/datafile/objectloader.h>
+#include "gl.h"
+#include "predicate.h"
+
+namespace Msp {
+namespace GL {
+
+enum TextureFilter
+{
+       /// No filtering
+       NEAREST = GL_NEAREST,
+
+       /// Bilinear filtering
+       LINEAR = GL_LINEAR,
+
+       /// Mipmapping without filtering
+       NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
+
+       /// Linear filtering between two mipmap levels
+       NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
+
+       /// Bilinear filtering on the closest mipmap level
+       LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
+
+       /// Trilinear filtering between two mipmap levels
+       LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
+};
+
+
+enum TextureWrap
+{
+       /// Tile the texture infinitely
+       REPEAT = GL_REPEAT,
+
+       /// Extend the texels at the edge of the texture to infinity
+       CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
+
+       /// Tile the texture, with every other repetition mirrored
+       MIRRORED_REPEAT = GL_MIRRORED_REPEAT
+};
+
+class Texture;
+
+
+/**
+Samplers are used to access texture data in shaders.  To use a sampler with a
+texture, bind it to the same texture unit.  Each texture has a default sampler
+which is used if no external sampler is bound.
+
+A texture is generally rendered at a size that's either smaller or larger than
+its native size, so that the texture coordinates do not exactly correspond to
+the texels of the texture.  The kind of filtering used, if any, is determined
+by the minification and magnification filter parameters.  The default is LINEAR
+for magnification and NEAREST_MIPMAP_LINEAR for minification.
+
+If texture coordinates fall outside of the principal range of the texture,
+wrapping is applied.  The default for all directions is REPEAT.
+*/
+class Sampler
+{
+public:
+       class Loader: public DataFile::ObjectLoader<Sampler>
+       {
+       public:
+               Loader(Sampler &);
+       private:
+               void init();
+
+               void filter(TextureFilter);
+               void mag_filter(TextureFilter);
+               void max_anisotropy(float);
+               void min_filter(TextureFilter);
+               void wrap(TextureWrap);
+               void wrap_r(TextureWrap);
+               void wrap_s(TextureWrap);
+               void wrap_t(TextureWrap);
+       };
+
+private:
+       enum ParameterMask
+       {
+               MIN_FILTER = 1,
+               MAG_FILTER = 2,
+               MAX_ANISOTROPY = 4,
+               WRAP_S = 8,
+               WRAP_T = 16,
+               WRAP_R = 32,
+               COMPARE = 64
+       };
+
+       unsigned id;
+       const Texture *owner;
+       TextureFilter min_filter;
+       TextureFilter mag_filter;
+       float max_anisotropy;
+       TextureWrap wrap_s;
+       TextureWrap wrap_t;
+       TextureWrap wrap_r;
+       bool compare;
+       Predicate cmp_func;
+       mutable int dirty_params;
+
+public:
+       Sampler();
+       Sampler(const Texture &);
+private:
+       void init();
+
+       void update_parameter(int) const;
+       void set_parameter_i(unsigned, int) const;
+       void set_parameter_f(unsigned, float) const;
+
+public:
+       void set_min_filter(TextureFilter);
+       void set_mag_filter(TextureFilter);
+
+       /** Sets filter for both minification and magnification.  If a mipmapping
+       filter is specified, LINEAR is used for magnification. */
+       void set_filter(TextureFilter);
+
+       TextureFilter get_min_filter() const { return min_filter; }
+       TextureFilter get_mag_filter() const { return mag_filter; }
+
+       void set_max_anisotropy(float);
+       float get_max_anisotropy() const { return max_anisotropy; }
+
+       /** Sets the wrapping mode for all coordinates. */
+       void set_wrap(TextureWrap);
+
+       void set_wrap_s(TextureWrap);
+       void set_wrap_t(TextureWrap);
+       void set_wrap_r(TextureWrap);
+
+       /** Disables depth comparison. */
+       void disable_compare();
+
+       /** Enables depth comparison and sets the compare function.  Only has an
+       effect when used with a depth texture.  When depth comparison is enabled,
+       the third component of the texture coordinate is compared against the texel
+       value, and the result is returned as the texture sample.*/
+       void set_compare(Predicate);
+
+       bool is_compare_enabled() const { return compare; }
+       Predicate get_compare_function() const { return cmp_func; }
+
+       void bind() const { bind_to(0); }
+       void bind_to(unsigned) const;
+
+       static const Sampler *current(unsigned = 0);
+       static void unbind() { unbind_from(0); }
+       static void unbind_from(unsigned);
+
+       void unload();
+};
+
+
+bool is_mipmapped(TextureFilter);
+
+void operator>>(const LexicalConverter &, TextureFilter &);
+void operator>>(const LexicalConverter &, TextureWrap &);
+
+} // namespace GL
+} // namespace Msp
+
+#endif