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>
10 #include "texturecube.h"
17 TextureCubeFace TextureCube::face_order[6] =
27 Vector3 TextureCube::directions[6] =
37 unsigned TextureCube::orientations[12] =
47 TextureCube::TextureCube():
48 Texture(GL_TEXTURE_CUBE_MAP),
52 static Require _req(ARB_texture_cube_map);
53 if(ARB_seamless_cube_map)
54 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
57 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
61 if(fmt!=format || sz!=size || (lv && lv!=levels))
62 throw incompatible_data("TextureCube::storage");
66 throw invalid_argument("TextureCube::storage");
70 levels = get_n_levels();
72 levels = min(levels, lv);
75 void TextureCube::allocate(unsigned level)
78 throw invalid_operation("TextureCube::allocate");
80 throw invalid_argument("TextureCube::allocate");
81 if(allocated&(64<<level))
84 bool direct = ARB_texture_storage && ARB_direct_state_access;
87 glActiveTexture(GL_TEXTURE0);
88 glBindTexture(target, id);
94 glBindTexture(target, 0);
97 void TextureCube::allocate_(unsigned level)
99 if(ARB_texture_storage)
101 if(ARB_direct_state_access)
102 glTextureStorage2D(id, levels, storage_fmt, size, size);
104 glTexStorage2D(target, levels, storage_fmt, size, size);
106 allocated |= (64<<levels)-1;
110 for(unsigned i=0; i<6; ++i)
111 image_(enumerate_faces(i), level, 0);
115 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
118 throw invalid_operation("TextureCube::image");
120 throw out_of_range("TextureCube::image");
122 if(ARB_texture_storage)
124 unsigned lsz = get_level_size(level);
125 return sub_image(face, level, 0, 0, lsz, lsz, data);
128 glActiveTexture(GL_TEXTURE0);
129 glBindTexture(target, id);
131 image_(face, level, data);
133 if(auto_gen_mipmap && level==0 && (allocated&63)==63)
136 allocated |= (64<<levels)-1;
139 glBindTexture(target, 0);
142 void TextureCube::image_(TextureCubeFace face, unsigned level, const void *data)
146 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
150 unsigned lsz = get_level_size(level);
151 PixelComponents comp = get_components(storage_fmt);
152 GLenum type = get_gl_type(get_component_type(storage_fmt));
153 glTexImage2D(face, level, storage_fmt, lsz, lsz, 0, comp, type, data);
157 allocated |= 1<<get_face_index(face);
158 if((allocated&63)==63)
161 else if(!(allocated&(64<<level)))
163 for(unsigned i=0; i<6; ++i)
164 if(enumerate_faces(i)!=face)
165 glTexImage2D(enumerate_faces(i), level, storage_fmt, lsz, lsz, 0, comp, type, 0);
167 allocated |= 64<<level;
171 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
173 if(comp!=get_components(format) || type!=get_component_type(format))
174 throw incompatible_data("TextureCube::image");
175 image(face, level, data);
178 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
181 throw invalid_operation("TextureCube::sub_image");
183 throw out_of_range("TextureCube::sub_image");
185 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
188 glActiveTexture(GL_TEXTURE0);
189 glBindTexture(target, id);
194 PixelComponents comp = get_components(storage_fmt);
195 GLenum type = get_gl_type(get_component_type(storage_fmt));
196 if(ARB_direct_state_access)
197 glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
199 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
201 if(auto_gen_mipmap && level==0)
205 glBindTexture(target, 0);
208 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
210 if(comp!=get_components(format) || type!=get_component_type(format))
211 throw incompatible_data("TextureCube::subimage");
212 sub_image(face, level, x, y, wd, ht, data);
215 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
217 unsigned w = img.get_width();
218 unsigned h = img.get_height();
220 throw incompatible_data("TextureCube::image");
222 PixelFormat fmt = pixelformat_from_image(img);
223 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w);
225 image(face, 0, img.get_pixels());
228 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool)
233 void TextureCube::image(const Graphics::Image &img, unsigned lv)
235 unsigned w = img.get_width();
236 unsigned h = img.get_height();
239 throw incompatible_data("TextureCube::image");
242 PixelFormat fmt = pixelformat_from_image(img);
244 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
245 else if(w!=size || h!=size)
246 throw incompatible_data("TextureCube::image");
248 const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
249 unsigned face_size = img.get_stride()*size;
250 for(unsigned i=0; i<6; ++i)
251 image(enumerate_faces(i), 0, pixels+i*face_size);
254 unsigned TextureCube::get_n_levels() const
257 for(unsigned s=size; s; s>>=1, ++n) ;
261 unsigned TextureCube::get_level_size(unsigned level) const
266 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
269 throw out_of_range("TextureCube::enumerate_faces");
270 return face_order[i];
273 unsigned TextureCube::get_face_index(TextureCubeFace face)
277 case POSITIVE_X: return 0;
278 case NEGATIVE_X: return 1;
279 case POSITIVE_Y: return 2;
280 case NEGATIVE_Y: return 3;
281 case POSITIVE_Z: return 4;
282 case NEGATIVE_Z: return 5;
283 default: throw invalid_argument("TextureCube::get_face_index");
287 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
289 return directions[get_face_index(face)];
292 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
294 return directions[orientations[get_face_index(face)*2]];
297 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
299 return directions[orientations[get_face_index(face)*2+1]];
302 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
304 float s = (u+0.5f)*2.0f/size-1.0f;
305 float t = (v+0.5f)*2.0f/size-1.0f;
306 const Vector3 &fv = get_face_direction(face);
307 const Vector3 &sv = get_s_direction(face);
308 const Vector3 &tv = get_t_direction(face);
312 UInt64 TextureCube::get_data_size() const
314 return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
318 TextureCube::Loader::Loader(TextureCube &t):
319 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
324 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
325 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
330 void TextureCube::Loader::init()
332 add("external_image", &Loader::external_image);
333 add("image_data", &Loader::image_data);
334 add("raw_data", &Loader::raw_data);
335 add("storage", &Loader::storage);
336 add("storage", &Loader::storage_levels);
339 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
342 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
345 obj.image(face, img);
348 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
351 IO::Memory mem(data.data(), data.size());
354 obj.image(face, img);
357 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
359 obj.image(face, 0, data.data());
362 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
367 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
369 obj.storage(fmt, s, l);
373 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
375 const string &str = conv.get();
376 if(str=="POSITIVE_X")
378 else if(str=="NEGATIVE_X")
380 else if(str=="POSITIVE_Y")
382 else if(str=="NEGATIVE_Y")
384 else if(str=="POSITIVE_Z")
386 else if(str=="NEGATIVE_Z")
389 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));