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