]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.h
Check the flat qualifier from the correct member
[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 #include "sampler_backend.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 Stores settings affecting how values are obtained from textures in a shader.
52 Samplers are always used together with textures.
53
54 Texture coordinates are first transformed according to the wrap mode for each
55 axis.  One or more texel values are then read and combined according to the
56 filtering mode.
57 */
58 class Sampler: public SamplerBackend
59 {
60         friend SamplerBackend;
61
62 public:
63         class Loader: public DataFile::ObjectLoader<Sampler>
64         {
65         public:
66                 Loader(Sampler &);
67         private:
68                 void init();
69
70                 void border_color(float, float, float, float);
71                 void compare(Predicate);
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                 BORDER_COLOR = 64,
92                 COMPARE = 128
93         };
94
95         TextureFilter min_filter = NEAREST_MIPMAP_LINEAR;
96         TextureFilter mag_filter = LINEAR;
97         float max_anisotropy = 1.0f;
98         TextureWrap wrap_s = REPEAT;
99         TextureWrap wrap_t = REPEAT;
100         TextureWrap wrap_r = REPEAT;
101         Color border_color = { 0.0f, 0.0f, 0.0f, 0.0f };
102         bool compare = false;
103         Predicate cmp_func = LEQUAL;
104         mutable int dirty_params = 0;
105
106         void update() const;
107
108 public:
109         /** Sets filter to use when the texture is drawn at a size smaller than
110         original. */
111         void set_min_filter(TextureFilter);
112
113         /** Sets filter to use when the texture is drawn at a size larger than
114         original.  Mipmapped filters can't be used. */
115         void set_mag_filter(TextureFilter);
116
117         /** Sets filter for both minification and magnification.  If a mipmapping
118         filter is specified, LINEAR is used for magnification. */
119         void set_filter(TextureFilter);
120
121         TextureFilter get_min_filter() const { return min_filter; }
122         TextureFilter get_mag_filter() const { return mag_filter; }
123
124         /** Sets the maximum aspect ratio for anisotropic filtering.  If greater
125         than 1, filtering will consider more than than four samples when the texture
126         is drawn at an oblique angle. */
127         void set_max_anisotropy(float);
128
129         float get_max_anisotropy() const { return max_anisotropy; }
130
131         void set_wrap_s(TextureWrap);
132         void set_wrap_t(TextureWrap);
133         void set_wrap_r(TextureWrap);
134
135         /** Sets the wrapping mode for all coordinates. */
136         void set_wrap(TextureWrap);
137
138         /** Sets the border color for CLAMP_TO_BORDER wrap mode. */
139         void set_border_color(const Color &);
140
141         const Color &get_border_color() const { return border_color; }
142
143         /** Disables depth comparison. */
144         void disable_compare();
145
146         /** Enables depth comparison and sets the compare function.  Only has an
147         effect when used with a depth texture.  When depth comparison is enabled,
148         the last component of the texture coordinate is compared against the texel
149         value, and the result is returned as the texture sample.*/
150         void set_compare(Predicate);
151
152         bool is_compare_enabled() const { return compare; }
153         Predicate get_compare_function() const { return cmp_func; }
154
155         void refresh() const { if(dirty_params) update(); }
156
157         using SamplerBackend::set_debug_name;
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