1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
4 #include <msp/datafile/dynamicobjectloader.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
7 #include "pixelformat.h"
9 #include "texture_backend.h"
15 Base class for textures. Most operations are defined in subclasses.
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.
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.
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
30 class Texture: public TextureBackend, public Resource
32 friend TextureBackend;
35 class Loader: public DataFile::CollectionObjectLoader<Texture>
41 Loader(Texture &t): Loader(t, 0) { }
42 Loader(Texture &t, Collection &c): Loader(t, &c) { }
44 Loader(Texture &, Collection *);
46 virtual void finish();
49 void load_external_image(Graphics::Image &, const std::string &);
52 void external_data(const std::string &);
53 void external_image(bool, const std::string &);
54 void generate_mipmap(bool);
55 void image_data(const std::string &);
56 void mipmap_levels(unsigned);
57 void raw_data(const std::string &);
61 class GenericLoader: public DataFile::DynamicObjectLoader<Texture>
66 GenericLoader(Collection &c): DynamicObjectLoader<Texture>(&c) { }
69 virtual const TypeRegistry &get_type_registry() const { return get_texture_registry(); }
73 PixelFormat format = NO_PIXELFORMAT;
74 PixelFormat storage_fmt = NO_PIXELFORMAT;
75 ComponentSwizzle swizzle = NO_SWIZZLE;
76 unsigned n_levels = 0;
77 bool use_srgb_format = false;
78 bool auto_gen_mipmap = false;
82 void set_format(PixelFormat);
83 static unsigned count_levels(unsigned);
85 void stage_pixels(void *, const void *, std::size_t);
88 PixelFormat get_format() const { return format; }
90 using TextureBackend::generate_mipmap;
92 /** Replaces contents of an entire mipmap level. Allocated storage must
93 exist. The image data is interpreted according to the storage format and
94 must have size matching the selected mipmap level. */
95 virtual void image(unsigned level, const void *) = 0;
97 /** Loads an image into the texture from a file. */
98 virtual void load_image(const std::string &, unsigned = 0);
100 /** Sets the texture's contents from an image. If storage has not been
101 allocated yet, it will be set to match the image. Otherwise the image must
102 be compatible with the existing storage. Subclasses may impose restrictions
103 on the image's dimensions. */
104 virtual void image(const Graphics::Image &, unsigned = 0) = 0;
106 virtual std::size_t get_data_size() const { return 0; }
108 using TextureBackend::set_debug_name;
111 static GenericLoader::TypeRegistry &get_texture_registry();