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) && OES_required_internalformat)
99 fmt = get_default_sized_pixelformat(fmt);
101 FormatSwizzle swiz = NO_SWIZZLE;
102 if(ARB_texture_rg && ARB_texture_swizzle)
104 if(fmt==LUMINANCE8 || fmt==SLUMINANCE8)
107 swiz = R_TO_LUMINANCE;
109 else if(fmt==LUMINANCE8_ALPHA8 || fmt==SLUMINANCE8_ALPHA8)
112 swiz = RG_TO_LUMINANCE_ALPHA;
116 require_pixelformat(fmt);
120 update_parameter(FORMAT_SWIZZLE);
123 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
125 if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
126 return get_base_pixelformat(ifmt);
131 void Texture::update_parameter(int mask) const
133 if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
135 TexUnit *unit = TexUnit::find_unit(this);
138 dirty_params |= mask;
146 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
148 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
149 if(mask&MAX_ANISOTROPY)
150 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
152 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
154 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
156 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
157 if(mask&GENERATE_MIPMAP)
158 set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap);
160 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
161 if(mask&COMPARE_FUNC)
162 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
163 if(mask&FORMAT_SWIZZLE)
165 if(get_gl_api()==OPENGL_ES2)
167 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
168 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
169 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
170 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
174 if(ARB_direct_state_access)
175 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
177 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
182 void Texture::set_parameter_i(GLenum param, int value) const
184 if(ARB_direct_state_access)
185 glTextureParameteri(id, param, value);
187 glTexParameteri(target, param, value);
190 void Texture::set_parameter_f(GLenum param, float value) const
192 if(ARB_direct_state_access)
193 glTextureParameterf(id, param, value);
195 glTexParameterf(target, param, value);
198 void Texture::set_min_filter(TextureFilter f)
201 update_parameter(MIN_FILTER);
204 void Texture::set_mag_filter(TextureFilter f)
207 update_parameter(MAG_FILTER);
210 void Texture::set_filter(TextureFilter f)
213 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
216 void Texture::set_max_anisotropy(float a)
219 throw invalid_argument("Texture::set_max_anisotropy");
221 static Require _req(EXT_texture_filter_anisotropic);
223 update_parameter(MAX_ANISOTROPY);
226 void Texture::set_wrap(TextureWrap w)
234 void Texture::set_wrap_s(TextureWrap w)
237 update_parameter(WRAP_S);
240 void Texture::set_wrap_t(TextureWrap w)
243 update_parameter(WRAP_T);
246 void Texture::set_wrap_r(TextureWrap w)
248 static Require _req(EXT_texture3D);
250 update_parameter(WRAP_R);
253 bool Texture::can_generate_mipmap()
255 return (EXT_framebuffer_object || SGIS_generate_mipmap);
258 void Texture::set_generate_mipmap(bool gm)
260 if(gm && !EXT_framebuffer_object)
261 static Require _req(SGIS_generate_mipmap);
263 if(!EXT_framebuffer_object)
264 update_parameter(GENERATE_MIPMAP);
267 void Texture::auto_generate_mipmap()
269 // glGenerateMipmap is defined here
270 if(EXT_framebuffer_object)
272 if(ARB_direct_state_access)
273 glGenerateTextureMipmap(id);
275 glGenerateMipmap(target);
279 void Texture::set_compare_enabled(bool c)
282 static Require _req(ARB_shadow);
284 update_parameter(COMPARE);
287 void Texture::set_compare_func(Predicate f)
289 static Require _req(ARB_shadow);
291 update_parameter(COMPARE_FUNC);
294 void Texture::load_image(const string &fn, bool srgb)
302 void Texture::bind_to(unsigned i) const
307 manager->resource_used(*this);
315 TexUnit &unit = TexUnit::get_unit(i);
316 const Texture *old = unit.get_texture();
317 if(unit.set_texture(this))
320 manager->resource_used(*this);
323 if(unit.supports_legacy())
325 if(old && old->target!=target)
326 glDisable(old->target);
327 if(!old || old->target!=target)
330 glBindTexture(target, id);
334 update_parameter(dirty_params);
340 const Texture *Texture::current(unsigned i)
342 return TexUnit::get_unit(i).get_texture();
345 void Texture::unbind_from(unsigned i)
347 TexUnit &unit = TexUnit::get_unit(i);
348 const Texture *cur = unit.get_texture();
349 if(unit.set_texture(0))
352 glBindTexture(cur->target, 0);
353 if(unit.supports_legacy())
354 glDisable(cur->target);
359 Texture::Loader::Loader(Texture &t):
360 DataFile::CollectionObjectLoader<Texture>(t, 0)
365 Texture::Loader::Loader(Texture &t, Collection &c):
366 DataFile::CollectionObjectLoader<Texture>(t, &c)
371 void Texture::Loader::init()
373 if(Resources *res = dynamic_cast<Resources *>(coll))
374 srgb = res->get_srgb_conversion();
378 add("external_image", &Loader::external_image);
379 add("filter", &Loader::filter);
380 add("generate_mipmap", &Loader::generate_mipmap);
381 add("image_data", &Loader::image_data);
382 add("mag_filter", &Loader::mag_filter);
383 add("max_anisotropy", &Loader::max_anisotropy);
384 add("min_filter", &Loader::min_filter);
385 add("wrap", &Loader::wrap);
386 add("wrap_r", &Loader::wrap_r);
387 add("wrap_s", &Loader::wrap_s);
388 add("wrap_t", &Loader::wrap_t);
391 void Texture::Loader::external_image(const string &fn)
394 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
397 obj.image(img, srgb);
400 void Texture::Loader::filter(TextureFilter f)
405 void Texture::Loader::generate_mipmap(bool gm)
407 obj.set_generate_mipmap(gm);
410 void Texture::Loader::image_data(const string &data)
413 IO::Memory mem(data.data(), data.size());
416 obj.image(img, srgb);
419 void Texture::Loader::mag_filter(TextureFilter f)
421 obj.set_mag_filter(f);
424 void Texture::Loader::max_anisotropy(float a)
426 obj.set_max_anisotropy(a);
429 void Texture::Loader::min_filter(TextureFilter f)
431 obj.set_min_filter(f);
434 void Texture::Loader::wrap(TextureWrap w)
439 void Texture::Loader::wrap_r(TextureWrap w)
444 void Texture::Loader::wrap_s(TextureWrap w)
449 void Texture::Loader::wrap_t(TextureWrap w)
455 bool is_mipmapped(TextureFilter filter)
457 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
458 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);