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