]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.h
Check the flat qualifier from the correct member
[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/linal/vector.h>
6 #include "texture2d_backend.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /**
12 Two-dimensional texture, consisting of a rectangular array of texels.
13 */
14 class Texture2D: public Texture2DBackend
15 {
16         friend Texture2DBackend;
17
18 public:
19         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
20         {
21         public:
22                 Loader(Texture2D &);
23                 Loader(Texture2D &, Collection &);
24         private:
25                 void init();
26
27                 void storage(PixelFormat, unsigned, unsigned);
28                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
29         };
30
31         /**
32         An RAII handle for asynchronously writing texel data into a texture.
33         */
34         class AsyncTransfer: public Texture2DBackend::AsyncTransfer
35         {
36                 friend Texture2DBackend;
37                 friend class Texture2D;
38                 friend class Texture2DBackend::AsyncTransfer;
39
40         private:
41                 Texture2D *texture = 0;
42                 unsigned level = 0;
43                 unsigned x = 0;
44                 unsigned y = 0;
45                 unsigned width = 0;
46                 unsigned height = 0;
47                 std::size_t data_size = 0;
48                 void *dest_addr = 0;
49
50                 AsyncTransfer(Texture2D &, unsigned, unsigned, unsigned, unsigned, unsigned);
51         public:
52                 AsyncTransfer() = default;
53                 AsyncTransfer(AsyncTransfer &&);
54                 AsyncTransfer &operator=(AsyncTransfer &&);
55                 ~AsyncTransfer();
56
57         public:
58                 /** Returns an address for writing the texel data.  It should not be used
59                 beyond the lifetime of the object. */
60                 void *get_address() { return dest_addr; }
61         };
62
63 private:
64         class AsyncLoader;
65
66         unsigned width = 0;
67         unsigned height = 0;
68
69 public:
70         Texture2D() = default;
71         Texture2D(Texture2D &&) = default;
72         virtual ~Texture2D();
73
74         /** Sets storage format and dimensions and allocates memory for the texture.
75         If lv is zero, a complete mipmap pyramid is automatically created.  Storage
76         cannot be changed once set. */
77         void storage(PixelFormat, unsigned wd, unsigned ht, unsigned lv = 0);
78
79         void image(unsigned level, const void *) override;
80
81         /** Replaces a rectangular region of the texture.  Allocated storage must
82         exist.  The image data is interpreted according to the storage format and
83         the region must be fully inside the selected mipmap level. */
84         void sub_image(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *);
85
86         AsyncTransfer sub_image_async(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht);
87
88         virtual void image(const Graphics::Image &, unsigned = 0);
89
90         unsigned get_width() const { return width; }
91         unsigned get_height() const { return height; }
92
93 private:
94         unsigned get_n_levels() const;
95         LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
96
97 public:
98         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
99 };
100
101 } // namespace GL
102 } // namespace Msp
103
104 #endif