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>
9 #include "texturecube.h"
16 TextureCubeFace TextureCube::face_order[6] =
26 Vector3 TextureCube::directions[6] =
36 unsigned TextureCube::orientations[12] =
46 TextureCube::TextureCube():
47 Texture(GL_TEXTURE_CUBE_MAP),
51 static Require _req(ARB_texture_cube_map);
52 if(ARB_seamless_cube_map)
53 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
56 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
60 if(fmt!=format || sz!=size || (lv && lv!=levels))
61 throw incompatible_data("TextureCube::storage");
65 throw invalid_argument("TextureCube::storage");
69 levels = get_n_levels();
71 levels = min(levels, lv);
74 void TextureCube::allocate(unsigned level)
77 throw invalid_operation("TextureCube::allocate");
79 throw invalid_argument("TextureCube::allocate");
80 if(allocated&(64<<level))
83 bool direct = ARB_texture_storage && ARB_direct_state_access;
86 glActiveTexture(GL_TEXTURE0);
87 glBindTexture(target, id);
93 glBindTexture(target, 0);
96 void TextureCube::allocate_(unsigned level)
98 if(ARB_texture_storage)
100 GLenum fmt = get_gl_pixelformat(storage_fmt);
101 if(ARB_direct_state_access)
102 glTextureStorage2D(id, levels, fmt, size, size);
104 glTexStorage2D(target, levels, 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 GLenum fmt = get_gl_pixelformat(storage_fmt);
152 GLenum comp = get_gl_components(get_components(storage_fmt));
153 GLenum type = get_gl_type(get_component_type(storage_fmt));
154 glTexImage2D(face, level, fmt, lsz, lsz, 0, comp, type, data);
158 allocated |= 1<<get_face_index(face);
159 if((allocated&63)==63)
162 else if(!(allocated&(64<<level)))
164 for(unsigned i=0; i<6; ++i)
165 if(enumerate_faces(i)!=face)
166 glTexImage2D(enumerate_faces(i), level, fmt, lsz, lsz, 0, comp, type, 0);
168 allocated |= 64<<level;
172 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
174 if(comp!=get_components(format) || type!=get_component_type(format))
175 throw incompatible_data("TextureCube::image");
176 image(face, level, data);
179 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
182 throw invalid_operation("TextureCube::sub_image");
184 throw out_of_range("TextureCube::sub_image");
186 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
189 glActiveTexture(GL_TEXTURE0);
190 glBindTexture(target, id);
195 GLenum comp = get_gl_components(get_components(storage_fmt));
196 GLenum type = get_gl_type(get_component_type(storage_fmt));
197 if(ARB_direct_state_access)
198 glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
200 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
202 if(auto_gen_mipmap && level==0)
206 glBindTexture(target, 0);
209 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
211 if(comp!=get_components(format) || type!=get_component_type(format))
212 throw incompatible_data("TextureCube::subimage");
213 sub_image(face, level, x, y, wd, ht, data);
216 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
218 unsigned w = img.get_width();
219 unsigned h = img.get_height();
221 throw incompatible_data("TextureCube::image");
223 PixelFormat fmt = pixelformat_from_image(img);
224 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w);
226 image(face, 0, img.get_pixels());
229 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool)
234 void TextureCube::image(const Graphics::Image &img, unsigned lv)
236 unsigned w = img.get_width();
237 unsigned h = img.get_height();
240 throw incompatible_data("TextureCube::image");
243 PixelFormat fmt = pixelformat_from_image(img);
245 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
246 else if(w!=size || h!=size)
247 throw incompatible_data("TextureCube::image");
249 const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
250 unsigned face_size = img.get_stride()*size;
251 for(unsigned i=0; i<6; ++i)
252 image(enumerate_faces(i), 0, pixels+i*face_size);
255 unsigned TextureCube::get_n_levels() const
258 for(unsigned s=size; s; s>>=1, ++n) ;
262 unsigned TextureCube::get_level_size(unsigned level) const
267 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
270 throw out_of_range("TextureCube::enumerate_faces");
271 return face_order[i];
274 unsigned TextureCube::get_face_index(TextureCubeFace face)
278 case POSITIVE_X: return 0;
279 case NEGATIVE_X: return 1;
280 case POSITIVE_Y: return 2;
281 case NEGATIVE_Y: return 3;
282 case POSITIVE_Z: return 4;
283 case NEGATIVE_Z: return 5;
284 default: throw invalid_argument("TextureCube::get_face_index");
288 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
290 return directions[get_face_index(face)];
293 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
295 return directions[orientations[get_face_index(face)*2]];
298 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
300 return directions[orientations[get_face_index(face)*2+1]];
303 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
305 float s = (u+0.5f)*2.0f/size-1.0f;
306 float t = (v+0.5f)*2.0f/size-1.0f;
307 const Vector3 &fv = get_face_direction(face);
308 const Vector3 &sv = get_s_direction(face);
309 const Vector3 &tv = get_t_direction(face);
313 UInt64 TextureCube::get_data_size() const
315 return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
319 TextureCube::Loader::Loader(TextureCube &t):
320 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
325 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
326 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
331 void TextureCube::Loader::init()
333 add("external_image", &Loader::external_image);
334 add("image_data", &Loader::image_data);
335 add("raw_data", &Loader::raw_data);
336 add("storage", &Loader::storage);
337 add("storage", &Loader::storage_levels);
340 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
343 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
346 obj.image(face, img);
349 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
352 IO::Memory mem(data.data(), data.size());
355 obj.image(face, img);
358 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
360 obj.image(face, 0, data.data());
363 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
368 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
370 obj.storage(fmt, s, l);
374 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
376 const string &str = conv.get();
377 if(str=="POSITIVE_X")
379 else if(str=="NEGATIVE_X")
381 else if(str=="POSITIVE_Y")
383 else if(str=="NEGATIVE_Y")
385 else if(str=="POSITIVE_Z")
387 else if(str=="NEGATIVE_Z")
390 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));