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