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