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