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/io/memory.h>
8 #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),
70 auto_gen_mipmap(false),
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 FormatSwizzle swiz = NO_SWIZZLE;
100 if(ARB_texture_rg && ARB_texture_swizzle)
105 swiz = R_TO_LUMINANCE;
107 else if(fmt==LUMINANCE_ALPHA)
110 swiz = RG_TO_LUMINANCE_ALPHA;
114 if(!get_component_size(fmt) && OES_required_internalformat)
115 fmt = get_default_sized_pixelformat(fmt);
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
136 dirty_params |= mask;
140 if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
142 TexUnit *unit = TexUnit::find_unit(this);
145 dirty_params |= mask;
153 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
155 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
156 if(mask&MAX_ANISOTROPY)
157 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
159 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
161 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
163 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
165 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
166 if(mask&COMPARE_FUNC)
167 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
168 if(mask&FORMAT_SWIZZLE)
170 if(get_gl_api()==OPENGL_ES2)
172 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
173 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
174 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
175 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
179 if(ARB_direct_state_access)
180 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
182 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
187 void Texture::set_parameter_i(GLenum param, int value) const
189 if(ARB_direct_state_access)
190 glTextureParameteri(id, param, value);
192 glTexParameteri(target, param, value);
195 void Texture::set_parameter_f(GLenum param, float value) const
197 if(ARB_direct_state_access)
198 glTextureParameterf(id, param, value);
200 glTexParameterf(target, param, value);
203 void Texture::set_min_filter(TextureFilter f)
206 update_parameter(MIN_FILTER);
209 void Texture::set_mag_filter(TextureFilter f)
212 update_parameter(MAG_FILTER);
215 void Texture::set_filter(TextureFilter f)
218 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
221 void Texture::set_max_anisotropy(float a)
224 throw invalid_argument("Texture::set_max_anisotropy");
226 static Require _req(EXT_texture_filter_anisotropic);
228 if(EXT_texture_filter_anisotropic)
229 update_parameter(MAX_ANISOTROPY);
232 void Texture::set_wrap(TextureWrap w)
240 void Texture::set_wrap_s(TextureWrap w)
243 update_parameter(WRAP_S);
246 void Texture::set_wrap_t(TextureWrap w)
249 update_parameter(WRAP_T);
252 void Texture::set_wrap_r(TextureWrap w)
254 static Require _req(EXT_texture3D);
256 update_parameter(WRAP_R);
259 bool Texture::can_generate_mipmap()
261 return EXT_framebuffer_object;
264 void Texture::generate_mipmap()
266 // glGenerateMipmap is defined here
267 static Require _req(EXT_framebuffer_object);
269 if(ARB_direct_state_access)
270 glGenerateTextureMipmap(id);
273 BindRestore _bind(this);
274 glGenerateMipmap(target);
278 void Texture::set_auto_generate_mipmap(bool gm)
281 static Require _req(EXT_framebuffer_object);
283 auto_gen_mipmap = gm;
286 void Texture::set_compare_enabled(bool c)
289 static Require _req(ARB_shadow);
291 update_parameter(COMPARE);
294 void Texture::set_compare_func(Predicate f)
296 static Require _req(ARB_shadow);
298 update_parameter(COMPARE_FUNC);
301 void Texture::load_image(const string &fn, bool srgb)
303 load_image(fn, 0, srgb);
306 void Texture::load_image(const string &fn, unsigned lv, bool srgb)
311 image(img, lv, srgb);
314 void Texture::image(const Graphics::Image &img, bool srgb)
319 void Texture::bind_to(unsigned i) const
324 manager->resource_used(*this);
332 TexUnit &unit = TexUnit::get_unit(i);
333 if(unit.set_texture(this))
336 manager->resource_used(*this);
338 if(ARB_direct_state_access)
339 glBindTextureUnit(i, id);
343 glBindTexture(target, id);
348 update_parameter(dirty_params);
354 const Texture *Texture::current(unsigned i)
356 return TexUnit::get_unit(i).get_texture();
359 void Texture::unbind_from(unsigned i)
361 TexUnit &unit = TexUnit::get_unit(i);
362 const Texture *cur = unit.get_texture();
363 if(unit.set_texture(0))
365 if(ARB_direct_state_access)
366 glBindTextureUnit(i, 0);
370 glBindTexture(cur->target, 0);
376 Texture::Loader::Loader(Texture &t):
377 DataFile::CollectionObjectLoader<Texture>(t, 0)
382 Texture::Loader::Loader(Texture &t, Collection &c):
383 DataFile::CollectionObjectLoader<Texture>(t, &c)
388 void Texture::Loader::init()
391 if(Resources *res = dynamic_cast<Resources *>(coll))
392 srgb = res->get_srgb_conversion();
396 add("external_image", &Loader::external_image);
397 add("filter", &Loader::filter);
398 add("generate_mipmap", &Loader::generate_mipmap);
399 add("image_data", &Loader::image_data);
400 add("mag_filter", &Loader::mag_filter);
401 add("max_anisotropy", &Loader::max_anisotropy);
402 add("min_filter", &Loader::min_filter);
403 add("mipmap_levels", &Loader::mipmap_levels);
404 add("wrap", &Loader::wrap);
405 add("wrap_r", &Loader::wrap_r);
406 add("wrap_s", &Loader::wrap_s);
407 add("wrap_t", &Loader::wrap_t);
410 unsigned Texture::Loader::get_levels() const
412 return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
415 void Texture::Loader::external_image(const string &fn)
418 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
420 throw IO::file_not_found(fn);
423 obj.image(img, get_levels(), srgb);
426 void Texture::Loader::filter(TextureFilter f)
431 void Texture::Loader::generate_mipmap(bool gm)
433 obj.set_auto_generate_mipmap(gm);
436 void Texture::Loader::image_data(const string &data)
439 IO::Memory mem(data.data(), data.size());
442 obj.image(img, get_levels(), srgb);
445 void Texture::Loader::mag_filter(TextureFilter f)
447 obj.set_mag_filter(f);
450 void Texture::Loader::max_anisotropy(float a)
452 obj.set_max_anisotropy(a);
455 void Texture::Loader::min_filter(TextureFilter f)
457 obj.set_min_filter(f);
460 void Texture::Loader::mipmap_levels(unsigned l)
465 void Texture::Loader::wrap(TextureWrap w)
470 void Texture::Loader::wrap_r(TextureWrap w)
475 void Texture::Loader::wrap_s(TextureWrap w)
480 void Texture::Loader::wrap_t(TextureWrap w)
486 bool is_mipmapped(TextureFilter filter)
488 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
489 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);