]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.h
Rearrange soucre files into subdirectories
[libs/gl.git] / source / core / sampler.h
1 #ifndef MSP_GL_SAMPLER_H_
2 #define MSP_GL_SAMPLER_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "gl.h"
6 #include "predicate.h"
7
8 namespace Msp {
9 namespace GL {
10
11 enum TextureFilter
12 {
13         /// No filtering
14         NEAREST = GL_NEAREST,
15
16         /// Bilinear filtering
17         LINEAR = GL_LINEAR,
18
19         /// Mipmapping without filtering
20         NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
21
22         /// Linear filtering between two mipmap levels
23         NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
24
25         /// Bilinear filtering on the closest mipmap level
26         LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
27
28         /// Trilinear filtering between two mipmap levels
29         LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
30 };
31
32
33 enum TextureWrap
34 {
35         /// Tile the texture infinitely
36         REPEAT = GL_REPEAT,
37
38         /// Extend the texels at the edge of the texture to infinity
39         CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
40
41         /// Tile the texture, with every other repetition mirrored
42         MIRRORED_REPEAT = GL_MIRRORED_REPEAT
43 };
44
45 class Texture;
46
47
48 /**
49 Samplers are used to access texture data in shaders.  To use a sampler with a
50 texture, bind it to the same texture unit.  Each texture has a default sampler
51 which is used if no external sampler is bound.
52
53 A texture is generally rendered at a size that's either smaller or larger than
54 its native size, so that the texture coordinates do not exactly correspond to
55 the texels of the texture.  The kind of filtering used, if any, is determined
56 by the minification and magnification filter parameters.  The default is LINEAR
57 for magnification and NEAREST_MIPMAP_LINEAR for minification.
58
59 If texture coordinates fall outside of the principal range of the texture,
60 wrapping is applied.  The default for all directions is REPEAT.
61 */
62 class Sampler
63 {
64 public:
65         class Loader: public DataFile::ObjectLoader<Sampler>
66         {
67         public:
68                 Loader(Sampler &);
69         private:
70                 void init();
71
72                 void filter(TextureFilter);
73                 void mag_filter(TextureFilter);
74                 void max_anisotropy(float);
75                 void min_filter(TextureFilter);
76                 void wrap(TextureWrap);
77                 void wrap_r(TextureWrap);
78                 void wrap_s(TextureWrap);
79                 void wrap_t(TextureWrap);
80         };
81
82 private:
83         enum ParameterMask
84         {
85                 MIN_FILTER = 1,
86                 MAG_FILTER = 2,
87                 MAX_ANISOTROPY = 4,
88                 WRAP_S = 8,
89                 WRAP_T = 16,
90                 WRAP_R = 32,
91                 COMPARE = 64
92         };
93
94         unsigned id;
95         const Texture *owner;
96         TextureFilter min_filter;
97         TextureFilter mag_filter;
98         float max_anisotropy;
99         TextureWrap wrap_s;
100         TextureWrap wrap_t;
101         TextureWrap wrap_r;
102         bool compare;
103         Predicate cmp_func;
104         mutable int dirty_params;
105
106 public:
107         Sampler();
108         Sampler(const Texture &);
109 private:
110         void init();
111
112         void update_parameter(int) const;
113         void set_parameter_i(unsigned, int) const;
114         void set_parameter_f(unsigned, float) const;
115
116 public:
117         void set_min_filter(TextureFilter);
118         void set_mag_filter(TextureFilter);
119
120         /** Sets filter for both minification and magnification.  If a mipmapping
121         filter is specified, LINEAR is used for magnification. */
122         void set_filter(TextureFilter);
123
124         TextureFilter get_min_filter() const { return min_filter; }
125         TextureFilter get_mag_filter() const { return mag_filter; }
126
127         void set_max_anisotropy(float);
128         float get_max_anisotropy() const { return max_anisotropy; }
129
130         /** Sets the wrapping mode for all coordinates. */
131         void set_wrap(TextureWrap);
132
133         void set_wrap_s(TextureWrap);
134         void set_wrap_t(TextureWrap);
135         void set_wrap_r(TextureWrap);
136
137         /** Disables depth comparison. */
138         void disable_compare();
139
140         /** Enables depth comparison and sets the compare function.  Only has an
141         effect when used with a depth texture.  When depth comparison is enabled,
142         the third component of the texture coordinate is compared against the texel
143         value, and the result is returned as the texture sample.*/
144         void set_compare(Predicate);
145
146         bool is_compare_enabled() const { return compare; }
147         Predicate get_compare_function() const { return cmp_func; }
148
149         void bind() const { bind_to(0); }
150         void bind_to(unsigned) const;
151
152         static const Sampler *current(unsigned = 0);
153         static void unbind() { unbind_from(0); }
154         static void unbind_from(unsigned);
155
156         void unload();
157 };
158
159
160 bool is_mipmapped(TextureFilter);
161
162 void operator>>(const LexicalConverter &, TextureFilter &);
163 void operator>>(const LexicalConverter &, TextureWrap &);
164
165 } // namespace GL
166 } // namespace Msp
167
168 #endif