]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Separate abstract pixel compositions from concrete pixel formats
[libs/gl.git] / source / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_texture_cube_map.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/io/memory.h>
5 #include <msp/strings/format.h>
6 #include "bindable.h"
7 #include "error.h"
8 #include "pixelstore.h"
9 #include "texturecube.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 TextureCubeFace TextureCube::face_order[6] =
17 {
18         POSITIVE_X,
19         NEGATIVE_X,
20         POSITIVE_Y,
21         NEGATIVE_Y,
22         POSITIVE_Z,
23         NEGATIVE_Z
24 };
25
26 Vector3 TextureCube::directions[6] =
27 {
28         Vector3(1, 0, 0),
29         Vector3(-1, 0, 0),
30         Vector3(0, 1, 0),
31         Vector3(0, -1, 0),
32         Vector3(0, 0, 1),
33         Vector3(0, 0, -1)
34 };
35
36 unsigned TextureCube::orientations[12] =
37 {
38         5, 3,
39         4, 3,
40         0, 4,
41         0, 5,
42         0, 3,
43         1, 3
44 };
45
46 TextureCube::TextureCube():
47         Texture(GL_TEXTURE_CUBE_MAP),
48         size(0),
49         allocated(0)
50 {
51         static Require _req(ARB_texture_cube_map);
52 }
53
54 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
55 {
56         if(size>0)
57                 throw invalid_operation("TextureCube::storage");
58         if(sz==0)
59                 throw invalid_argument("TextureCube::storage");
60
61         set_internal_format(fmt);
62         size = sz;
63         levels = get_n_levels();
64         if(lv>0)
65                 levels = min(levels, lv);
66 }
67
68 void TextureCube::allocate(unsigned level)
69 {
70         if(size==0)
71                 throw invalid_operation("TextureCube::allocate");
72         if(level>=levels)
73                 throw invalid_argument("TextureCube::allocate");
74         if(allocated&(64<<level))
75                 return;
76
77         if(ARB_texture_storage)
78         {
79                 BindRestore _bind(this);
80                 glTexStorage2D(target, levels, ifmt, size, size);
81                 apply_swizzle();
82                 allocated |= (1<<levels)-1;
83         }
84         else
85         {
86                 PixelComponents comp = get_components(ifmt);
87                 DataType type = get_component_type(ifmt);
88                 for(unsigned i=0; i<6; ++i)
89                         image(enumerate_faces(i), level, comp, type, 0);
90         }
91 }
92
93 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
94 {
95         if(size==0)
96                 throw invalid_operation("TextureCube::image");
97
98         unsigned s = get_level_size(level);
99         if(s==0)
100                 throw out_of_range("TextureCube::image");
101
102         if(ARB_texture_storage)
103                 return sub_image(face, level, 0, 0, s, s, comp, type, data);
104
105         BindRestore _bind(this);
106
107         if(!allocated)
108         {
109                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
110                 apply_swizzle();
111         }
112         glTexImage2D(face, level, ifmt, s, s, 0, get_upload_components(comp), type, data);
113
114         if(level==0)
115         {
116                 allocated |= 1<<get_face_index(face);
117                 if((allocated&63)==63)
118                 {
119                         allocated |= 64;
120                         if(auto_gen_mipmap)
121                         {
122                                 generate_mipmap();
123                                 allocated |= (64<<levels)-1;
124                         }
125                 }
126         }
127         else if(!(allocated&(64<<level)))
128         {
129                 for(unsigned i=0; i<6; ++i)
130                         if(enumerate_faces(i)!=face)
131                                 glTexImage2D(enumerate_faces(i), level, ifmt, s, s, 0, comp, type, 0);
132
133                 allocated |= 64<<level;
134         }
135 }
136
137 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
138 {
139         if(size==0)
140                 throw invalid_operation("TextureCube::sub_image");
141
142         BindRestore _bind(this);
143         allocate(level);
144
145         glTexSubImage2D(face, level, x, y, wd, ht, get_upload_components(comp), type, data);
146
147         if(auto_gen_mipmap && level==0)
148                 generate_mipmap();
149 }
150
151 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
152 {
153         unsigned w = img.get_width();
154         unsigned h = img.get_height();
155         PixelFormat fmt = pixelformat_from_image(img);
156         if(size==0)
157         {
158                 if(w!=h)
159                         throw incompatible_data("TextureCube::image");
160
161                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w);
162         }
163         else if(w!=size || h!=size)
164                 throw incompatible_data("TextureCube::image");
165
166         PixelStore pstore = PixelStore::from_image(img);
167         BindRestore _bind_ps(pstore);
168
169         image(face, 0, get_components(fmt), get_component_type(fmt), img.get_data());
170 }
171
172 void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
173 {
174         unsigned w = img.get_width();
175         unsigned h = img.get_height();
176
177         if(h!=w*6)
178                 throw incompatible_data("TextureCube::image");
179         h /= 6;
180
181         PixelFormat fmt = pixelformat_from_image(img);
182         PixelComponents comp = get_components(fmt);
183         DataType type = get_component_type(fmt);
184         if(size==0)
185                 storage(make_pixelformat(comp, type, srgb), w, lv);
186         else if(w!=size || h!=size)
187                 throw incompatible_data("TextureCube::image");
188
189         PixelStore pstore = PixelStore::from_image(img);
190         BindRestore _bind_ps(pstore);
191
192         const char *cdata = reinterpret_cast<const char *>(img.get_data());
193         unsigned face_size = img.get_stride()*size;
194         for(unsigned i=0; i<6; ++i)
195                 image(enumerate_faces(i), 0, comp, type, cdata+i*face_size);
196 }
197
198 unsigned TextureCube::get_n_levels() const
199 {
200         unsigned n = 0;
201         for(unsigned s=size; s; s>>=1, ++n) ;
202         return n;
203 }
204
205 unsigned TextureCube::get_level_size(unsigned level) const
206 {
207         return size>>level;
208 }
209
210 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
211 {
212         if(i>=6)
213                 throw out_of_range("TextureCube::enumerate_faces");
214         return face_order[i];
215 }
216
217 unsigned TextureCube::get_face_index(TextureCubeFace face)
218 {
219         switch(face)
220         {
221         case POSITIVE_X: return 0;
222         case NEGATIVE_X: return 1;
223         case POSITIVE_Y: return 2;
224         case NEGATIVE_Y: return 3;
225         case POSITIVE_Z: return 4;
226         case NEGATIVE_Z: return 5;
227         default: throw invalid_argument("TextureCube::get_face_index");
228         }
229 }
230
231 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
232 {
233         return directions[get_face_index(face)];
234 }
235
236 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
237 {
238         return directions[orientations[get_face_index(face)*2]];
239 }
240
241 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
242 {
243         return directions[orientations[get_face_index(face)*2+1]];
244 }
245
246 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
247 {
248         float s = (u+0.5f)*2.0f/size-1.0f;
249         float t = (v+0.5f)*2.0f/size-1.0f;
250         const Vector3 &fv = get_face_direction(face);
251         const Vector3 &sv = get_s_direction(face);
252         const Vector3 &tv = get_t_direction(face);
253         return fv+s*sv+t*tv;
254 }
255
256 UInt64 TextureCube::get_data_size() const
257 {
258         return id ? size*size*6*get_pixel_size(ifmt) : 0;
259 }
260
261
262 TextureCube::Loader::Loader(TextureCube &t):
263         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
264 {
265         init();
266 }
267
268 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
269         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
270 {
271         init();
272 }
273
274 void TextureCube::Loader::init()
275 {
276         add("external_image", &Loader::external_image);
277         add("image_data", &Loader::image_data);
278         add("raw_data", &Loader::raw_data);
279         add("storage", &Loader::storage);
280         add("storage", &Loader::storage_levels);
281 }
282
283 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
284 {
285         Graphics::Image img;
286         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
287         img.load_io(*io);
288
289         obj.image(face, img, srgb);
290 }
291
292 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
293 {
294         Graphics::Image img;
295         IO::Memory mem(data.data(), data.size());
296         img.load_io(mem);
297
298         obj.image(face, img, srgb);
299 }
300
301 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
302 {
303         obj.image(face, 0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
304 }
305
306 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
307 {
308         obj.storage(fmt, s);
309 }
310
311 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
312 {
313         obj.storage(fmt, s, l);
314 }
315
316
317 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
318 {
319         const string &str = conv.get();
320         if(str=="POSITIVE_X")
321                 face = POSITIVE_X;
322         else if(str=="NEGATIVE_X")
323                 face = NEGATIVE_X;
324         else if(str=="POSITIVE_Y")
325                 face = POSITIVE_Y;
326         else if(str=="NEGATIVE_Y")
327                 face = NEGATIVE_Y;
328         else if(str=="POSITIVE_Z")
329                 face = POSITIVE_Z;
330         else if(str=="NEGATIVE_Z")
331                 face = NEGATIVE_Z;
332         else
333                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
334 }
335
336 } // namespace GL
337 } // namespace Msp