]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Derive ProgramCompiler::DeclarationCombiner from BlockModifier
[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) && MSP_sized_internal_formats)
99         {
100                 unsigned size = (fmt==DEPTH_COMPONENT ? get_gl_api()==OPENGL_ES2 ? 2 : 4 : 1);
101                 fmt = get_sized_pixelformat(fmt, size);
102         }
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, gen_mipmap);
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(ARB_direct_state_access)
169                         glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
170                 else
171                         glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
172         }
173 }
174
175 void Texture::set_parameter_i(GLenum param, int value) const
176 {
177         if(ARB_direct_state_access)
178                 glTextureParameteri(id, param, value);
179         else
180                 glTexParameteri(target, param, value);
181 }
182
183 void Texture::set_parameter_f(GLenum param, float value) const
184 {
185         if(ARB_direct_state_access)
186                 glTextureParameterf(id, param, value);
187         else
188                 glTexParameterf(target, param, value);
189 }
190
191 void Texture::set_min_filter(TextureFilter f)
192 {
193         min_filter = f;
194         update_parameter(MIN_FILTER);
195 }
196
197 void Texture::set_mag_filter(TextureFilter f)
198 {
199         mag_filter = f;
200         update_parameter(MAG_FILTER);
201 }
202
203 void Texture::set_filter(TextureFilter f)
204 {
205         set_min_filter(f);
206         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
207 }
208
209 void Texture::set_max_anisotropy(float a)
210 {
211         if(a<1.0f)
212                 throw invalid_argument("Texture::set_max_anisotropy");
213         else if(a>1.0f)
214                 static Require _req(EXT_texture_filter_anisotropic);
215         max_anisotropy = a;
216         update_parameter(MAX_ANISOTROPY);
217 }
218
219 void Texture::set_wrap(TextureWrap w)
220 {
221         set_wrap_s(w);
222         set_wrap_t(w);
223         if(EXT_texture3D)
224                 set_wrap_r(w);
225 }
226
227 void Texture::set_wrap_s(TextureWrap w)
228 {
229         wrap_s = w;
230         update_parameter(WRAP_S);
231 }
232
233 void Texture::set_wrap_t(TextureWrap w)
234 {
235         wrap_t = w;
236         update_parameter(WRAP_T);
237 }
238
239 void Texture::set_wrap_r(TextureWrap w)
240 {
241         static Require _req(EXT_texture3D);
242         wrap_r = w;
243         update_parameter(WRAP_R);
244 }
245
246 bool Texture::can_generate_mipmap()
247 {
248         return (EXT_framebuffer_object || SGIS_generate_mipmap);
249 }
250
251 void Texture::set_generate_mipmap(bool gm)
252 {
253         if(gm && !EXT_framebuffer_object)
254                 static Require _req(SGIS_generate_mipmap);
255         gen_mipmap = gm;
256         if(!EXT_framebuffer_object)
257                 update_parameter(GENERATE_MIPMAP);
258 }
259
260 void Texture::auto_generate_mipmap()
261 {
262         // glGenerateMipmap is defined here
263         if(EXT_framebuffer_object)
264         {
265                 if(ARB_direct_state_access)
266                         glGenerateTextureMipmap(id);
267                 else
268                         glGenerateMipmap(target);
269         }
270 }
271
272 void Texture::set_compare_enabled(bool c)
273 {
274         if(c)
275                 static Require _req(ARB_shadow);
276         compare = c;
277         update_parameter(COMPARE);
278 }
279
280 void Texture::set_compare_func(Predicate f)
281 {
282         static Require _req(ARB_shadow);
283         cmp_func = f;
284         update_parameter(COMPARE_FUNC);
285 }
286
287 void Texture::load_image(const string &fn, bool srgb)
288 {
289         Graphics::Image img;
290         img.load_file(fn);
291
292         image(img, srgb);
293 }
294
295 void Texture::bind_to(unsigned i) const
296 {
297         if(!id)
298         {
299                 if(manager)
300                         manager->resource_used(*this);
301                 if(!id)
302                 {
303                         unbind_from(i);
304                         return;
305                 }
306         }
307
308         TexUnit &unit = TexUnit::get_unit(i);
309         const Texture *old = unit.get_texture();
310         if(unit.set_texture(this))
311         {
312                 if(manager)
313                         manager->resource_used(*this);
314
315                 unit.bind();
316                 if(unit.supports_legacy())
317                 {
318                         if(old && old->target!=target)
319                                 glDisable(old->target);
320                         if(!old || old->target!=target)
321                                 glEnable(target);
322                 }
323                 glBindTexture(target, id);
324
325                 if(dirty_params)
326                 {
327                         update_parameter(dirty_params);
328                         dirty_params = 0;
329                 }
330         }
331 }
332
333 const Texture *Texture::current(unsigned i)
334 {
335         return TexUnit::get_unit(i).get_texture();
336 }
337
338 void Texture::unbind_from(unsigned i)
339 {
340         TexUnit &unit = TexUnit::get_unit(i);
341         const Texture *cur = unit.get_texture();
342         if(unit.set_texture(0))
343         {
344                 unit.bind();
345                 glBindTexture(cur->target, 0);
346                 if(unit.supports_legacy())
347                         glDisable(cur->target);
348         }
349 }
350
351
352 Texture::Loader::Loader(Texture &t):
353         DataFile::CollectionObjectLoader<Texture>(t, 0)
354 {
355         init();
356 }
357
358 Texture::Loader::Loader(Texture &t, Collection &c):
359         DataFile::CollectionObjectLoader<Texture>(t, &c)
360 {
361         init();
362 }
363
364 void Texture::Loader::init()
365 {
366         if(Resources *res = dynamic_cast<Resources *>(coll))
367                 srgb = res->get_srgb_conversion();
368         else
369                 srgb = false;
370
371         add("external_image", &Loader::external_image);
372         add("filter", &Loader::filter);
373         add("generate_mipmap", &Loader::generate_mipmap);
374         add("image_data", &Loader::image_data);
375         add("mag_filter", &Loader::mag_filter);
376         add("max_anisotropy", &Loader::max_anisotropy);
377         add("min_filter", &Loader::min_filter);
378         add("wrap",       &Loader::wrap);
379         add("wrap_r",     &Loader::wrap_r);
380         add("wrap_s",     &Loader::wrap_s);
381         add("wrap_t",     &Loader::wrap_t);
382 }
383
384 void Texture::Loader::external_image(const string &fn)
385 {
386         Graphics::Image img;
387         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
388         img.load_io(*io);
389
390         obj.image(img, srgb);
391 }
392
393 void Texture::Loader::filter(TextureFilter f)
394 {
395         obj.set_filter(f);
396 }
397
398 void Texture::Loader::generate_mipmap(bool gm)
399 {
400         obj.set_generate_mipmap(gm);
401 }
402
403 void Texture::Loader::image_data(const string &data)
404 {
405         Graphics::Image img;
406         IO::Memory mem(data.data(), data.size());
407         img.load_io(mem);
408
409         obj.image(img, srgb);
410 }
411
412 void Texture::Loader::mag_filter(TextureFilter f)
413 {
414         obj.set_mag_filter(f);
415 }
416
417 void Texture::Loader::max_anisotropy(float a)
418 {
419         obj.set_max_anisotropy(a);
420 }
421
422 void Texture::Loader::min_filter(TextureFilter f)
423 {
424         obj.set_min_filter(f);
425 }
426
427 void Texture::Loader::wrap(TextureWrap w)
428 {
429         obj.set_wrap(w);
430 }
431
432 void Texture::Loader::wrap_r(TextureWrap w)
433 {
434         obj.set_wrap_r(w);
435 }
436
437 void Texture::Loader::wrap_s(TextureWrap w)
438 {
439         obj.set_wrap_s(w);
440 }
441
442 void Texture::Loader::wrap_t(TextureWrap w)
443 {
444         obj.set_wrap_t(w);
445 }
446
447
448 bool is_mipmapped(TextureFilter filter)
449 {
450         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
451                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
452 }
453
454 } // namespace GL
455 } // namespace Msp