]> git.tdb.fi Git - libs/gl.git/blob - source/core/texturecube.cpp
Nudge the FrameFormat floating-point flag one bit to the left
[libs/gl.git] / source / core / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_seamless_cube_map.h>
4 #include <msp/gl/extensions/arb_texture_cube_map.h>
5 #include <msp/gl/extensions/arb_texture_storage.h>
6 #include <msp/io/memory.h>
7 #include <msp/strings/format.h>
8 #include "error.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         if(ARB_seamless_cube_map)
53                 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
54 }
55
56 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
57 {
58         if(size>0)
59         {
60                 if(fmt!=format || sz!=size || (lv && lv!=levels))
61                         throw incompatible_data("TextureCube::storage");
62                 return;
63         }
64         if(sz==0)
65                 throw invalid_argument("TextureCube::storage");
66
67         set_format(fmt);
68         size = sz;
69         levels = get_n_levels();
70         if(lv>0)
71                 levels = min(levels, lv);
72 }
73
74 void TextureCube::allocate(unsigned level)
75 {
76         if(size==0)
77                 throw invalid_operation("TextureCube::allocate");
78         if(level>=levels)
79                 throw invalid_argument("TextureCube::allocate");
80
81         bool direct = ARB_texture_storage && ARB_direct_state_access;
82         if(!direct)
83         {
84                 glActiveTexture(GL_TEXTURE0);
85                 glBindTexture(target, id);
86         }
87
88         allocate_(level);
89
90         if(!direct)
91                 glBindTexture(target, 0);
92 }
93
94 void TextureCube::allocate_(unsigned level)
95 {
96         if(allocated&(64<<level))
97                 return;
98
99         if(ARB_texture_storage)
100         {
101                 GLenum fmt = get_gl_pixelformat(storage_fmt);
102                 if(ARB_direct_state_access)
103                         glTextureStorage2D(id, levels, fmt, size, size);
104                 else
105                         glTexStorage2D(target, levels, fmt, size, size);
106                 apply_swizzle();
107                 allocated |= (64<<levels)-1;
108         }
109         else
110         {
111                 for(unsigned i=0; i<6; ++i)
112                         image_(enumerate_faces(i), level, 0);
113         }
114 }
115
116 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
117 {
118         if(size==0)
119                 throw invalid_operation("TextureCube::image");
120         if(level>=levels)
121                 throw out_of_range("TextureCube::image");
122
123         if(ARB_texture_storage)
124         {
125                 unsigned lsz = get_level_size(level);
126                 return sub_image(face, level, 0, 0, lsz, lsz, data);
127         }
128
129         glActiveTexture(GL_TEXTURE0);
130         glBindTexture(target, id);
131
132         image_(face, level, data);
133
134         if(auto_gen_mipmap && level==0 && (allocated&63)==63)
135         {
136                 generate_mipmap_();
137                 allocated |= (64<<levels)-1;
138         }
139
140         glBindTexture(target, 0);
141 }
142
143 void TextureCube::image_(TextureCubeFace face, unsigned level, const void *data)
144 {
145         if(!allocated)
146         {
147                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
148                 apply_swizzle();
149         }
150
151         unsigned lsz = get_level_size(level);
152         GLenum fmt = get_gl_pixelformat(storage_fmt);
153         GLenum comp = get_gl_components(get_components(storage_fmt));
154         GLenum type = get_gl_type(get_component_type(storage_fmt));
155         glTexImage2D(face, level, fmt, lsz, lsz, 0, comp, type, data);
156
157         if(level==0)
158         {
159                 allocated |= 1<<get_face_index(face);
160                 if((allocated&63)==63)
161                         allocated |= 64;
162         }
163         else if(!(allocated&(64<<level)))
164         {
165                 for(unsigned i=0; i<6; ++i)
166                         if(enumerate_faces(i)!=face)
167                                 glTexImage2D(enumerate_faces(i), level, fmt, lsz, lsz, 0, comp, type, 0);
168
169                 allocated |= 64<<level;
170         }
171 }
172
173 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
174 {
175         if(comp!=get_components(format) || type!=get_component_type(format))
176                 throw incompatible_data("TextureCube::image");
177         image(face, level, data);
178 }
179
180 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
181 {
182         if(size==0)
183                 throw invalid_operation("TextureCube::sub_image");
184         if(level>=levels)
185                 throw out_of_range("TextureCube::sub_image");
186
187         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
188         if(!direct)
189         {
190                 glActiveTexture(GL_TEXTURE0);
191                 glBindTexture(target, id);
192         }
193
194         allocate_(level);
195
196         GLenum comp = get_gl_components(get_components(storage_fmt));
197         GLenum type = get_gl_type(get_component_type(storage_fmt));
198         if(ARB_direct_state_access)
199                 glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
200         else
201                 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
202
203         if(auto_gen_mipmap && level==0)
204                 generate_mipmap_();
205
206         if(!direct)
207                 glBindTexture(target, 0);
208 }
209
210 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
211 {
212         if(comp!=get_components(format) || type!=get_component_type(format))
213                 throw incompatible_data("TextureCube::subimage");
214         sub_image(face, level, x, y, wd, ht, data);
215 }
216
217 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
218 {
219         unsigned w = img.get_width();
220         unsigned h = img.get_height();
221         if(w!=h)
222                 throw incompatible_data("TextureCube::image");
223
224         PixelFormat fmt = pixelformat_from_image(img);
225         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w);
226
227         image(face, 0, img.get_pixels());
228 }
229
230 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool)
231 {
232         image(face, img);
233 }
234
235 void TextureCube::image(const Graphics::Image &img, unsigned lv)
236 {
237         unsigned w = img.get_width();
238         unsigned h = img.get_height();
239
240         if(h!=w*6)
241                 throw incompatible_data("TextureCube::image");
242         h /= 6;
243
244         PixelFormat fmt = pixelformat_from_image(img);
245         if(size==0)
246                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
247         else if(w!=size || h!=size)
248                 throw incompatible_data("TextureCube::image");
249
250         const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
251         unsigned face_size = img.get_stride()*size;
252         for(unsigned i=0; i<6; ++i)
253                 image(enumerate_faces(i), 0, pixels+i*face_size);
254 }
255
256 unsigned TextureCube::get_n_levels() const
257 {
258         unsigned n = 0;
259         for(unsigned s=size; s; s>>=1, ++n) ;
260         return n;
261 }
262
263 unsigned TextureCube::get_level_size(unsigned level) const
264 {
265         return size>>level;
266 }
267
268 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
269 {
270         if(i>=6)
271                 throw out_of_range("TextureCube::enumerate_faces");
272         return face_order[i];
273 }
274
275 unsigned TextureCube::get_face_index(TextureCubeFace face)
276 {
277         switch(face)
278         {
279         case POSITIVE_X: return 0;
280         case NEGATIVE_X: return 1;
281         case POSITIVE_Y: return 2;
282         case NEGATIVE_Y: return 3;
283         case POSITIVE_Z: return 4;
284         case NEGATIVE_Z: return 5;
285         default: throw invalid_argument("TextureCube::get_face_index");
286         }
287 }
288
289 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
290 {
291         return directions[get_face_index(face)];
292 }
293
294 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
295 {
296         return directions[orientations[get_face_index(face)*2]];
297 }
298
299 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
300 {
301         return directions[orientations[get_face_index(face)*2+1]];
302 }
303
304 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
305 {
306         float s = (u+0.5f)*2.0f/size-1.0f;
307         float t = (v+0.5f)*2.0f/size-1.0f;
308         const Vector3 &fv = get_face_direction(face);
309         const Vector3 &sv = get_s_direction(face);
310         const Vector3 &tv = get_t_direction(face);
311         return fv+s*sv+t*tv;
312 }
313
314 UInt64 TextureCube::get_data_size() const
315 {
316         return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
317 }
318
319
320 TextureCube::Loader::Loader(TextureCube &t):
321         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
322 {
323         init();
324 }
325
326 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
327         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
328 {
329         init();
330 }
331
332 void TextureCube::Loader::init()
333 {
334         add("external_image", &Loader::external_image);
335         add("image_data", &Loader::image_data);
336         add("raw_data", &Loader::raw_data);
337         add("storage", &Loader::storage);
338         add("storage", &Loader::storage_levels);
339 }
340
341 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
342 {
343         Graphics::Image img;
344         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
345         img.load_io(*io);
346
347         obj.image(face, img);
348 }
349
350 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
351 {
352         Graphics::Image img;
353         IO::Memory mem(data.data(), data.size());
354         img.load_io(mem);
355
356         obj.image(face, img);
357 }
358
359 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
360 {
361         obj.image(face, 0, data.data());
362 }
363
364 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
365 {
366         obj.storage(fmt, s);
367 }
368
369 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
370 {
371         obj.storage(fmt, s, l);
372 }
373
374
375 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
376 {
377         const string &str = conv.get();
378         if(str=="POSITIVE_X")
379                 face = POSITIVE_X;
380         else if(str=="NEGATIVE_X")
381                 face = NEGATIVE_X;
382         else if(str=="POSITIVE_Y")
383                 face = POSITIVE_Y;
384         else if(str=="NEGATIVE_Y")
385                 face = NEGATIVE_Y;
386         else if(str=="POSITIVE_Z")
387                 face = POSITIVE_Z;
388         else if(str=="NEGATIVE_Z")
389                 face = NEGATIVE_Z;
390         else
391                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
392 }
393
394 } // namespace GL
395 } // namespace Msp