]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.h
Remove the separate allocation step from textures and buffers
[libs/gl.git] / source / core / texture3d.h
1 #ifndef MSP_GL_TEXTURE3D_H_
2 #define MSP_GL_TEXTURE3D_H_
3
4 #include <string>
5 #include <msp/linal/vector.h>
6 #include "texture.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /**
12 Three-dimensional texture.  Consists of an array of texels in the shape of a
13 right cuboid.  Texture coordinates have a principal range of [0, 1].
14 */
15 class Texture3D: public Texture
16 {
17 public:
18         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>
19         {
20         public:
21                 Loader(Texture3D &);
22                 Loader(Texture3D &, Collection &);
23         private:
24                 void init();
25
26                 void raw_data(const std::string &);
27                 void storage(PixelFormat, unsigned, unsigned, unsigned);
28                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned, unsigned);
29         };
30
31 protected:
32         unsigned width;
33         unsigned height;
34         unsigned depth;
35         unsigned levels;
36
37         Texture3D(GLenum);
38 public:
39         Texture3D();
40
41         /** Defines storage structure for the texture.  If lv is zero, the number
42         of mipmap levels is automatically determined from storage dimensions.
43
44         Must be called before an image can be uploaded.  Once storage is defined,
45         it can't be changed. */
46         void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv = 0);
47
48         /** Updates the contents of the entire texture.  Storage must be defined
49         beforehand.  The image data must have dimensions and format matching the
50         defined storage. */
51         void image(unsigned level, const void *data);
52
53         /** Updates a cuboid-shaped region of the texture.  Storage must be defined
54         beforehand.  The image data must be in a format mathing the defined storage
55         and the update region must be fully inside the texture. */
56         void sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data);
57
58         /** Updates the contents of the entire texture from an image.  If storage
59         has not been defined, it will be set to match the image.  In this case the
60         image will be treated as a stack of square layers and its height must be
61         divisible by its width.  Otherwise the image must match the defined
62         storage. */
63         virtual void image(const Graphics::Image &, unsigned = 0);
64
65         using Texture::image;
66
67         unsigned get_width() const { return width; }
68         unsigned get_height() const { return height; }
69         unsigned get_depth() const { return depth; }
70 protected:
71         unsigned get_n_levels() const;
72         LinAl::Vector<unsigned, 3> get_level_size(unsigned) const;
73
74 public:
75         virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
76         virtual std::uint64_t get_data_size() const;
77         virtual void unload() { }
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif