]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.h
Make the sampler-related enums independent of OpenGL constants
[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 "color.h"
6 #include "gl.h"
7 #include "predicate.h"
8
9 namespace Msp {
10 namespace GL {
11
12 enum TextureFilter
13 {
14         /// No filtering
15         NEAREST,
16
17         /// Bilinear filtering
18         LINEAR,
19
20         /// Mipmapping without filtering
21         NEAREST_MIPMAP_NEAREST,
22
23         /// Linear filtering between two mipmap levels
24         NEAREST_MIPMAP_LINEAR,
25
26         /// Bilinear filtering on the closest mipmap level
27         LINEAR_MIPMAP_NEAREST,
28
29         /// Trilinear filtering between two mipmap levels
30         LINEAR_MIPMAP_LINEAR
31 };
32
33
34 enum TextureWrap
35 {
36         /// Tile the texture infinitely
37         REPEAT,
38
39         /// Extend the texels at the edge of the texture to infinity
40         CLAMP_TO_EDGE,
41
42         /// Sampling outside the texture will return border color
43         CLAMP_TO_BORDER,
44
45         /// Tile the texture, with every other repetition mirrored
46         MIRRORED_REPEAT
47 };
48
49
50 /**
51 Samplers are used to access texture data in shaders.  To use a sampler with a
52 texture, bind it to the same texture unit.  Each texture has a default sampler
53 which is used if no external sampler is bound.
54
55 A texture is generally rendered at a size that's either smaller or larger than
56 its native size, so that the texture coordinates do not exactly correspond to
57 the texels of the texture.  The kind of filtering used, if any, is determined
58 by the minification and magnification filter parameters.  The default is LINEAR
59 for magnification and NEAREST_MIPMAP_LINEAR for minification.
60
61 If texture coordinates fall outside of the principal range of the texture,
62 wrapping is applied.  The default for all directions is REPEAT.
63 */
64 class Sampler
65 {
66 public:
67         class Loader: public DataFile::ObjectLoader<Sampler>
68         {
69         public:
70                 Loader(Sampler &);
71         private:
72                 void init();
73
74                 void border_color(float, float, float, float);
75                 void compare(Predicate);
76                 void filter(TextureFilter);
77                 void mag_filter(TextureFilter);
78                 void max_anisotropy(float);
79                 void min_filter(TextureFilter);
80                 void wrap(TextureWrap);
81                 void wrap_r(TextureWrap);
82                 void wrap_s(TextureWrap);
83                 void wrap_t(TextureWrap);
84         };
85
86 private:
87         enum ParameterMask
88         {
89                 MIN_FILTER = 1,
90                 MAG_FILTER = 2,
91                 MAX_ANISOTROPY = 4,
92                 WRAP_S = 8,
93                 WRAP_T = 16,
94                 WRAP_R = 32,
95                 BORDER_COLOR = 64,
96                 COMPARE = 128
97         };
98
99         unsigned id;
100         TextureFilter min_filter;
101         TextureFilter mag_filter;
102         float max_anisotropy;
103         TextureWrap wrap_s;
104         TextureWrap wrap_t;
105         TextureWrap wrap_r;
106         Color border_color;
107         bool compare;
108         Predicate cmp_func;
109         mutable int dirty_params;
110
111 public:
112         Sampler();
113
114 private:
115         void update() 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         void set_border_color(const Color &);
139         const Color &get_border_color() const { return border_color; }
140
141         /** Disables depth comparison. */
142         void disable_compare();
143
144         /** Enables depth comparison and sets the compare function.  Only has an
145         effect when used with a depth texture.  When depth comparison is enabled,
146         the third component of the texture coordinate is compared against the texel
147         value, and the result is returned as the texture sample.*/
148         void set_compare(Predicate);
149
150         bool is_compare_enabled() const { return compare; }
151         Predicate get_compare_function() const { return cmp_func; }
152
153         void refresh() const { if(dirty_params) update(); }
154
155         unsigned get_id() const { return id; }
156
157         void set_debug_name(const std::string &);
158 };
159
160
161 bool is_mipmapped(TextureFilter);
162 GLenum get_gl_filter(TextureFilter);
163 GLenum get_gl_wrap(TextureWrap);
164
165 void operator>>(const LexicalConverter &, TextureFilter &);
166 void operator>>(const LexicalConverter &, TextureWrap &);
167
168 } // namespace GL
169 } // namespace Msp
170
171 #endif