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