1 #include <msp/gl/extensions/arb_shadow.h>
2 #include <msp/gl/extensions/ext_framebuffer_object.h>
3 #include <msp/gl/extensions/ext_texture3d.h>
4 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
5 #include <msp/gl/extensions/sgis_generate_mipmap.h>
6 #include <msp/io/memory.h>
7 #include <msp/strings/format.h>
9 #include "resourcemanager.h"
10 #include "resources.h"
19 void operator>>(const LexicalConverter &c, TextureFilter &tf)
21 if(c.get()=="NEAREST")
23 else if(c.get()=="LINEAR")
25 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
26 tf = NEAREST_MIPMAP_NEAREST;
27 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
28 tf = NEAREST_MIPMAP_LINEAR;
29 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
30 tf = LINEAR_MIPMAP_NEAREST;
31 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
32 tf = LINEAR_MIPMAP_LINEAR;
34 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
38 void operator>>(const LexicalConverter &c, TextureWrap &tw)
42 else if(c.get()=="CLAMP_TO_EDGE")
44 else if(c.get()=="MIRRORED_REPEAT")
47 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
51 Texture::Texture(GLenum t, ResourceManager *m):
54 min_filter(NEAREST_MIPMAP_LINEAR),
67 glGenTextures(1, &id);
72 while(TexUnit *unit = TexUnit::find_unit(this))
73 unbind_from(unit->get_index());
76 glDeleteTextures(1, &id);
79 DataType Texture::get_alloc_type(PixelFormat fmt)
81 return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
84 void Texture::update_parameter(int mask) const
86 if(TexUnit::current().get_texture()!=this)
88 TexUnit *unit = TexUnit::find_unit(this);
99 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
101 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
102 if(mask&MAX_ANISOTROPY)
103 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
105 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
107 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
109 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
110 if(mask&GENERATE_MIPMAP)
111 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
113 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
114 if(mask&COMPARE_FUNC)
115 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
118 void Texture::set_min_filter(TextureFilter f)
121 update_parameter(MIN_FILTER);
124 void Texture::set_mag_filter(TextureFilter f)
127 update_parameter(MAG_FILTER);
130 void Texture::set_filter(TextureFilter f)
133 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
136 void Texture::set_max_anisotropy(float a)
139 throw invalid_argument("Texture::set_max_anisotropy");
141 static Require _req(EXT_texture_filter_anisotropic);
143 update_parameter(MAX_ANISOTROPY);
146 void Texture::set_wrap(TextureWrap w)
154 void Texture::set_wrap_s(TextureWrap w)
157 update_parameter(WRAP_S);
160 void Texture::set_wrap_t(TextureWrap w)
163 update_parameter(WRAP_T);
166 void Texture::set_wrap_r(TextureWrap w)
168 static Require _req(EXT_texture3D);
170 update_parameter(WRAP_R);
173 void Texture::set_generate_mipmap(bool gm)
176 static Require _req(SGIS_generate_mipmap);
178 if(get_gl_api()!=OPENGL_ES2)
179 update_parameter(GENERATE_MIPMAP);
182 void Texture::auto_generate_mipmap()
184 if(get_gl_api()==OPENGL_ES2)
186 // glGenerateMipmap is defined here
187 static Require _req(EXT_framebuffer_object);
188 glGenerateMipmap(target);
192 void Texture::set_compare_enabled(bool c)
195 static Require _req(ARB_shadow);
197 update_parameter(COMPARE);
200 void Texture::set_compare_func(Predicate f)
202 static Require _req(ARB_shadow);
204 update_parameter(COMPARE_FUNC);
207 void Texture::load_image(const string &fn, bool srgb)
215 void Texture::bind_to(unsigned i) const
220 manager->resource_used(*this);
228 TexUnit &unit = TexUnit::get_unit(i);
229 const Texture *old = unit.get_texture();
230 if(unit.set_texture(this))
233 manager->resource_used(*this);
236 if(unit.supports_legacy())
238 if(old && old->target!=target)
239 glDisable(old->target);
240 if(!old || old->target!=target)
243 glBindTexture(target, id);
247 update_parameter(dirty_params);
253 const Texture *Texture::current(unsigned i)
255 return TexUnit::get_unit(i).get_texture();
258 void Texture::unbind_from(unsigned i)
260 TexUnit &unit = TexUnit::get_unit(i);
261 const Texture *cur = unit.get_texture();
262 if(unit.set_texture(0))
265 glBindTexture(cur->target, 0);
266 if(unit.supports_legacy())
267 glDisable(cur->target);
272 Texture::Loader::Loader(Texture &t):
273 DataFile::CollectionObjectLoader<Texture>(t, 0)
278 Texture::Loader::Loader(Texture &t, Collection &c):
279 DataFile::CollectionObjectLoader<Texture>(t, &c)
284 void Texture::Loader::init()
286 if(Resources *res = dynamic_cast<Resources *>(coll))
287 srgb = res->get_srgb_conversion();
291 add("external_image", &Loader::external_image);
292 add("filter", &Loader::filter);
293 add("generate_mipmap", &Loader::generate_mipmap);
294 add("image_data", &Loader::image_data);
295 add("mag_filter", &Loader::mag_filter);
296 add("max_anisotropy", &Loader::max_anisotropy);
297 add("min_filter", &Loader::min_filter);
298 add("wrap", &Loader::wrap);
299 add("wrap_r", &Loader::wrap_r);
300 add("wrap_s", &Loader::wrap_s);
301 add("wrap_t", &Loader::wrap_t);
304 void Texture::Loader::external_image(const string &fn)
307 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
310 obj.image(img, srgb);
313 void Texture::Loader::filter(TextureFilter f)
318 void Texture::Loader::generate_mipmap(bool gm)
320 obj.set_generate_mipmap(gm);
323 void Texture::Loader::image_data(const string &data)
326 IO::Memory mem(data.data(), data.size());
329 obj.image(img, srgb);
332 void Texture::Loader::mag_filter(TextureFilter f)
334 obj.set_mag_filter(f);
337 void Texture::Loader::max_anisotropy(float a)
339 obj.set_max_anisotropy(a);
342 void Texture::Loader::min_filter(TextureFilter f)
344 obj.set_min_filter(f);
347 void Texture::Loader::wrap(TextureWrap w)
352 void Texture::Loader::wrap_r(TextureWrap w)
357 void Texture::Loader::wrap_s(TextureWrap w)
362 void Texture::Loader::wrap_t(TextureWrap w)
368 bool is_mipmapped(TextureFilter filter)
370 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
371 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);