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