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