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