]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.h
Add missing keyword to set sampler compare mode from datafile
[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 compare(Predicate);
73                 void filter(TextureFilter);
74                 void mag_filter(TextureFilter);
75                 void max_anisotropy(float);
76                 void min_filter(TextureFilter);
77                 void wrap(TextureWrap);
78                 void wrap_r(TextureWrap);
79                 void wrap_s(TextureWrap);
80                 void wrap_t(TextureWrap);
81         };
82
83 private:
84         enum ParameterMask
85         {
86                 MIN_FILTER = 1,
87                 MAG_FILTER = 2,
88                 MAX_ANISOTROPY = 4,
89                 WRAP_S = 8,
90                 WRAP_T = 16,
91                 WRAP_R = 32,
92                 COMPARE = 64
93         };
94
95         unsigned id;
96         const Texture *owner;
97         TextureFilter min_filter;
98         TextureFilter mag_filter;
99         float max_anisotropy;
100         TextureWrap wrap_s;
101         TextureWrap wrap_t;
102         TextureWrap wrap_r;
103         bool compare;
104         Predicate cmp_func;
105         mutable int dirty_params;
106
107 public:
108         Sampler();
109         Sampler(const Texture &);
110 private:
111         void init();
112
113         void update_parameter(int) const;
114         void set_parameter_i(unsigned, int) const;
115         void set_parameter_f(unsigned, float) const;
116
117 public:
118         void set_min_filter(TextureFilter);
119         void set_mag_filter(TextureFilter);
120
121         /** Sets filter for both minification and magnification.  If a mipmapping
122         filter is specified, LINEAR is used for magnification. */
123         void set_filter(TextureFilter);
124
125         TextureFilter get_min_filter() const { return min_filter; }
126         TextureFilter get_mag_filter() const { return mag_filter; }
127
128         void set_max_anisotropy(float);
129         float get_max_anisotropy() const { return max_anisotropy; }
130
131         /** Sets the wrapping mode for all coordinates. */
132         void set_wrap(TextureWrap);
133
134         void set_wrap_s(TextureWrap);
135         void set_wrap_t(TextureWrap);
136         void set_wrap_r(TextureWrap);
137
138         /** Disables depth comparison. */
139         void disable_compare();
140
141         /** Enables depth comparison and sets the compare function.  Only has an
142         effect when used with a depth texture.  When depth comparison is enabled,
143         the third component of the texture coordinate is compared against the texel
144         value, and the result is returned as the texture sample.*/
145         void set_compare(Predicate);
146
147         bool is_compare_enabled() const { return compare; }
148         Predicate get_compare_function() const { return cmp_func; }
149
150         void bind() const { bind_to(0); }
151         void bind_to(unsigned) const;
152
153         static const Sampler *current(unsigned = 0);
154         static void unbind() { unbind_from(0); }
155         static void unbind_from(unsigned);
156
157         void unload();
158 };
159
160
161 bool is_mipmapped(TextureFilter);
162
163 void operator>>(const LexicalConverter &, TextureFilter &);
164 void operator>>(const LexicalConverter &, TextureWrap &);
165
166 } // namespace GL
167 } // namespace Msp
168
169 #endif