]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Use ARB_texture_storage for defining texture storage
[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         BindRestore _bind(this);
81         if(ARB_texture_storage)
82                 sub_image(face, level, 0, 0, s, s, fmt, type, data);
83         else
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         allocate(level);
102
103         BindRestore _bind(this);
104         glTexSubImage2D(face, level, x, y, wd, ht, fmt, type, data);
105 }
106
107 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
108 {
109         unsigned w = img.get_width();
110         unsigned h = img.get_height();
111         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112         if(size==0)
113         {
114                 if(w!=h)
115                         throw incompatible_data("TextureCube::image");
116
117                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
118         }
119         else if(w!=size || h!=size)
120                 throw incompatible_data("TextureCube::image");
121
122         PixelStore pstore = PixelStore::from_image(img);
123         BindRestore _bind_ps(pstore);
124
125         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
126 }
127
128 void TextureCube::image(const Graphics::Image &img, bool srgb)
129 {
130         unsigned w = img.get_width();
131         unsigned h = img.get_height();
132
133         if(h!=w*6)
134                 throw incompatible_data("TextureCube::image");
135         h /= 6;
136
137         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
138         if(size==0)
139                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
140         else if(w!=size || h!=size)
141                 throw incompatible_data("TextureCube::image");
142
143         PixelStore pstore = PixelStore::from_image(img);
144         BindRestore _bind_ps(pstore);
145
146         const char *cdata = reinterpret_cast<const char *>(img.get_data());
147         unsigned face_size = img.get_stride()*size;
148         for(unsigned i=0; i<6; ++i)
149                 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
150 }
151
152 unsigned TextureCube::get_n_levels() const
153 {
154         unsigned n = 0;
155         for(unsigned s=size; s; s>>=1, ++n) ;
156         return n;
157 }
158
159 unsigned TextureCube::get_level_size(unsigned level) const
160 {
161         return size>>level;
162 }
163
164 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
165 {
166         switch(i)
167         {
168         case 0: return POSITIVE_X;
169         case 1: return NEGATIVE_X;
170         case 2: return POSITIVE_Y;
171         case 3: return NEGATIVE_Y;
172         case 4: return POSITIVE_Z;
173         case 5: return NEGATIVE_Z;
174         default: throw out_of_range("TextureCube::enumerate_faces");
175         }
176 }
177
178 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
179 {
180         switch(face)
181         {
182         case POSITIVE_X: return directions[0];
183         case NEGATIVE_X: return directions[1];
184         case POSITIVE_Y: return directions[2];
185         case NEGATIVE_Y: return directions[3];
186         case POSITIVE_Z: return directions[4];
187         case NEGATIVE_Z: return directions[5];
188         default: throw invalid_argument("TextureCube::get_face_direction");
189         }
190 }
191
192 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
193 {
194         switch(face)
195         {
196         case POSITIVE_X: return directions[5];
197         case NEGATIVE_X: return directions[4];
198         case POSITIVE_Y: return directions[0];
199         case NEGATIVE_Y: return directions[0];
200         case POSITIVE_Z: return directions[0];
201         case NEGATIVE_Z: return directions[1];
202         default: throw invalid_argument("TextureCube::get_s_direction");
203         }
204 }
205
206 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
207 {
208         switch(face)
209         {
210         case POSITIVE_X: return directions[3];
211         case NEGATIVE_X: return directions[3];
212         case POSITIVE_Y: return directions[4];
213         case NEGATIVE_Y: return directions[5];
214         case POSITIVE_Z: return directions[3];
215         case NEGATIVE_Z: return directions[3];
216         default: throw invalid_argument("TextureCube::get_t_direction");
217         }
218 }
219
220 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
221 {
222         float s = (u+0.5f)*2.0f/size-1.0f;
223         float t = (v+0.5f)*2.0f/size-1.0f;
224         const Vector3 &fv = get_face_direction(face);
225         const Vector3 &sv = get_s_direction(face);
226         const Vector3 &tv = get_t_direction(face);
227         return fv+s*sv+t*tv;
228 }
229
230 UInt64 TextureCube::get_data_size() const
231 {
232         return id ? size*size*6*get_pixel_size(ifmt) : 0;
233 }
234
235
236 TextureCube::Loader::Loader(TextureCube &t):
237         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
238 {
239         init();
240 }
241
242 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
243         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
244 {
245         init();
246 }
247
248 void TextureCube::Loader::init()
249 {
250         add("external_image", &Loader::external_image);
251         add("image_data", &Loader::image_data);
252         add("raw_data", &Loader::raw_data);
253         add("storage", &Loader::storage);
254 }
255
256 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
257 {
258         Graphics::Image img;
259         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
260         img.load_io(*io);
261
262         obj.image(face, img, srgb);
263 }
264
265 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
266 {
267         Graphics::Image img;
268         IO::Memory mem(data.data(), data.size());
269         img.load_io(mem);
270
271         obj.image(face, img, srgb);
272 }
273
274 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
275 {
276         obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
277 }
278
279 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
280 {
281         if(srgb)
282                 fmt = get_srgb_pixelformat(fmt);
283         obj.storage(fmt, s);
284 }
285
286
287 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
288 {
289         const string &str = conv.get();
290         if(str=="POSITIVE_X")
291                 face = POSITIVE_X;
292         else if(str=="NEGATIVE_X")
293                 face = NEGATIVE_X;
294         else if(str=="POSITIVE_Y")
295                 face = POSITIVE_Y;
296         else if(str=="NEGATIVE_Y")
297                 face = NEGATIVE_Y;
298         else if(str=="POSITIVE_Z")
299                 face = POSITIVE_Z;
300         else if(str=="NEGATIVE_Z")
301                 face = NEGATIVE_Z;
302         else
303                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
304 }
305
306 } // namespace GL
307 } // namespace Msp