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