1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_texture_cube_map.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/io/memory.h>
5 #include <msp/strings/format.h>
8 #include "pixelstore.h"
9 #include "texturecube.h"
16 Vector3 TextureCube::directions[6] =
26 TextureCube::TextureCube():
27 Texture(GL_TEXTURE_CUBE_MAP),
32 static Require _req(ARB_texture_cube_map);
35 void TextureCube::storage(PixelFormat fmt, unsigned sz)
38 throw invalid_operation("TextureCube::storage");
40 throw invalid_argument("TextureCube::storage");
42 if(MSP_sized_internal_formats)
43 fmt = get_sized_pixelformat(fmt);
44 require_pixelformat(fmt);
50 void TextureCube::allocate(unsigned level)
52 if(allocated&(1<<level))
55 if(ARB_texture_storage)
57 BindRestore _bind(this);
58 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
59 glTexStorage2D(target, n_levels, ifmt, size, size);
60 allocated |= (1<<n_levels)-1;
64 PixelFormat base_fmt = get_base_pixelformat(ifmt);
65 DataType type = get_alloc_type(base_fmt);
66 for(unsigned i=0; i<6; ++i)
67 image(enumerate_faces(i), level, base_fmt, type, 0);
71 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
74 throw invalid_operation("TextureCube::image");
76 unsigned s = get_level_size(level);
78 throw out_of_range("TextureCube::image");
80 BindRestore _bind(this);
81 if(ARB_texture_storage)
82 sub_image(face, level, 0, 0, s, s, fmt, type, data);
84 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
86 // XXX Allocation should be tracked per-face, but we'll run out of bits
87 allocated |= 1<<level;
88 if(gen_mipmap && level==0)
90 // TODO Only do this once all faces are created
91 auto_generate_mipmap();
92 allocated |= (1<<get_n_levels())-1;
96 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
99 throw invalid_operation("TextureCube::sub_image");
103 BindRestore _bind(this);
104 glTexSubImage2D(face, level, x, y, wd, ht, fmt, type, data);
107 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
109 unsigned w = img.get_width();
110 unsigned h = img.get_height();
111 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
115 throw incompatible_data("TextureCube::image");
117 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
119 else if(w!=size || h!=size)
120 throw incompatible_data("TextureCube::image");
122 PixelStore pstore = PixelStore::from_image(img);
123 BindRestore _bind_ps(pstore);
125 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
128 void TextureCube::image(const Graphics::Image &img, bool srgb)
130 unsigned w = img.get_width();
131 unsigned h = img.get_height();
134 throw incompatible_data("TextureCube::image");
137 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
139 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
140 else if(w!=size || h!=size)
141 throw incompatible_data("TextureCube::image");
143 PixelStore pstore = PixelStore::from_image(img);
144 BindRestore _bind_ps(pstore);
146 const char *cdata = reinterpret_cast<const char *>(img.get_data());
147 unsigned face_size = img.get_stride()*size;
148 for(unsigned i=0; i<6; ++i)
149 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
152 unsigned TextureCube::get_n_levels() const
155 for(unsigned s=size; s; s>>=1, ++n) ;
159 unsigned TextureCube::get_level_size(unsigned level) const
164 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
168 case 0: return POSITIVE_X;
169 case 1: return NEGATIVE_X;
170 case 2: return POSITIVE_Y;
171 case 3: return NEGATIVE_Y;
172 case 4: return POSITIVE_Z;
173 case 5: return NEGATIVE_Z;
174 default: throw out_of_range("TextureCube::enumerate_faces");
178 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
182 case POSITIVE_X: return directions[0];
183 case NEGATIVE_X: return directions[1];
184 case POSITIVE_Y: return directions[2];
185 case NEGATIVE_Y: return directions[3];
186 case POSITIVE_Z: return directions[4];
187 case NEGATIVE_Z: return directions[5];
188 default: throw invalid_argument("TextureCube::get_face_direction");
192 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
196 case POSITIVE_X: return directions[5];
197 case NEGATIVE_X: return directions[4];
198 case POSITIVE_Y: return directions[0];
199 case NEGATIVE_Y: return directions[0];
200 case POSITIVE_Z: return directions[0];
201 case NEGATIVE_Z: return directions[1];
202 default: throw invalid_argument("TextureCube::get_s_direction");
206 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
210 case POSITIVE_X: return directions[3];
211 case NEGATIVE_X: return directions[3];
212 case POSITIVE_Y: return directions[4];
213 case NEGATIVE_Y: return directions[5];
214 case POSITIVE_Z: return directions[3];
215 case NEGATIVE_Z: return directions[3];
216 default: throw invalid_argument("TextureCube::get_t_direction");
220 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
222 float s = (u+0.5f)*2.0f/size-1.0f;
223 float t = (v+0.5f)*2.0f/size-1.0f;
224 const Vector3 &fv = get_face_direction(face);
225 const Vector3 &sv = get_s_direction(face);
226 const Vector3 &tv = get_t_direction(face);
230 UInt64 TextureCube::get_data_size() const
232 return id ? size*size*6*get_pixel_size(ifmt) : 0;
236 TextureCube::Loader::Loader(TextureCube &t):
237 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
242 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
243 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
248 void TextureCube::Loader::init()
250 add("external_image", &Loader::external_image);
251 add("image_data", &Loader::image_data);
252 add("raw_data", &Loader::raw_data);
253 add("storage", &Loader::storage);
256 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
259 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
262 obj.image(face, img, srgb);
265 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
268 IO::Memory mem(data.data(), data.size());
271 obj.image(face, img, srgb);
274 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
276 obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
279 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
282 fmt = get_srgb_pixelformat(fmt);
287 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
289 const string &str = conv.get();
290 if(str=="POSITIVE_X")
292 else if(str=="NEGATIVE_X")
294 else if(str=="POSITIVE_Y")
296 else if(str=="NEGATIVE_Y")
298 else if(str=="POSITIVE_Z")
300 else if(str=="NEGATIVE_Z")
303 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));