]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
Move filter heuristic for mipmap levels to Texture::Loader
[libs/gl.git] / source / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/core/attributes.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
7 #include "datatype.h"
8 #include "gl.h"
9 #include "pixelformat.h"
10 #include "predicate.h"
11 #include "resource.h"
12
13 namespace Msp {
14 namespace GL {
15
16 enum TextureFilter
17 {
18         /// No filtering
19         NEAREST = GL_NEAREST,
20
21         /// Bilinear filtering
22         LINEAR = GL_LINEAR,
23
24         /// Mipmapping without filtering
25         NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
26
27         /// Linear filtering between two mipmap levels
28         NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
29
30         /// Bilinear filtering on the closest mipmap level
31         LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
32
33         /// Trilinear filtering between two mipmap levels
34         LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
35 };
36
37
38 enum TextureWrap
39 {
40         /// Tile the texture infinitely
41         REPEAT = GL_REPEAT,
42
43         /// Extend the texels at the edge of the texture to infinity
44         CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
45
46         /// Tile the texture, with every other repetition mirrored
47         MIRRORED_REPEAT = GL_MIRRORED_REPEAT
48 };
49
50
51 /**
52 Base class for textures.  This class only defines operations common for all
53 texture types and is not instantiable.  For specifying images for textures, see
54 one of the dimensioned texture classes.
55
56 A texture is generally rendered at a size that's either smaller or larger than
57 its native size, so that the texture coordinates do not exactly correspond to
58 the texels of the texture.  The kind of filtering used, if any, is determined
59 by the minification and magnification filter parameters.  The default is LINEAR
60 for magnification and NEAREST_MIPMAP_LINEAR for minification.
61
62 When a mipmapped filter is in use, the texture consists of a stack of mipmap
63 images.  Level 0 is the base image.  Each level above 0 has half the size of
64 the previous level, rounded down and clamped to 1.  The level with size 1 in
65 all dimensions is the last mipmap level.  All levels must be allocated for the
66 texture to be usable.
67
68 If texture coordinates fall outside of the principal range of the texture,
69 wrapping is applied.  The default for all directions is REPEAT.
70 */
71 class Texture: public Resource
72 {
73 protected:
74         class Loader: public DataFile::CollectionObjectLoader<Texture>
75         {
76         protected:
77                 unsigned levels;
78                 bool srgb;
79
80         public:
81                 Loader(Texture &);
82                 Loader(Texture &, Collection &);
83         private:
84                 void init();
85
86                 unsigned get_levels() const;
87
88                 void external_image(const std::string &);
89                 void filter(TextureFilter);
90                 void generate_mipmap(bool);
91                 void image_data(const std::string &);
92                 void mag_filter(TextureFilter);
93                 void max_anisotropy(float);
94                 void min_filter(TextureFilter);
95                 void mipmap_levels(unsigned);
96                 void wrap(TextureWrap);
97                 void wrap_r(TextureWrap);
98                 void wrap_s(TextureWrap);
99                 void wrap_t(TextureWrap);
100         };
101
102         enum ParameterMask
103         {
104                 MIN_FILTER = 1,
105                 MAG_FILTER = 2,
106                 WRAP_S = 4,
107                 WRAP_T = 8,
108                 WRAP_R = 16,
109                 COMPARE = 64,
110                 COMPARE_FUNC = 128,
111                 MAX_ANISOTROPY = 256,
112                 FORMAT_SWIZZLE = 512
113         };
114
115         enum FormatSwizzle
116         {
117                 NO_SWIZZLE,
118                 R_TO_LUMINANCE,
119                 RG_TO_LUMINANCE_ALPHA
120         };
121
122         unsigned id;
123         GLenum target;
124         PixelFormat ifmt;
125         FormatSwizzle swizzle;
126         TextureFilter min_filter;
127         TextureFilter mag_filter;
128         float max_anisotropy;
129         TextureWrap wrap_s;
130         TextureWrap wrap_t;
131         TextureWrap wrap_r;
132         bool auto_gen_mipmap;
133         bool compare;
134         Predicate cmp_func;
135         mutable int dirty_params;
136
137         static int swizzle_orders[];
138
139         Texture(GLenum, ResourceManager * = 0);
140         Texture(const Texture &);
141         Texture &operator=(const Texture &);
142 public:
143         ~Texture();
144
145 protected:
146         static DataType get_alloc_type(PixelFormat);
147         void set_internal_format(PixelFormat);
148         PixelFormat get_upload_format(PixelFormat) const;
149
150         void update_parameter(int) const;
151         void set_parameter_i(GLenum, int) const;
152         void set_parameter_f(GLenum, float) const;
153 public:
154         void set_min_filter(TextureFilter);
155         void set_mag_filter(TextureFilter);
156
157         /** Sets filter for both minification and magnification.  Since mipmapping
158         is not applicable to magnification, LINEAR is used instead. */
159         void set_filter(TextureFilter);
160
161         DEPRECATED void set_mipmap_levels(unsigned) { }
162
163         void set_max_anisotropy(float);
164
165         /** Sets the wrapping mode for all coordinates. */
166         void set_wrap(TextureWrap);
167
168         void set_wrap_s(TextureWrap);
169         void set_wrap_t(TextureWrap);
170         void set_wrap_r(TextureWrap);
171
172         static bool can_generate_mipmap();
173
174         void generate_mipmap();
175
176         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
177         when a texture image is uploaded. */
178         void set_auto_generate_mipmap(bool);
179
180         /// Deprecated.  Use set_auto_generate_mipmap instead.
181         DEPRECATED void set_generate_mipmap(bool g) { set_auto_generate_mipmap(g); }
182
183         /** Sets depth texture comparison.  Has no effect on other formats.  When
184         comparison is enabled, the third component of the texture coordinate is
185         compared against the texel value, and the result is returned as the texture
186         sample. */
187         void set_compare_enabled(bool);
188
189         /** Sets the function to use for depth comparison. */
190         void set_compare_func(Predicate);
191
192         /// Loads a Graphics::Image from a file and uploads it to the texture.
193         virtual void load_image(const std::string &, bool srgb = false);
194
195         virtual void load_image(const std::string &, unsigned, bool srgb = false);
196
197         /** Uploads an image to the texture.  If storage has not been defined, it
198         will be set to match the image.  Otherwise the image must be compatible
199         with the defined storage.  Semantics depend on the type of texture.
200
201         If srgb is true and storage is determined by this call, then an sRGB pixel
202         format will be used. */
203         virtual void image(const Graphics::Image &, bool srgb = false);
204
205         virtual void image(const Graphics::Image &, unsigned, bool srgb = false) = 0;
206
207         GLenum get_target() const { return target; }
208         unsigned get_id() const { return id; }
209
210         void bind() const { bind_to(0); }
211         void bind_to(unsigned) const;
212
213         static const Texture *current(unsigned = 0);
214         static void unbind() { unbind_from(0); }
215         static void unbind_from(unsigned);
216
217         virtual UInt64 get_data_size() const { return 0; }
218 };
219
220
221 bool is_mipmapped(TextureFilter);
222
223 } // namespace GL
224 } // namespace Msp
225
226 #endif