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