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