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