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