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