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