]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.h
Mark constant data as const
[libs/gl.git] / source / core / 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 "gl.h"
7 #include "pixelformat.h"
8 #include "resource.h"
9
10 namespace Msp {
11 namespace GL {
12
13 /**
14 Base class for textures.  This class only defines operations common for all
15 texture types and is not instantiable.  For specifying images for textures,
16 see one of the dimensioned texture classes.
17
18 A texture can consinst of a stack of images, called a mipmap.  The dimensions
19 of each mipmap level are half that of the previous level.  The mipmap stack
20 can be used for texture minification; see the Sampler class for details.
21 */
22 class Texture: public Resource
23 {
24 protected:
25         class Loader: public DataFile::CollectionObjectLoader<Texture>
26         {
27         protected:
28                 unsigned levels;
29
30         public:
31                 Loader(Texture &);
32                 Loader(Texture &, Collection &);
33         private:
34                 void init();
35
36                 virtual void finish();
37
38         protected:
39                 void load_external_image(Graphics::Image &, const std::string &);
40
41         private:
42                 void external_image(const std::string &);
43                 void external_image_srgb(const std::string &);
44                 void external_image_common(const std::string &);
45                 void generate_mipmap(bool);
46                 void image_data(const std::string &);
47                 void mipmap_levels(unsigned);
48         };
49
50         enum FormatSwizzle
51         {
52                 NO_SWIZZLE,
53                 R_TO_LUMINANCE,
54                 RG_TO_LUMINANCE_ALPHA,
55                 RGB_TO_BGR
56         };
57
58         unsigned id;
59         GLenum target;
60         PixelFormat format;
61         PixelFormat storage_fmt;
62         FormatSwizzle swizzle;
63         bool use_srgb_format;
64         bool auto_gen_mipmap;
65         std::string debug_name;
66
67         static const int swizzle_orders[];
68         static Texture *scratch_binding;
69
70         Texture(GLenum, ResourceManager * = 0);
71         Texture(const Texture &);
72         Texture &operator=(const Texture &);
73 public:
74         ~Texture();
75
76 protected:
77         void generate_id();
78         void set_format(PixelFormat);
79         void apply_swizzle();
80         void set_parameter_i(GLenum, int) const;
81
82 public:
83         PixelFormat get_format() const { return format; }
84
85         void generate_mipmap();
86
87         /// Loads a Graphics::Image from a file and uploads it to the texture.
88         virtual void load_image(const std::string &, unsigned = 0);
89
90         /** Uploads an image to the texture.  If storage has not been defined, it
91         will be set to match the image.  Otherwise the image must be compatible
92         with the defined storage.  Semantics depend on the type of texture.  */
93         virtual void image(const Graphics::Image &, unsigned = 0) = 0;
94
95         GLenum get_target() const { return target; }
96         unsigned get_id() const { return id; }
97
98         virtual std::uint64_t get_data_size() const { return 0; }
99
100         void set_debug_name(const std::string &);
101
102 protected:
103         void bind_scratch();
104 public:
105         static void unbind_scratch();
106 };
107
108 } // namespace GL
109 } // namespace Msp
110
111 #endif