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