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/strings/format.h>
8 #include "resourcemanager.h"
18 void operator>>(const LexicalConverter &c, TextureFilter &tf)
20 if(c.get()=="NEAREST")
22 else if(c.get()=="LINEAR")
24 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
25 tf = NEAREST_MIPMAP_NEAREST;
26 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
27 tf = NEAREST_MIPMAP_LINEAR;
28 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
29 tf = LINEAR_MIPMAP_NEAREST;
30 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
31 tf = LINEAR_MIPMAP_LINEAR;
33 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
37 void operator>>(const LexicalConverter &c, TextureWrap &tw)
41 else if(c.get()=="CLAMP_TO_EDGE")
43 else if(c.get()=="MIRRORED_REPEAT")
46 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
50 Texture::Texture(GLenum t, ResourceManager *m):
53 min_filter(NEAREST_MIPMAP_LINEAR),
66 glGenTextures(1, &id);
71 while(TexUnit *unit = TexUnit::find_unit(this))
72 unbind_from(unit->get_index());
75 glDeleteTextures(1, &id);
78 DataType Texture::get_alloc_type(PixelFormat fmt)
80 return (fmt==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
83 void Texture::update_parameter(int mask) const
85 if(TexUnit::current().get_texture()!=this)
87 TexUnit *unit = TexUnit::find_unit(this);
98 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
100 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
101 if(mask&MAX_ANISOTROPY)
102 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
104 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
106 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
108 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
109 if(mask&GENERATE_MIPMAP)
110 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
112 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
113 if(mask&COMPARE_FUNC)
114 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
117 void Texture::set_min_filter(TextureFilter f)
120 update_parameter(MIN_FILTER);
123 void Texture::set_mag_filter(TextureFilter f)
126 update_parameter(MAG_FILTER);
129 void Texture::set_filter(TextureFilter f)
132 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
135 void Texture::set_max_anisotropy(float a)
138 throw invalid_argument("Texture::set_max_anisotropy");
140 static Require _req(EXT_texture_filter_anisotropic);
142 update_parameter(MAX_ANISOTROPY);
145 void Texture::set_wrap(TextureWrap w)
153 void Texture::set_wrap_s(TextureWrap w)
156 update_parameter(WRAP_S);
159 void Texture::set_wrap_t(TextureWrap w)
162 update_parameter(WRAP_T);
165 void Texture::set_wrap_r(TextureWrap w)
167 static Require _req(EXT_texture3D);
169 update_parameter(WRAP_R);
172 void Texture::set_generate_mipmap(bool gm)
175 static Require _req(SGIS_generate_mipmap);
177 if(get_gl_api()!=OPENGL_ES2)
178 update_parameter(GENERATE_MIPMAP);
181 void Texture::auto_generate_mipmap()
183 if(get_gl_api()==OPENGL_ES2)
185 // glGenerateMipmap is defined here
186 static Require _req(EXT_framebuffer_object);
187 glGenerateMipmap(target);
191 void Texture::set_compare_enabled(bool c)
194 static Require _req(ARB_shadow);
196 update_parameter(COMPARE);
199 void Texture::set_compare_func(Predicate f)
201 static Require _req(ARB_shadow);
203 update_parameter(COMPARE_FUNC);
206 void Texture::bind_to(unsigned i) const
211 manager->resource_used(*this);
219 TexUnit &unit = TexUnit::get_unit(i);
220 const Texture *old = unit.get_texture();
221 if(unit.set_texture(this))
224 manager->resource_used(*this);
227 if(unit.supports_legacy())
229 if(old && old->target!=target)
230 glDisable(old->target);
231 if(!old || old->target!=target)
234 glBindTexture(target, id);
238 update_parameter(dirty_params);
244 const Texture *Texture::current(unsigned i)
246 return TexUnit::get_unit(i).get_texture();
249 void Texture::unbind_from(unsigned i)
251 TexUnit &unit = TexUnit::get_unit(i);
252 const Texture *cur = unit.get_texture();
253 if(unit.set_texture(0))
256 glBindTexture(cur->target, 0);
257 if(unit.supports_legacy())
258 glDisable(cur->target);
263 Texture::Loader::Loader(Texture &t):
264 DataFile::CollectionObjectLoader<Texture>(t, 0)
269 Texture::Loader::Loader(Texture &t, Collection &c):
270 DataFile::CollectionObjectLoader<Texture>(t, &c)
275 void Texture::Loader::init()
277 if(Resources *res = dynamic_cast<Resources *>(coll))
278 srgb = res->get_srgb_conversion();
282 add("filter", &Loader::filter);
283 add("max_anisotropy", &Loader::max_anisotropy);
284 add("generate_mipmap", &Loader::generate_mipmap);
285 add("mag_filter", &Loader::mag_filter);
286 add("min_filter", &Loader::min_filter);
287 add("wrap", &Loader::wrap);
288 add("wrap_r", &Loader::wrap_r);
289 add("wrap_s", &Loader::wrap_s);
290 add("wrap_t", &Loader::wrap_t);
293 void Texture::Loader::filter(TextureFilter f)
298 void Texture::Loader::generate_mipmap(bool gm)
300 obj.set_generate_mipmap(gm);
303 void Texture::Loader::mag_filter(TextureFilter f)
305 obj.set_mag_filter(f);
308 void Texture::Loader::max_anisotropy(float a)
310 obj.set_max_anisotropy(a);
313 void Texture::Loader::min_filter(TextureFilter f)
315 obj.set_min_filter(f);
318 void Texture::Loader::wrap(TextureWrap w)
323 void Texture::Loader::wrap_r(TextureWrap w)
328 void Texture::Loader::wrap_s(TextureWrap w)
333 void Texture::Loader::wrap_t(TextureWrap w)