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