]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Implement 2D array textures
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
4 #include "bindable.h"
5 #include "error.h"
6 #include "pixelstore.h"
7 #include "texture3d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture3D::Texture3D(GLenum t):
15         Texture(t),
16         ifmt(RGB),
17         width(0),
18         height(0),
19         depth(0),
20         allocated(0)
21 { }
22
23 Texture3D::Texture3D():
24         Texture(GL_TEXTURE_3D),
25         ifmt(RGB),
26         width(0),
27         height(0),
28         depth(0),
29         allocated(0)
30 {
31         static Require _req(EXT_texture3D);
32 }
33
34 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
35 {
36         if(width>0)
37                 throw invalid_operation("Texture3D::storage");
38         if(wd==0 || ht==0 || dp==0)
39                 throw invalid_argument("Texture3D::storage");
40         require_pixelformat(fmt);
41
42         width = wd;
43         height = ht;
44         depth = dp;
45         ifmt = fmt;
46 }
47
48 void Texture3D::allocate(unsigned level)
49 {
50         if(allocated&(1<<level))
51                 return;
52
53         PixelFormat base_fmt = get_base_pixelformat(ifmt);
54         image(level, base_fmt, get_alloc_type(base_fmt), 0);
55 }
56
57 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
58 {
59         if(width==0 || height==0 || depth==0)
60                 throw invalid_operation("Texture3D::image");
61
62         unsigned w = width;
63         unsigned h = height;
64         unsigned d = depth;
65         get_level_size(level, w, h, d);
66
67         BindRestore _bind(this);
68         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
69
70         allocated |= 1<<level;
71         if(gen_mipmap && level==0)
72         {
73                 auto_generate_mipmap();
74                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
75                 allocated |= (1<<level)-1;
76         }
77 }
78
79 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
80 {
81         if(width==0 || height==0 || depth==0)
82                 throw invalid_operation("Texture3D::image");
83
84         allocate(level);
85
86         BindRestore _bind(this);
87         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
88 }
89
90 void Texture3D::load_image(const string &fn, int dp)
91 {
92         Graphics::Image img;
93         img.load_file(fn);
94
95         unsigned w = img.get_width();
96         unsigned h = img.get_height();
97         unsigned d = 1;
98
99         if(dp==-1)
100         {
101                 if(h%w)
102                         throw incompatible_data("Texture3D::load_image");
103                 d = h/w;
104                 h = w;
105         }
106         else if(dp==-2)
107         {
108                 for(d=h; d*d>h; d>>=2) ;
109                 for(; d*d<h; ++d) ;
110                 if(d*d!=h)
111                         throw incompatible_data("Texture3D::load_image");
112                 h = d;
113         }
114         else if(dp>0)
115         {
116                 d = dp;
117                 if(h%d)
118                         throw incompatible_data("Texture3D::load_image");
119                 h /= d;
120         }
121         else
122                 throw invalid_argument("Texture3D::load_image");
123
124         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
125         if(width==0)
126                 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
127         else if(w!=width || h!=height || d!=depth)
128                 throw incompatible_data("Texture3D::load_image");
129
130         PixelStore pstore = PixelStore::from_image(img);
131         BindRestore _bind_ps(pstore);
132
133         image(0, fmt, UNSIGNED_BYTE, img.get_data());
134 }
135
136 void Texture3D::image(const Graphics::Image &img, bool srgb)
137 {
138         unsigned w = img.get_width();
139         unsigned h = img.get_height();
140         unsigned d = 1;
141
142         if(depth)
143         {
144                 if(h%depth)
145                         throw incompatible_data("Texture3D::load_image");
146                 h /= depth;
147                 d = depth;
148         }
149         else
150         {
151                 if(h%w)
152                         throw incompatible_data("Texture3D::load_image");
153                 d = h/w;
154                 h = w;
155         }
156
157         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
158         if(width==0)
159                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
160         else if(w!=width || h!=height || d!=depth)
161                 throw incompatible_data("Texture3D::load_image");
162
163         PixelStore pstore = PixelStore::from_image(img);
164         BindRestore _bind_ps(pstore);
165
166         image(0, fmt, UNSIGNED_BYTE, img.get_data());
167 }
168
169 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
170 {
171         w >>= level;
172         h >>= level;
173         if(target!=GL_TEXTURE_2D_ARRAY)
174                 d >>= level;
175
176         if(!w && (h || d))
177                 w = 1;
178         if(!h && (w || d))
179                 h = 1;
180         if(!d && (w || h))
181                 d = 1;
182 }
183
184 UInt64 Texture3D::get_data_size() const
185 {
186         return id ? width*height*depth*get_pixel_size(ifmt) : 0;
187 }
188
189
190 Texture3D::Loader::Loader(Texture3D &t):
191         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
192 {
193         init();
194 }
195
196 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
197         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
198 {
199         init();
200 }
201
202 void Texture3D::Loader::init()
203 {
204         add("raw_data", &Loader::raw_data);
205         add("storage", &Loader::storage);
206 }
207
208 void Texture3D::Loader::raw_data(const string &data)
209 {
210         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
211 }
212
213 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
214 {
215         if(srgb)
216                 fmt = get_srgb_pixelformat(fmt);
217         obj.storage(fmt, w, h, d);
218 }
219
220 } // namespace GL
221 } // namespace Msp