]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Immediate update camera aspect in View::set_camera
[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, get_upload_format(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         fmt = get_upload_format(fmt);
105         if(ARB_direct_state_access)
106                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
107         else
108                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
109
110         if(gen_mipmap && level==0)
111                 auto_generate_mipmap();
112 }
113
114 void Texture3D::load_image(const string &fn, int dp)
115 {
116         Graphics::Image img;
117         img.load_file(fn);
118
119         unsigned w = img.get_width();
120         unsigned h = img.get_height();
121         unsigned d = 1;
122
123         if(dp==-1)
124         {
125                 if(h%w)
126                         throw incompatible_data("Texture3D::load_image");
127                 d = h/w;
128                 h = w;
129         }
130         else if(dp==-2)
131         {
132                 for(d=h; d*d>h; d>>=2) ;
133                 for(; d*d<h; ++d) ;
134                 if(d*d!=h)
135                         throw incompatible_data("Texture3D::load_image");
136                 h = d;
137         }
138         else if(dp>0)
139         {
140                 d = dp;
141                 if(h%d)
142                         throw incompatible_data("Texture3D::load_image");
143                 h /= d;
144         }
145         else
146                 throw invalid_argument("Texture3D::load_image");
147
148         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
149         if(width==0)
150                 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
151         else if(w!=width || h!=height || d!=depth)
152                 throw incompatible_data("Texture3D::load_image");
153
154         PixelStore pstore = PixelStore::from_image(img);
155         BindRestore _bind_ps(pstore);
156
157         image(0, fmt, UNSIGNED_BYTE, img.get_data());
158 }
159
160 void Texture3D::image(const Graphics::Image &img, bool srgb)
161 {
162         unsigned w = img.get_width();
163         unsigned h = img.get_height();
164         unsigned d = 1;
165
166         if(depth)
167         {
168                 if(h%depth)
169                         throw incompatible_data("Texture3D::load_image");
170                 h /= depth;
171                 d = depth;
172         }
173         else
174         {
175                 if(h%w)
176                         throw incompatible_data("Texture3D::load_image");
177                 d = h/w;
178                 h = w;
179         }
180
181         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
182         if(width==0)
183                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
184         else if(w!=width || h!=height || d!=depth)
185                 throw incompatible_data("Texture3D::load_image");
186
187         PixelStore pstore = PixelStore::from_image(img);
188         BindRestore _bind_ps(pstore);
189
190         image(0, fmt, UNSIGNED_BYTE, img.get_data());
191 }
192
193 unsigned Texture3D::get_n_levels() const
194 {
195         unsigned s = max(width, height);
196         if(target!=GL_TEXTURE_2D_ARRAY)
197                 s = max(s, depth);
198         unsigned n = 0;
199         for(; s; s>>=1, ++n) ;
200         return n;
201 }
202
203 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
204 {
205         w >>= level;
206         h >>= level;
207         if(target!=GL_TEXTURE_2D_ARRAY)
208                 d >>= level;
209
210         if(!w && (h || d))
211                 w = 1;
212         if(!h && (w || d))
213                 h = 1;
214         if(!d && (w || h))
215                 d = 1;
216 }
217
218 UInt64 Texture3D::get_data_size() const
219 {
220         return id ? width*height*depth*get_pixel_size(ifmt) : 0;
221 }
222
223
224 Texture3D::Loader::Loader(Texture3D &t):
225         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
226 {
227         init();
228 }
229
230 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
231         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
232 {
233         init();
234 }
235
236 void Texture3D::Loader::init()
237 {
238         add("raw_data", &Loader::raw_data);
239         add("storage", &Loader::storage);
240 }
241
242 void Texture3D::Loader::raw_data(const string &data)
243 {
244         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
245 }
246
247 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
248 {
249         if(srgb)
250                 fmt = get_srgb_pixelformat(fmt);
251         obj.storage(fmt, w, h, d);
252 }
253
254 } // namespace GL
255 } // namespace Msp