X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fsampler.h;h=c9f8ec963bd462ea3bf978a3990b0ee481d55986;hp=bb02269f8c0ec9bcfd4408b3e48fa49702b90e06;hb=HEAD;hpb=dbc91b65728ab9c0e574bb1127cfe4d2da55de7f diff --git a/source/core/sampler.h b/source/core/sampler.h index bb02269f..c9f8ec96 100644 --- a/source/core/sampler.h +++ b/source/core/sampler.h @@ -3,8 +3,8 @@ #include #include "color.h" -#include "gl.h" #include "predicate.h" +#include "sampler_backend.h" namespace Msp { namespace GL { @@ -12,59 +12,53 @@ namespace GL { enum TextureFilter { /// No filtering - NEAREST = GL_NEAREST, + NEAREST, /// Bilinear filtering - LINEAR = GL_LINEAR, + LINEAR, /// Mipmapping without filtering - NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST, + NEAREST_MIPMAP_NEAREST, /// Linear filtering between two mipmap levels - NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR, + NEAREST_MIPMAP_LINEAR, /// Bilinear filtering on the closest mipmap level - LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST, + LINEAR_MIPMAP_NEAREST, /// Trilinear filtering between two mipmap levels - LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR + LINEAR_MIPMAP_LINEAR }; enum TextureWrap { /// Tile the texture infinitely - REPEAT = GL_REPEAT, + REPEAT, /// Extend the texels at the edge of the texture to infinity - CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE, + CLAMP_TO_EDGE, /// Sampling outside the texture will return border color - CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER, + CLAMP_TO_BORDER, /// Tile the texture, with every other repetition mirrored - MIRRORED_REPEAT = GL_MIRRORED_REPEAT + 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. +Stores settings affecting how values are obtained from textures in a shader. +Samplers are always used together with textures. + +Texture coordinates are first transformed according to the wrap mode for each +axis. One or more texel values are then read and combined according to the +filtering mode. */ -class Sampler +class Sampler: public SamplerBackend { + friend SamplerBackend; + public: class Loader: public DataFile::ObjectLoader { @@ -98,29 +92,26 @@ private: COMPARE = 128 }; - unsigned id; - TextureFilter min_filter; - TextureFilter mag_filter; - float max_anisotropy; - TextureWrap wrap_s; - TextureWrap wrap_t; - TextureWrap wrap_r; - Color border_color; - bool compare; - Predicate cmp_func; - mutable int dirty_params; - -public: - Sampler(); + TextureFilter min_filter = NEAREST_MIPMAP_LINEAR; + TextureFilter mag_filter = LINEAR; + float max_anisotropy = 1.0f; + TextureWrap wrap_s = REPEAT; + TextureWrap wrap_t = REPEAT; + TextureWrap wrap_r = REPEAT; + Color border_color = { 0.0f, 0.0f, 0.0f, 0.0f }; + bool compare = false; + Predicate cmp_func = LEQUAL; + mutable int dirty_params = 0; -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; + void update() const; public: + /** Sets filter to use when the texture is drawn at a size smaller than + original. */ void set_min_filter(TextureFilter); + + /** Sets filter to use when the texture is drawn at a size larger than + original. Mipmapped filters can't be used. */ void set_mag_filter(TextureFilter); /** Sets filter for both minification and magnification. If a mipmapping @@ -130,17 +121,23 @@ public: TextureFilter get_min_filter() const { return min_filter; } TextureFilter get_mag_filter() const { return mag_filter; } + /** Sets the maximum aspect ratio for anisotropic filtering. If greater + than 1, filtering will consider more than than four samples when the texture + is drawn at an oblique angle. */ void set_max_anisotropy(float); - float get_max_anisotropy() const { return max_anisotropy; } - /** Sets the wrapping mode for all coordinates. */ - void set_wrap(TextureWrap); + float get_max_anisotropy() const { return max_anisotropy; } void set_wrap_s(TextureWrap); void set_wrap_t(TextureWrap); void set_wrap_r(TextureWrap); + /** Sets the wrapping mode for all coordinates. */ + void set_wrap(TextureWrap); + + /** Sets the border color for CLAMP_TO_BORDER wrap mode. */ void set_border_color(const Color &); + const Color &get_border_color() const { return border_color; } /** Disables depth comparison. */ @@ -148,21 +145,16 @@ public: /** 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 + the last 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 refresh() const { if(dirty_params) update(); } - void set_debug_name(const std::string &); + using SamplerBackend::set_debug_name; };