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