]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.h
Update and improve documentation
[libs/gl.git] / source / core / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/datafile/dynamicobjectloader.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
7 #include "pixelformat.h"
8 #include "resource.h"
9 #include "texture_backend.h"
10
11 namespace Msp {
12 namespace GL {
13
14 /**
15 Base class for textures.  Most operations are defined in subclasses.
16
17 Memory must be allocated for the texture by calling storage().  Each subclass
18 provides this function with parameters appropriate to that type of texture.
19 Contents can then be modified using the image() and sub_image() functions
20 provided by subclasses.
21
22 Most types of textures can consist of a pyramid of images, called a mipmap.
23 The dimensions of each mipmap level are half that of the previous level.
24
25 Textures can be used as either a data source or target for draw commands.  To
26 read from a texture in a shader, it must be paired with a Sampler to determine
27 how the texels are accessed.  To draw into a texture, it must be attached to a
28 Framebuffer.
29 */
30 class Texture: public TextureBackend, public Resource
31 {
32         friend TextureBackend;
33
34 protected:
35         class Loader: public DataFile::CollectionObjectLoader<Texture>
36         {
37         protected:
38                 unsigned levels;
39
40         public:
41                 Loader(Texture &t): Loader(t, 0) { }
42                 Loader(Texture &t, Collection &c): Loader(t, &c) { }
43         private:
44                 Loader(Texture &, Collection *);
45
46                 virtual void finish();
47
48         protected:
49                 void load_external_image(Graphics::Image &, const std::string &);
50
51         private:
52                 void external_image(const std::string &);
53                 void external_image_srgb(const std::string &);
54                 void external_image_common(const std::string &);
55                 void generate_mipmap(bool);
56                 void image_data(const std::string &);
57                 void mipmap_levels(unsigned);
58         };
59
60 public:
61         class GenericLoader: public DataFile::DynamicObjectLoader<Texture>
62         {
63                 friend class Texture;
64
65         public:
66                 GenericLoader(Collection &c): DynamicObjectLoader<Texture>(&c) { }
67
68         protected:
69                 virtual const TypeRegistry &get_type_registry() const { return get_texture_registry(); }
70         };
71
72 protected:
73         enum FormatSwizzle
74         {
75                 NO_SWIZZLE,
76                 R_TO_LUMINANCE,
77                 RG_TO_LUMINANCE_ALPHA,
78                 RGB_TO_BGR
79         };
80
81         PixelFormat format;
82         PixelFormat storage_fmt;
83         FormatSwizzle swizzle;
84         bool use_srgb_format;
85         bool auto_gen_mipmap;
86
87         Texture(unsigned);
88
89         void set_format(PixelFormat);
90
91 public:
92         PixelFormat get_format() const { return format; }
93
94         using TextureBackend::generate_mipmap;
95
96         /** Loads an image into the texture from a file. */
97         virtual void load_image(const std::string &, unsigned = 0);
98
99         /** Sets the texture's contents from an image.  If storage has not been
100         allocated yet, it will be set to match the image.  Otherwise the image must
101         be compatible with the existing storage.  Subclasses may impose restrictions
102         on the image's dimensions. */
103         virtual void image(const Graphics::Image &, unsigned = 0) = 0;
104
105         virtual std::size_t get_data_size() const { return 0; }
106
107         using TextureBackend::set_debug_name;
108
109 private:
110         static GenericLoader::TypeRegistry &get_texture_registry();
111 };
112
113 } // namespace GL
114 } // namespace Msp
115
116 #endif