]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Rearrange texture bind calls to keep them closer to where they're needed
[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 Vector3 TextureCube::directions[6] =
17 {
18         Vector3(1, 0, 0),
19         Vector3(-1, 0, 0),
20         Vector3(0, 1, 0),
21         Vector3(0, -1, 0),
22         Vector3(0, 0, 1),
23         Vector3(0, 0, -1)
24 };
25
26 TextureCube::TextureCube():
27         Texture(GL_TEXTURE_CUBE_MAP),
28         ifmt(RGB),
29         size(0),
30         allocated(0)
31 {
32         static Require _req(ARB_texture_cube_map);
33 }
34
35 void TextureCube::storage(PixelFormat fmt, unsigned sz)
36 {
37         if(size>0)
38                 throw invalid_operation("TextureCube::storage");
39         if(sz==0)
40                 throw invalid_argument("TextureCube::storage");
41
42         if(MSP_sized_internal_formats)
43                 fmt = get_sized_pixelformat(fmt);
44         require_pixelformat(fmt);
45
46         ifmt = fmt;
47         size = sz;
48 }
49
50 void TextureCube::allocate(unsigned level)
51 {
52         if(allocated&(1<<level))
53                 return;
54
55         if(ARB_texture_storage)
56         {
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;
61         }
62         else
63         {
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);
68         }
69 }
70
71 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
72 {
73         if(size==0)
74                 throw invalid_operation("TextureCube::image");
75
76         unsigned s = get_level_size(level);
77         if(s==0)
78                 throw out_of_range("TextureCube::image");
79
80         if(ARB_texture_storage)
81                 return sub_image(face, level, 0, 0, s, s, fmt, type, data);
82
83         BindRestore _bind(this);
84         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
85
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)
89         {
90                 // TODO Only do this once all faces are created
91                 auto_generate_mipmap();
92                 allocated |= (1<<get_n_levels())-1;
93         }
94 }
95
96 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
97 {
98         if(size==0)
99                 throw invalid_operation("TextureCube::sub_image");
100
101         BindRestore _bind(this);
102         allocate(level);
103
104         glTexSubImage2D(face, level, x, y, wd, ht, fmt, type, data);
105
106         if(gen_mipmap && level==0)
107                 auto_generate_mipmap();
108 }
109
110 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
111 {
112         unsigned w = img.get_width();
113         unsigned h = img.get_height();
114         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
115         if(size==0)
116         {
117                 if(w!=h)
118                         throw incompatible_data("TextureCube::image");
119
120                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
121         }
122         else if(w!=size || h!=size)
123                 throw incompatible_data("TextureCube::image");
124
125         PixelStore pstore = PixelStore::from_image(img);
126         BindRestore _bind_ps(pstore);
127
128         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
129 }
130
131 void TextureCube::image(const Graphics::Image &img, bool srgb)
132 {
133         unsigned w = img.get_width();
134         unsigned h = img.get_height();
135
136         if(h!=w*6)
137                 throw incompatible_data("TextureCube::image");
138         h /= 6;
139
140         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
141         if(size==0)
142                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
143         else if(w!=size || h!=size)
144                 throw incompatible_data("TextureCube::image");
145
146         PixelStore pstore = PixelStore::from_image(img);
147         BindRestore _bind_ps(pstore);
148
149         const char *cdata = reinterpret_cast<const char *>(img.get_data());
150         unsigned face_size = img.get_stride()*size;
151         for(unsigned i=0; i<6; ++i)
152                 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
153 }
154
155 unsigned TextureCube::get_n_levels() const
156 {
157         unsigned n = 0;
158         for(unsigned s=size; s; s>>=1, ++n) ;
159         return n;
160 }
161
162 unsigned TextureCube::get_level_size(unsigned level) const
163 {
164         return size>>level;
165 }
166
167 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
168 {
169         switch(i)
170         {
171         case 0: return POSITIVE_X;
172         case 1: return NEGATIVE_X;
173         case 2: return POSITIVE_Y;
174         case 3: return NEGATIVE_Y;
175         case 4: return POSITIVE_Z;
176         case 5: return NEGATIVE_Z;
177         default: throw out_of_range("TextureCube::enumerate_faces");
178         }
179 }
180
181 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
182 {
183         switch(face)
184         {
185         case POSITIVE_X: return directions[0];
186         case NEGATIVE_X: return directions[1];
187         case POSITIVE_Y: return directions[2];
188         case NEGATIVE_Y: return directions[3];
189         case POSITIVE_Z: return directions[4];
190         case NEGATIVE_Z: return directions[5];
191         default: throw invalid_argument("TextureCube::get_face_direction");
192         }
193 }
194
195 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
196 {
197         switch(face)
198         {
199         case POSITIVE_X: return directions[5];
200         case NEGATIVE_X: return directions[4];
201         case POSITIVE_Y: return directions[0];
202         case NEGATIVE_Y: return directions[0];
203         case POSITIVE_Z: return directions[0];
204         case NEGATIVE_Z: return directions[1];
205         default: throw invalid_argument("TextureCube::get_s_direction");
206         }
207 }
208
209 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
210 {
211         switch(face)
212         {
213         case POSITIVE_X: return directions[3];
214         case NEGATIVE_X: return directions[3];
215         case POSITIVE_Y: return directions[4];
216         case NEGATIVE_Y: return directions[5];
217         case POSITIVE_Z: return directions[3];
218         case NEGATIVE_Z: return directions[3];
219         default: throw invalid_argument("TextureCube::get_t_direction");
220         }
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