]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.h
Refactor uploading texture data from a buffer
[libs/gl.git] / source / core / texture2d.h
1 #ifndef MSP_GL_TEXTURE2D_H_
2 #define MSP_GL_TEXTURE2D_H_
3
4 #include <string>
5 #include <msp/graphics/image.h>
6 #include <msp/linal/vector.h>
7 #include "texture.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Buffer;
13
14 /**
15 Two-dimensional texture.  Consists of an array of texels in the shape of a
16 rectangle.  Texture coordinate have a range of [0, 1].  Coordinates outside of
17 this range are subject to wrapping.  This is the most common type of texture.
18 */
19 class Texture2D: public Texture
20 {
21 public:
22         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
23         {
24         public:
25                 Loader(Texture2D &);
26                 Loader(Texture2D &, Collection &);
27         private:
28                 void init();
29
30                 void raw_data(const std::string &);
31                 void storage(PixelFormat, unsigned, unsigned);
32                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
33         };
34
35 private:
36         class AsyncLoader;
37
38         unsigned width;
39         unsigned height;
40         unsigned levels;
41
42 public:
43         Texture2D(ResourceManager * = 0);
44         virtual ~Texture2D();
45
46         /** Defines storage structure for the texture.  If lv is zero, the number
47         of mipmap levels is automatically determined from storage dimensions.
48
49         Must be called before an image can be uploaded.  Once storage is defined,
50         it can't be changed. */
51         void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv = 0);
52
53         /** Updates the contents of the entire texture.  Storage must be defined
54         beforehand.  The image data must have dimensions and format matching the
55         defined storage. */
56         virtual void image(unsigned level, const void *data);
57
58         /** Updates a rectangular region of the texture.  Storage must be defined
59         beforehand.  The image data must be in a format mathing the defined storage
60         and the update region must be fully inside the texture. */
61         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data);
62
63 private:
64         void sub_image(unsigned, int, int, unsigned, unsigned, const Buffer &, unsigned);
65
66 public:
67         /** Updates the contents of the entire texture from an image.  If storage
68         has not been defined, it will be set to match the image.  Otherwise the
69         image must match the defined storage. */
70         virtual void image(const Graphics::Image &, unsigned lv = 0);
71
72         using Texture::image;
73
74         unsigned get_width() const { return width; }
75         unsigned get_height() const { return height; }
76
77 private:
78         unsigned get_n_levels() const;
79         LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
80
81 public:
82         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
83         virtual std::uint64_t get_data_size() const;
84         virtual void unload();
85 };
86
87 } // namespace GL
88 } // namespace Msp
89
90 #endif