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),
77 else if(ARB_direct_state_access)
78 glCreateTextures(target, 1, &id);
80 glGenTextures(1, &id);
85 while(TexUnit *unit = TexUnit::find_unit(this))
86 unbind_from(unit->get_index());
89 glDeleteTextures(1, &id);
92 DataType Texture::get_alloc_type(PixelFormat fmt)
94 return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
97 void Texture::set_internal_format(PixelFormat fmt)
99 if(!get_component_size(fmt) && OES_required_internalformat)
100 fmt = get_default_sized_pixelformat(fmt);
102 FormatSwizzle swiz = NO_SWIZZLE;
103 if(ARB_texture_rg && ARB_texture_swizzle)
105 if(fmt==LUMINANCE8 || fmt==SLUMINANCE8)
108 swiz = R_TO_LUMINANCE;
110 else if(fmt==LUMINANCE8_ALPHA8 || fmt==SLUMINANCE8_ALPHA8)
113 swiz = RG_TO_LUMINANCE_ALPHA;
117 require_pixelformat(fmt);
121 update_parameter(FORMAT_SWIZZLE);
124 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
126 if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
127 return get_base_pixelformat(ifmt);
132 void Texture::update_parameter(int mask) const
134 if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
136 TexUnit *unit = TexUnit::find_unit(this);
139 dirty_params |= mask;
147 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
149 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
150 if(mask&MAX_ANISOTROPY)
151 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
153 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
155 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
157 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
158 if(mask&GENERATE_MIPMAP)
159 set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
161 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
162 if(mask&COMPARE_FUNC)
163 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
164 if(mask&FORMAT_SWIZZLE)
166 if(get_gl_api()==OPENGL_ES2)
168 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
169 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
170 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
171 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
175 if(ARB_direct_state_access)
176 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
178 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
183 void Texture::set_parameter_i(GLenum param, int value) const
185 if(ARB_direct_state_access)
186 glTextureParameteri(id, param, value);
188 glTexParameteri(target, param, value);
191 void Texture::set_parameter_f(GLenum param, float value) const
193 if(ARB_direct_state_access)
194 glTextureParameterf(id, param, value);
196 glTexParameterf(target, param, value);
199 void Texture::set_min_filter(TextureFilter f)
202 update_parameter(MIN_FILTER);
205 void Texture::set_mag_filter(TextureFilter f)
208 update_parameter(MAG_FILTER);
211 void Texture::set_filter(TextureFilter f)
214 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
217 void Texture::set_max_anisotropy(float a)
220 throw invalid_argument("Texture::set_max_anisotropy");
222 static Require _req(EXT_texture_filter_anisotropic);
224 if(EXT_texture_filter_anisotropic)
225 update_parameter(MAX_ANISOTROPY);
228 void Texture::set_wrap(TextureWrap w)
236 void Texture::set_wrap_s(TextureWrap w)
239 update_parameter(WRAP_S);
242 void Texture::set_wrap_t(TextureWrap w)
245 update_parameter(WRAP_T);
248 void Texture::set_wrap_r(TextureWrap w)
250 static Require _req(EXT_texture3D);
252 update_parameter(WRAP_R);
255 bool Texture::can_generate_mipmap()
257 return (EXT_framebuffer_object || SGIS_generate_mipmap);
260 void Texture::set_generate_mipmap(bool gm)
262 if(gm && !EXT_framebuffer_object)
263 static Require _req(SGIS_generate_mipmap);
265 if(!EXT_framebuffer_object)
266 update_parameter(GENERATE_MIPMAP);
269 void Texture::auto_generate_mipmap()
271 // glGenerateMipmap is defined here
272 if(EXT_framebuffer_object)
274 if(ARB_direct_state_access)
275 glGenerateTextureMipmap(id);
277 glGenerateMipmap(target);
281 void Texture::set_compare_enabled(bool c)
284 static Require _req(ARB_shadow);
286 update_parameter(COMPARE);
289 void Texture::set_compare_func(Predicate f)
291 static Require _req(ARB_shadow);
293 update_parameter(COMPARE_FUNC);
296 void Texture::load_image(const string &fn, bool srgb)
304 void Texture::bind_to(unsigned i, bool legacy) const
309 manager->resource_used(*this);
317 legacy = (legacy && is_legacy_target(target));
319 TexUnit &unit = TexUnit::get_unit(i);
320 const Texture *old = unit.get_texture();
321 bool old_legacy = unit.get_texture_legacy();
322 if(unit.set_texture(this, legacy))
325 manager->resource_used(*this);
327 if(ARB_direct_state_access && !old_legacy && (!unit.supports_legacy() || !legacy))
328 glBindTextureUnit(i, id);
332 if(unit.supports_legacy())
334 if(old && old->target!=target && old_legacy)
335 glDisable(old->target);
336 if((!old || old->target!=target) && legacy)
339 glBindTexture(target, id);
344 update_parameter(dirty_params);
350 const Texture *Texture::current(unsigned i)
352 return TexUnit::get_unit(i).get_texture();
355 void Texture::unbind_from(unsigned i)
357 TexUnit &unit = TexUnit::get_unit(i);
358 const Texture *cur = unit.get_texture();
359 bool legacy = unit.get_texture_legacy();
360 if(unit.set_texture(0))
362 if(ARB_direct_state_access && !legacy)
363 glBindTextureUnit(i, 0);
367 glBindTexture(cur->target, 0);
368 if(unit.supports_legacy() && legacy)
369 glDisable(cur->target);
374 bool Texture::is_legacy_target(GLenum target)
376 return target<GL_TEXTURE_1D_ARRAY;
380 Texture::Loader::Loader(Texture &t):
381 DataFile::CollectionObjectLoader<Texture>(t, 0)
386 Texture::Loader::Loader(Texture &t, Collection &c):
387 DataFile::CollectionObjectLoader<Texture>(t, &c)
392 void Texture::Loader::init()
394 if(Resources *res = dynamic_cast<Resources *>(coll))
395 srgb = res->get_srgb_conversion();
399 add("external_image", &Loader::external_image);
400 add("filter", &Loader::filter);
401 add("generate_mipmap", &Loader::generate_mipmap);
402 add("image_data", &Loader::image_data);
403 add("mag_filter", &Loader::mag_filter);
404 add("max_anisotropy", &Loader::max_anisotropy);
405 add("min_filter", &Loader::min_filter);
406 add("wrap", &Loader::wrap);
407 add("wrap_r", &Loader::wrap_r);
408 add("wrap_s", &Loader::wrap_s);
409 add("wrap_t", &Loader::wrap_t);
412 void Texture::Loader::external_image(const string &fn)
415 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
418 obj.image(img, srgb);
421 void Texture::Loader::filter(TextureFilter f)
426 void Texture::Loader::generate_mipmap(bool gm)
428 obj.set_generate_mipmap(gm);
431 void Texture::Loader::image_data(const string &data)
434 IO::Memory mem(data.data(), data.size());
437 obj.image(img, srgb);
440 void Texture::Loader::mag_filter(TextureFilter f)
442 obj.set_mag_filter(f);
445 void Texture::Loader::max_anisotropy(float a)
447 obj.set_max_anisotropy(a);
450 void Texture::Loader::min_filter(TextureFilter f)
452 obj.set_min_filter(f);
455 void Texture::Loader::wrap(TextureWrap w)
460 void Texture::Loader::wrap_r(TextureWrap w)
465 void Texture::Loader::wrap_s(TextureWrap w)
470 void Texture::Loader::wrap_t(TextureWrap w)
476 bool is_mipmapped(TextureFilter filter)
478 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
479 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);