1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_shadow.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
4 #include <msp/gl/extensions/ext_texture3d.h>
5 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
6 #include <msp/gl/extensions/sgis_generate_mipmap.h>
7 #include <msp/io/memory.h>
8 #include <msp/strings/format.h>
10 #include "resourcemanager.h"
11 #include "resources.h"
20 void operator>>(const LexicalConverter &c, TextureFilter &tf)
22 if(c.get()=="NEAREST")
24 else if(c.get()=="LINEAR")
26 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
27 tf = NEAREST_MIPMAP_NEAREST;
28 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
29 tf = NEAREST_MIPMAP_LINEAR;
30 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
31 tf = LINEAR_MIPMAP_NEAREST;
32 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
33 tf = LINEAR_MIPMAP_LINEAR;
35 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
39 void operator>>(const LexicalConverter &c, TextureWrap &tw)
43 else if(c.get()=="CLAMP_TO_EDGE")
45 else if(c.get()=="MIRRORED_REPEAT")
48 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
52 Texture::Texture(GLenum t, ResourceManager *m):
55 min_filter(NEAREST_MIPMAP_LINEAR),
67 else if(ARB_direct_state_access)
68 glCreateTextures(target, 1, &id);
70 glGenTextures(1, &id);
75 while(TexUnit *unit = TexUnit::find_unit(this))
76 unbind_from(unit->get_index());
79 glDeleteTextures(1, &id);
82 DataType Texture::get_alloc_type(PixelFormat fmt)
84 return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
87 void Texture::update_parameter(int mask) const
89 if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
91 TexUnit *unit = TexUnit::find_unit(this);
102 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
104 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
105 if(mask&MAX_ANISOTROPY)
106 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
108 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
110 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
112 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
113 if(mask&GENERATE_MIPMAP)
114 set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
116 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
117 if(mask&COMPARE_FUNC)
118 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
121 void Texture::set_parameter_i(GLenum param, int value) const
123 if(ARB_direct_state_access)
124 glTextureParameteri(id, param, value);
126 glTexParameteri(target, param, value);
129 void Texture::set_parameter_f(GLenum param, float value) const
131 if(ARB_direct_state_access)
132 glTextureParameterf(id, param, value);
134 glTexParameterf(target, param, value);
137 void Texture::set_min_filter(TextureFilter f)
140 update_parameter(MIN_FILTER);
143 void Texture::set_mag_filter(TextureFilter f)
146 update_parameter(MAG_FILTER);
149 void Texture::set_filter(TextureFilter f)
152 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
155 void Texture::set_max_anisotropy(float a)
158 throw invalid_argument("Texture::set_max_anisotropy");
160 static Require _req(EXT_texture_filter_anisotropic);
162 update_parameter(MAX_ANISOTROPY);
165 void Texture::set_wrap(TextureWrap w)
173 void Texture::set_wrap_s(TextureWrap w)
176 update_parameter(WRAP_S);
179 void Texture::set_wrap_t(TextureWrap w)
182 update_parameter(WRAP_T);
185 void Texture::set_wrap_r(TextureWrap w)
187 static Require _req(EXT_texture3D);
189 update_parameter(WRAP_R);
192 void Texture::set_generate_mipmap(bool gm)
194 if(gm && !EXT_framebuffer_object)
195 static Require _req(SGIS_generate_mipmap);
197 if(!EXT_framebuffer_object)
198 update_parameter(GENERATE_MIPMAP);
201 void Texture::auto_generate_mipmap()
203 // glGenerateMipmap is defined here
204 if(EXT_framebuffer_object)
206 if(ARB_direct_state_access)
207 glGenerateTextureMipmap(id);
209 glGenerateMipmap(target);
213 void Texture::set_compare_enabled(bool c)
216 static Require _req(ARB_shadow);
218 update_parameter(COMPARE);
221 void Texture::set_compare_func(Predicate f)
223 static Require _req(ARB_shadow);
225 update_parameter(COMPARE_FUNC);
228 void Texture::load_image(const string &fn, bool srgb)
236 void Texture::bind_to(unsigned i) const
241 manager->resource_used(*this);
249 TexUnit &unit = TexUnit::get_unit(i);
250 const Texture *old = unit.get_texture();
251 if(unit.set_texture(this))
254 manager->resource_used(*this);
257 if(unit.supports_legacy())
259 if(old && old->target!=target)
260 glDisable(old->target);
261 if(!old || old->target!=target)
264 glBindTexture(target, id);
268 update_parameter(dirty_params);
274 const Texture *Texture::current(unsigned i)
276 return TexUnit::get_unit(i).get_texture();
279 void Texture::unbind_from(unsigned i)
281 TexUnit &unit = TexUnit::get_unit(i);
282 const Texture *cur = unit.get_texture();
283 if(unit.set_texture(0))
286 glBindTexture(cur->target, 0);
287 if(unit.supports_legacy())
288 glDisable(cur->target);
293 Texture::Loader::Loader(Texture &t):
294 DataFile::CollectionObjectLoader<Texture>(t, 0)
299 Texture::Loader::Loader(Texture &t, Collection &c):
300 DataFile::CollectionObjectLoader<Texture>(t, &c)
305 void Texture::Loader::init()
307 if(Resources *res = dynamic_cast<Resources *>(coll))
308 srgb = res->get_srgb_conversion();
312 add("external_image", &Loader::external_image);
313 add("filter", &Loader::filter);
314 add("generate_mipmap", &Loader::generate_mipmap);
315 add("image_data", &Loader::image_data);
316 add("mag_filter", &Loader::mag_filter);
317 add("max_anisotropy", &Loader::max_anisotropy);
318 add("min_filter", &Loader::min_filter);
319 add("wrap", &Loader::wrap);
320 add("wrap_r", &Loader::wrap_r);
321 add("wrap_s", &Loader::wrap_s);
322 add("wrap_t", &Loader::wrap_t);
325 void Texture::Loader::external_image(const string &fn)
328 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
331 obj.image(img, srgb);
334 void Texture::Loader::filter(TextureFilter f)
339 void Texture::Loader::generate_mipmap(bool gm)
341 obj.set_generate_mipmap(gm);
344 void Texture::Loader::image_data(const string &data)
347 IO::Memory mem(data.data(), data.size());
350 obj.image(img, srgb);
353 void Texture::Loader::mag_filter(TextureFilter f)
355 obj.set_mag_filter(f);
358 void Texture::Loader::max_anisotropy(float a)
360 obj.set_max_anisotropy(a);
363 void Texture::Loader::min_filter(TextureFilter f)
365 obj.set_min_filter(f);
368 void Texture::Loader::wrap(TextureWrap w)
373 void Texture::Loader::wrap_r(TextureWrap w)
378 void Texture::Loader::wrap_s(TextureWrap w)
383 void Texture::Loader::wrap_t(TextureWrap w)
389 bool is_mipmapped(TextureFilter filter)
391 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
392 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);