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