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