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>
11 #include "resourcemanager.h"
12 #include "resources.h"
21 void operator>>(const LexicalConverter &c, TextureFilter &tf)
23 if(c.get()=="NEAREST")
25 else if(c.get()=="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;
36 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
40 void operator>>(const LexicalConverter &c, TextureWrap &tw)
44 else if(c.get()=="CLAMP_TO_EDGE")
46 else if(c.get()=="MIRRORED_REPEAT")
49 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
53 int Texture::swizzle_orders[] =
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
60 Texture::Texture(GLenum t, ResourceManager *m):
64 min_filter(NEAREST_MIPMAP_LINEAR),
76 else if(ARB_direct_state_access)
77 glCreateTextures(target, 1, &id);
79 glGenTextures(1, &id);
84 while(TexUnit *unit = TexUnit::find_unit(this))
85 unbind_from(unit->get_index());
88 glDeleteTextures(1, &id);
91 DataType Texture::get_alloc_type(PixelFormat fmt)
93 return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
96 void Texture::set_internal_format(PixelFormat fmt)
98 if(!get_component_size(fmt) && MSP_sized_internal_formats)
100 unsigned size = (fmt==DEPTH_COMPONENT ? get_gl_api()==OPENGL_ES2 ? 2 : 4 : 1);
101 fmt = get_sized_pixelformat(fmt, size);
104 FormatSwizzle swiz = NO_SWIZZLE;
105 if(ARB_texture_rg && ARB_texture_swizzle)
107 if(fmt==LUMINANCE8 || fmt==SLUMINANCE8)
110 swiz = R_TO_LUMINANCE;
112 else if(fmt==LUMINANCE8_ALPHA8 || fmt==SLUMINANCE8_ALPHA8)
115 swiz = RG_TO_LUMINANCE_ALPHA;
119 require_pixelformat(fmt);
123 update_parameter(FORMAT_SWIZZLE);
126 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
128 if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
129 return get_base_pixelformat(ifmt);
134 void Texture::update_parameter(int mask) const
136 if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
138 TexUnit *unit = TexUnit::find_unit(this);
141 dirty_params |= mask;
149 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_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);
155 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
157 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
159 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
160 if(mask&GENERATE_MIPMAP)
161 set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
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)
168 if(ARB_direct_state_access)
169 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
171 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
175 void Texture::set_parameter_i(GLenum param, int value) const
177 if(ARB_direct_state_access)
178 glTextureParameteri(id, param, value);
180 glTexParameteri(target, param, value);
183 void Texture::set_parameter_f(GLenum param, float value) const
185 if(ARB_direct_state_access)
186 glTextureParameterf(id, param, value);
188 glTexParameterf(target, param, value);
191 void Texture::set_min_filter(TextureFilter f)
194 update_parameter(MIN_FILTER);
197 void Texture::set_mag_filter(TextureFilter f)
200 update_parameter(MAG_FILTER);
203 void Texture::set_filter(TextureFilter f)
206 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
209 void Texture::set_max_anisotropy(float a)
212 throw invalid_argument("Texture::set_max_anisotropy");
214 static Require _req(EXT_texture_filter_anisotropic);
216 update_parameter(MAX_ANISOTROPY);
219 void Texture::set_wrap(TextureWrap w)
227 void Texture::set_wrap_s(TextureWrap w)
230 update_parameter(WRAP_S);
233 void Texture::set_wrap_t(TextureWrap w)
236 update_parameter(WRAP_T);
239 void Texture::set_wrap_r(TextureWrap w)
241 static Require _req(EXT_texture3D);
243 update_parameter(WRAP_R);
246 void Texture::set_generate_mipmap(bool gm)
248 if(gm && !EXT_framebuffer_object)
249 static Require _req(SGIS_generate_mipmap);
251 if(!EXT_framebuffer_object)
252 update_parameter(GENERATE_MIPMAP);
255 void Texture::auto_generate_mipmap()
257 // glGenerateMipmap is defined here
258 if(EXT_framebuffer_object)
260 if(ARB_direct_state_access)
261 glGenerateTextureMipmap(id);
263 glGenerateMipmap(target);
267 void Texture::set_compare_enabled(bool c)
270 static Require _req(ARB_shadow);
272 update_parameter(COMPARE);
275 void Texture::set_compare_func(Predicate f)
277 static Require _req(ARB_shadow);
279 update_parameter(COMPARE_FUNC);
282 void Texture::load_image(const string &fn, bool srgb)
290 void Texture::bind_to(unsigned i) const
295 manager->resource_used(*this);
303 TexUnit &unit = TexUnit::get_unit(i);
304 const Texture *old = unit.get_texture();
305 if(unit.set_texture(this))
308 manager->resource_used(*this);
311 if(unit.supports_legacy())
313 if(old && old->target!=target)
314 glDisable(old->target);
315 if(!old || old->target!=target)
318 glBindTexture(target, id);
322 update_parameter(dirty_params);
328 const Texture *Texture::current(unsigned i)
330 return TexUnit::get_unit(i).get_texture();
333 void Texture::unbind_from(unsigned i)
335 TexUnit &unit = TexUnit::get_unit(i);
336 const Texture *cur = unit.get_texture();
337 if(unit.set_texture(0))
340 glBindTexture(cur->target, 0);
341 if(unit.supports_legacy())
342 glDisable(cur->target);
347 Texture::Loader::Loader(Texture &t):
348 DataFile::CollectionObjectLoader<Texture>(t, 0)
353 Texture::Loader::Loader(Texture &t, Collection &c):
354 DataFile::CollectionObjectLoader<Texture>(t, &c)
359 void Texture::Loader::init()
361 if(Resources *res = dynamic_cast<Resources *>(coll))
362 srgb = res->get_srgb_conversion();
366 add("external_image", &Loader::external_image);
367 add("filter", &Loader::filter);
368 add("generate_mipmap", &Loader::generate_mipmap);
369 add("image_data", &Loader::image_data);
370 add("mag_filter", &Loader::mag_filter);
371 add("max_anisotropy", &Loader::max_anisotropy);
372 add("min_filter", &Loader::min_filter);
373 add("wrap", &Loader::wrap);
374 add("wrap_r", &Loader::wrap_r);
375 add("wrap_s", &Loader::wrap_s);
376 add("wrap_t", &Loader::wrap_t);
379 void Texture::Loader::external_image(const string &fn)
382 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
385 obj.image(img, srgb);
388 void Texture::Loader::filter(TextureFilter f)
393 void Texture::Loader::generate_mipmap(bool gm)
395 obj.set_generate_mipmap(gm);
398 void Texture::Loader::image_data(const string &data)
401 IO::Memory mem(data.data(), data.size());
404 obj.image(img, srgb);
407 void Texture::Loader::mag_filter(TextureFilter f)
409 obj.set_mag_filter(f);
412 void Texture::Loader::max_anisotropy(float a)
414 obj.set_max_anisotropy(a);
417 void Texture::Loader::min_filter(TextureFilter f)
419 obj.set_min_filter(f);
422 void Texture::Loader::wrap(TextureWrap w)
427 void Texture::Loader::wrap_r(TextureWrap w)
432 void Texture::Loader::wrap_s(TextureWrap w)
437 void Texture::Loader::wrap_t(TextureWrap w)
443 bool is_mipmapped(TextureFilter filter)
445 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
446 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);