]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Use vector when there's no reason to use some other container
[libs/gl.git] / source / texture.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_shadow.h>
3 #include <msp/gl/extensions/arb_texture_swizzle.h>
4 #include <msp/gl/extensions/ext_framebuffer_object.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
7 #include <msp/gl/extensions/sgis_generate_mipmap.h>
8 #include <msp/io/memory.h>
9 #include <msp/strings/format.h>
10 #include "error.h"
11 #include "resourcemanager.h"
12 #include "resources.h"
13 #include "texture.h"
14 #include "texunit.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 void operator>>(const LexicalConverter &c, TextureFilter &tf)
22 {
23         if(c.get()=="NEAREST")
24                 tf = NEAREST;
25         else if(c.get()=="LINEAR")
26                 tf = LINEAR;
27         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
28                 tf = NEAREST_MIPMAP_NEAREST;
29         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
30                 tf = NEAREST_MIPMAP_LINEAR;
31         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
32                 tf = LINEAR_MIPMAP_NEAREST;
33         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
34                 tf = LINEAR_MIPMAP_LINEAR;
35         else
36                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
37 }
38
39
40 void operator>>(const LexicalConverter &c, TextureWrap &tw)
41 {
42         if(c.get()=="REPEAT")
43                 tw = REPEAT;
44         else if(c.get()=="CLAMP_TO_EDGE")
45                 tw = CLAMP_TO_EDGE;
46         else if(c.get()=="MIRRORED_REPEAT")
47                 tw = MIRRORED_REPEAT;
48         else
49                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
50 }
51
52
53 int Texture::swizzle_orders[] =
54 {
55         GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
56         GL_RED, GL_RED, GL_RED, GL_ONE,
57         GL_RED, GL_RED, GL_RED, GL_GREEN
58 };
59
60 Texture::Texture(GLenum t, ResourceManager *m):
61         id(0),
62         target(t),
63         ifmt(RGB),
64         min_filter(NEAREST_MIPMAP_LINEAR),
65         mag_filter(LINEAR),
66         wrap_s(REPEAT),
67         wrap_t(REPEAT),
68         wrap_r(REPEAT),
69         gen_mipmap(false),
70         compare(false),
71         cmp_func(LEQUAL),
72         dirty_params(0)
73 {
74         if(m)
75                 set_manager(m);
76         else if(ARB_direct_state_access)
77                 glCreateTextures(target, 1, &id);
78         else
79                 glGenTextures(1, &id);
80 }
81
82 Texture::~Texture()
83 {
84         while(TexUnit *unit = TexUnit::find_unit(this))
85                 unbind_from(unit->get_index());
86
87         if(id)
88                 glDeleteTextures(1, &id);
89 }
90
91 DataType Texture::get_alloc_type(PixelFormat fmt)
92 {
93         return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
94 }
95
96 void Texture::set_internal_format(PixelFormat fmt)
97 {
98         if(!get_component_size(fmt) && OES_required_internalformat)
99                 fmt = get_default_sized_pixelformat(fmt);
100
101         FormatSwizzle swiz = NO_SWIZZLE;
102         if(ARB_texture_rg && ARB_texture_swizzle)
103         {
104                 if(fmt==LUMINANCE8 || fmt==SLUMINANCE8)
105                 {
106                         fmt = R8;
107                         swiz = R_TO_LUMINANCE;
108                 }
109                 else if(fmt==LUMINANCE8_ALPHA8 || fmt==SLUMINANCE8_ALPHA8)
110                 {
111                         fmt = RG8;
112                         swiz = RG_TO_LUMINANCE_ALPHA;
113                 }
114         }
115
116         require_pixelformat(fmt);
117         ifmt = fmt;
118         swizzle = swiz;
119         if(swizzle)
120                 update_parameter(FORMAT_SWIZZLE);
121 }
122
123 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
124 {
125         if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
126                 return get_base_pixelformat(ifmt);
127         else
128                 return fmt;
129 }
130
131 void Texture::update_parameter(int mask) const
132 {
133         if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
134         {
135                 TexUnit *unit = TexUnit::find_unit(this);
136                 if(!unit)
137                 {
138                         dirty_params |= mask;
139                         return;
140                 }
141
142                 unit->bind();
143         }
144
145         if(mask&MIN_FILTER)
146                 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
147         if(mask&MAG_FILTER)
148                 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
149         if(mask&MAX_ANISOTROPY)
150                 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
151         if(mask&WRAP_S)
152                 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
153         if(mask&WRAP_T)
154                 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
155         if(mask&WRAP_R)
156                 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
157         if(mask&GENERATE_MIPMAP)
158                 set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
159         if(mask&COMPARE)
160                 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
161         if(mask&COMPARE_FUNC)
162                 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
163         if(mask&FORMAT_SWIZZLE)
164         {
165                 if(get_gl_api()==OPENGL_ES2)
166                 {
167                         set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
168                         set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
169                         set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
170                         set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
171                 }
172                 else
173                 {
174                         if(ARB_direct_state_access)
175                                 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
176                         else
177                                 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
178                 }
179         }
180 }
181
182 void Texture::set_parameter_i(GLenum param, int value) const
183 {
184         if(ARB_direct_state_access)
185                 glTextureParameteri(id, param, value);
186         else
187                 glTexParameteri(target, param, value);
188 }
189
190 void Texture::set_parameter_f(GLenum param, float value) const
191 {
192         if(ARB_direct_state_access)
193                 glTextureParameterf(id, param, value);
194         else
195                 glTexParameterf(target, param, value);
196 }
197
198 void Texture::set_min_filter(TextureFilter f)
199 {
200         min_filter = f;
201         update_parameter(MIN_FILTER);
202 }
203
204 void Texture::set_mag_filter(TextureFilter f)
205 {
206         mag_filter = f;
207         update_parameter(MAG_FILTER);
208 }
209
210 void Texture::set_filter(TextureFilter f)
211 {
212         set_min_filter(f);
213         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
214 }
215
216 void Texture::set_max_anisotropy(float a)
217 {
218         if(a<1.0f)
219                 throw invalid_argument("Texture::set_max_anisotropy");
220         else if(a>1.0f)
221                 static Require _req(EXT_texture_filter_anisotropic);
222         max_anisotropy = a;
223         update_parameter(MAX_ANISOTROPY);
224 }
225
226 void Texture::set_wrap(TextureWrap w)
227 {
228         set_wrap_s(w);
229         set_wrap_t(w);
230         if(EXT_texture3D)
231                 set_wrap_r(w);
232 }
233
234 void Texture::set_wrap_s(TextureWrap w)
235 {
236         wrap_s = w;
237         update_parameter(WRAP_S);
238 }
239
240 void Texture::set_wrap_t(TextureWrap w)
241 {
242         wrap_t = w;
243         update_parameter(WRAP_T);
244 }
245
246 void Texture::set_wrap_r(TextureWrap w)
247 {
248         static Require _req(EXT_texture3D);
249         wrap_r = w;
250         update_parameter(WRAP_R);
251 }
252
253 bool Texture::can_generate_mipmap()
254 {
255         return (EXT_framebuffer_object || SGIS_generate_mipmap);
256 }
257
258 void Texture::set_generate_mipmap(bool gm)
259 {
260         if(gm && !EXT_framebuffer_object)
261                 static Require _req(SGIS_generate_mipmap);
262         gen_mipmap = gm;
263         if(!EXT_framebuffer_object)
264                 update_parameter(GENERATE_MIPMAP);
265 }
266
267 void Texture::auto_generate_mipmap()
268 {
269         // glGenerateMipmap is defined here
270         if(EXT_framebuffer_object)
271         {
272                 if(ARB_direct_state_access)
273                         glGenerateTextureMipmap(id);
274                 else
275                         glGenerateMipmap(target);
276         }
277 }
278
279 void Texture::set_compare_enabled(bool c)
280 {
281         if(c)
282                 static Require _req(ARB_shadow);
283         compare = c;
284         update_parameter(COMPARE);
285 }
286
287 void Texture::set_compare_func(Predicate f)
288 {
289         static Require _req(ARB_shadow);
290         cmp_func = f;
291         update_parameter(COMPARE_FUNC);
292 }
293
294 void Texture::load_image(const string &fn, bool srgb)
295 {
296         Graphics::Image img;
297         img.load_file(fn);
298
299         image(img, srgb);
300 }
301
302 void Texture::bind_to(unsigned i) const
303 {
304         if(!id)
305         {
306                 if(manager)
307                         manager->resource_used(*this);
308                 if(!id)
309                 {
310                         unbind_from(i);
311                         return;
312                 }
313         }
314
315         TexUnit &unit = TexUnit::get_unit(i);
316         const Texture *old = unit.get_texture();
317         if(unit.set_texture(this))
318         {
319                 if(manager)
320                         manager->resource_used(*this);
321
322                 unit.bind();
323                 if(unit.supports_legacy())
324                 {
325                         if(old && old->target!=target)
326                                 glDisable(old->target);
327                         if(!old || old->target!=target)
328                                 glEnable(target);
329                 }
330                 glBindTexture(target, id);
331
332                 if(dirty_params)
333                 {
334                         update_parameter(dirty_params);
335                         dirty_params = 0;
336                 }
337         }
338 }
339
340 const Texture *Texture::current(unsigned i)
341 {
342         return TexUnit::get_unit(i).get_texture();
343 }
344
345 void Texture::unbind_from(unsigned i)
346 {
347         TexUnit &unit = TexUnit::get_unit(i);
348         const Texture *cur = unit.get_texture();
349         if(unit.set_texture(0))
350         {
351                 unit.bind();
352                 glBindTexture(cur->target, 0);
353                 if(unit.supports_legacy())
354                         glDisable(cur->target);
355         }
356 }
357
358
359 Texture::Loader::Loader(Texture &t):
360         DataFile::CollectionObjectLoader<Texture>(t, 0)
361 {
362         init();
363 }
364
365 Texture::Loader::Loader(Texture &t, Collection &c):
366         DataFile::CollectionObjectLoader<Texture>(t, &c)
367 {
368         init();
369 }
370
371 void Texture::Loader::init()
372 {
373         if(Resources *res = dynamic_cast<Resources *>(coll))
374                 srgb = res->get_srgb_conversion();
375         else
376                 srgb = false;
377
378         add("external_image", &Loader::external_image);
379         add("filter", &Loader::filter);
380         add("generate_mipmap", &Loader::generate_mipmap);
381         add("image_data", &Loader::image_data);
382         add("mag_filter", &Loader::mag_filter);
383         add("max_anisotropy", &Loader::max_anisotropy);
384         add("min_filter", &Loader::min_filter);
385         add("wrap",       &Loader::wrap);
386         add("wrap_r",     &Loader::wrap_r);
387         add("wrap_s",     &Loader::wrap_s);
388         add("wrap_t",     &Loader::wrap_t);
389 }
390
391 void Texture::Loader::external_image(const string &fn)
392 {
393         Graphics::Image img;
394         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
395         img.load_io(*io);
396
397         obj.image(img, srgb);
398 }
399
400 void Texture::Loader::filter(TextureFilter f)
401 {
402         obj.set_filter(f);
403 }
404
405 void Texture::Loader::generate_mipmap(bool gm)
406 {
407         obj.set_generate_mipmap(gm);
408 }
409
410 void Texture::Loader::image_data(const string &data)
411 {
412         Graphics::Image img;
413         IO::Memory mem(data.data(), data.size());
414         img.load_io(mem);
415
416         obj.image(img, srgb);
417 }
418
419 void Texture::Loader::mag_filter(TextureFilter f)
420 {
421         obj.set_mag_filter(f);
422 }
423
424 void Texture::Loader::max_anisotropy(float a)
425 {
426         obj.set_max_anisotropy(a);
427 }
428
429 void Texture::Loader::min_filter(TextureFilter f)
430 {
431         obj.set_min_filter(f);
432 }
433
434 void Texture::Loader::wrap(TextureWrap w)
435 {
436         obj.set_wrap(w);
437 }
438
439 void Texture::Loader::wrap_r(TextureWrap w)
440 {
441         obj.set_wrap_r(w);
442 }
443
444 void Texture::Loader::wrap_s(TextureWrap w)
445 {
446         obj.set_wrap_s(w);
447 }
448
449 void Texture::Loader::wrap_t(TextureWrap w)
450 {
451         obj.set_wrap_t(w);
452 }
453
454
455 bool is_mipmapped(TextureFilter filter)
456 {
457         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
458                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
459 }
460
461 } // namespace GL
462 } // namespace Msp