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