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